package com.wteam.controller.wxlogin;

import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.utils.HttpUtils;
import com.wteam.framework.common.utils.QRCode;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.wxcode.WxAccessToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
@RequestMapping("/v1/QRCode")
@Tag(name = "小程序相关接口管理")
public class QRCodeController {

    /**
     * 后端返回前端一个获取accessToken的路径接口--返回access_token和expires_in
     * 获取accessToken
     *
     * @return
     */
    @ApiOperation(value = "后端返回前端一个获取accessToken的路径接口")
    @GetMapping("/getAccessToken")
    public ResultMessage getAccessToken() {
//     "appId":"wxe5ca58e8a4a615c6","appSecret":"cb8fd4e6c3895db6f11619e08bb8f277------------->嘉文的
//     "appId":"wx55c3db66f27015af","appSecret":"c17d17e230ee71395a70cd34d6783200"
        //todo 修改为客户的appId和appSecret
        String WxAccessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + "wx9a649e9875a872ec" + "&secret=" + "b445bf60f367a6c10e26c7ae07117721";
        HttpUtils httpUtils = new HttpUtils();
        String s = httpUtils.get(WxAccessTokenUrl);
        ResultMessage wxCodeResultMessage = new ResultMessage<WxAccessToken>();
        wxCodeResultMessage.setCode(200);
        wxCodeResultMessage.setResult(s);
        wxCodeResultMessage.setSuccess(true);
        wxCodeResultMessage.setMessage("success");
        return wxCodeResultMessage;
    }


    /**
     * 无限制的获取小程序码
     *
     * @param wxaccessToken
     * @param page
     * @param scene
     * @param checkPath
     * @param envVersion
     * @return
     */
    @ApiOperation(value = "获取小程序码无限制")
    @GetMapping(value = "/getUnlimited")
    public ResultMessage getUnlimited(@RequestParam("wxaccessToken") String wxaccessToken, @RequestParam("page") String page, @RequestParam("scene") String scene, @RequestParam("checkPath") String checkPath, @RequestParam("envVersion") String envVersion) {
        QRCode qrCode = new QRCode();
        Object wxCode = qrCode.getWxCode(wxaccessToken, page, scene, checkPath, envVersion);
//        ResultMessage wxCodeResultMessage = new ResultMessage<>();
//        wxCodeResultMessage.setCode(wxCode.getStatusCode());
//        wxCodeResultMessage.setResult(wxCode.getData());
//        wxCodeResultMessage.setMessage("success");
//        wxCodeResultMessage.setSuccess(true);
        return ResultUtil.data(wxCode);

    }

    @GetMapping(value = "/getLimit")
    @ApiOperation(value = "获取小程序码128字节")
    public ResultMessage getlimit(@RequestParam("wxaccessToken") String wxaccessToken, @RequestParam("page") String page) {
        QRCode qrCode = new QRCode();
        Object wxCode = qrCode.getWxCodelimit(wxaccessToken, page);
//        ResultMessage wxCodeResultMessage = new ResultMessage<>();
//        wxCodeResultMessage.setCode(wxCode.getStatusCode());
//        wxCodeResultMessage.setResult(wxCode.getData());
//        wxCodeResultMessage.setMessage("success");
//        wxCodeResultMessage.setSuccess(true);
        return ResultUtil.data(wxCode);
    }


}
