派生自 projectDept/qhighschool

胡仁荣
2023-04-07 4c61d5fd5c273cadffe9f20464b5341a23f4e60f
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
package com.qxueyou.scc.wx.service.impl;
 
import com.alibaba.druid.util.HttpClientUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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;
    }
//    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();
//    }
 
 
}