派生自 projectDept/qhighschool

EricsHu
2023-04-10 5196a1d1e00ced009a2d4e8ea0aa5f3bb7cefe33
src/main/java/com/qxueyou/scc/wx/service/impl/WechatService.java
@@ -1,8 +1,11 @@
package com.qxueyou.scc.wx.service.impl;
import cn.hutool.http.HttpUtil;
import com.alibaba.druid.util.HttpClientUtils;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qxueyou.scc.base.model.Result;
import com.qxueyou.scc.base.service.ICacheService;
import com.qxueyou.scc.teach.student.model.StuStudent;
import com.qxueyou.scc.wx.service.IWechatService;
@@ -65,6 +68,55 @@
        JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doPost(requestUrl, requestUrlParam));
        return jsonObject;
    }
    @Override
    public Result getNumber(String tonken, String number) {
        JSONObject jsonCode = new JSONObject();
        jsonCode.put("code",number);
        String resPhone = HttpClientUtil.doPostUrl("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + tonken, jsonCode);
        if(StringUtils.isEmpty(resPhone) || !resPhone.contains("phone_info") ||  !resPhone.contains("phoneNumber")){
            return new Result(false,"微信官方修改了小程序获取用户手机号码相关接口!");
        }
        JSONObject resPhoneInfo = JSON.parseObject(resPhone);
        JSONObject phoneInfo=resPhoneInfo.getJSONObject("phone_info");
        System.out.println(resPhoneInfo);
        System.out.println(phoneInfo);
        String phoneNumber = phoneInfo.get("phoneNumber").toString();
        return new Result(true,"获取手机号成功",phoneNumber);
//        String requestUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+tonken+jsonObject.toJSONString();
//        Map<String, String> requestUrlParam = new HashMap<>();
//        requestUrlParam.put("code", number);
//        JSONObject s =JSON.parseObject(HttpClientUtil.doPost(requestUrl));
//        return s;
    }
    @Override
    public JSONObject gettoken(String appid, String secret) {
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token";
        Map<String, String> requestUrlParam = new HashMap<>();
        requestUrlParam.put("grant_type", "client_credential");
        requestUrlParam.put("appid", appid);
        requestUrlParam.put("secret", secret);
        JSONObject s = JSON.parseObject(HttpClientUtil.doGet(requestUrl, requestUrlParam));
        return s;
    }
//    private String getWechatHeadImgUrl(String unionId) {
//        WechatUserDO wxUser = this.template.findOne(Query.query(condition().and("unionId").is(unionId)), WechatUserDO.class);
//        return wxUser.getHeadImgUrl();