add sms code

This commit is contained in:
2025-07-30 19:04:45 +08:00
parent 7d4ba584a8
commit ed696e7c90
15 changed files with 729 additions and 1 deletions

View File

@@ -0,0 +1,105 @@
package com.ttstd.videotablet.controller;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.ttstd.videotablet.result.Result;
import com.ttstd.videotablet.sms.SendSms;
import com.ttstd.videotablet.utils.TextUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@RestController
public class SmsController {
private static final int LOGIN_CODE_TTL = 5;
//引入 redis
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/verify_code")
public Result getMethodName(@RequestParam("number") String number) {
if (TextUtils.isEmpty(number)) {
return Result.error().message("phone number is empty");
}
String oldCode = stringRedisTemplate.opsForValue().get(number);
if (TextUtils.isEmpty(oldCode)) {
String code = SendSms.generatedcode(6);
return sendCode(number, code);
} else {
return sendCode(number, oldCode);
}
}
private Result sendCode(String number, String code) {
try {
com.aliyun.dysmsapi20170525.Client client = createClient();
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
.setSignName("同同的小站")
.setTemplateCode("SMS_468370574")
.setPhoneNumbers(number)
.setTemplateParam("{\"code\":\"" + code + "\"}");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
// 复制代码运行请自行打印 API 的返回值
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
// System.out.println(sendSmsResponse.body.toMap());
// System.out.println(sendSmsResponse.getBody().getMessage());
// System.out.println(sendSmsResponse.getBody().getCode());
if ("OK".equals(sendSmsResponse.getBody().getCode())) {
String randomString = RandomStringUtils.randomAlphanumeric(32);
Map<String, Object> map = new HashMap<>();
map.put("verify_key", randomString);
map.put("sms", sendSmsResponse.getBody().getMessage());
//4.保存验证码到Redis,并且设置有效期5分钟
stringRedisTemplate.opsForValue().set(number, code, LOGIN_CODE_TTL, TimeUnit.MINUTES);
return Result.ok().data(map);
} else {
return Result.error().message(sendSmsResponse.getBody().getMessage());
}
} catch (TeaException error) {
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
System.out.println(error.getMessage());
// 诊断地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
return Result.error().data("sms", error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
System.out.println(error.getMessage());
// 诊断地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
return Result.error().data("sms", error.message);
}
}
/**
* 使用AK&SK初始化账号Client
*
* @return Client
* @throws Exception
*/
public com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
// 建议使用更安全的 STS 方式更多鉴权访问方式请参见https://help.aliyun.com/document_detail/378657.html。
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
.setAccessKeyId("LTAI5tEVYLeg7U3bP58n3Xkj")
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
.setAccessKeySecret("bbPSlxkaGgkhUPaCOP7CANaNNY1b2p");
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
}