package com.fs.app.controller;

import com.fs.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Synchronized;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;


@RestController
@Api("微信配置")
@RequestMapping("/app/wx/mp")
public class WxMpController {

    @Autowired
    private WxMpService wxMpService;


    @GetMapping("/getWeiXinConfig")
    @ApiOperation("微信url验证")
    @Synchronized
    public R getWxConfig(@RequestParam String url) throws WxErrorException {
        try {
            String sLink = URLDecoder.decode(url, "UTF-8");
            final WxJsapiSignature jsapiSignature = wxMpService.createJsapiSignature(sLink);
            return R.ok().put("data", jsapiSignature);
        } catch (UnsupportedEncodingException e) {
            // URL解码异常
            return R.error(e.getMessage());
        }
    }


}
