package com.sinata.rest.modular.system.controller;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.sinata.common.enums.EnumRemoteSet;
|
import com.sinata.rest.common.ApiUtils;
|
import com.sinata.rest.core.aliyun.sms.HwSmsApi;
|
import com.sinata.rest.core.util.ToolUtil;
|
import com.sinata.rest.modular.member.model.MemUser;
|
import com.sinata.rest.modular.member.service.IMemUserService;
|
import com.sinata.rest.modular.system.model.RemoteSet;
|
import com.sinata.rest.modular.system.model.SmsRecord;
|
import com.sinata.rest.modular.system.service.IRemoteSetService;
|
import com.sinata.rest.modular.system.service.ISmsRecordService;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiImplicitParams;
|
import io.swagger.annotations.ApiOperation;
|
import java.time.LocalDateTime;
|
import java.util.Objects;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.util.StringUtils;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
|
@Slf4j
|
@RestController
|
@RequestMapping("/sms")
|
@Api(tags = "验证码相关", description = "验证码相关")
|
public class SmsController {
|
|
@Autowired
|
ISmsRecordService smsRecordService;
|
|
@Autowired
|
IRemoteSetService remoteSetService;
|
|
@Autowired
|
IMemUserService memUserService;
|
private static final String note = "发送验证码 type = 1 注册; type = 2 登录 type = 3 修改密码 type = 4 修改手机号 type = 5修改交易密码";
|
|
@GetMapping(value = "/send")
|
@ApiOperation(value = "发送验证码", notes = note, response = ApiUtils.class)
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "type", value = "验证码类型", dataType = "String", paramType = "query"),
|
@ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query"),
|
})
|
public Object sendSms(Integer type, String phone) {
|
if (type == null) {
|
return ApiUtils.returnNG(null, "发送失败");
|
}
|
if (StringUtils.isEmpty(phone)) {
|
return ApiUtils.returnNG(null, "发送失败");
|
}
|
LambdaQueryWrapper<MemUser> userWrapper = new LambdaQueryWrapper<>();
|
userWrapper.eq(MemUser::getPhone, phone);
|
MemUser user = memUserService.getOne(userWrapper);
|
if (Objects.isNull(user)) {
|
return ApiUtils.returnNG(null, "手机号不存在,请注册");
|
}
|
// 条件查询
|
LambdaQueryWrapper<SmsRecord> wrapper = new LambdaQueryWrapper();
|
wrapper.eq(SmsRecord::getCodeType, type).eq(SmsRecord::getPhone, phone);
|
SmsRecord r = smsRecordService.getOne(wrapper);
|
RemoteSet set = remoteSetService.getById(EnumRemoteSet.SMS_SWITCH.index);
|
boolean open = false;
|
if (set != null) {
|
open = Integer.parseInt(set.getValueStr()) != 0;
|
}
|
if (r == null) {
|
r = new SmsRecord();
|
r.setPhone(phone);
|
r.setCodeType(type);
|
r.setCode(generateCode(open));
|
doSendCode(open, phone, r.getCode());
|
smsRecordService.save(r);
|
} else {
|
if (r.getIsCheck() == 0) {
|
if (r.getCreateTime().plusMinutes(1).isAfter(LocalDateTime.now())) {
|
return ApiUtils.returnNG(null, "发送过于频繁了,请稍后再试");
|
}
|
}
|
r.setCreateTime(LocalDateTime.now());
|
r.setCode(generateCode(open));
|
doSendCode(open, phone, r.getCode());
|
r.setIsCheck(0);
|
smsRecordService.updateById(r);
|
}
|
return ApiUtils.returnOK(null, "发送成功!");
|
}
|
|
@GetMapping(value = "/check")
|
@ApiOperation(value = "验证验证码", notes = note, response = ApiUtils.class)
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "type", value = "验证码类型", dataType = "String", paramType = "query"),
|
@ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query"),
|
@ApiImplicitParam(name = "code", value = "验证码", dataType = "String", paramType = "query")
|
})
|
public Object checkSms(Integer type, String phone, String code) {
|
if (type == null) {
|
return ApiUtils.returnNG(null, "请求失败");
|
}
|
if (StringUtils.isEmpty(phone)) {
|
return ApiUtils.returnNG(null, "请求失败");
|
}
|
if (StringUtils.isEmpty(code)) {
|
return ApiUtils.returnNG(null, "请求失败");
|
}
|
String msg = smsRecordService.justVerifyCode(type, phone, code);
|
if (msg.isEmpty()) {
|
return ApiUtils.returnOK(null, "验证成功!");
|
} else {
|
return ApiUtils.returnNG(null, msg);
|
}
|
}
|
|
//生成验证码
|
private String generateCode(boolean open) {
|
if (open) {
|
return ToolUtil.getRandomNumber(4);
|
} else {
|
return "2020";
|
}
|
}
|
|
//发送验证码
|
private void doSendCode(boolean open, String phone, String code) {
|
if (open) {
|
try{
|
HwSmsApi.sendSms("+86", phone, code);
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}
|
}
|
|
}
|