From ae06a9791f2e0e52f3efc9eff4fbc9ab96604505 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Thu, 29 Oct 2020 18:43:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manager/miniapp/WechatMiniAppManager.java | 61 ++++++ .../wechat/pojo/WechatMiniAppUserInfo.java | 15 +- .../pojo/WechatMiniAppUserInfoDetail.java | 187 ++++++++++++++++++ .../impl/WechatMiniAppAuthServiceImpl.java | 3 +- .../com/cm/common/wechat/task/WechatTask.java | 9 + 5 files changed, 272 insertions(+), 3 deletions(-) create mode 100644 cloud-common-wechat/src/main/java/com/cm/common/wechat/pojo/WechatMiniAppUserInfoDetail.java diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/manager/miniapp/WechatMiniAppManager.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/manager/miniapp/WechatMiniAppManager.java index 531c32a..a905e9b 100644 --- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/manager/miniapp/WechatMiniAppManager.java +++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/manager/miniapp/WechatMiniAppManager.java @@ -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 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 tokenList = new ArrayList<>(); + for (Map.Entry 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(); + } } diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/pojo/WechatMiniAppUserInfo.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/pojo/WechatMiniAppUserInfo.java index f84c42f..4fe6418 100644 --- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/pojo/WechatMiniAppUserInfo.java +++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/pojo/WechatMiniAppUserInfo.java @@ -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(); } diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/pojo/WechatMiniAppUserInfoDetail.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/pojo/WechatMiniAppUserInfoDetail.java new file mode 100644 index 0000000..5d55f9f --- /dev/null +++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/pojo/WechatMiniAppUserInfoDetail.java @@ -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(); + } + } + +} diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/service/impl/WechatMiniAppAuthServiceImpl.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/service/impl/WechatMiniAppAuthServiceImpl.java index 85bf99b..4a8fc1c 100644 --- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/service/impl/WechatMiniAppAuthServiceImpl.java +++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/service/impl/WechatMiniAppAuthServiceImpl.java @@ -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); } diff --git a/cloud-common-wechat/src/main/java/com/cm/common/wechat/task/WechatTask.java b/cloud-common-wechat/src/main/java/com/cm/common/wechat/task/WechatTask.java index f32ef79..aedd22a 100644 --- a/cloud-common-wechat/src/main/java/com/cm/common/wechat/task/WechatTask.java +++ b/cloud-common-wechat/src/main/java/com/cm/common/wechat/task/WechatTask.java @@ -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(); + } + }