parent
c4c24b42c7
commit
1b59077f3e
12 changed files with 213 additions and 35 deletions
@ -0,0 +1,71 @@ |
||||
package com.ynxbd.wx.wxfactory.bean.auth; |
||||
|
||||
import com.ynxbd.common.helper.common.DateHelper; |
||||
import com.ynxbd.common.helper.common.JsonHelper; |
||||
import com.ynxbd.wx.config.WeChatConfig; |
||||
import com.ynxbd.wx.wxfactory.AesWxHelper; |
||||
import lombok.Getter; |
||||
import lombok.NoArgsConstructor; |
||||
import lombok.Setter; |
||||
import lombok.ToString; |
||||
import org.apache.commons.lang3.ObjectUtils; |
||||
|
||||
import java.io.Serializable; |
||||
|
||||
@Getter |
||||
@Setter |
||||
@ToString |
||||
@NoArgsConstructor |
||||
public class AuthData implements Serializable { |
||||
private static final long serialVersionUID = 202511101525001L; |
||||
|
||||
private String openid; |
||||
private String createTime; |
||||
private String avatar; |
||||
private String nickName; |
||||
|
||||
public String createToken(String userOpenid, String avatar, String nickName) { |
||||
if (userOpenid == null) { |
||||
return null; |
||||
} |
||||
this.openid = userOpenid; |
||||
this.avatar = avatar; |
||||
this.nickName = nickName; |
||||
this.createTime = DateHelper.getCurDateTime(); |
||||
String dataJson = JsonHelper.toJsonString(this); |
||||
if (ObjectUtils.isEmpty(dataJson)) { |
||||
return null; |
||||
} |
||||
return AesWxHelper.encryptHex(dataJson, WeChatConfig.getAesKey(), WeChatConfig.getAesIV()); |
||||
} |
||||
|
||||
public String decodeToken(String token) { |
||||
if (ObjectUtils.isEmpty(token)) { |
||||
return null; |
||||
} |
||||
String dataJson = AesWxHelper.decryptHex(token, WeChatConfig.getAesKey(), WeChatConfig.getAesIV()); |
||||
if (ObjectUtils.isEmpty(dataJson)) { |
||||
return null; |
||||
} |
||||
AuthData authData = JsonHelper.parseObject(dataJson, AuthData.class); |
||||
if (authData == null) { |
||||
return null; |
||||
} |
||||
String cacheOpenid = authData.getOpenid(); |
||||
if (ObjectUtils.isEmpty(cacheOpenid)) { |
||||
return null; |
||||
} |
||||
|
||||
String cacheTime = authData.getCreateTime(); |
||||
Boolean hasValidity = DateHelper.inDateRangeByDay(-3, cacheTime); |
||||
if (hasValidity != null && hasValidity) { // 在有效期内
|
||||
this.openid = cacheOpenid; |
||||
this.avatar = authData.getAvatar(); |
||||
this.nickName = authData.getNickName(); |
||||
this.createTime = cacheTime; |
||||
return cacheOpenid; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue