天天看点

《果然新鲜》电商项目(19)- 公众号获取注册码功能

文章目录

  • ​​1.引言​​
  • ​​2.开发前准备​​
  • ​​2.1 注册码功能开发思路​​
  • ​​2.2 Redis服务器搭建​​
  • ​​2.3.开发前项目结构整理​​
  • ​​3.注册功能开发​​
  • ​​3.3.注册功能开发​​
  • ​​3.3.1.Redis封装​​
  • ​​3.3.2 正则表达式工具类​​
  • ​​3.3.3 MsgHandler验证码处理​​
  • ​​3.2 验证注册码​​
  • ​​4.测试​​
  • ​​4.1 获取注册码​​
  • ​​4.2 校验注册码​​
  • ​​5.总结​​

1.引言

上一节​​《果然新鲜电商项目(18)- 项目整合WxJava》​​,主要讲解如何把WxJava框架整合到我们的电商项目,并完成了“鹦鹉学舌”的功能。

本文主要实现「注册码功能」。在公众号里输入手机号码获取注册码的功能,验证注册码的功能。

2.开发前准备

2.1 注册码功能开发思路

本文的开发流程主要如下:

《果然新鲜》电商项目(19)- 公众号获取注册码功能

2.2 Redis服务器搭建

Redis的安装教程,本文不再具体详述.

《果然新鲜》电商项目(19)- 公众号获取注册码功能

2.3.开发前项目结构整理

目前项目结构是这样的:

《果然新鲜》电商项目(19)- 公众号获取注册码功能

其实项目结构还可以优化的,有两点:

  1. 将服务接口层的实体类抽出到一个模块,专门用来管理实体类。
  2. 统一规定微服务接口状态码

整理后的结构如下:

《果然新鲜》电商项目(19)- 公众号获取注册码功能

BaseResponse代码如下:

package com.guoranxinxian.entity;

import lombok.Data;

/**
 * description: 微服务接口封装,T为返回接口data的数据类型
 */
@Data
public class BaseResponse<T> {

    /**
     * 返回码
     */
    private Integer code;
    /**
     * 消息
     */
    private String msg;
    /**
     * 返回
     */
    private T data;
    // 分页

    public BaseResponse() {

    }

    public BaseResponse(Integer code, String msg, T data) {
        super();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
}      

BaseApiService代码如下:

package com.guoranxinxian.entity;

import com.guoranxinxian.api.BaseResponse;
import com.guoranxinxian.constants.Constants;
import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * description: 统一返回code信息
 */
@Data
@Component
public class BaseApiService<T> {

    public BaseResponse<T> setResultError(Integer code, String msg) {
        return setResult(code, msg, null);
    }

    // 返回错误,可以传msg
    public BaseResponse<T> setResultError(String msg) {
        return setResult(Constants.HTTP_RES_CODE_500, msg, null);
    }

    // 返回成功,可以传data值
    public BaseResponse<T> setResultSuccess(Object data) {
        return setResult(Constants.HTTP_RES_CODE_200, Constants.HTTP_RES_CODE_200_VALUE, data);
    }

    // 返回成功,沒有data值
    public BaseResponse<T> setResultSuccess() {
        return setResult(Constants.HTTP_RES_CODE_200, Constants.HTTP_RES_CODE_200_VALUE, null);
    }

    // 返回成功,沒有data值
    public BaseResponse<T> setResultSuccess(String msg) {
        return setResult(Constants.HTTP_RES_CODE_200, msg, null);
    }

    // 通用封装
    public BaseResponse<T> setResult(Integer code, String msg, Object data) {
        return new BaseResponse(code, msg, data);
    }

}      

Constants代码如下:

package com.guoranxinxian.constants;

/**
 * description: 统一常量信息
 */
public interface Constants {

    // 响应请求成功
    String HTTP_RES_CODE_200_VALUE = "success";

    // 系统错误
    String HTTP_RES_CODE_500_VALUE = "fial";

    // 响应请求成功code
    Integer HTTP_RES_CODE_200 = 200;

    // 系统错误
    Integer HTTP_RES_CODE_500 = 500;

    // 未关联QQ账号
    Integer HTTP_RES_CODE_201 = 201;

    // 发送邮件
    String MSG_EMAIL = "email";

    // 会员token
    String TOKEN_MEMBER = "TOKEN_MEMBER";

    // 用户有效期 90天
    Long TOKEN_MEMBER_TIME = (long) (60 * 60 * 24 * 90);
    int COOKIE_TOKEN_MEMBER_TIME = (60 * 60 * 24 * 90);

    // cookie 会员 totoken 名称
    String COOKIE_MEMBER_TOKEN = "cookie_member_token";

