Commit 304574d6 authored by liuyang's avatar liuyang

添加换绑手机号功能

parent 5bc2ee20
......@@ -32,6 +32,7 @@ public class BizConstants {
public static final String CONFIG_KEY_ENTERPRICE_VIP_PRICE = "enterprice_vip_price";
public static final String SMS_TEMPLATE_CODE = "T_LG_CAPTCHA";
public static final String SMS_CHECK_USER_TEMPLATE_CODE = "T_CK_CAPTCHA";
public static final String MOBILE_CAPTCHA_PREFIX = "CAPTCHA_";
public static final String CACHE_KE_CONSUME_RECORD = "CONSUME_RECORD";
......
package com.qkdata.biz.sms.controller;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import com.qkdata.biz.common.BizConstants;
import com.qkdata.biz.sms.service.SmsService;
......@@ -30,14 +31,18 @@ public class SmsController {
@Qualifier("stringRedisTemplate")
private StringRedisTemplate redisTemplate;
@ApiOperation("发送验证码")
@SysLog("发送验证码")
@ApiOperation("发送登录验证码")
@SysLog("发送登录验证码")
@PostMapping("/sendCode")
public Result<String> sendCode(@RequestBody @Validated SendValCodeReq req){
if (StrUtil.isBlank(req.getTemplateCode())){
//默认使用登陆验证码模板
req.setTemplateCode(BizConstants.SMS_TEMPLATE_CODE);
}
Map<String,Object> param = Maps.newHashMap();
String captcha = RandomDigitGenerator.generate(6);
param.put("code",captcha);
SmsV2Condition condition = new SmsV2Condition(req.getMobile(), BizConstants.SMS_TEMPLATE_CODE,param,null);
SmsV2Condition condition = new SmsV2Condition(req.getMobile(), req.getTemplateCode(),param,null);
smsService.sendSmsV3(condition);
//将captcha存入Redis,过期时间为5分钟
redisTemplate.opsForValue().set(BizConstants.MOBILE_CAPTCHA_PREFIX + req.getMobile(),captcha,60*5, TimeUnit.SECONDS);
......
......@@ -11,4 +11,5 @@ public class SendValCodeReq {
@NotBlank(message = "请填写手机号")
@Pattern(regexp = ValidatorPattern.MOBILE_PATTERN, message = "手机号格式不正确")
private String mobile;
private String templateCode;
}
......@@ -289,4 +289,23 @@ public class SysUserService extends BaseServiceImpl<SysUserMapper, SysUserPO> {
orgUserReceiveRecordPO.setProductType(ProductTypeEnum.ENTERPRISE_COURSE_BUY);
orgUserReceiveRecordService.validateAndSave(Lists.newArrayList(orgUserReceiveRecordPO));
}
/**
* 修改手机号
* @param oldMobile
* @param newMobile
*/
public void changeUsername(String oldMobile, String newMobile) {
SysUserPO oldUser = getByUsername(oldMobile);
if (oldUser == null){
throw new BusinessException("当前用户不存在");
}
SysUserPO newUser = getByUsername(newMobile);
if (newUser != null){
throw new BusinessException("该手机枵已存在,不能进行换绑操作");
}
oldUser.setUsername(newMobile);
updateById(oldUser);
}
}
package com.qkdata.biz.web.controller;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.google.common.base.Strings;
import com.qkdata.biz.common.BizConstants;
import com.qkdata.biz.management.service.CourseConsumeRecordService;
import com.qkdata.biz.management.service.CourseService;
......@@ -8,6 +9,7 @@ import com.qkdata.biz.management.service.ProductOrderService;
import com.qkdata.biz.management.vo.ProductOrderModel;
import com.qkdata.biz.management.vo.QueryFavoriteCourseModel;
import com.qkdata.biz.management.vo.QueryProductOrderModel;
import com.qkdata.biz.sys.entity.SysUserPO;
import com.qkdata.biz.sys.service.SysUserService;
import com.qkdata.biz.web.service.UserCenterService;
import com.qkdata.biz.web.vo.*;
......@@ -18,13 +20,19 @@ import com.qkdata.common.base.model.Result;
import com.qkdata.common.util.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@Api(tags = "我的相关数据接口-手机端")
@RestController
@RequestMapping("/api/userCenter")
......@@ -39,6 +47,11 @@ public class UserCenterController {
private ProductOrderService orderService;
@Autowired
private CourseConsumeRecordService courseConsumeRecordService;
@Value("${spring.profiles.active:default}")
private String env;
@Autowired
@Qualifier("stringRedisTemplate")
private StringRedisTemplate redisTemplate;
@ApiOperation("获取个人信息")
@GetMapping("/userInfo")
......@@ -160,4 +173,47 @@ public class UserCenterController {
}
return Result.succeed(flag);
}
@ApiOperation("换绑-验证原手机号")
@PostMapping("/user/checkMobile")
@RequiresRoles(value = {BizConstants.ROLE_USER,BizConstants.ROLE_ENTERPRISE_ADMIN},logical = Logical.OR)
@SysLog("换绑-验证原手机号")
public Result<String> checkMobile(@RequestBody @Validated LoginByCodeModel model){
checkMobileCode(model);
String username = UserContext.getUser().getUsername();
if (!username.equals(model.getMobile())){
throw new BusinessException("手机号码与当前用户不一致");
}
SysUserPO user = sysUserService.getByUsername(model.getMobile());
if (user == null){
throw new BusinessException("手机号码不存在");
}
return Result.succeed("ok");
}
@ApiOperation("换绑-变更手机号")
@PostMapping("/user/changeMobile")
@RequiresRoles(value = {BizConstants.ROLE_USER,BizConstants.ROLE_ENTERPRISE_ADMIN},logical = Logical.OR)
@SysLog("换绑-变更手机号")
public Result<String> changeMobile(@RequestBody @Validated LoginByCodeModel model){
checkMobileCode(model);
sysUserService.changeUsername(UserContext.getUser().getUsername(),model.getMobile());
return Result.succeed("ok");
}
private void checkMobileCode(LoginByCodeModel model){
// if (!"dev".equals(env)){
String cache_captcha = (String) redisTemplate.opsForValue().get(BizConstants.MOBILE_CAPTCHA_PREFIX+model.getMobile());
if (!Strings.isNullOrEmpty(cache_captcha)){
cache_captcha = cache_captcha.trim();
if (!cache_captcha.equals(model.getCode())){
log.error("验证码错误:redis值为{},输入值为{}",cache_captcha,model.getCode());
throw new BusinessException("验证码错误");
}
}else {
throw new BusinessException("验证码错误");
}
// }
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment