新增小程序用户管理

This commit is contained in:
wenc000 2020-10-29 18:43:54 +08:00
parent b48dbd5e27
commit ae06a9791f
5 changed files with 272 additions and 3 deletions

View File

@ -1,9 +1,15 @@
package com.cm.common.wechat.manager.miniapp;
import com.cm.common.wechat.config.pojo.WechatMiniAppProperties;
import com.cm.common.wechat.pojo.WechatMiniAppUserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
@ -17,11 +23,66 @@ import org.slf4j.LoggerFactory;
public class WechatMiniAppManager {
private static final Logger LOG = LoggerFactory.getLogger(WechatMiniAppManager.class);
private static WechatMiniAppManager wechatMiniAppManager = WechatMiniAppManagerBuilder.wechatMiniAppManager;
// 超时分钟
private final int TIME_OUT_SECOND = 120 * 60;
private WechatMiniAppProperties wechatMiniAppProperties;
private WechatMiniAppManager() {
}
private Map<String, WechatMiniAppUserInfo> users = new ConcurrentHashMap<>();
public static WechatMiniAppManager getInstance() {
return wechatMiniAppManager;
}
/**
* 添加用户
*
* @param token
* @param wechatMiniAppUserInfo
*/
public void addUser(String token, WechatMiniAppUserInfo wechatMiniAppUserInfo) {
wechatMiniAppUserInfo.setUpdateTime(System.currentTimeMillis());
users.put(token, wechatMiniAppUserInfo);
}
/**
* 获取小程序应用
*
* @param token
* @return
*/
public WechatMiniAppUserInfo getWechatMiniAppUserInfoByToken(String token) {
return users.get(token);
}
/**
* 删除超时用户
*/
public void removeTimeoutUser() {
LOG.debug("delete timeout wechat mini app user start");
long startTime = System.currentTimeMillis();
List<String> tokenList = new ArrayList<>();
for (Map.Entry<String, WechatMiniAppUserInfo> kv : users.entrySet()) {
// 超时
if ((startTime - kv.getValue().getUpdateTime()) / 1000 > TIME_OUT_SECOND) {
tokenList.add(kv.getKey());
}
}
for (String token : tokenList) {
users.remove(token);
}
long endTime = System.currentTimeMillis();
LOG.debug("delete timeout wechat mini app user end, delete count: {}, useTime: {}ms", tokenList.size(), endTime - startTime);
}
public void setWechatMiniAppProperties(WechatMiniAppProperties wechatMiniAppProperties) {
this.wechatMiniAppProperties = wechatMiniAppProperties;
}
private static class WechatMiniAppManagerBuilder {
public static final WechatMiniAppManager wechatMiniAppManager = new WechatMiniAppManager();
}
}

View File

