增加绑定手机号接口

This commit is contained in:
TinyAnts 2022-09-08 14:22:18 +08:00
parent 5a0043fcd2
commit fb86e38040
3 changed files with 18 additions and 3 deletions

View File

@ -2,7 +2,7 @@ package com.mdd.front.controller;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.utils.ConfigUtil;
import com.mdd.common.exception.OperateException;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.IUserService;
import com.mdd.front.vo.user.UserCenterVo;
@ -10,8 +10,13 @@ import com.mdd.front.vo.user.UserInfoVo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;
import java.util.regex.Pattern;
/**
* 用户管理表
*/
@RestController
@RequestMapping("api/user")
public class UserController {
@ -67,8 +72,15 @@ public class UserController {
*/
@PostMapping("/bindMobile")
public Object bindMobile(@RequestBody Map<String, String> params) {
Assert.notNull(params.get("type"), "type参数缺失");
Assert.notNull(params.get("mobile"), "mobile参数缺失");
Assert.notNull(params.get("code"), "code参数缺失");
boolean type = Arrays.asList("bind", "change").contains(params.get("type"));
Assert.isTrue(type, "type类型只能是[bind/change]");
if(!Pattern.matches("^[1][3,4,5,6,7,8,9][0-9]{9}$", params.get("mobile"))){
throw new OperateException("手机号格式不正确!");
}
Integer userId = LikeFrontThreadLocal.getUserId();
iUserService.bindMobile(params, userId);
return AjaxResult.success();

View File

@ -172,11 +172,12 @@ public class UserServiceImpl implements IUserService {
*/
@Override
public void bindMobile(Map<String, String> params, Integer userId) {
String type = params.getOrDefault("type", "");
String mobile = params.getOrDefault("mobile", "");
String code = params.getOrDefault("code", "").toLowerCase();
String code = params.getOrDefault("code", "").toLowerCase();
// 校验验证码
int typeCode = NoticeEnum.SMS_BIND_MOBILE_CODE.getCode();
int typeCode = type.equals("bind") ? NoticeEnum.SMS_BIND_MOBILE_CODE.getCode() : NoticeEnum.SMS_CHANGE_MOBILE_CODE.getCode() ;
Object smsCode = RedisUtil.get(GlobalConfig.redisSmsCode+typeCode+":"+mobile);
if (StringUtil.isNull(smsCode) || !smsCode.toString().equals(code)) {
throw new OperateException("验证码错误!");

View File

@ -7,6 +7,7 @@ import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
@Data
@ -21,6 +22,7 @@ public class SmsParam implements Serializable {
@NotNull(message = "mobile参数缺失")
@NotEmpty(message = "手机号不能为空")
@Length(min = 11, max = 11, message = "手机号只能为11位")
@Pattern(regexp = "^[1][3,4,5,6,7,8,9][0-9]{9}$", message = "手机号格式有误")
private String mobile;
}