1.添加依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.24</version>
</dependency>
2.控制层
@Api(tags = "短信业务接口管理")
@RequestMapping("p/sms")
@RestController
public class SendController {
@Autowired
private SendService sendService;
/**
* 获取短信验证码
* @param map 手机号码
* @return
*/
@ApiOperation("获取短信验证码")
@PostMapping("send")
public Result<String> sendPhoneMsg(@RequestBody Map<String,Object> map) {
sendService.sendPhoneMsg(map);
return Result.success("短信发送成功");
}
/**
* 绑定手机号码
* @param map 手机号码phonenum,短信验证码code
* @return
*/
// p/sms/savePhone
@ApiOperation("绑定手机号码")
@PostMapping("savePhone")
public Result<String> saveMsgPhone(@RequestBody Map<String,Object> map) {
Boolean saved = sendService.saveMsgPhone(map);
return Result.handle(saved);
}
}
3.接口
public interface SendService {
/**
* 获取短信验证码
* @param map
*/
void sendPhoneMsg(Map<String, Object> map);
/**
* 绑定手机号码
* @param map
* @return
*/
Boolean saveMsgPhone(Map<String, Object> map);
}
- 接口实现
@Service
public class SendServiceImpl implements SendService {
@Autowired
private AliyunDxConfig aliyunDxConfig;
@Autowired
private MemberMapper memberMapper;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public void sendPhoneMsg(Map<String, Object> map) {
// 准备配置对象
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
.setAccessKeyId(aliyunDxConfig.getAccessKeyID())
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
.setAccessKeySecret(aliyunDxConfig.getAccessKeySecret());
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
config.endpoint = aliyunDxConfig.getEndpoint();
try {
// 创建客户端对象
com.aliyun.dysmsapi20170525.Client client = new com.aliyun.dysmsapi20170525.Client(config);
// 获取手机号码
String phonenum = (String) map.get("phonenum");
// 生成一个随机数字
String randomNumber = RandomUtil.randomNumbers(4);
// 将生成的随机数字存放到redis中
stringRedisTemplate.opsForValue().set(MemberConstants.MSG_PHONE_PREFIX+phonenum, randomNumber, Duration.ofMinutes(30));
// 创建模版参数
String templateParam = "{\"code\":\""+randomNumber+"\"}";
// 创建请求参数对象
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
.setPhoneNumbers(phonenum)
.setSignName(aliyunDxConfig.getSignName())
.setTemplateCode(aliyunDxConfig.getTemplateCode())
.setTemplateParam(templateParam);
// 发送请求
client.sendSmsWithOptions(sendSmsRequest, new com.aliyun.teautil.models.RuntimeOptions());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public Boolean saveMsgPhone(Map<String, Object> map) {
// 获取会员输入的短信验证码
String code = (String) map.get("code");
// 获取会员手机号码
String phonenum = (String) map.get("phonenum");
// 从redis中获取当前手机号码对应验证码
String redisCode = stringRedisTemplate.opsForValue().get(MemberConstants.MSG_PHONE_PREFIX + phonenum);
// 判断验证码是否正确
if (!code.equals(redisCode)) {
throw new BusinessException("请输入正确的短信验证码");
}
// 将会员手机号码更新到会员信息中
Member member = new Member();
member.setUserMobile(phonenum);
// 获取当前会员的openId
String openId = AuthUtils.getMemberOpenId();
return memberMapper.update(member,new LambdaUpdateWrapper<Member>()
.eq(Member::getOpenId,openId)
)>0;
}
}
5.配置类
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "aliyun.dx")
@RefreshScope
public class AliyunDxConfig {
/**
* 阿里云平台访问API接口accessKeyID
*/
private String accessKeyID;
/**
* 阿里云平台访问API接口accessKeySecret
*/
private String accessKeySecret;
/**
* 短信签名
*/
private String signName;
/**
* 短信模版CODE
*/
private String templateCode;
/**
* API访问端点
*/
private String endpoint;
}
测试成功