扫描登录工具类

This commit is contained in:
lujiang 2025-05-21 09:16:42 +08:00
parent 766a8c86a4
commit a5c15e82e4

View File

@ -0,0 +1,76 @@
package com.ics.util;
import com.alibaba.fastjson.JSONObject;
import com.ics.common.utils.StringUtils;
import com.ics.controller.mobile.SmallWxOkHttp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* created at 2025-2-18 11:20
*
* @author lujiang
* @version 1.0.0
* @since 1.0.0
*/
@Component("wxSmallUtil")
public class WxSmallUtil {
@Autowired
private RedisTemplate<String, String> redisTemplate;
private final static String WX_SMALL_APP_ID = "wxe043ce243cd9de5b"; //小程序id
private final static String WX_SMALL_APP_SECRET = "5e7211116d2e183d997a01476a523744"; //小程序密钥
/**获取小程序token*/
private String getAccessToken() {
String key = "webchatSMALL";
// 从缓存获取微信公众号 AccessToken
String accessToken = redisTemplate.opsForValue().get(key);
if (StringUtils.isBlank(accessToken)) {
String url = "https://api.weixin.qq.com/cgi-bin/token";
// 参数
Map<String, String> map = new HashMap<>();
map.put("grant_type", "client_credential");
map.put("appid", WX_SMALL_APP_ID);
map.put("secret", WX_SMALL_APP_SECRET);
// 发送请求
JSONObject jsonObject = SmallWxOkHttp.sendGet(url, map);
accessToken = jsonObject.getString("access_token");
redisTemplate.opsForValue().set(key, accessToken, 7000, TimeUnit.SECONDS);
}
return accessToken;
}
/**
* 获取小程序加密URL Link
* @return
*/
public String getLink() {
String key = "webchatSMALLUrl";
String urlLink = redisTemplate.opsForValue().get(key);
if (StringUtils.isBlank(urlLink)) {
String url = "https://api.weixin.qq.com/wxa/generate_urllink?expire_type=1&expire_interval=1&access_token=" + getAccessToken();
JSONObject jsonObject = SmallWxOkHttp.sendPost(url, null);
urlLink = jsonObject.getString("url_link");
redisTemplate.opsForValue().set(key, urlLink, 23, TimeUnit.HOURS);
}
return urlLink;
}
// public static void main(String[] args) {
// String at = "89_G_e95jSJXO5OPnmlDkg28C_H1E4wvAHoAZBc01pgWlGUJTpKCG5OyTaR3TVIc3z4W8dWchMGkudMisH6qD0fx3jo380k_dEGTPO-bAEA4RuKSvGGG0Ht6EYCy-sZUOiAIATOI";
// String url = "https://api.weixin.qq.com/wxa/generate_urllink?expire_type=1&expire_interval=1&access_token=" + at;
// JSONObject jsonObject = SmallWxOkHttp.sendPost(url, null);
//
// String urlLink = jsonObject.getString("url_link");
// System.out.println(jsonObject.toString());
// System.out.println(urlLink);
// }
}