From def94c3996006d89267624746f958a07201f786b Mon Sep 17 00:00:00 2001 From: wanggeng888 <450292408@qq.com> Date: Sun, 2 May 2021 12:22:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=9B=9E=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OfficialAccountWechatController.java | 16 +++--- .../account/IOfficialAccountUserDao.java | 7 +++ .../module/wechat/enums/XmlMsgTypeEnum.java | 3 +- .../result/OfficialAccountEventResult.java | 11 ++++ .../account/BaseOfficialAccountService.java | 31 +++++++++++ .../account/IOfficialAccountTextService.java | 30 +++++++++++ .../account/IOfficialAccountUserService.java | 10 +++- .../impl/OfficialAccountTextServiceImpl.java | 54 +++++++++++++++++++ .../impl/OfficialAccountUserServiceImpl.java | 26 ++++++++- .../mapper/official-account-user-mapper.xml | 18 ++++++- 10 files changed, 196 insertions(+), 10 deletions(-) create mode 100644 module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/BaseOfficialAccountService.java create mode 100644 module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/IOfficialAccountTextService.java create mode 100644 module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/impl/OfficialAccountTextServiceImpl.java 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 index 5c655b90..a0bc5d71 100644 --- 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 @@ -9,6 +9,7 @@ import ink.wgink.module.wechat.enums.XmlMsgTypeEnum; import ink.wgink.module.wechat.pojo.vos.official.account.OfficialAccountUserVO; import ink.wgink.module.wechat.result.OfficialAccountEventResult; import ink.wgink.module.wechat.service.official.account.IOfficialAccountCheckService; +import ink.wgink.module.wechat.service.official.account.IOfficialAccountTextService; import ink.wgink.module.wechat.service.official.account.IOfficialAccountUserService; import ink.wgink.properties.wechat.official.account.OfficialAccountProperties; import ink.wgink.util.xml.XMLUtil; @@ -49,6 +50,8 @@ public class OfficialAccountWechatController extends DefaultBaseController { private IOfficialAccountCheckService officialAccountCheckService; @Autowired private IOfficialAccountUserService officialAccountUserService; + @Autowired + private IOfficialAccountTextService officialAccountTextService; @RequestMapping("event") public void event(HttpServletRequest request, HttpServletResponse response) throws IOException { @@ -83,19 +86,20 @@ public class OfficialAccountWechatController extends DefaultBaseController { */ private void handleMsgType(AsyncContext asyncContext, HttpServletRequest request, HttpServletResponse response) throws IOException { OfficialAccountEventResult officialAccountEventResult = getEventResult(request); - // 解析完流之后,直接返回,防止微信服务器重复请求 - PrintWriter writer = response.getWriter(); - writer.write(""); - writer.flush(); - asyncContext.complete(); // 处理消息事件 if (officialAccountEventResult == null) { throw new ParamsException("事件错误"); } if (StringUtils.equalsIgnoreCase(XmlMsgTypeEnum.EVENT.getValue(), officialAccountEventResult.getMsgType())) { + // 解析完流之后,直接返回,防止微信服务器重复请求 + PrintWriter writer = response.getWriter(); + writer.write(""); + writer.flush(); + asyncContext.complete(); handleEvent(officialAccountEventResult, request, response); + } else if (StringUtils.equalsIgnoreCase(XmlMsgTypeEnum.TEXT.getValue(), officialAccountEventResult.getMsgType())) { + officialAccountTextService.getReplay(officialAccountEventResult, asyncContext, response); } - } /** 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 index 0b910854..bfdde06b 100644 --- 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 @@ -47,6 +47,13 @@ public interface IOfficialAccountUserDao { */ void update(Map params) throws UpdateException; + /** + * 修改用户码(邀请码) + * @param params + * @throws UpdateException + */ + void updateUserCode(Map params) throws UpdateException; + /** * 删除 * 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 index 1a89df54..230e3617 100644 --- 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 @@ -12,7 +12,8 @@ package ink.wgink.module.wechat.enums; */ public enum XmlMsgTypeEnum { - EVENT("event"); + EVENT("event"), + TEXT("text"); private String value; diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/result/OfficialAccountEventResult.java b/module-wechat/src/main/java/ink/wgink/module/wechat/result/OfficialAccountEventResult.java index b6c75398..a38804f0 100644 --- a/module-wechat/src/main/java/ink/wgink/module/wechat/result/OfficialAccountEventResult.java +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/result/OfficialAccountEventResult.java @@ -18,6 +18,7 @@ public class OfficialAccountEventResult { private String msgType; private String event; private String eventKey; + private String content; public String getToUserName() { return toUserName == null ? "" : toUserName; @@ -67,6 +68,14 @@ public class OfficialAccountEventResult { this.eventKey = eventKey; } + public String getContent() { + return content == null ? "" : content; + } + + public void setContent(String content) { + this.content = content; + } + @Override public String toString() { final StringBuilder sb = new StringBuilder("{"); @@ -82,6 +91,8 @@ public class OfficialAccountEventResult { .append(event).append('\"'); sb.append(",\"eventKey\":\"") .append(eventKey).append('\"'); + sb.append(",\"content\":\"") + .append(content).append('\"'); sb.append('}'); return sb.toString(); } diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/BaseOfficialAccountService.java b/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/BaseOfficialAccountService.java new file mode 100644 index 00000000..e6edc382 --- /dev/null +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/BaseOfficialAccountService.java @@ -0,0 +1,31 @@ +package ink.wgink.module.wechat.service.official.account; + +import ink.wgink.common.base.DefaultBaseService; +import ink.wgink.interfaces.consts.ISystemConstant; + +import javax.servlet.AsyncContext; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: BaseOfficialAccountService + * @Description: 公众号基类 + * @Author: wanggeng + * @Date: 2021/5/2 11:11 上午 + * @Version: 1.0 + */ +public class BaseOfficialAccountService extends DefaultBaseService { + + protected void write(AsyncContext asyncContext, HttpServletResponse response, String content) throws IOException { + response.setCharacterEncoding(ISystemConstant.CHARSET_UTF8); + PrintWriter writer = response.getWriter(); + writer.write(content); + writer.flush(); + asyncContext.complete(); + } + +} diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/IOfficialAccountTextService.java b/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/IOfficialAccountTextService.java new file mode 100644 index 00000000..591f3892 --- /dev/null +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/IOfficialAccountTextService.java @@ -0,0 +1,30 @@ +package ink.wgink.module.wechat.service.official.account; + +import ink.wgink.module.wechat.result.OfficialAccountEventResult; + +import javax.servlet.AsyncContext; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: IOfficialAccountAutoReplyService + * @Description: 自动回复 + * @Author: wanggeng + * @Date: 2021/5/2 10:54 上午 + * @Version: 1.0 + */ +public interface IOfficialAccountTextService { + + /** + * 回复 + * + * @param officialAccountEventResult + * @param asyncContext + * @param response + * @throws IOException + */ + void getReplay(OfficialAccountEventResult officialAccountEventResult, AsyncContext asyncContext, HttpServletResponse response) throws IOException; +} diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/IOfficialAccountUserService.java b/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/IOfficialAccountUserService.java index dde1fe28..499a15ad 100644 --- a/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/IOfficialAccountUserService.java +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/IOfficialAccountUserService.java @@ -1,5 +1,6 @@ package ink.wgink.module.wechat.service.official.account; +import ink.wgink.interfaces.wechat.official.account.IAccountOfficialUserBaseService; import ink.wgink.module.wechat.enums.XmlEventTypeEnum; import ink.wgink.module.wechat.pojo.dtos.official.account.OfficialAccountUserDTO; import ink.wgink.module.wechat.pojo.pos.official.account.OfficialAccountUserPO; @@ -20,7 +21,7 @@ import java.util.Map; * @Date: 2021/4/28 6:15 下午 * @Version: 1.0 */ -public interface IOfficialAccountUserService { +public interface IOfficialAccountUserService extends IAccountOfficialUserBaseService { /** * 公众号用户前缀 @@ -100,4 +101,11 @@ public interface IOfficialAccountUserService { */ SuccessResultList> listPage(ListPage page); + /** + * 获取用户码 + * + * @param openId 公众号用户openId + * @return + */ + String getUserCodeByOpenId(String openId); } diff --git a/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/impl/OfficialAccountTextServiceImpl.java b/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/impl/OfficialAccountTextServiceImpl.java new file mode 100644 index 00000000..fbf7f3f9 --- /dev/null +++ b/module-wechat/src/main/java/ink/wgink/module/wechat/service/official/account/impl/OfficialAccountTextServiceImpl.java @@ -0,0 +1,54 @@ +package ink.wgink.module.wechat.service.official.account.impl; + +import ink.wgink.module.wechat.result.OfficialAccountEventResult; +import ink.wgink.module.wechat.service.official.account.BaseOfficialAccountService; +import ink.wgink.module.wechat.service.official.account.IOfficialAccountTextService; +import ink.wgink.module.wechat.service.official.account.IOfficialAccountUserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.servlet.AsyncContext; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: OfficialAccountTextServiceImpl + * @Description: 自动回复 + * @Author: wanggeng + * @Date: 2021/5/2 10:54 上午 + * @Version: 1.0 + */ +@Service +public class OfficialAccountTextServiceImpl extends BaseOfficialAccountService implements IOfficialAccountTextService { + + public static String[] KEYWORD_ARRAY = {"验证码"}; + @Autowired + private IOfficialAccountUserService officialAccountUserService; + + @Override + public void getReplay(OfficialAccountEventResult officialAccountEventResult, AsyncContext asyncContext, HttpServletResponse response) throws IOException { + String content = officialAccountEventResult.getContent().replaceAll("\\s+", ""); + if (content.contains(KEYWORD_ARRAY[0])) { + String userCode = officialAccountUserService.getUserCodeByOpenId(officialAccountEventResult.getFromUserName()); + write(asyncContext, response, responseXML(officialAccountEventResult.getFromUserName(), + officialAccountEventResult.getToUserName(), "您的验证码为:" + userCode)); + return; + } + write(asyncContext, response, ""); + } + + private String responseXML(String toUser, String fromUserName, String replay) { + return "" + + System.currentTimeMillis() + + ""; + } +} 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 index 7c2a6a5c..297bde76 100644 --- 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 @@ -16,6 +16,7 @@ import ink.wgink.service.user.service.IUserService; import ink.wgink.util.date.DateUtil; import ink.wgink.util.map.HashMapUtil; import ink.wgink.util.string.WStringUtil; +import ink.wgink.util.verification.share.ShareCodeUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -70,7 +71,6 @@ public class OfficialAccountUserServiceImpl extends DefaultBaseService implement // 用户码,用于绑定账号时使用 // 绑定用户 officialAccountUserVO.setUserId(userId); - officialAccountUserVO.setUserCode(WStringUtil.randomSubStr(String.valueOf(System.currentTimeMillis()), 6)); // 标记为初始化账号 officialAccountUserVO.setIsInitAccount(1); Map params = HashMapUtil.beanToMap(officialAccountUserVO); @@ -79,6 +79,10 @@ public class OfficialAccountUserServiceImpl extends DefaultBaseService implement params.put("gmtCreate", time); params.put("gmtModified", time); officialAccountUserDao.save(params); + // 更新用户码 + Long id = Long.parseLong(params.get("id").toString()); + String userCode = ShareCodeUtil.gen(id); + updateUserCode(openId, userCode); } @Override @@ -132,4 +136,24 @@ public class OfficialAccountUserServiceImpl extends DefaultBaseService implement public SuccessResultList> listPage(ListPage page) { return null; } + + @Override + public String getUserCodeByOpenId(String openId) { + OfficialAccountUserPO officialAccountUserPO = getPO(officialAccountProperties.getAppId(), openId); + return officialAccountUserPO.getUserCode(); + } + + + /** + * 更新用户码(邀请码) + * @param openId + * @param userCode + */ + private void updateUserCode(String openId, String userCode) { + Map params = getHashMap(6); + params.put("appId", officialAccountProperties.getAppId()); + params.put("openId", openId); + params.put("userCode", userCode); + officialAccountUserDao.updateUserCode(params); + } } 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 index 91808a88..fe248404 100644 --- 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 @@ -8,6 +8,7 @@ + @@ -43,7 +44,7 @@ - + INSERT INTO wechat_official_account_user( app_id, open_id, @@ -96,6 +97,19 @@ open_id = #{openId} + + + UPDATE + wechat_official_account_user + SET + user_code = #{userCode} + WHERE + app_id = #{appId} + AND + open_id = #{openId} + + +