huanghongfa
2021-04-23 d6002ab9f50eaf8d33878e9742b9b0063d08fa55
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
package com.panzhihua.common.utlis;
 
import com.alibaba.fastjson.JSON;
import com.panzhihua.common.constants.SecurityConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
@Slf4j
@Component
public class WxXCXTempSend {
 
    private static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
 
    public static final String APP_ID = "wx0cef797390444b75";
    private static final String APP_SECRET = "c7ea9aaa7e391a487e8a5b9ba61045d1";
 
    @Autowired
    private StringRedisTemplate redisTemplate;
 
    /**
     * 获取小程序token,(ps:0=token获取失败)
     *
     * @return
     */
    public String getAccessToken() throws Exception {
        String accessToken = "";
        Boolean aBoolean = redisTemplate.hasKey(SecurityConstants.APPLETS_ACCESS_TOKEN);
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        if(aBoolean){
            Long expire = redisTemplate.boundHashOps(SecurityConstants.APPLETS_ACCESS_TOKEN).getExpire();
            if(expire <= 0){
                redisTemplate.delete(SecurityConstants.APPLETS_ACCESS_TOKEN);//如果过期则删除
                accessToken = getAppAccessToken();
                valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN, accessToken,110, TimeUnit.MINUTES);
            }else{
                accessToken = valueOperations.get(SecurityConstants.APPLETS_ACCESS_TOKEN);
            }
        }else{
            accessToken = getAppAccessToken();
            valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN, accessToken,110, TimeUnit.MINUTES);
        }
        return accessToken;
    }
 
    private String getAppAccessToken() throws Exception{
        String accessToken = "0";
        try {
            //此处APP_ID APP_SECRET  在微信小程序后端可见
//            String accessTokenUrl = String.format(TEMP_URL, APP_ID, APP_SECRET);
            String accessTokenUrl = ACCESS_TOKEN_URL + "&appid=" + APP_ID + "&secret=" + APP_SECRET;
            String result = HttpClientUtil.httpGet(accessTokenUrl, null, null);
            Map<String, Object> resultMap = JSON.parseObject(result, Map.class);
            if(resultMap.containsKey("access_token")) {
                accessToken = resultMap.get("access_token").toString();
            }
        } catch (IOException ioe) {
            log.error("小程序http请求异常");
            ioe.printStackTrace();
        }
        return accessToken;
    }
 
    public static void main(String[] args) throws Exception {
//        String accessToken = "0";
//        try {
//            //此处APP_ID APP_SECRET  在微信小程序后端可见
//            String accessTokenUrl = String.format(TEMP_URL, APP_ID, APP_SECRET);
//            String result = HttpClientUtil.httpGet(accessTokenUrl, null, null);
//            Map<String, Object> resultMap = JSON.parseObject(result, Map.class);
//            if(resultMap.containsKey("access_token")) {
//                accessToken = resultMap.get("access_token").toString();
//            }
//        } catch (IOException ioe) {
//            log.error("小程序http请求异常");
//            ioe.printStackTrace();
//        }
//        System.out.println(accessToken);
    }
 
 
 
}