AliSmsServiceImpl.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package com.xy.service;
  2. import cn.hutool.json.JSONUtil;
  3. import com.xy.dto.SmsDTO;
  4. import com.xy.dto.SmsSendCommonDTO;
  5. import com.xy.dto.SmsSendDTO;
  6. import com.xy.enums.SmsSceneEnum;
  7. import com.xy.error.CommRuntimeException;
  8. import com.xy.utils.AuthorizeUtils;
  9. import com.xy.utils.R;
  10. import io.swagger.annotations.Api;
  11. import io.swagger.annotations.ApiOperation;
  12. import lombok.AllArgsConstructor;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.validation.annotation.Validated;
  16. import org.springframework.web.bind.annotation.RequestBody;
  17. import java.util.Map;
  18. /**
  19. * 阿里短信服务
  20. *
  21. * @author 谭斌
  22. * @date 2023/08/10
  23. */
  24. @Slf4j
  25. @Service
  26. @AllArgsConstructor
  27. @Api(tags = "阿里云短信服务")
  28. public class AliSmsServiceImpl implements AliSmsService {
  29. private SmsUtil smsUtil;
  30. @Override
  31. @ApiOperation("发送验证码")
  32. public R sendCode(@RequestBody @Validated SmsSendDTO smsSendDTO) {
  33. String mobile = smsSendDTO.getMobile();
  34. Integer scene = smsSendDTO.getScene();
  35. SmsSceneEnum smsSceneEnum = SmsSceneEnum.getCodeByScene(scene);
  36. if (smsSceneEnum == null) {
  37. throw new CommRuntimeException("发送场景有误!");
  38. }
  39. //发短信验证码
  40. smsUtil.sendSmsCode(mobile, smsSceneEnum, AuthorizeUtils.getLoginId(String.class));
  41. return R.ok();
  42. }
  43. @Override
  44. public R sendCodeByUserInfo(SmsSendDTO smsSendDTO) {
  45. String mobile = smsSendDTO.getMobile();
  46. Integer scene = smsSendDTO.getScene();
  47. Long userInfoId = smsSendDTO.getUserInfoId();
  48. SmsSceneEnum smsSceneEnum = SmsSceneEnum.getCodeByScene(scene);
  49. if (smsSceneEnum == null) {
  50. throw new CommRuntimeException("发送场景有误!");
  51. }
  52. //发短信验证码
  53. smsUtil.sendSmsCode(mobile, smsSceneEnum, String.valueOf(userInfoId));
  54. return R.ok();
  55. }
  56. @Override
  57. public R<Boolean> verifyCode(SmsDTO.Validation smsDTO) {
  58. return R.ok(smsUtil.verifyCode(smsDTO.getMobile(), smsDTO.getSmsSceneEnum(), smsDTO.getUid(), smsDTO.getCode()));
  59. }
  60. @Override
  61. public R sendMsgCommon(SmsSendCommonDTO smsSendDTO) {
  62. String mobile = smsSendDTO.getMobile();
  63. Integer scene = smsSendDTO.getScene();
  64. Map<String, Object> templateParams = smsSendDTO.getTemplateParams();
  65. Long msgConfigId = smsSendDTO.getMsgConfigId();
  66. SmsSceneEnum smsSceneEnum = SmsSceneEnum.getCodeByScene(scene);
  67. if (smsSceneEnum == null) {
  68. log.error("通用短信发送发送场景有误");
  69. throw new CommRuntimeException("发送场景有误!");
  70. }
  71. //发短信验证码
  72. smsUtil.sendSmsCommon(mobile, msgConfigId, templateParams);
  73. return R.ok();
  74. }
  75. }