fix
Helius
2021-08-13 1e9d98f01a04c6c4cdd3ebc4bed1cdd1c452b511
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
package com.matrix.system.shopXcx.api;
 
import com.matrix.component.tools.HttpRequest;
import com.matrix.component.tools.HttpResponse;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.PropertiesUtil;
import com.matrix.system.common.bean.BusParameterSettings;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.dao.BusParameterSettingsDao;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
 
@Service
public class WeChatApiTools {
 
    @Autowired
    private BusParameterSettingsDao busParameterSettingsDao;
 
    /**
     * 微信登录url
     */
    private static final String WECHAT_LOGIN_URL = "wechar_login_url";
 
    /**
     * 上一次获取时间
     */
    private static Long preTime;
 
    /**
     * 当前有效的微信token
     */
    private static String accessToken = "";
 
 
 
    /**
     * 获取小程序登录地址
     * 
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年5月23日
     * @param code
     * @return
     */
    public  String getXcxLoginUrl(String code,Long companyId) {
        String wechatLoginUrl = PropertiesUtil.getString(WECHAT_LOGIN_URL);
        return String.format(wechatLoginUrl, getAppid(companyId), getSecret(companyId), code);
    }
 
    /**
     * 获取小程序APPId
     * @return
     */
    public  String  getAppid(Long companyId){
        BusParameterSettings appId = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.MINIPROGRAM_APPID, companyId);
        return  appId.getParamValue();
    }
 
    /**
     * 获取小程序秘钥
     * @return
     */
    public  String  getSecret(Long companyId){
        BusParameterSettings secret = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.MINIPROGRAM_SECRET, companyId);
        return  secret.getParamValue();
    }
 
 
 
    /**
     * 清空token
     */
    public static void cleanAccessToken() {
        preTime = null;
        accessToken = "";
    }
 
    public  String getAccessToken(Long companyId) throws IOException {
 
        if (isTokenInvalid()) {
            synchronized (accessToken) {
                if (isTokenInvalid()) {
                    LogUtil.info("刷新微信accessToken");
                    HttpRequest reqObj = new HttpRequest();
                    HttpResponse result = null;
                    result = reqObj
                            .sendHttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
                                    + getAppid(companyId) + "&secret=" + getSecret(companyId), null);
                    JSONObject json = JSONObject.fromObject(result.getDataString());
                    String access_token = json.getString("access_token");
                    accessToken = access_token;
                    preTime = System.currentTimeMillis();
                    LogUtil.info("返回刷新的accessToken={}", accessToken);
                    return access_token;
                } else {
                    return accessToken;
                }
            }
        } else {
            LogUtil.info("返回现有accessToken={}", accessToken);
            return accessToken;
        }
    }
 
    /**
     * token是否无效
     *  TODO 由于现在有多个环境,为了防止冲突每次都获取新的token
     * @return
     */
    private static boolean isTokenInvalid() {
        /**        if (StringUtils.isNotBlank(accessToken) && preTime != null) {
         Long now = System.currentTimeMillis() / 1000;
         Long pre = preTime / 1000;
         boolean invalid = (now - pre) > 6200;
         return invalid;
         } else {
         return true;
         }*/
        return true;
    }
 
 
}