派生自 projectDept/qhighschool

EricsHu
2023-04-10 5196a1d1e00ced009a2d4e8ea0aa5f3bb7cefe33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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;
import com.qxueyou.scc.wx.utils.HttpClientUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import java.io.IOException;
import java.net.URI;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
 
@Service
public class WechatService implements IWechatService {
 
//    @Autowired
//    IUserService userService;
 
    /**
     * 小程序appID
     */
    @Value("${wx.appId}")
    private String appId;
 
 
    /**
     * 小程序secret
     */
    @Value("${wx.secret}")
    private String secret;
 
 
 
    @Autowired
    private ICacheService cacheService;
 
    private final Logger logger = LogManager.getLogger();
 
 
    @Override
    public JSONObject getSessionKeyOrOpenId(String code) {
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
        Map<String, String> requestUrlParam = new HashMap<>();
        // https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN
        //小程序appId
        requestUrlParam.put("appid", appId);
        //小程序secret
        requestUrlParam.put("secret", secret);
        //小程序端返回的code
        requestUrlParam.put("js_code", code);
        //默认参数
        requestUrlParam.put("grant_type", "authorization_code");
        //发送post请求读取调用微信接口获取openid用户唯一标识
        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();
//    }
 
//    @Override
//    public Result isBindWx(String userId) {
//        UserStudentDO studentDO = read(UserStudentDO.class, userId);
//        boolean status = false;
//        if (StringUtils.isNotBlank(studentDO.getUnionId())) {
//            status = true;
//        }
//        String url = "";
//        if (status) {
//            url = getWechatHeadImgUrl(studentDO.getUnionId());
//        }
//        return new Result(true, "status", status, "url", url);
//    }
//
//    public Result getSignature(String url) {
//        try {
//            long timestamp = System.currentTimeMillis() / 1000;
//            String randomStr = RandomUtils.getRandomStr();
//            //远程调用获取ticket
////            String jsapiTicket ="";//getJsapiTicket(false);
//            RestTemplate restTemplate = new RestTemplate();
//
//            String jsapiTicket = restTemplate.getForObject("http://192.168.1.111/job/ticket", String.class);
//
//            String signature = SHA1.genWithAmple("jsapi_ticket=" + jsapiTicket,
//                    "noncestr=" + randomStr, "timestamp=" + timestamp, "url=" + url);
//            WxJsapiSignature jsapiSignature = new WxJsapiSignature();
//            jsapiSignature.setAppId(wxProperties.getAppId());
//            jsapiSignature.setTimestamp(timestamp);
//            jsapiSignature.setNonceStr(randomStr);
//            jsapiSignature.setUrl(url);
//            jsapiSignature.setSignature(signature);
//            return new Result(true, jsapiSignature);
//        } catch (Exception e) {
//            log.error(e, e);
//            return new Result(false, "获取签名失败");
//        }
//    }
 
//    @Override
//    public String getOpenId(String userId) {
//        UserStudentDO user = read(UserStudentDO.class, userId);
//
//        return user.getOpenId();
//    }
 
 
}