新增验证码自动回复

This commit is contained in:
wanggeng888 2021-05-02 12:22:30 +08:00
parent 20bb1efcdc
commit def94c3996
10 changed files with 196 additions and 10 deletions

View File

@ -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);
}
}
/**

View File

@ -47,6 +47,13 @@ public interface IOfficialAccountUserDao {
*/
void update(Map<String, Object> params) throws UpdateException;
/**
* 修改用户码邀请码
* @param params
* @throws UpdateException
*/
void updateUserCode(Map<String, Object> params) throws UpdateException;
/**
* 删除
*

View File

@ -12,7 +12,8 @@ package ink.wgink.module.wechat.enums;
*/
public enum XmlMsgTypeEnum {
EVENT("event");
EVENT("event"),
TEXT("text");
private String value;

View File

@ -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();
}

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -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<List<OfficialAccountUserDTO>> listPage(ListPage page);
/**
* 获取用户码
*
* @param openId 公众号用户openId
* @return
*/
String getUserCodeByOpenId(String openId);
}

View File

@ -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 "<xml><ToUserName><![CDATA[" +
toUser +
"]]></ToUserName><FromUserName><![CDATA[" +
fromUserName +
"]]></FromUserName><CreateTime>" +
System.currentTimeMillis()
+ "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" +
replay
+ "]]></Content></xml>";
}
}

View File

@ -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<String, Object> 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<List<OfficialAccountUserDTO>> 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<String, Object> params = getHashMap(6);
params.put("appId", officialAccountProperties.getAppId());
params.put("openId", openId);
params.put("userCode", userCode);
officialAccountUserDao.updateUserCode(params);
}
}

View File

@ -8,6 +8,7 @@
<result column="app_id" property="appId"/>
<result column="open_id" property="openId"/>
<result column="user_id" property="userId"/>
<result column="user_code" property="userCode"/>
<result column="is_init_account" property="isInitAccount"/>
<result column="gmt_create" property="gmtCreate"/>
<result column="gmt_modified" property="gmtModified"/>
@ -43,7 +44,7 @@
</update>
<!-- 保存 -->
<insert id="save" parameterType="map" flushCache="true">
<insert id="save" parameterType="map" flushCache="true" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
INSERT INTO wechat_official_account_user(
app_id,
open_id,
@ -96,6 +97,19 @@
open_id = #{openId}
</update>
<!-- 修改用户码(邀请码) -->
<update id="updateUserCode" parameterType="map" flushCache="true">
UPDATE
wechat_official_account_user
SET
user_code = #{userCode}
WHERE
app_id = #{appId}
AND
open_id = #{openId}
</update>
<!-- 详情 -->
<select id="get" parameterType="map" resultMap="officialAccountUserDTO" useCache="true">
SELECT
@ -148,6 +162,7 @@
app_id,
open_id,
user_id,
user_code,
status,
is_init_account,
gmt_create,
@ -175,6 +190,7 @@
app_id,
open_id,
user_id,
user_code,
status,
is_init_account,
gmt_create,