问题 10 短信部分

This commit is contained in:
wangshuai 2024-07-16 13:47:01 +08:00
parent 2487ae8c8b
commit 3a902627ac
8 changed files with 94 additions and 46 deletions

View File

@ -4,8 +4,9 @@ import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.sign.Base64;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.ISmsService;
import com.ruoyi.system.service.ISysConfigService;
@ -13,10 +14,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomStringUtils;
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.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.TimeUnit;
@ -38,9 +36,13 @@ public class SmsController {
@Autowired
private ISmsService iSmsService;
@PostMapping("sendSms")
@PostMapping("/sendSms")
@ApiOperation("短信发送接口")
public AjaxResult sendSms(@RequestPart String phone) {
public AjaxResult sendSms(@RequestBody LoginBody loginBody) {
String phone = loginBody.getPhone();
if(StringUtils.isEmpty(phone)){
return AjaxResult.error("请输入手机号码!");
}
AjaxResult ajax = AjaxResult.success();
boolean captchaEnabled = configService.selectCaptchaEnabled();
ajax.put("captchaEnabled", captchaEnabled);
@ -54,7 +56,16 @@ public class SmsController {
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
String smsCode = generateCode();
redisCache.setCacheObject(verifyKey, smsCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
iSmsService.sendSmsOne(phone, smsCode);
String[] phoneNumber = new String[]{phone};
String[] params = new String[]{smsCode};
// String status = iSmsService.sendSms(phoneNumber, params);
if("Ok".equals("Ok")){
System.out.println(phone + "sms code = "+smsCode);
System.out.println(phone + "sms code = "+smsCode);
System.out.println(phone + "sms code = "+smsCode);
System.out.println(phone + "sms code = "+smsCode);
}
ajax.put("uuid", uuid);
return ajax;
}

View File

@ -56,7 +56,7 @@ public class SysLoginController
{
AjaxResult ajax = AjaxResult.success();
// 生成令牌
String token = loginService.loginSms(loginBody.getUsername(), loginBody.getCode(),
String token = loginService.loginSms(loginBody.getPhone(), loginBody.getSmsCode(),
loginBody.getUuid());
ajax.put(Constants.TOKEN, token);
return ajax;

View File

@ -122,6 +122,7 @@ public class SysLoginService
AuthenticationContextHolder.setContext(authenticationToken);
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
authentication = authenticationManager.authenticate(authenticationToken);
}
catch (Exception e)
{

View File

@ -10,7 +10,7 @@ public interface ISmsService {
* @param params
* @return
*/
String sendSms(String[] phoneNumber, String[] params) throws TencentCloudSDKException;
String sendSms(String[] phoneNumber, String[] params);
/**
* 验证验证码

View File

@ -25,7 +25,7 @@ public class SmsServiceImpl implements ISmsService {
private StringRedisTemplate redisTemplate;
@Override
public String sendSms(String[] phoneNumber, String[] params) throws TencentCloudSDKException {
public String sendSms(String[] phoneNumber, String[] params) {
SendSmsRequest req = new SendSmsRequest();
req.setSignName(tencentCloudProperties.getSignName());
req.setSmsSdkAppId(tencentCloudProperties.getSmsSdkAppId());
@ -34,7 +34,12 @@ public class SmsServiceImpl implements ISmsService {
* 示例如+8613711112222 其中前面有一个+ 86为国家码13711112222为手机号最多不要超过200个手机号 */
req.setPhoneNumberSet(phoneNumber);
req.setTemplateParamSet(params);
final SendSmsResponse resp = tencentCloudProperties.smsClient().SendSms(req);
final SendSmsResponse resp;
try {
resp = tencentCloudProperties.smsClient().SendSms(req);
} catch (TencentCloudSDKException e) {
throw new ServiceException(e.getMessage());
}
// 输出json格式的字符串回包
System.out.println(SendSmsResponse.toJsonString(resp));
return resp.getSendStatusSet()[0].getCode();
@ -52,7 +57,6 @@ public class SmsServiceImpl implements ISmsService {
@Override
public void sendSmsOne(String phone, String smsCode) {
try {
/*验证手机号是否存在*/
if(false){
throw new ServiceException("手机号不存在,请联系管理员注册!");
@ -64,9 +68,6 @@ public class SmsServiceImpl implements ISmsService {
if("Ok".equals(status)){
redisTemplate.opsForValue().set(phone, smsCode);
}
} catch (TencentCloudSDKException e) {
throw new ServiceException(e.getMessage());
}
}
}

View File

@ -59,7 +59,10 @@ export function getCodeImg() {
})
}
export function sendSms(data) {
export function sendSms(phone) {
const data = {
phone
}
return request({
url: '/sendSms',
headers: {

View File

@ -1,4 +1,4 @@
import { login, logout, getInfo } from '@/api/login'
import { login, loginSms, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
const user = {
@ -49,6 +49,21 @@ const user = {
})
})
},
// 登录
LoginSms({ commit }, userInfo) {
const phone = userInfo.phone.trim()
const smsCode = userInfo.smsCode
const uuid = userInfo.uuid
return new Promise((resolve, reject) => {
loginSms(phone, smsCode, uuid).then(res => {
setToken(res.token)
commit('SET_TOKEN', res.token)
resolve()
}).catch(error => {
reject(error)
})
})
},
// 获取用户信息
GetInfo({ commit, state }) {

View File

@ -77,7 +77,7 @@
auto-complete="off"
placeholder="验证码"
style="width: 63%"
@keyup.enter.native="handleLogin"
@keyup.enter.native="submitForm"
>
<svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
</el-input>
@ -87,7 +87,7 @@
</el-form-item>
<el-form-item>
<el-button
:loading="loading"
:loading="loading2"
size="medium"
type="primary"
style="width:100%;"
@ -105,7 +105,7 @@
</template>
<script>
import { getCodeImg } from "@/api/login";
import { getCodeImg , sendSms } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
@ -126,6 +126,7 @@ export default {
loginForm2: {
phone:"",
smsCode: "",
uuid: "",
loginType: 'sms'
},
countdown: 0,
@ -145,6 +146,7 @@ export default {
]
},
loading: false,
loading2: false,
//
captchaEnabled: true,
//
@ -178,7 +180,8 @@ export default {
//
this.$refs.loginForm2.validate(valid => {
if (valid) {
console.log('发送验证码到手机:', this.loginForm.phone);
const phone = this.loginForm2.phone
console.log('发送验证码到手机:', phone);
this.countdown = 60; // 60
this.intervalId = setInterval(() => {
if (this.countdown > 0) {
@ -187,24 +190,38 @@ export default {
clearInterval(this.intervalId);
}
}, 1000);
alert('提交成功!');
sendSms(phone).then(res => {
this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
if (this.captchaEnabled) {
this.loginForm2.uuid = res.uuid;
}
});
} else {
console.log('验证失败!');
return false;
}
}
);
});
},
submitForm() {
//
console.log('提交登录信息:', this.loginForm2);
if(this.loginForm2.smsCode==""||this.loginForm2.smsCode.length!=6){
this.$modal.msgError("请输入短信验证码");
return false;
}
//
this.$refs.loginForm2.validate((valid) => {
if (valid) {
this.loading2 = true;
//
this.$store.dispatch("LoginSms", this.loginForm2).then(() => {
this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
}).catch(() => {
this.loading2 = false;
// if (this.captchaEnabled) {
// this.sendCode();
// }
});
}
});
},