| | |
| | | import com.aliyun.tea.TeaException; |
| | | import com.aliyun.teaopenapi.models.Config; |
| | | import com.aliyun.teautil.models.RuntimeOptions; |
| | | import com.ruoyi.system.model.TSysMessageConfig; |
| | | import com.ruoyi.system.service.TSysMessageConfigService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.util.Map; |
| | | |
| | | @Component |
| | | @Slf4j |
| | | public class MsgUtils { |
| | | |
| | | @Value("${code.config.accessKeyId}") |
| | | private String accessKeyId; |
| | | @Value("${code.config.accessKeySecret}") |
| | | private String accessKeySecret; |
| | | @Value("${code.config.signName}") |
| | | private String signName; |
| | | @Autowired |
| | | private TSysMessageConfigService messageConfigService; |
| | | |
| | | /** |
| | | * 使用AK&SK初始化账号Client |
| | |
| | | return new com.aliyun.dysmsapi20170525.Client(config); |
| | | } |
| | | |
| | | public void sendMsg(String phone,String code,String templateCode) throws Exception { |
| | | com.aliyun.dysmsapi20170525.Client client = MsgUtils.createClient(accessKeyId,accessKeySecret); |
| | | public void sendMsg(String phone, Map<String,String> params, String templateCode) throws Exception { |
| | | TSysMessageConfig sysMessageConfig = messageConfigService.getById(1); |
| | | com.aliyun.dysmsapi20170525.Client client = MsgUtils.createClient(sysMessageConfig.getAccessKeyId(),sysMessageConfig.getAccessKeySecret()); |
| | | SendSmsRequest sendSmsRequest = new SendSmsRequest() |
| | | .setSignName(signName) |
| | | .setSignName(sysMessageConfig.getSignName()) |
| | | .setTemplateCode(templateCode) |
| | | .setPhoneNumbers(phone) |
| | | .setTemplateParam("{\"code\":\""+code+"\"}"); |
| | | .setPhoneNumbers(phone); |
| | | if(!CollectionUtils.isEmpty(params)){ |
| | | StringBuilder builder = new StringBuilder(); |
| | | params.forEach((key,value)->{ |
| | | builder.append("\""+key+"\":\""+value+"\""); |
| | | builder.append(","); |
| | | }); |
| | | builder.deleteCharAt(builder.length()-1); |
| | | sendSmsRequest.setTemplateParam("{"+builder.toString()+"}"); |
| | | } |
| | | RuntimeOptions runtime = new RuntimeOptions(); |
| | | try { |
| | | // 复制代码运行请自行打印 API 的返回值 |
| | |
| | | log.info("短信发送失败:{}",error.message); |
| | | } |
| | | } |
| | | |
| | | } |