@ -17,6 +17,7 @@ public class WechatMiniAppUserInfo {
private String unionid;
private Integer errcode;
private String errmsg;
private long updateTime;
public String getOpenid() {
return openid == null ? "" : openid;
@ -58,6 +59,14 @@ public class WechatMiniAppUserInfo {
this.errmsg = errmsg;
}
public long getUpdateTime() {
return updateTime;
}
public void setUpdateTime(long updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
@ -67,10 +76,12 @@ public class WechatMiniAppUserInfo {
.append(session_key).append('\"');
sb.append(",\"unionid\":\"")
.append(unionid).append('\"');
sb.append(",\"errcode\":\"")
.append(errcode).append('\"');
sb.append(",\"errcode\":")
.append(errcode);
sb.append(",\"errmsg\":\"")
.append(errmsg).append('\"');
sb.append(",\"updateTime\":")
.append(updateTime);
sb.append('}');
return sb.toString();
}

View File

@ -0,0 +1,187 @@
package com.cm.common.wechat.pojo;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: WechatMiniAppUserInfoDetail
* @Description:
* @Author: WangGeng
* @Date: 2020/10/29 18:36
* @Version: 1.0
**/
public class WechatMiniAppUserInfoDetail {
private String openId;
private String nickName;
private Integer gender;
private String city;
private String province;
private String country;
private String avatarUrl;
private String unionId;
private String phoneNumber;
private String purePhoneNumber;
private String countryCode;
private WaterMark watermark;
public String getOpenId() {
return openId == null ? "" : openId.trim();
}
public void setOpenId(String openId) {
this.openId = openId;
}
public String getNickName() {
return nickName == null ? "" : nickName.trim();
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getCity() {
return city == null ? "" : city.trim();
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province == null ? "" : province.trim();
}
public void setProvince(String province) {
this.province = province;
}
public String getCountry() {
return country == null ? "" : country.trim();
}
public void setCountry(String country) {
this.country = country;
}
public String getAvatarUrl() {
return avatarUrl == null ? "" : avatarUrl.trim();
}
public void setAvatarUrl(String avatarUrl) {
this.avatarUrl = avatarUrl;
}
public String getUnionId() {
return unionId == null ? "" : unionId.trim();
}
public void setUnionId(String unionId) {
this.unionId = unionId;
}
public String getPhoneNumber() {
return phoneNumber == null ? "" : phoneNumber.trim();
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPurePhoneNumber() {
return purePhoneNumber == null ? "" : purePhoneNumber.trim();
}
public void setPurePhoneNumber(String purePhoneNumber) {
this.purePhoneNumber = purePhoneNumber;
}
public String getCountryCode() {
return countryCode == null ? "" : countryCode.trim();
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public WaterMark getWatermark() {
return watermark;
}
public void setWatermark(WaterMark watermark) {
this.watermark = watermark;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"openId\":\"")
.append(openId).append('\"');
sb.append(",\"nickName\":\"")
.append(nickName).append('\"');
sb.append(",\"gender\":")
.append(gender);
sb.append(",\"city\":\"")
.append(city).append('\"');
sb.append(",\"province\":\"")
.append(province).append('\"');
sb.append(",\"country\":\"")
.append(country).append('\"');
sb.append(",\"avatarUrl\":\"")
.append(avatarUrl).append('\"');
sb.append(",\"unionId\":\"")
.append(unionId).append('\"');
sb.append(",\"phoneNumber\":\"")
.append(phoneNumber).append('\"');
sb.append(",\"purePhoneNumber\":\"")
.append(purePhoneNumber).append('\"');
sb.append(",\"countryCode\":\"")
.append(countryCode).append('\"');
sb.append(",\"watermark\":")
.append(watermark);
sb.append('}');
return sb.toString();
}
public static class WaterMark {
private String appid;
private Long timestamp;
public String getAppid() {
return appid == null ? "" : appid.trim();
}
public void setAppid(String appid) {
this.appid = appid;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"appid\":\"")
.append(appid).append('\"');
sb.append(",\"timestamp\":")
.append(timestamp);
sb.append('}');
return sb.toString();
}
}
}

View File

@ -8,6 +8,7 @@ import com.cm.common.result.SuccessResultData;
import com.cm.common.utils.AesUtil;
import com.cm.common.utils.http.RestRequestUtil;
import com.cm.common.wechat.config.pojo.WechatMiniAppProperties;
import com.cm.common.wechat.manager.miniapp.WechatMiniAppManager;
import com.cm.common.wechat.pojo.WechatMiniAppLoginVO;
import com.cm.common.wechat.pojo.WechatMiniAppUserInfo;
import com.cm.common.wechat.pojo.WechatOfficialAccountUserInfo;
@ -73,10 +74,10 @@ public class WechatMiniAppAuthServiceImpl implements IWechatMiniAppAuthService {
if (wechatMiniAppUserInfo.getErrcode() != null && wechatMiniAppUserInfo.getErrcode() != 0) {
throw new WechatAccessTokenForUserException(String.format("获取用户信息失败,错误码:%s错误信息%s", wechatMiniAppUserInfo.getErrcode(), wechatMiniAppUserInfo.getErrmsg()));
}
LOG.debug("绑定用户 | 登录");
String wechatSignInfo = Base64.encodeBase64String(AesUtil.aesCommonEncoder("WECHAT_SIGN_INFO", new StringBuilder(wechatMiniAppUserInfo.getOpenid()).append("_WenG_").append(wechatMiniAppProperties.getAppKey()).toString()).getBytes("UTF-8"));
String token = getAppToken(wechatSignInfo);
WechatMiniAppManager.getInstance().addUser(token, wechatMiniAppUserInfo);
return new SuccessResultData(token);
}

View File

@ -1,6 +1,7 @@
package com.cm.common.wechat.task;
import com.cm.common.wechat.config.pojo.WechatOfficialAccountProperties;
import com.cm.common.wechat.manager.miniapp.WechatMiniAppManager;
import com.cm.common.wechat.manager.officialaccount.WechatOfficialAccountManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -39,4 +40,12 @@ public class WechatTask {
WechatOfficialAccountManager.getInstance().refreshAccessToken();
}
/**
* 删除小程序超时用户每10分钟执行一次
*/
@Scheduled(cron = "0 0/10 * * * ?")
public void removeTimeoutMiniAppUser() {
WechatMiniAppManager.getInstance().removeTimeoutUser();
}
}