天天看點

《果然新鮮》電商項目(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.總結

繼續閱讀