package com.ssm.controller;

import com.alibaba.fastjson.JSONObject;
import com.ssm.manage.utils.WxHttpUtil;
import com.ssm.manage.utils.authorizedLoginUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @Description:  [微信小程序登录获取用户信息接口, 解密数据获取用户信息包括UnionID(需要微信开放平台认证开发者账户绑定的小程序)]
 * @Author:       pjqdyd
 * @Version:      [v1.0.0]
 */

@RestController
@RequestMapping("/wx")
@Api("微信")
public class WxLoginControlller {

    public static final String APPID = "wx094b8c7fb88ba28d"; //申请小程序的AppId
    public static final String APP_SECRET = "88c651eda1a8396cbedae829dbfbd69c"; //生成的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
     * @param iv 前端wx.getUserInfo获取的加密偏移值
     * @param encryptedData 前端wx.getUserInfo加密的用户数据
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("微信获取")
    public String wxLogin(@RequestParam("code") String code,
                          @RequestParam("iv") String iv,
                          @RequestParam("encryptedData") String encryptedData) throws Exception{

        if (StringUtils.isBlank(code) || StringUtils.isBlank(iv) || StringUtils.isBlank(encryptedData)){
            return "失败-参数不能为空";
        }

        //装载请求参数的Map集合,通过code,appid,app_secret获取用户的OpenId和session_key
        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和ession_key
        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");

        //表示用户认证登录信息正确
        if(!StringUtils.isBlank(openId) || !StringUtils.isBlank(session_key)){

            //解密用户数据
            JSONObject jsonUserInfo = authorizedLoginUtil.useEncryptedDataDecode(encryptedData, session_key, iv);

            if (jsonUserInfo != null){
                System.out.println(jsonUserInfo.toString());
                System.out.println(jsonUserInfo.getString("country"));
                System.out.println(jsonUserInfo.getString("openId"));
                String purePhoneNumber = (String) jsonUserInfo.get("purePhoneNumber");
                return purePhoneNumber;
            }
        }


        return "Error";
    }


}