    // 微信code
    String WEIXINCODE_KEY = "weixin.code";

    // 微信注册码有效期3分钟
    Long WEIXINCODE_TIMEOUT = 180l;

    // 用户信息不存在
    Integer HTTP_RES_CODE_EXISTMOBILE_203 = 203;

    // token
    String MEMBER_TOKEN_KEYPREFIX = "guoranxinxian.member.login";

    // 安卓的登陆类型
    String MEMBER_LOGIN_TYPE_ANDROID = "Android";
    // IOS的登陆类型
    String MEMBER_LOGIN_TYPE_IOS = "IOS";
    // PC的登陆类型
    String MEMBER_LOGIN_TYPE_PC = "PC";

    // 登陆超时时间 有效期 90天
    Long MEMBRE_LOGIN_TOKEN_TIME = 77776000L;
}      

3.注册功能开发

在上一节已经​

​WxJava​

​​框架整合到我们的电商项目,并在​

​MsgHandler​

​类里面处理收到的信息,完成了“鹦鹉学舌”的功能,下面开始讲解如何实现注册码功能。

3.3.注册功能开发

3.3.1.Redis封装

  1. 添加​

    ​maven​

    ​​依赖(​

    ​guoranxinxian-shop-common-core​

    ​模块里添加):
<!-- 集成redis -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>      
  1. 在业务模块(​

    ​guoranxinxian-shop-service-weixin​

    ​​)配置​

    ​redis​

    ​​(也可以配置到​

    ​Apollo​

    ​配置中心):
###服务名称(服务注册到eureka名称)
spring:

  application:
    name: guoranxinxian-shop-service-weixin

  redis:
    host: 127.0.0.1
    port: 6379
    jedis:
      pool:
        max-idle: 100
        min-idle: 1
        max-active: 1000
        max-wait: -1
### 公众号默认回复消息
guoranxinian:
  weixin:
    registration:
      code:
        ###微信注册码消息
        message:  您的注册码为:%s,欢迎您注册我们的系统!
    ###默认提示消息
    default:
      registration:
        code:
          message: 我们已经收到您的消息,将有客服会及时回复您的!      
《果然新鲜》电商项目(19)- 公众号获取注册码功能
  1. 定义​

    ​RedisUtil​

    ​​工具类(​

    ​guoranxinxian-shop-common-core​

    ​模块里添加):
package com.guoranxinxian.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * description: Redis工具类
 */
@Component
public class RedisUtil {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * description: 如果key存在的话返回fasle 不存在的话返回true
     */
    public Boolean setNx(String key, String value, Long timeout) {
        Boolean setIfAbsent = stringRedisTemplate.opsForValue().setIfAbsent(key, value);
        if (timeout != null) {
            stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
        }
        return setIfAbsent;
    }

    /**
     * 存放string类型
     *
     * @param key
     *            key
     * @param data
     *            数据
     * @param timeout
     *            超时间
     */
    public void setString(String key, String data, Long timeout) {
        try {

            stringRedisTemplate.opsForValue().set(key, data);
            if (timeout != null) {
                stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
            }

        } catch (Exception e) {

        }

    }

    /**
     * 开启Redis 事务
     *
     */
    public void begin() {
        // 开启Redis 事务权限
        stringRedisTemplate.setEnableTransactionSupport(true);
        // 开启事务
        stringRedisTemplate.multi();

    }

    /**
     * 提交事务
     *
     */
    public void exec() {
        // 成功提交事务
        stringRedisTemplate.exec();
    }

    /**
     * 回滚Redis 事务
     */
    public void discard() {
        stringRedisTemplate.discard();
    }

    /**
     * 存放string类型
     *
     * @param key
     *            key
     * @param data
     *            数据
     */
    public void setString(String key, String data) {
        setString(key, data, null);
    }

    /**
     * 根据key查询string类型
     *
     * @param key
     * @return
     */
    public String getString(String key) {
        String value = stringRedisTemplate.opsForValue().get(key);
        return value;
    }

    /**
     * 根据对应的key删除key
     *
     * @param key
     */
    public Boolean delKey(String key) {
        return stringRedisTemplate.delete(key);

    }

    public void setList(String key, List<String> listToken) {
        stringRedisTemplate.opsForList().leftPushAll(key, listToken);
    }
    public StringRedisTemplate getStringRedisTemplate() {
        return stringRedisTemplate;
    }
}      

3.3.2 正则表达式工具类

package com.guoranxinxian.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * description: 正则表达式工具类
 */
public class RegexUtils {

