package com.ruoyi.web.controller.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.http.HttpRequest;
import com.aliyuncs.http.HttpUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpClientUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import oshi.driver.mac.net.NetStat;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tjf
 * @Date: 2022/02/22/14:11
 */
@RestController
@RequestMapping("/weChat")
public class WechatController {

    @PostMapping("/login")
    public AjaxResult getPhone(@RequestBody String code) {
        if (StringUtils.isNotBlank(code)) {
            code = JSONObject.parseObject(code).getString("code");
        }
        //刷制品小程序appid
        String appSecret = "ff62fc579b51a4f6636b08d9f9b25d69";
        String appId = "wx1ada5448f249d1ad";
        //获取接口调用凭证
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
        String jsonStr = HttpUtils.sendGet(url);
        JSONObject result = JSONObject.parseObject(jsonStr);
        //{"access_token":"ACCESS_TOKEN","expires_in":7200}
        String accessToken = result.getString("access_token");
        if (StringUtils.isNotBlank(accessToken)) {
            //获取手机号
            String phoneUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken;
            JSONObject json = new JSONObject();
            json.put("code",code);
            String strPhone = null;
            try {
                strPhone = HttpClientUtils.doPost(phoneUrl, json);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //{
            //    "errcode":0,
            //    "errmsg":"ok",
            //    "phone_info": {
            //        "phoneNumber":"xxxxxx",
            //        "purePhoneNumber": "xxxxxx",
            //        "countryCode": 86,
            //        "watermark": {
            //            "timestamp": 1637744274,
            //            "appid": "xxxx"
            //        }
            //    }
            //}
            JSONObject resultPhone = JSONObject.parseObject(strPhone);
            String errmsg = resultPhone.getString("errmsg");
            if ("ok".equals(errmsg)) {
                String phone = JSONObject.parseObject(resultPhone.getString("phone_info")).getString("phoneNumber");
                return AjaxResult.success(phone);
            }
        }
        return AjaxResult.error("获取手机号失败");
    }
}
