手机号授权: 相关类模块转移、补充手机格式校验

This commit is contained in:
lzm
2022-11-24 21:59:26 +08:00
committed by yeungchihang
parent a923ef8dc2
commit 4f2808ca03
20 changed files with 72 additions and 66 deletions

View File

@@ -173,6 +173,7 @@ public class CommonUtil {
* @param phone
* @return
*/
@Deprecated
public static boolean checkIsPhone(String phone) {
if (StringUtils.isEmpty(phone)){
return false;
@@ -251,9 +252,10 @@ public class CommonUtil {
* @return
*/
public static boolean checkPhoneFormat(String areaCode, String phone) {
if (!checkNumberOnly(phone)) {
if (!checkNumberOnly(phone) || !checkNumberOnly(areaCode)) {
return false;
}
String realPhone = phone;
if (realPhone.startsWith(areaCode)) {
realPhone = realPhone.replaceFirst(areaCode, "");

View File

@@ -1,4 +1,4 @@
package com.accompany.business.model.phone;
package com.accompany.core.model.phone;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;

View File

@@ -1,4 +1,4 @@
package com.accompany.business.model.phone;
package com.accompany.core.model.phone;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;

View File

@@ -1,6 +1,6 @@
package com.accompany.business.mybatismapper;
package com.accompany.core.mybatismapper;
import com.accompany.business.model.phone.PhoneAuthApplyRecord;
import com.accompany.core.model.phone.PhoneAuthApplyRecord;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

View File

@@ -1,6 +1,7 @@
package com.accompany.business.mybatismapper;
package com.accompany.core.mybatismapper;
import com.accompany.business.model.phone.PhoneAuthRecord;
import com.accompany.core.model.phone.PhoneAuthApplyRecord;
import com.accompany.core.model.phone.PhoneAuthRecord;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

View File

@@ -139,7 +139,7 @@ public class AccountService extends ServiceImpl<AccountMapper, Account> {
public Boolean updateAccountErbanNo(Long uid, Long erbanNo) {
Account account = getById(uid);
if (!StringUtils.isEmpty(account)) {
if (!CommonUtil.checkIsPhone(account.getPhone())) {
if (!CommonUtil.checkPhoneFormat(account.getPhoneAreaCode(),account.getPhone())) {
account.setPhone(String.valueOf(erbanNo));
}
account.setErbanNo(erbanNo);

View File

@@ -1,6 +1,6 @@
package com.accompany.business.service.phone;
package com.accompany.core.service.phone;
import com.accompany.business.model.phone.PhoneAuthApplyRecord;
import com.accompany.core.model.phone.PhoneAuthApplyRecord;
import com.accompany.common.device.DeviceInfo;
import com.baomidou.mybatisplus.extension.service.IService;
@@ -14,7 +14,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
public interface PhoneAuthApplyRecordService extends IService<PhoneAuthApplyRecord> {
void apply(String phone, String code, String phoneAreaCode);
void applyAuthCode(String phone, String phoneAreaCode);
void boundAuthCode(Long uid, String phone, String authCode, String phoneAreaCode, DeviceInfo deviceInfo);

View File

@@ -1,6 +1,6 @@
package com.accompany.business.service.phone;
package com.accompany.core.service.phone;
import com.accompany.business.model.phone.PhoneAuthRecord;
import com.accompany.core.model.phone.PhoneAuthRecord;
import com.baomidou.mybatisplus.extension.service.IService;
/**

View File

@@ -1,10 +1,10 @@
package com.accompany.business.service.phone.impl;
package com.accompany.core.service.phone.impl;
import com.accompany.business.model.phone.PhoneAuthApplyRecord;
import com.accompany.business.model.phone.PhoneAuthRecord;
import com.accompany.business.mybatismapper.PhoneAuthApplyRecordMapper;
import com.accompany.business.service.phone.PhoneAuthApplyRecordService;
import com.accompany.business.service.phone.PhoneAuthRecordService;
import com.accompany.core.model.phone.PhoneAuthApplyRecord;
import com.accompany.core.model.phone.PhoneAuthRecord;
import com.accompany.core.mybatismapper.PhoneAuthApplyRecordMapper;
import com.accompany.core.service.phone.PhoneAuthApplyRecordService;
import com.accompany.core.service.phone.PhoneAuthRecordService;
import com.accompany.common.constant.Constant;
import com.accompany.common.device.DeviceInfo;
import com.accompany.common.redis.RedisKey;
@@ -17,7 +17,6 @@ import com.accompany.core.service.SysConfService;
import com.accompany.core.service.account.AccountService;
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;
@@ -43,8 +42,6 @@ public class PhoneAuthApplyRecordServiceImpl extends ServiceImpl<PhoneAuthApplyR
@Autowired
private JedisLockService jedisLockService;
@Autowired
private SmsService smsService;
@Autowired
private PhoneAuthRecordService phoneAuthRecordService;
@Autowired
private AccountService accountService;
@@ -54,8 +51,8 @@ public class PhoneAuthApplyRecordServiceImpl extends ServiceImpl<PhoneAuthApplyR
@Override
@Transactional(rollbackFor = Exception.class, transactionManager = "mybatisplusTransactionManager")
public void apply(String phone, String code, String phoneAreaCode) {
if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
public void applyAuthCode(String phone, String phoneAreaCode) {
if (StringUtils.isEmpty(phone)) {
throw new ServiceException(BusiStatus.PARAMETERILLEGAL);
}
@@ -69,12 +66,6 @@ public class PhoneAuthApplyRecordServiceImpl extends ServiceImpl<PhoneAuthApplyR
throw new ServiceException(BusiStatus.SERVER_BUSY);
}
try {
String phoneStr = phoneAreaCode + phone;
boolean flag = smsService.verifySmsCode(phoneStr, code);
if (flag) {
throw new ServiceException(BusiStatus.SMSCODEERROR);
}
PhoneAuthApplyRecord applyRecord = getAuthApplyRecord(phone, phoneAreaCode);
if (applyRecord != null) {
if (Constant.PhoneAuthApplyStatus.wait_audit.equals(applyRecord.getStatus())) {

View File

@@ -1,10 +1,8 @@
package com.accompany.business.service.phone.impl;
package com.accompany.core.service.phone.impl;
import com.accompany.business.model.phone.PhoneAuthRecord;
import com.accompany.business.mybatismapper.PhoneAuthRecordMapper;
import com.accompany.business.service.phone.PhoneAuthRecordService;
import com.accompany.common.constant.Constant;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.accompany.core.model.phone.PhoneAuthRecord;
import com.accompany.core.mybatismapper.PhoneAuthRecordMapper;
import com.accompany.core.service.phone.PhoneAuthRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

View File

@@ -29,4 +29,11 @@ public interface PhoneBlackService extends IService<PhoneBlack> {
* @return
*/
boolean checkIsNeedIntercept(String phone);
/**
* 检查手机号是否需要拦截 => 根据区号 + 手机号检测
* @param phone
* @return
*/
boolean checkIsNeedInterceptWithPhoneAreaCode(String phoneAreaCode,String phone);
}

View File

@@ -369,7 +369,7 @@ public class UsersBaseService extends BaseService {
Users user = usersMapper.selectByPrimaryKey(uid);
if (user != null) {
//如果手机号是平台号,则更换为靓号
if (StringUtils.isBlank(user.getPhone()) || !CommonUtil.checkIsPhone(user.getPhone())) {
if (StringUtils.isBlank(user.getPhone()) || !CommonUtil.checkPhoneFormat(user.getPhoneAreaCode(),user.getPhone())) {
user.setPhone(String.valueOf(erbanNo));
}
user.setErbanNo(erbanNo);

View File

@@ -49,4 +49,22 @@ public class PhoneBlackServiceImpl extends ServiceImpl<PhoneBlackMapper, PhoneBl
PhoneBlack phoneBlack = getOne(wrapper);
return phoneBlack != null;
}
@Override
public boolean checkIsNeedInterceptWithPhoneAreaCode(String phoneAreaCode,String phone) {
if (StringUtils.isBlank(phone)) {
throw new ServiceException("手機號不能為空");
}
if (!CommonUtil.checkPhoneFormat(phoneAreaCode,phone)) {
return false;
}
String phonePrefix = phone;
if (phone.length() > 7) phonePrefix = phone.substring(0, 7);
QueryWrapper<PhoneBlack> wrapper = new QueryWrapper<>();
wrapper.lambda().eq(PhoneBlack::getPhonePrefix, phonePrefix).eq(PhoneBlack::getInterceptStatus, Constant.status.valid)
.eq(PhoneBlack::getStatus, Constant.status.valid);
PhoneBlack phoneBlack = getOne(wrapper);
return phoneBlack != null;
}
}

View File

@@ -4,7 +4,7 @@
<mapper namespace="com.accompany.business.model..dao.PhoneAuthApplyRecordMapper">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.accompany.business.model.phone.PhoneAuthApplyRecord" id="phoneAuthApplyRecordMap">
<resultMap type="com.accompany.core.model.phone.PhoneAuthApplyRecord" id="phoneAuthApplyRecordMap">
<result property="id" column="id"/>
<result property="phoneAreaCode" column="phone_area_code"/>
<result property="phone" column="phone"/>

View File

@@ -4,7 +4,7 @@
<mapper namespace="com.accompany.business.model..dao.PhoneAuthRecordMapper">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.accompany.business.model.phone.PhoneAuthRecord" id="phoneAuthRecordMap">
<resultMap type="com.accompany.core.model.phone.PhoneAuthRecord" id="phoneAuthRecordMap">
<result property="id" column="id"/>
<result property="uid" column="uid"/>
<result property="phone" column="phone"/>

View File

@@ -1,16 +1,12 @@
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;
@@ -70,19 +66,19 @@ public class SmsController extends BaseController {
mobile = userMobile != null && userMobile.length() == 11 ? userMobile : mobile;
}
logger.info("sendSmsCode2, mobile:{}, type:{}, uid:{}", mobile, type, uid);
if (phoneBlackService.checkIsNeedIntercept(mobile)) {
if (phoneBlackService.checkIsNeedInterceptWithPhoneAreaCode(phoneAreaCode,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.checkPhoneFormat(phoneAreaCode,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);
}
@ApiParam("校验手机验证码")
@RequestMapping(value = "verify")
public BusiResult verify(@RequestParam("mobile") String mobile,
public BusiResult verify(@RequestParam("mobile") String mobile,@RequestParam("phoneAreaCode") String phoneAreaCode,
@RequestParam("code") String code,HttpServletRequest request) {
Long uid = getUid(request);
mobile = decryptSensitiveInfo(request, mobile);
@@ -90,7 +86,7 @@ public class SmsController extends BaseController {
Users users = usersBaseService.getUsersByUid(uid);
mobile = users == null ? mobile : users.getPhone();
}
if (!CommonUtil.checkValidPhone(mobile)) {
if (!CommonUtil.checkPhoneFormat(phoneAreaCode,mobile)) {
return new BusiResult(BusiStatus.PHONE_INVALID);
}
boolean verify = smsService.verifySmsCode(mobile, code);

View File

@@ -107,7 +107,7 @@ public class WithdrawController extends BaseController {
@RequestParam("phoneAreaCode") String phoneAreaCode,
HttpServletRequest request) throws Exception{
phone = decryptSensitiveInfo(request, phone);
if (phoneBlackService.checkIsNeedIntercept(phone)) {
if (phoneBlackService.checkIsNeedInterceptWithPhoneAreaCode(phoneAreaCode,phone)) {
throw new ServiceException(BusiStatus.PHONE_BE_INTERCEPTED);
}
return new BusiResult(withdrawService.boundPhone(getUid(), phone, code,phoneAreaCode));

View File

@@ -1,8 +1,7 @@
package com.accompany.business.controller.phone;
import com.accompany.business.common.BaseController;
import com.accompany.business.service.phone.PhoneAuthApplyRecordService;
import com.accompany.business.service.phone.PhoneAuthRecordService;
import com.accompany.core.service.phone.PhoneAuthApplyRecordService;
import com.accompany.common.annotation.Authorization;
import com.accompany.common.device.DeviceInfo;
import com.accompany.common.result.BusiResult;
@@ -35,11 +34,10 @@ public class PhoneAuthApplyRecordController extends BaseController {
@ApiImplicitParams({
@ApiImplicitParam(name = "phoneAreaCode", value = "区号", required = true, dataType = "String"),
@ApiImplicitParam(name = "phone", value = "手机号 如: 178xxxxxxxx", 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);
public BusiResult apply(String phoneAreaCode, String phone) {
phoneAuthApplyRecordService.applyAuthCode(phone,phoneAreaCode);
return new BusiResult(BusiStatus.SUCCESS);
}
@@ -66,7 +64,7 @@ public class PhoneAuthApplyRecordController extends BaseController {
@ApiImplicitParam(name = "phone", value = "手机号 如: 178xxxxxxxx", required = true, dataType = "String"),
})
@GetMapping("/isBoundPhoneAuthCode")
public BusiResult apply(String phone,String phoneAreaCode) {
public BusiResult isBoundPhoneAuthCode(String phone,String phoneAreaCode) {
Boolean flag = phoneAuthApplyRecordService.isBoundPhoneAuthCode(phone,phoneAreaCode);
return new BusiResult(BusiStatus.SUCCESS,flag);
}

View File

@@ -1,16 +1,8 @@
package com.accompany.business.controller.phone;
import com.accompany.business.common.BaseController;
import com.accompany.business.service.phone.PhoneAuthRecordService;
import com.accompany.common.result.BusiResult;
import com.accompany.common.status.BusiStatus;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.accompany.core.service.phone.PhoneAuthRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

View File

@@ -17,6 +17,7 @@ import com.accompany.core.service.SysConfService;
import com.accompany.core.service.account.AccountService;
import com.accompany.core.service.common.JedisLockService;
import com.accompany.core.service.common.JedisService;
import com.accompany.core.service.phone.PhoneAuthApplyRecordService;
import com.accompany.core.service.user.PhoneBlackService;
import com.accompany.core.util.JwtUtils;
import com.accompany.core.util.KeyStore;
@@ -85,6 +86,8 @@ public class AccountController extends BaseController {
private MyUserDetailsService myUserDetailsService;
@Autowired
private PhoneBlackService phoneBlackService;
@Autowired
private PhoneAuthApplyRecordService phoneAuthApplyRecordService;
/**
* 通过手机号码注册