手机号授权: 手机授权码申请

This commit is contained in:
lzm
2022-11-23 23:25:42 +08:00
committed by yeungchihang
parent 86d3962d89
commit eb0fcf4ec5
13 changed files with 176 additions and 10 deletions

View File

@@ -5727,5 +5727,21 @@ public class Constant {
public static final Byte AGREE = 1;//同意
}
/**
* 中国大陆区号
*/
public final static String CHINA_MAINLAND_PHONE_AREA_CODE = "86";
/**
* 手机申请授权码记录状态
*/
public static class PhoneAuthApplyStatus {
// 0待审核 1通过 2拒绝
public static final Byte wait_audit = 1;//未处理
public static final Byte pass = 2;//同意
public static final Byte fail = 3;//拒绝
}
}

View File

@@ -36,6 +36,7 @@ public class SmsConstant {
public static final Byte SUPER_ADMIN_LOGIN = 10; //超管登录
public static final Byte BINDING_BANK_CARD = 11; //绑定提现银行卡
public static final Byte H5_BINDING_BANK_CARD = 12; //h5 绑定提现银行卡
public static final Byte AUTH_CODE = 13; // 获取手机授权码
}
/**

View File

@@ -24,6 +24,7 @@ public enum SmsTypeEnum {
H5_BINDING_BANK_CARD(12,"h5 绑定提现银行卡"),
RESET_PASSWORD_FOR_NO_LOGIN(13,"非登录态重置密码"),
RESET_PASSWORD_FOR_HAS_LOGIN(14,"登录态重置密码"),
PHONE_AUTH_APPLY_CODE(15,"手机授权码"),
;
public final int value;

View File

@@ -1781,7 +1781,9 @@ public enum RedisKey {
clan_apply_join, //模厅申请加入家族
lock_clan_apply,
lock_clan_quit
lock_clan_quit,
phone_auth_apply_lock,// 申请手机授权码锁
;

View File

@@ -23,6 +23,8 @@ public class AliyunSmsConfig {
private String accessKeySecret;
private String signName;
private String templateCode;
private String authSuccessTemplateCode; // 手机授权码 - 申请成功
private String authFailTemplateCode; // 手机授权码 - 申请失败
private String product;
private String domain;
private String regionId;

View File

@@ -1,5 +1,6 @@
package com.accompany.business.model.phone;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@@ -22,7 +23,11 @@ public class PhoneAuthRecord implements Serializable {
/**
*
*/
@TableId
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
*
*/
private Long uid;
/**
* 注册时使用的手机号

View File

@@ -1,6 +1,7 @@
package com.accompany.business.service.phone;
import com.accompany.business.model.phone.PhoneAuthApplyRecord;
import com.accompany.common.device.DeviceInfo;
import com.baomidou.mybatisplus.extension.service.IService;
/**
@@ -13,4 +14,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
public interface PhoneAuthApplyRecordService extends IService<PhoneAuthApplyRecord> {
void apply(String phone, String code, String phoneAreaCode);
}

View File

@@ -3,9 +3,23 @@ package com.accompany.business.service.phone.impl;
import com.accompany.business.model.phone.PhoneAuthApplyRecord;
import com.accompany.business.mybatismapper.PhoneAuthApplyRecordMapper;
import com.accompany.business.service.phone.PhoneAuthApplyRecordService;
import com.accompany.common.constant.Constant;
import com.accompany.common.device.DeviceInfo;
import com.accompany.common.redis.RedisKey;
import com.accompany.common.status.BusiStatus;
import com.accompany.common.utils.StringUtils;
import com.accompany.core.exception.ServiceException;
import com.accompany.core.service.common.JedisLockService;
import com.accompany.core.service.common.JedisService;
import com.accompany.sms.service.SmsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
/**
* 手机授权码注册管理
*
@@ -16,5 +30,89 @@ import org.springframework.stereotype.Service;
@Service
public class PhoneAuthApplyRecordServiceImpl extends ServiceImpl<PhoneAuthApplyRecordMapper, PhoneAuthApplyRecord> implements PhoneAuthApplyRecordService {
@Autowired
private JedisService jedisService;
@Autowired
private JedisLockService jedisLockService;
@Autowired
private SmsService smsService;
@Override
public void apply(String phone, String code, String phoneAreaCode) {
if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
throw new ServiceException(BusiStatus.PARAMETERILLEGAL);
}
if (!phone.startsWith("86")) {
throw new ServiceException("手機號格式不正確");
}
String lockeKey = RedisKey.phone_auth_apply_lock.getKey(phone);
String lockVal = jedisLockService.lock(lockeKey);
if (StringUtils.isEmpty(lockVal)) {
throw new ServiceException(BusiStatus.SERVER_BUSY);
}
try {
boolean flag = smsService.verifySmsCode(phone, code);
if (flag) {
throw new ServiceException(BusiStatus.SMSCODEERROR);
}
QueryWrapper<PhoneAuthApplyRecord> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(PhoneAuthApplyRecord::getPhone,phone);
PhoneAuthApplyRecord applyRecord = this.getOne(queryWrapper);
if (applyRecord != null) {
if (Constant.PhoneAuthApplyStatus.wait_audit.equals(applyRecord.getStatus())) {
throw new ServiceException("該手機號已提交申請,請耐心等待");
}
if (Constant.PhoneAuthApplyStatus.pass.equals(applyRecord.getStatus())) {
throw new ServiceException("該手機號申請已通過,請查收短信哦");
}
applyRecord.setRemark("");
applyRecord.setOperator("");
applyRecord.setRemark("");
} else {
applyRecord = new PhoneAuthApplyRecord();
applyRecord.setCreateTime(new Date());
}
applyRecord.setPhone(phone);
applyRecord.setAuthCode(getAuthCode());
applyRecord.setPhoneAreaCode(phone);
applyRecord.setHasUsed(Constant.GlobalStatus.in_valid);
applyRecord.setStatus(Constant.PhoneAuthApplyStatus.wait_audit);
this.saveOrUpdate(applyRecord);
} finally {
jedisLockService.unlock(lockeKey,lockVal);
}
}
public String getAuthCode() {
int digit = 8;
String inviteCode = RandomStringUtils.randomAlphanumeric(digit).toLowerCase();
int num = 0;
while (isExist(inviteCode)) {
num++;
inviteCode = RandomStringUtils.randomAlphanumeric(digit).toUpperCase();
if (num == 10) {
throw new ServiceException("授權碼生成失敗");
}
}
return inviteCode;
}
/**
* 判断授权码是否存在
* @param authCode
* @return
*/
private boolean isExist(String authCode) {
QueryWrapper<PhoneAuthApplyRecord> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(PhoneAuthApplyRecord::getAuthCode,authCode);
return this.getOne(queryWrapper) != null;
}
}

