Procházet zdrojové kódy

feature 代码提交

xiahan před 3 týdny
rodič
revize
ef7704017a

+ 1 - 3
qhse-common/src/main/java/com/rongwei/bscommon/system/service/AliyunSmsService.java

@@ -1,7 +1,5 @@
 package com.rongwei.bscommon.system.service;
 
-import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
-
 import java.util.Map;
 
 public interface AliyunSmsService {
@@ -13,5 +11,5 @@ public interface AliyunSmsService {
      * @param phone            手机号
      * @return 发送结果
      */
-    SendSmsResponse sendSms(String templateId, Map<String, String> templateParamMap, String phone);
+    void sendSms(String templateId, Map<String, Object> templateParamMap, String phone);
 }

+ 23 - 12
qhse-common/src/main/java/com/rongwei/bscommon/system/service/impl/AliyunSmsServiceImpl.java

@@ -11,13 +11,13 @@ import com.rongwei.bscommon.system.utils.QHSEUtils;
 import com.rongwei.bsentity.domain.SendRecordDo;
 import com.rongwei.rwadmincommon.system.vo.SysUserVo;
 import lombok.extern.slf4j.Slf4j;
-import org.checkerframework.checker.units.qual.A;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import java.util.Map;
 
+import static com.rongwei.bscommon.system.utils.QHSEConstant.OK_LOWER_CASE;
+
 @Service
 @Slf4j
 public class AliyunSmsServiceImpl implements AliyunSmsService {
@@ -49,32 +49,42 @@ public class AliyunSmsServiceImpl implements AliyunSmsService {
      * @return 发送结果
      */
     @Override
-    public SendSmsResponse sendSms(String templateId,
-                                   Map<String, String> templateParamMap, String phone) {
+    public void sendSms(String templateId,
+                        Map<String, Object> templateParamMap, String phone) {
         String templateParam = com.aliyun.teautil.Common.toJSONString(templateParamMap);
         SendSmsResponse resp = null;
+        SendSmsRequest sendSmsRequest = new SendSmsRequest();
+        RuntimeOptions runtime = null;
+        com.aliyun.dysmsapi20170525.Client client = null;
         try {
-            com.aliyun.dysmsapi20170525.Client client = createClient();
+            client = createClient();
             // 创建请求
-            SendSmsRequest sendSmsRequest = new SendSmsRequest();
             sendSmsRequest.setSignName(aliyunSmsConfig.getSignName());
             sendSmsRequest.setTemplateCode(templateId);
             sendSmsRequest.setTemplateParam(templateParam);
             sendSmsRequest.setPhoneNumbers(phone);
-            RuntimeOptions runtime = new RuntimeOptions();
+            runtime = new RuntimeOptions();
             resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
-            log.info("发送短信结果:{}", com.aliyun.teautil.Common.toJSONString(resp));
-            // 保存发送记录
-            saveSendRecord(resp, sendSmsRequest);
         } catch (TeaException error) {
             log.error("诊断地址:{}", error.getData().get("Recommend"));
             log.error("短信发送异常 模板编号为:{},参数:{},手机号:{} - 原因: {} | 异常类型: {}", templateId, templateParam, phone, error.getMessage(), error.getClass().getSimpleName(), error);
+            // 保存发送记录
+            saveSendRecord(resp, sendSmsRequest, "ERROR");
             throw new RuntimeException("短信发送异常!请联系系统管理员");
         } catch (Exception _error) {
             log.error("短信发送异常 模板编号为:{},参数:{},手机号:{} - 原因: {} | 异常类型: {}", templateId, templateParam, phone, _error.getMessage(), _error.getClass().getSimpleName(), _error);
+            saveSendRecord(resp, sendSmsRequest, "ERROR");
             throw new RuntimeException("短信发送异常!请联系系统管理员");
         }
-        return resp;
+        String sendCode = resp.body.code;
+        if (OK_LOWER_CASE.equals(sendCode)) {
+            log.info("短信发送成功");
+            saveSendRecord(resp, sendSmsRequest, "SUCCESS");
+        } else {
+            log.info("短信发送失败,错误码为:{}", sendCode);
+            saveSendRecord(resp, sendSmsRequest, "ERROR");
+            throw new RuntimeException("短信发送失败!" + resp.body.message);
+        }
     }
 
     /**
@@ -83,13 +93,14 @@ public class AliyunSmsServiceImpl implements AliyunSmsService {
      * @param resp           发送结果
      * @param sendSmsRequest 发送请求
      */
-    private void saveSendRecord(SendSmsResponse resp, SendSmsRequest sendSmsRequest) {
+    private void saveSendRecord(SendSmsResponse resp, SendSmsRequest sendSmsRequest, String state) {
         SendRecordDo sendRecordDo = new SendRecordDo();
         sendRecordDo.setResult(com.aliyun.teautil.Common.toJSONString(resp));
         sendRecordDo.setTemplateCode(sendSmsRequest.getTemplateCode());
         sendRecordDo.setTemplateParam(sendSmsRequest.getTemplateParam());
         sendRecordDo.setPhoneNumber(sendSmsRequest.getPhoneNumbers());
         sendRecordDo.setSignName(sendSmsRequest.getSignName());
+        sendRecordDo.setRoption(state);
         SysUserVo currentUser = QHSEUtils.getCurrentUser();
         QHSEUtils.initModelGeneralParameters(sendRecordDo, currentUser);
         sendRecordService.save(sendRecordDo);

+ 3 - 8
qhse-common/src/main/java/com/rongwei/bscommon/system/service/impl/MobileVehicleProcessingServiceImpl.java

@@ -1,6 +1,5 @@
 package com.rongwei.bscommon.system.service.impl;
 
-import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.rongwei.bscommon.system.config.SmsCommonConfig;
 import com.rongwei.bscommon.system.dao.MobileVehicleProcessingDao;
@@ -42,16 +41,12 @@ public class MobileVehicleProcessingServiceImpl extends ServiceImpl<MobileVehicl
             log.info("开始发送短信,手机号:{}", item.getPhone());
             // 发送短信
             try {
-                Map<String, String> templateParamMap = new HashMap<>();
+                Map<String, Object> templateParamMap = new HashMap<>();
                 templateParamMap.put("type", smsCommonConfig.getOverSpeedType());
                 templateParamMap.put("desc", String.format(smsCommonConfig.getOverSpeedDesc(), item.getPlateNumber(), DateUtils.format(item.getProcessDate(), DATE_TIME_PATTERN), item.getSpeed()));
-                SendSmsResponse res = aliyunSmsService.sendSms(smsCommonConfig.getTempId(), templateParamMap, item.getPhone());
-                if (res.statusCode != 200) {
-                    log.error("发送短信失败,手机号:{}", item.getPhone());
-                    return R.error("超速短信失败!请联系系统管理员");
-                }
+                aliyunSmsService.sendSms(smsCommonConfig.getTempId(), templateParamMap, item.getPhone());
             } catch (Exception e) {
-                throw new RuntimeException("超速短信发送异常!请联系系统管理员");
+                throw new RuntimeException(e.getMessage());
             }
             log.info("发送短信成功,手机号:{}", item.getPhone());
             item.setNotificationTime(new Date());

+ 12 - 7
qhse-common/src/main/java/com/rongwei/bscommon/system/service/impl/QHSELoginServiceImpl.java

@@ -8,6 +8,7 @@ import com.rongwei.rwadmincommon.system.domain.SysUserDo;
 import com.rongwei.rwadmincommon.system.domain.UserLoginVo;
 import com.rongwei.rwcommon.base.BaseDo;
 import com.rongwei.rwcommon.base.R;
+import com.rongwei.rwcommon.utils.DateUtils;
 import com.rongwei.rwcommon.utils.SecurityUtil;
 import com.rongwei.rwcommon.utils.StringUtils;
 import org.slf4j.Logger;
@@ -22,6 +23,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Random;
 
+import static com.rongwei.bscommon.system.utils.QHSEConstant.ERROR_LOWER_CASE;
+
 /**
  * QHSELoginServiceImpl class
  *
@@ -100,7 +103,7 @@ public class QHSELoginServiceImpl implements QHSELoginService {
             throw new RuntimeException("密码错误");
         }
         // 生成6位数验证码
-        String smsCode = generateSms();
+        int smsCode = generateSms();
         Map<String, Object> map = new HashMap<>();
         map.put("smsCode", smsCode);
         if (!smsCodeConfig.isSendEnabled()) {
@@ -108,9 +111,10 @@ public class QHSELoginServiceImpl implements QHSELoginService {
             return R.ok(map);
         }
         try {
-            aliyunSmsService.sendSms(smsCodeConfig.getTempId(), new HashMap<String, String>() {{
-                put("code", smsCode);
-            }}, mobile);
+            Map<String, Object> templateParamMap = new HashMap<>();
+            templateParamMap.put("code", smsCode);
+
+            aliyunSmsService.sendSms(smsCodeConfig.getTempId(),templateParamMap, mobile);
         } catch (Exception e) {
             log.error("验证码发送失败原因为");
             throw new RuntimeException(e.getMessage());
@@ -121,9 +125,10 @@ public class QHSELoginServiceImpl implements QHSELoginService {
         return R.ok(map);
     }
 
-    public String generateSms() {
-        int code = new Random().nextInt(1000000); // 生成0-999999的随机数
-        return String.format("%06d", code); // 格式化为6位,不足补零
+    public int generateSms() {
+        int lowerBound = 10000;
+        int code = new Random().nextInt(1000000-10000); // 生成0-9999的随机数
+        return lowerBound + code;
     }
 
 

+ 3 - 0
qhse-common/src/main/java/com/rongwei/bscommon/system/utils/QHSEConstant.java

@@ -48,5 +48,8 @@ public class QHSEConstant {
 
     }
     public static final String DEFAULT_WATER_MARK="ZPMC-NT-%s-%s";
+
+    public static final String OK_LOWER_CASE="OK";
+    public static final String ERROR_LOWER_CASE="ERROR";
 }
 

+ 3 - 7
qhse-server/src/main/java/com/rongwei/controller/SendMessageController.java

@@ -1,8 +1,7 @@
 package com.rongwei.controller;
 
-import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
 import com.rongwei.bscommon.system.service.AliyunSmsService;
-import com.rongwei.bsentity.Dto.SendMessageDto;
+import com.rongwei.bsentity.dto.SendMessageDto;
 import com.rongwei.rwcommon.base.R;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -27,11 +26,8 @@ public class SendMessageController {
      */
     @PostMapping("/text")
     public R sendTextMessage(@RequestBody SendMessageDto dto) throws Exception {
-        log.info("开始发送短信");
-        SendSmsResponse res = aliyunSmsService.sendSms(dto.getTemplateId(), dto.getParam(), dto.getPhone());
-        if (res.statusCode != 200) {
-            return R.errorWithMsg("发送短信失败");
-        }
+        log.info("开始发送短信参数为:{}", dto);
+        aliyunSmsService.sendSms(dto.getTemplateId(), dto.getParam(), dto.getPhone());
         return R.okWithMsg("发送短信成功");
     }
 }