diff --git a/basic-properties/src/main/java/ink/wgink/properties/wechat/official/account/OfficialAccountProperties.java b/basic-properties/src/main/java/ink/wgink/properties/wechat/official/account/OfficialAccountProperties.java index 58ce796e..4bac42d3 100644 --- a/basic-properties/src/main/java/ink/wgink/properties/wechat/official/account/OfficialAccountProperties.java +++ b/basic-properties/src/main/java/ink/wgink/properties/wechat/official/account/OfficialAccountProperties.java @@ -24,6 +24,7 @@ public class OfficialAccountProperties { private String appId; private String appSecret; private String grantType; + private String configToken; public Boolean getApiCrossOrigin() { return apiCrossOrigin; @@ -80,4 +81,12 @@ public class OfficialAccountProperties { public void setGrantType(String grantType) { this.grantType = grantType; } + + public String getConfigToken() { + return configToken == null ? "" : configToken; + } + + public void setConfigToken(String configToken) { + this.configToken = configToken; + } } diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/controller/official/account/OfficialAccountWechatController.java b/module-wechat/src/main/java/ink/wgink/module/wechat/controller/official/account/OfficialAccountWechatController.java new file mode 100644 index 00000000..72284513 --- /dev/null +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/controller/official/account/OfficialAccountWechatController.java @@ -0,0 +1,113 @@ +package ink.wgink.module.wechat.controller.official.account; + +import ink.wgink.common.base.DefaultBaseController; +import ink.wgink.exceptions.ParamsException; +import ink.wgink.interfaces.consts.ISystemConstant; +import ink.wgink.module.wechat.enums.XmlEventTypeEnum; +import ink.wgink.module.wechat.enums.XmlMsgTypeEnum; +import ink.wgink.module.wechat.pojo.vos.official.account.OfficialAccountUserPO; +import ink.wgink.module.wechat.result.OfficialAccountEventResult; +import ink.wgink.module.wechat.service.official.account.IOfficialAccountCheckService; +import ink.wgink.module.wechat.service.official.account.IOfficialAccountUserService; +import ink.wgink.util.xml.XMLUtil; +import io.swagger.annotations.Api; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpMethod; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.AsyncContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.concurrent.CompletableFuture; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: OfficialAccountWechatController + * @Description: 微信公众号 + * @Author: wanggeng + * @Date: 2021/4/28 3:15 下午 + * @Version: 1.0 + */ +@Api(tags = ISystemConstant.API_TAGS_WECHAT_PREFIX + "微信公众号") +@RestController +@RequestMapping(ISystemConstant.WECHAT_PREFIX + "/official/account") +public class OfficialAccountWechatController extends DefaultBaseController { + + @Autowired + private IOfficialAccountCheckService officialAccountCheckService; + @Autowired + private IOfficialAccountUserService officialAccountUserService; + + @RequestMapping("event") + public void event(HttpServletRequest request, HttpServletResponse response) throws IOException { + + // 处理认证 + if (StringUtils.equalsIgnoreCase(HttpMethod.GET.toString(), request.getMethod())) { + officialAccountCheckService.checkSignature(request, response); + return; + } + // 处理事件 + if (StringUtils.equalsIgnoreCase(HttpMethod.POST.toString(), request.getMethod())) { + AsyncContext asyncContext = request.startAsync(); + CompletableFuture.runAsync(() -> { + handleMsgType(asyncContext, request, response); + }); + return; + } + } + + /** + * 处理消息类型 + * + * @param asyncContext + * @param request + * @param response + */ + private void handleMsgType(AsyncContext asyncContext, HttpServletRequest request, HttpServletResponse response) { + OfficialAccountEventResult officialAccountEventResult = getEventResult(request); + if (officialAccountEventResult == null) { + throw new ParamsException("事件错误"); + } + if (StringUtils.equalsIgnoreCase(XmlMsgTypeEnum.EVENT.getValue(), officialAccountEventResult.getMsgType())) { + handleEvent(officialAccountEventResult, asyncContext, request, response); + } + } + + // 处理事件 + private void handleEvent(OfficialAccountEventResult officialAccountEventResult, AsyncContext asyncContext, HttpServletRequest request, HttpServletResponse response) { + if (StringUtils.equalsIgnoreCase(XmlEventTypeEnum.SUBSCRIBE.getType(), officialAccountEventResult.getEvent())) { + OfficialAccountUserPO officialAccountUserPO = new OfficialAccountUserPO(); + officialAccountUserPO.setOpenId(officialAccountEventResult.getFromUserName()); + officialAccountUserService.save(officialAccountUserPO); + } else if(StringUtils.equalsIgnoreCase(XmlEventTypeEnum.UNSUBSCRIBE.getType(), officialAccountEventResult.getEvent())) { + officialAccountUserService.updateStatus(officialAccountEventResult.getFromUserName(), XmlEventTypeEnum.UNSUBSCRIBE); + } + } + + /** + * 事件结果 + * + * @param request + * @return + */ + private OfficialAccountEventResult getEventResult(HttpServletRequest request) { + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(request.getInputStream()))) { + StringBuilder content = new StringBuilder(); + for (String line; (line = bufferedReader.readLine()) != null; ) { + content.append(line); + } + return XMLUtil.xml2SampleBean(content.toString(), OfficialAccountEventResult.class); + } catch (Exception e) { + LOG.error(e.getMessage(), e); + } + return null; + } + +} diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/dao/official/account/IOfficialAccountUserDao.java b/module-wechat/src/main/java/ink/wgink/module/wechat/dao/official/account/IOfficialAccountUserDao.java new file mode 100644 index 00000000..cc541072 --- /dev/null +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/dao/official/account/IOfficialAccountUserDao.java @@ -0,0 +1,94 @@ +package ink.wgink.module.wechat.dao.official.account; + +import ink.wgink.exceptions.RemoveException; +import ink.wgink.exceptions.SaveException; +import ink.wgink.exceptions.SearchException; +import ink.wgink.exceptions.UpdateException; +import ink.wgink.module.wechat.pojo.dtos.official.account.OfficialAccountUserDTO; +import ink.wgink.module.wechat.pojo.vos.official.account.OfficialAccountUserPO; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: IAccountOfficialUserDao + * @Description: 公众号用户 + * @Author: wanggeng + * @Date: 2021/4/28 6:42 下午 + * @Version: 1.0 + */ +@Repository +public interface IOfficialAccountUserDao { + + /** + * 建表 + * + * @throws UpdateException + */ + void createTable() throws UpdateException; + + /** + * 新增 + * + * @param params + * @throws SaveException + */ + void save(Map params) throws SaveException; + + /** + * 修改 + * + * @param params + * @throws UpdateException + */ + void update(Map params) throws UpdateException; + + /** + * 删除 + * + * @param params + * @throws RemoveException + */ + void remove(Map params) throws RemoveException; + + /** + * 详情 + * + * @param params + * @return + * @throws SearchException + */ + OfficialAccountUserDTO get(Map params) throws SearchException; + + /** + * 详情 + * + * @param params + * @return + * @throws SearchException + */ + OfficialAccountUserPO getPO(Map params) throws SearchException; + + /** + * 列表 + * + * @param params + * @return + * @throws SearchException + */ + List list(Map params) throws SearchException; + + /** + * 列表 + * + * @param params + * @return + * @throws SearchException + */ + List listPO(Map params) throws SearchException; + +} diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/enums/ErrorMsgEnum.java b/module-wechat/src/main/java/ink/wgink/module/wechat/enums/ErrorMsgEnum.java index 675dd7b7..4ef5abf1 100644 --- a/module-wechat/src/main/java/ink/wgink/module/wechat/enums/ErrorMsgEnum.java +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/enums/ErrorMsgEnum.java @@ -17,6 +17,7 @@ public enum ErrorMsgEnum { ERROR_40001(40001, "AppSecret错误或者AppSecret不属于这个公众号,请开发者确认AppSecret的正确性"), ERROR_40002(40002, "请确保grant_type字段值为client_credential"), ERROR_40164(40164, "调用接口的IP地址不在白名单中,请在接口IP白名单中进行设置。(小程序及小游戏调用不要求IP地址在白名单内。)"), + ERROR_41002(41002, "appid为空"), ERROR_89503(89503, "此IP调用需要管理员确认,请联系管理员"), ERROR_89501(89501, "此IP正在等待管理员确认,请联系管理员"), ERROR_89506(89506, "24小时内该IP被管理员拒绝调用两次,24小时内不可再使用该IP调用"), diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/enums/XmlEventTypeEnum.java b/module-wechat/src/main/java/ink/wgink/module/wechat/enums/XmlEventTypeEnum.java new file mode 100644 index 00000000..39d5f232 --- /dev/null +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/enums/XmlEventTypeEnum.java @@ -0,0 +1,34 @@ +package ink.wgink.module.wechat.enums; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: EventTypeEnum + * @Description: 事件类型 + * @Author: wanggeng + * @Date: 2021/4/28 6:19 下午 + * @Version: 1.0 + */ +public enum XmlEventTypeEnum { + + SUBSCRIBE("subscribe", "订阅"), + UNSUBSCRIBE("unsubscribe", "取消订阅"); + + + private String type; + private String describe; + + XmlEventTypeEnum(String type, String describe) { + this.type = type; + this.describe = describe; + } + + public String getType() { + return type == null ? "" : type; + } + + public String getDescribe() { + return describe == null ? "" : describe; + } +} diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/enums/XmlMsgTypeEnum.java b/module-wechat/src/main/java/ink/wgink/module/wechat/enums/XmlMsgTypeEnum.java new file mode 100644 index 00000000..1a89df54 --- /dev/null +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/enums/XmlMsgTypeEnum.java @@ -0,0 +1,26 @@ +package ink.wgink.module.wechat.enums; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: XMLMsgTypeEnum + * @Description: xml消息类型 + * @Author: wanggeng + * @Date: 2021/4/28 6:20 下午 + * @Version: 1.0 + */ +public enum XmlMsgTypeEnum { + + EVENT("event"); + + private String value; + + XmlMsgTypeEnum(String value) { + this.value = value; + } + + public String getValue() { + return value == null ? "" : value; + } +} diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/official/account/manager/OfficialAccountAccessTokenManager.java b/module-wechat/src/main/java/ink/wgink/module/wechat/manager/OfficialAccountAccessTokenManager.java similarity index 87% rename from module-wechat/src/main/java/ink/wgink/module/wechat/official/account/manager/OfficialAccountAccessTokenManager.java rename to module-wechat/src/main/java/ink/wgink/module/wechat/manager/OfficialAccountAccessTokenManager.java index a6653980..596a3bcb 100644 --- a/module-wechat/src/main/java/ink/wgink/module/wechat/official/account/manager/OfficialAccountAccessTokenManager.java +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/manager/OfficialAccountAccessTokenManager.java @@ -1,7 +1,7 @@ -package ink.wgink.module.wechat.official.account.manager; +package ink.wgink.module.wechat.manager; -import ink.wgink.module.wechat.official.account.pojo.OfficialAccountAccessToken; -import ink.wgink.module.wechat.official.account.request.AccessTokenRestTemplate; +import ink.wgink.module.wechat.pojo.OfficialAccountAccessToken; +import ink.wgink.module.wechat.request.AccessTokenRestTemplate; import ink.wgink.properties.wechat.official.account.OfficialAccountProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,7 +43,7 @@ public class OfficialAccountAccessTokenManager { } LOG.debug("刷新公众号 AccessToken 开始"); AccessTokenRestTemplate accessTokenRestTemplate = new AccessTokenRestTemplate(officialAccountProperties); - accessTokenRestTemplate.post(officialAccountProperties.getAccessTokenUrl()); + accessTokenRestTemplate.get(accessTokenRestTemplate.getAccessTokenUrl()); LOG.debug("accessToken: {}", this.officialAccountAccessToken); LOG.debug("刷新公众号 AccessToken 结束"); } diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/official/account/pojo/OfficialAccountAccessToken.java b/module-wechat/src/main/java/ink/wgink/module/wechat/pojo/OfficialAccountAccessToken.java similarity index 95% rename from module-wechat/src/main/java/ink/wgink/module/wechat/official/account/pojo/OfficialAccountAccessToken.java rename to module-wechat/src/main/java/ink/wgink/module/wechat/pojo/OfficialAccountAccessToken.java index 3b6a3316..e9b244a0 100644 --- a/module-wechat/src/main/java/ink/wgink/module/wechat/official/account/pojo/OfficialAccountAccessToken.java +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/pojo/OfficialAccountAccessToken.java @@ -1,4 +1,4 @@ -package ink.wgink.module.wechat.official.account.pojo; +package ink.wgink.module.wechat.pojo; /** * When you feel like quitting. Think about why you started diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/pojo/dtos/official/account/OfficialAccountUserDTO.java b/module-wechat/src/main/java/ink/wgink/module/wechat/pojo/dtos/official/account/OfficialAccountUserDTO.java new file mode 100644 index 00000000..a0341887 --- /dev/null +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/pojo/dtos/official/account/OfficialAccountUserDTO.java @@ -0,0 +1,14 @@ +package ink.wgink.module.wechat.pojo.dtos.official.account; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: OfficialAccountUserDTO + * @Description: 公众号用户 + * @Author: wanggeng + * @Date: 2021/4/28 6:46 下午 + * @Version: 1.0 + */ +public class OfficialAccountUserDTO { +} diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/pojo/pos/official/account/OfficialAccountUserVO.java b/module-wechat/src/main/java/ink/wgink/module/wechat/pojo/pos/official/account/OfficialAccountUserVO.java new file mode 100644 index 00000000..b998f950 --- /dev/null +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/pojo/pos/official/account/OfficialAccountUserVO.java @@ -0,0 +1,60 @@ +package ink.wgink.module.wechat.pojo.pos.official.account; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: OfficialAccountUserVO + * @Description: 公众号用户 + * @Author: wanggeng + * @Date: 2021/4/28 6:10 下午 + * @Version: 1.0 + */ +public class OfficialAccountUserVO { + + private String appId; + private String openId; + private String userId; + private String userCode; + private Integer isInitAccount; + + public String getAppId() { + return appId == null ? "" : appId; + } + + public void setAppId(String appId) { + this.appId = appId; + } + + public String getOpenId() { + return openId == null ? "" : openId; + } + + public void setOpenId(String openId) { + this.openId = openId; + } + + public String getUserId() { + return userId == null ? "" : userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getUserCode() { + return userCode == null ? "" : userCode; + } + + public void setUserCode(String userCode) { + this.userCode = userCode; + } + + public Integer getIsInitAccount() { + return isInitAccount == null ? 0 : isInitAccount; + } + + public void setIsInitAccount(Integer isInitAccount) { + this.isInitAccount = isInitAccount; + } +} diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/pojo/vos/official/account/OfficialAccountUserPO.java b/module-wechat/src/main/java/ink/wgink/module/wechat/pojo/vos/official/account/OfficialAccountUserPO.java new file mode 100644 index 00000000..263ebe75 --- /dev/null +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/pojo/vos/official/account/OfficialAccountUserPO.java @@ -0,0 +1,69 @@ +package ink.wgink.module.wechat.pojo.vos.official.account; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: OfficialAccountUserPO + * @Description: 公众号用户 + * @Author: wanggeng + * @Date: 2021/4/28 6:13 下午 + * @Version: 1.0 + */ +public class OfficialAccountUserPO { + + private String appId; + private String openId; + private String userId; + private String userCode; + private Integer isInitAccount; + private String gmtCreate; + + public String getAppId() { + return appId == null ? "" : appId; + } + + public void setAppId(String appId) { + this.appId = appId; + } + + public String getOpenId() { + return openId == null ? "" : openId; + } + + public void setOpenId(String openId) { + this.openId = openId; + } + + public String getUserId() { + return userId == null ? "" : userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getUserCode() { + return userCode == null ? "" : userCode; + } + + public void setUserCode(String userCode) { + this.userCode = userCode; + } + + public Integer getIsInitAccount() { + return isInitAccount == null ? 0 : isInitAccount; + } + + public void setIsInitAccount(Integer isInitAccount) { + this.isInitAccount = isInitAccount; + } + + public String getGmtCreate() { + return gmtCreate == null ? "" : gmtCreate; + } + + public void setGmtCreate(String gmtCreate) { + this.gmtCreate = gmtCreate; + } +} diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/official/account/request/AccessTokenRestTemplate.java b/module-wechat/src/main/java/ink/wgink/module/wechat/request/AccessTokenRestTemplate.java similarity index 65% rename from module-wechat/src/main/java/ink/wgink/module/wechat/official/account/request/AccessTokenRestTemplate.java rename to module-wechat/src/main/java/ink/wgink/module/wechat/request/AccessTokenRestTemplate.java index 7304ccfc..e880eb5d 100644 --- a/module-wechat/src/main/java/ink/wgink/module/wechat/official/account/request/AccessTokenRestTemplate.java +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/request/AccessTokenRestTemplate.java @@ -1,13 +1,15 @@ -package ink.wgink.module.wechat.official.account.request; +package ink.wgink.module.wechat.request; import ink.wgink.module.wechat.enums.ErrorMsgEnum; -import ink.wgink.module.wechat.official.account.manager.OfficialAccountAccessTokenManager; -import ink.wgink.module.wechat.official.account.pojo.OfficialAccountAccessToken; -import ink.wgink.module.wechat.official.account.result.AccessTokenResult; +import ink.wgink.module.wechat.manager.OfficialAccountAccessTokenManager; +import ink.wgink.module.wechat.pojo.OfficialAccountAccessToken; +import ink.wgink.module.wechat.result.AccessTokenResult; import ink.wgink.properties.wechat.official.account.OfficialAccountProperties; import ink.wgink.util.request.AbstractRestTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.client.HttpClientErrorException; import java.util.HashMap; @@ -24,7 +26,7 @@ import java.util.concurrent.TimeUnit; * @Date: 2021/4/28 5:31 上午 * @Version: 1.0 */ -public class AccessTokenRestTemplate extends AbstractRestTemplate> { +public class AccessTokenRestTemplate extends AbstractRestTemplate { private static final Logger LOG = LoggerFactory.getLogger(AccessTokenRestTemplate.class); private OfficialAccountProperties officialAccountProperties; @@ -33,12 +35,12 @@ public class AccessTokenRestTemplate extends AbstractRestTemplate postBody() { - Map params = new HashMap<>(6); - params.put("grant_type", officialAccountProperties.getGrantType()); - params.put("appid", officialAccountProperties.getAppId()); - params.put("secret", officialAccountProperties.getAppSecret()); - return params; + public MultiValueMap postBody() { + MultiValueMap multiValueMap = new LinkedMultiValueMap<>(); + multiValueMap.add("grant_type", officialAccountProperties.getGrantType()); + multiValueMap.add("appid", officialAccountProperties.getAppId()); + multiValueMap.add("secret", officialAccountProperties.getAppSecret()); + return multiValueMap; } @Override @@ -62,7 +64,7 @@ public class AccessTokenRestTemplate extends AbstractRestTemplate sortList = new ArrayList<>(); + sortList.add(officialAccountProperties.getConfigToken()); + sortList.add(timestamp); + sortList.add(nonce); + Collections.sort(sortList); + StringBuilder newSignature = new StringBuilder(); + sortList.forEach(str -> { + newSignature.append(str); + }); + + if (!StringUtils.equals(signature, DigestUtils.sha1Hex(newSignature.toString()))) { + return false; + } + return true; + } + +} diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/impl/OfficialAccountUserServiceImpl.java b/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/impl/OfficialAccountUserServiceImpl.java new file mode 100644 index 00000000..589428fc --- /dev/null +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/impl/OfficialAccountUserServiceImpl.java @@ -0,0 +1,36 @@ +package ink.wgink.module.wechat.service.official.account.impl; + +import ink.wgink.common.base.DefaultBaseService; +import ink.wgink.module.wechat.dao.official.account.IOfficialAccountUserDao; +import ink.wgink.module.wechat.enums.XmlEventTypeEnum; +import ink.wgink.module.wechat.pojo.vos.official.account.OfficialAccountUserPO; +import ink.wgink.module.wechat.service.official.account.IOfficialAccountUserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: OfficialAccountUserServiceImpl + * @Description: 公众号用户 + * @Author: wanggeng + * @Date: 2021/4/28 6:15 下午 + * @Version: 1.0 + */ +@Service +public class OfficialAccountUserServiceImpl extends DefaultBaseService implements IOfficialAccountUserService { + + @Autowired + private IOfficialAccountUserDao officialAccountUserDao; + + @Override + public void save(OfficialAccountUserPO officialAccountUserPO) { + + } + + @Override + public void updateStatus(String fromUserName, XmlEventTypeEnum unsubscribe) { + + } +} diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/startup/WechatStartUp.java b/module-wechat/src/main/java/ink/wgink/module/wechat/startup/WechatStartUp.java index 38b79ff8..c24e73f0 100644 --- a/module-wechat/src/main/java/ink/wgink/module/wechat/startup/WechatStartUp.java +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/startup/WechatStartUp.java @@ -1,23 +1,15 @@ package ink.wgink.module.wechat.startup; -import ink.wgink.module.wechat.official.account.manager.OfficialAccountAccessTokenManager; -import ink.wgink.module.wechat.official.account.pojo.OfficialAccountEvent; +import ink.wgink.module.wechat.manager.OfficialAccountAccessTokenManager; import ink.wgink.properties.wechat.official.account.OfficialAccountProperties; -import ink.wgink.util.xml.XMLUtil; -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.Element; -import org.dom4j.io.SAXReader; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; -import java.io.ByteArrayInputStream; -import java.io.StringReader; -import java.nio.charset.StandardCharsets; -import java.util.List; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; /** * When you feel like quitting. Think about why you started @@ -37,9 +29,10 @@ public class WechatStartUp implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { + new Thread(() -> refreshOfficialAccountAccessToken()).start(); } - @Scheduled(cron = "0 0/5 * * * ? *") + @Scheduled(cron = "0 0/5 * * * ?") public void refreshOfficialAccountAccessToken() { OfficialAccountAccessTokenManager.getInstance().refreshAccessToken(officialAccountProperties); } diff --git a/module-wechat/src/main/resources/mybatis/mapper/official-account-user-mapper.xml b/module-wechat/src/main/resources/mybatis/mapper/official-account-user-mapper.xml new file mode 100644 index 00000000..b5b1e7a2 --- /dev/null +++ b/module-wechat/src/main/resources/mybatis/mapper/official-account-user-mapper.xml @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + CREATE TABLE IF NOT EXISTS `wechat_mini_app_user` ( + `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `app_id` varchar(255) DEFAULT NULL COMMENT 'appid', + `open_id` varchar(255) DEFAULT NULL COMMENT 'openid', + `user_id` varchar(255) DEFAULT NULL COMMENT '用户ID', + `is_init_account` int(1) DEFAULT '0' COMMENT '是否初始化账户', + `gmt_create` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `app_id` (`app_id`) USING BTREE, + KEY `open_id` (`open_id`) USING BTREE, + KEY `user_id` (`user_id`) USING BTREE + ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='小程序用户'; + + + + + INSERT INTO wechat_mini_app_user( + app_id, + open_id, + user_id, + is_init_account, + gmt_create + ) VALUES( + #{appId}, + #{openId}, + #{userId}, + #{isInitAccount}, + #{gmtCreate} + ) + + + + + + + + \ No newline at end of file