    /**
     * 验证Email
     *
     * @param email email地址,格式:[email protected][email protected],
     *              xxx代表邮件服务商
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkEmail(String email) {
        String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
        return Pattern.matches(regex, email);
    }

    /**
     * 验证身份证号码
     *
     * @param idCard 居民身份证号码15位或18位,最后一位可能是数字或字母
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkIdCard(String idCard) {
        String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";
        return Pattern.matches(regex, idCard);
    }

    /**
     * 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港))
     *
     * @param mobile 移动、联通、电信运营商的号码段
     *               <p>
     *               移动的号段:134(0-8)、135、136、137、138、139、147(预计用于TD上网卡)
     *               、150、151、152、157(TD专用)、158、159、187(未启用)、188(TD专用) 177 170 166
     *               开头
     *               </p>
     *               <p>
     *               联通的号段:130、131、132、155、156(世界风专用)、185(未启用)、186(3g)
     *               </p>
     *               <p>
     *               电信的号段:133、153、180(未启用)、189
     *               </p>
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkMobile(String mobile) {
        String regex = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
        return Pattern.matches(regex, mobile);
    }

    /**
     * 验证固定电话号码
     *
     * @param phone 电话号码,格式:国家(地区)电话代码 + 区号(城市代码) + 电话号码,如:+8602085588447
     *              <p>
     *              <b>国家(地区) 代码 :</b>标识电话号码的国家(地区)的标准国家(地区)代码。它包含从 0 到 9
     *              的一位或多位数字, 数字之后是空格分隔的国家(地区)代码。
     *              </p>
     *              <p>
     *              <b>区号(城市代码):</b>这可能包含一个或多个从 0 到 9 的数字,地区或城市代码放在圆括号——
     *              对不使用地区或城市代码的国家(地区),则省略该组件。
     *              </p>
     *              <p>
     *              <b>电话号码:</b>这包含从 0 到 9 的一个或多个数字
     *              </p>
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkPhone(String phone) {
        String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";
        return Pattern.matches(regex, phone);
    }

    /**
     * 验证整数(正整数和负整数)
     *
     * @param digit 一位或多位0-9之间的整数
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkDigit(String digit) {
        String regex = "\\-?[1-9]\\d+";
        return Pattern.matches(regex, digit);
    }

    /**
     * 验证整数和浮点数(正负整数和正负浮点数)
     *
     * @param decimals 一位或多位0-9之间的浮点数,如:1.23,233.30
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkDecimals(String decimals) {
        String regex = "\\-?[1-9]\\d+(\\.\\d+)?";
        return Pattern.matches(regex, decimals);
    }

    /**
     * 验证空白字符
     *
     * @param blankSpace 空白字符,包括:空格、\t、\n、\r、\f、\x0B
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkBlankSpace(String blankSpace) {
        String regex = "\\s+";
        return Pattern.matches(regex, blankSpace);
    }

    /**
     * 验证中文
     *
     * @param chinese 中文字符
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkChinese(String chinese) {
        String regex = "^[\u4E00-\u9FA5]+$";
        return Pattern.matches(regex, chinese);
    }

    /**
     * 验证日期(年月日)
     *
     * @param birthday 日期,格式:1992-09-03,或1992.09.03
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkBirthday(String birthday) {
        String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";
        return Pattern.matches(regex, birthday);
    }

    /**
     * 验证URL地址
     *
     * @param url 格式 或
     *         
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkURL(String url) {
        String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?";
        return Pattern.matches(regex, url);
    }

    /**
     * <pre>
     * 获取网址 URL 的一级域
     * </pre>
     *
     * @param url
     * @return
     */
    public static String getDomain(String url) {
        Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)",
                Pattern.CASE_INSENSITIVE);
        // 获取完整的域名
        // Pattern
        // p=Pattern.compile("[^//]*?\\.(com|cn|net|org|biz|info|cc|tv)",
        // Pattern.CASE_INSENSITIVE);
        Matcher matcher = p.matcher(url);
        matcher.find();
        return matcher.group();
    }

    /**
     * 匹配中国邮政编码
     *
     * @param postcode 邮政编码
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkPostcode(String postcode) {
        String regex = "[1-9]\\d{5}";
        return Pattern.matches(regex, postcode);
    }

    /**
     * 匹配IP地址(简单匹配,格式,如:192.168.1.1,127.0.0.1,没有匹配IP段的大小)
     *
     * @param ipAddress IPv4标准地址
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkIpAddress(String ipAddress) {
        String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))";
        return Pattern.matches(regex, ipAddress);
    }

}      

3.3.3 MsgHandler验证码处理

1.在服务模块(​

​guoranxinxian-shop-service​

​​)添加​

​Maven​

​依赖:

<dependency>
   <groupId>com.guoranxinxian</groupId>
   <artifactId>guoranxinxian-shop-common-core</artifactId>
   <version>1.0-SNAPSHOT</version>
</dependency>      

2.​

​MsgHandler​

​​生成验证码,并把验证码保存到​

​Redis​

​服务器,完整代码如下:

package com.guoranxinxian.mp.handler;

import com.guoranxinxian.constants.Constants;
import com.guoranxinxian.mp.builder.TextBuilder;
import com.guoranxinxian.util.RedisUtil;
import com.guoranxinxian.util.RegexUtils;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Map;

import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType;

@Component
public class MsgHandler extends AbstractHandler {

    /**
     * 发送验证码消息
     */
    @Value("${guoranxinian.weixin.registration.code.message}")
    private String registrationCodeMessage;
    /**
     * 默认回复消息
     */
    @Value("${guoranxinian.weixin.default.registration.code.message}")
    private String defaultRegistrationCodeMessage;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {

        if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
            // TODO 可以选择将消息保存到本地
        }
        // 当用户输入关键词如“你好”,“客服”等,并且有客服在线时,把消息转发给在线客服
        try {
            if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服")
                    && weixinService.getKefuService().kfOnlineList().getKfOnlineList().size() > 0) {
                return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE().fromUser(wxMessage.getToUser())
                        .toUser(wxMessage.getFromUser()).build();
            }
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        // 1.获取客户端发送的消息
        String fromContent = wxMessage.getContent();
        // 2.如果客户端发送消息为手机号码,则发送验证码
        if (RegexUtils.checkMobile(fromContent)) {
            // 3.生成随机四位注册码
            int registCode = registCode();
            String content = String.format(registrationCodeMessage, registCode);
            // 4.将验证码存放在Redis中
            redisUtil.setString(Constants.WEIXINCODE_KEY + fromContent, registCode + "", Constants.WEIXINCODE_TIMEOUT);
            return new TextBuilder().build(content, wxMessage, weixinService);
        }
        return new TextBuilder().build(defaultRegistrationCodeMessage, wxMessage, weixinService);
    }

    // 获取注册码
    private int registCode() {
        int registCode = (int) (Math.random() * 9000 + 1000);
        return registCode;
    }

}      

