package com.web.controller;

import com.hai.common.utils.*;
import com.hai.model.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

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

    @RequestMapping(value = "/getH5AccessToken", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "【H5】获取AccessToken")
    public ResponseData getH5AccessToken(@RequestParam(value = "code", required = true) String code) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("appid", "wxa075e8509802f826");
            params.put("secret", "0e606fc1378d35e359fcf3f15570b2c5");
            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);
        }
    }

}
