package com.xy.service; import cn.hutool.json.JSONUtil; import com.xy.dto.SmsDTO; import com.xy.dto.SmsSendCommonDTO; import com.xy.dto.SmsSendDTO; import com.xy.enums.SmsSceneEnum; import com.xy.error.CommRuntimeException; import com.xy.utils.AuthorizeUtils; import com.xy.utils.R; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestBody; import java.util.Map; /** * 阿里短信服务 * * @author 谭斌 * @date 2023/08/10 */ @Slf4j @Service @AllArgsConstructor @Api(tags = "阿里云短信服务") public class AliSmsServiceImpl implements AliSmsService { private SmsUtil smsUtil; @Override @ApiOperation("发送验证码") public R sendCode(@RequestBody @Validated SmsSendDTO smsSendDTO) { String mobile = smsSendDTO.getMobile(); Integer scene = smsSendDTO.getScene(); SmsSceneEnum smsSceneEnum = SmsSceneEnum.getCodeByScene(scene); if (smsSceneEnum == null) { throw new CommRuntimeException("发送场景有误!"); } //发短信验证码 smsUtil.sendSmsCode(mobile, smsSceneEnum, AuthorizeUtils.getLoginId(String.class)); return R.ok(); } @Override public R sendCodeByUserInfo(SmsSendDTO smsSendDTO) { String mobile = smsSendDTO.getMobile(); Integer scene = smsSendDTO.getScene(); Long userInfoId = smsSendDTO.getUserInfoId(); SmsSceneEnum smsSceneEnum = SmsSceneEnum.getCodeByScene(scene); if (smsSceneEnum == null) { throw new CommRuntimeException("发送场景有误!"); } //发短信验证码 smsUtil.sendSmsCode(mobile, smsSceneEnum, String.valueOf(userInfoId)); return R.ok(); } @Override public R verifyCode(SmsDTO.Validation smsDTO) { return R.ok(smsUtil.verifyCode(smsDTO.getMobile(), smsDTO.getSmsSceneEnum(), smsDTO.getUid(), smsDTO.getCode())); } @Override public R sendMsgCommon(SmsSendCommonDTO smsSendDTO) { String mobile = smsSendDTO.getMobile(); Integer scene = smsSendDTO.getScene(); Map templateParams = smsSendDTO.getTemplateParams(); Long msgConfigId = smsSendDTO.getMsgConfigId(); SmsSceneEnum smsSceneEnum = SmsSceneEnum.getCodeByScene(scene); if (smsSceneEnum == null) { log.error("通用短信发送发送场景有误"); throw new CommRuntimeException("发送场景有误!"); } //发短信验证码 smsUtil.sendSmsCommon(mobile, msgConfigId, templateParams); return R.ok(); } }