3.2 验证注册码

1.定义验证接口(微信服务接口层​

​guoranxinxian-shop-service-api-weixin​

​):

package com.guoranxinxian.service;

import com.alibaba.fastjson.JSONObject;
import com.guoranxinxian.api.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * description: 微信验证服务接口
 * create by: YangLinWei
 * create time: 2020/2/20 2:22 下午
 */
@Api(tags = "微信注册码验证码接口")
public interface VerificaCodeService {

    @ApiOperation(value = "根据手机号码验证码token是否正确")
    @GetMapping("/verificaWeixinCode")
    @ApiImplicitParams({
            // @ApiImplicitParam(paramType="header",name="name",dataType="String",required=true,value="用户的姓名",defaultValue="zhaojigang"),
            @ApiImplicitParam(paramType = "query", name = "phone", dataType = "String", required = true, value = "用户手机号码"),
            @ApiImplicitParam(paramType = "query", name = "weixinCode", dataType = "String", required = true, value = "微信注册码") })
    BaseResponse<JSONObject> verificaWeixinCode(String phone, String weixinCode);
}      

2.实现接口(微信业务层​

​guoranxinxian-shop-service-weixin​

​):

package com.guoranxinxian.impl;

import com.alibaba.fastjson.JSONObject;
import com.guoranxinxian.api.BaseResponse;
import com.guoranxinxian.constants.Constants;
import com.guoranxinxian.entity.BaseApiService;
import com.guoranxinxian.service.VerificaCodeService;
import com.guoranxinxian.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class VerificaCodeServiceImpl extends BaseApiService<JSONObject> implements VerificaCodeService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public BaseResponse<JSONObject> verificaWeixinCode(String phone, String weixinCode) {
        if (StringUtils.isEmpty(phone)) {
            return setResultError("手机号码不能为空!");
        }
        if (StringUtils.isEmpty(weixinCode)) {
            return setResultError("注册码不能为空!");
        }
        String code = redisUtil.getString(Constants.WEIXINCODE_KEY + phone);
        if (StringUtils.isEmpty(code)) {
            return setResultError("注册码已经过期,请重新发送验证码");
        }
        if (!code.equals(weixinCode)) {
            return setResultError("注册码不正确");
        }
        return setResultSuccess("注册码验证码正确");
    }

}      

4.测试

启动微信服务:​

​AppWeiXin​

4.1 获取注册码

注意:代码里设置微信注册码超时时间为3分钟!

首先发送手机号,可以看到:

《果然新鲜》电商项目(19)- 公众号获取注册码功能
《果然新鲜》电商项目(19)- 公众号获取注册码功能

4.2 校验注册码

5.总结

继续阅读