package com.ssm.controller;


import com.ssm.manage.query.UserInfoDTO;
import com.ssm.manage.utils.WxHttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description: [微信小程序简单登录接口, 只获取Openid和基本信息,不包括UnionID,基本信息由前端传入]
 * @Author: pjqdyd
 * @Version: [v1.0.0]
 */

@RestController
@RequestMapping("/wx/sample")
@Api("微信1")
public class WxSampleLoginController {


    public static final String APPID = "wx094b8c7fb88ba28d"; //申请小程序的AppId

    public static final Integer MCHID = 1627685754; //商户号ID

    public static final String APP_SECRET = "2777bb81ea8a1afbb112c07529041d2f"; //生成的AppSecret


    //请求微信后端的地址
    public static final String AUTH_URL = "https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={js_code}&grant_type={grant_type}";


    /**
     * 登录接口
     *
     * @param code 前端wx.login获取的code
     * @return
     */

    @PostMapping("/login")
    @ApiOperation("微信登录")
    public Object wxLogin(@RequestParam("code") String code, @RequestBody UserInfoDTO userInfoDTO) throws Exception {

        if (StringUtils.isBlank(code)) {
            return "失败-code不能为空";
        }

        //装载请求参数的Map集合,通过code,appid,app_secret获取用户的OpenId

        Map<String, String> paramsMap = new HashMap<>();

        paramsMap.put("appid", APPID);

        paramsMap.put("secret", APP_SECRET);

        paramsMap.put("js_code", code);

        paramsMap.put("grant_type", "authorization_code");

        //获取用户的OpenId
        Map<String, String> resultMap = new WxHttpUtil().getWxOpenIdAndSkey(AUTH_URL, paramsMap);

        System.out.println(resultMap);
        String openId = resultMap.get("openid");

        //String session_key = resultMap.get("session_key");
        System.out.println(userInfoDTO.toString());
        return resultMap;

    }
}