View File

@@ -5,6 +5,7 @@
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.accompany.business.model.phone.PhoneAuthRecord" id="phoneAuthRecordMap">
<result property="id" column="id"/>
<result property="uid" column="uid"/>
<result property="phone" column="phone"/>
<result property="authId" column="auth_id"/>

View File

@@ -1,12 +1,16 @@
package com.accompany.business.controller;
import com.accompany.business.common.BaseController;
import com.accompany.business.model.phone.PhoneAuthApplyRecord;
import com.accompany.business.service.phone.PhoneAuthApplyRecordService;
import com.accompany.common.constant.Constant;
import com.accompany.common.constant.SmsTypeEnum;
import com.accompany.common.device.DeviceInfo;
import com.accompany.common.result.BusiResult;
import com.accompany.common.status.BusiStatus;
import com.accompany.common.utils.CommonUtil;
import com.accompany.common.utils.IPUitls;
import com.accompany.common.utils.StringUtils;
import com.accompany.core.exception.ServiceException;
import com.accompany.core.model.Users;
import com.accompany.core.service.user.PhoneBlackService;
@@ -51,7 +55,7 @@ public class SmsController extends BaseController {
@ApiOperation("发送手机验证码")
@PostMapping(value = "getCode")
public BusiResult getCode(@RequestParam("mobile") String mobile,
public BusiResult getCode(@RequestParam("mobile") String mobile,String phoneAreaCode,
@RequestParam("type") Integer type, HttpServletRequest request) {
mobile = decryptSensitiveInfo(request, mobile);
String ip = IPUitls.getRealIpAddress(request);
@@ -69,10 +73,10 @@ public class SmsController extends BaseController {
if (phoneBlackService.checkIsNeedIntercept(mobile)) {
throw new ServiceException(BusiStatus.PHONE_BE_INTERCEPTED);
}
if (!CommonUtil.checkValidPhone(mobile)) {
return SmsTypeEnum.REGISTER.getValue() == type ? new BusiResult(BusiStatus.SMS_SEND_SUCCESS) :
new BusiResult(BusiStatus.PHONE_INVALID);
}
// if (!CommonUtil.checkValidPhone(mobile)) {
// return SmsTypeEnum.REGISTER.getValue() == type ? new BusiResult(BusiStatus.SMS_SEND_SUCCESS) :
// new BusiResult(BusiStatus.PHONE_INVALID);
// }
return smsService.sendSmsCode(mobile, type, deviceInfo, ip, null);
}

View File

@@ -2,11 +2,18 @@ package com.accompany.business.controller.phone;
import com.accompany.business.common.BaseController;
import com.accompany.business.service.phone.PhoneAuthApplyRecordService;
import com.accompany.common.device.DeviceInfo;
import com.accompany.common.result.BusiResult;
import com.accompany.common.status.BusiStatus;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* 手机授权码注册管理
*
@@ -16,10 +23,20 @@ import org.springframework.web.bind.annotation.RestController;
*/
@Slf4j
@RestController
@RequestMapping("/phone/auth/apply")
@RequestMapping("/phone/auth")
public class PhoneAuthApplyRecordController extends BaseController {
@Autowired
private PhoneAuthApplyRecordService phoneAuthApplyRecordService;
@ApiOperation("手机号申请授权码操作")
@ApiImplicitParams({
@ApiImplicitParam(name = "phoneAreaCode", value = "区号", required = true, dataType = "String"),
@ApiImplicitParam(name = "phone", value = "区号 + 手机号 如: 86178xxxxxxxx", required = true, dataType = "String"),
@ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String"),
})
@PostMapping("/apply")
public BusiResult apply(String phoneAreaCode, String phone, String code) {
phoneAuthApplyRecordService.apply(phone,code,phoneAreaCode);
return new BusiResult(BusiStatus.SUCCESS);
}
}

View File

@@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.RestController;
*/
@Slf4j
@RestController
@RequestMapping("/phone/auth")
@RequestMapping("/phone/auth/record")
public class PhoneAuthRecordController extends BaseController {
@Autowired
private PhoneAuthRecordService phoneAuthRecordService;

View File

@@ -0,0 +1,17 @@
package servicetest;
import com.accompany.sms.service.SmsService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class SmsSendTest extends CommonTest {
@Autowired
private SmsService smsService;
@Test
public void sendSms() throws InterruptedException {
smsService.sendSmsCode("8617817447469",15,null,"127.0.0.1","51145");
smsService.sendSmsCode("17817447469",15,null,"127.0.0.1","88154");
}
}