package com.cweb.controller;

import com.cweb.config.SysConst;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.model.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/wechat")
@Api(value = "微信授权")
public class WechatController {

    private static final Logger log = LoggerFactory.getLogger(WechatController.class);

    @RequestMapping(value = "/getAccessToken", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "解析")
    public ResponseData getAccessToken(@RequestParam(value = "code", required = true) String code) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("appid", SysConst.getSysConfig().getWxAppId());
            params.put("secret", SysConst.getSysConfig().getWxAppSecret());
            params.put("code", code);
            params.put("grant_type", "authorization_code");
            return ResponseMsgUtil.success(HttpsUtils.doGet("https://api.weixin.qq.com/sns/oauth2/access_token", params));

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }
}
