
package com.tmerclub.cloud.auth.controller;

import com.tmerclub.cloud.auth.config.WxConfig;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author tmerclub
 * @date 2021/7/31
 */
@RequestMapping("/wx/jsapi")
@Tag(name = "jsapi签名")
@RestController
public class JsapiSignatureController {

    @Autowired
    private WxConfig wxConfig;

    @PostMapping("/createJsapiSignature")
    @Operation(summary = "生成jsapi签名", description = "根据url生成当前页面的jsapi签名")
    public ServerResponseEntity<WxJsapiSignature> createJsapiSignature(@RequestBody String url) {
        WxJsapiSignature jsapiSignature;
        try {
            jsapiSignature = wxConfig.getWxMpService().createJsapiSignature(url);
        } catch (WxErrorException e) {
            throw new LuckException(e.getError().toString());
        }
        return ServerResponseEntity.success(jsapiSignature);
    }
}
