zyy
2025-11-04 6f096ab34e0de20d9b5d4d064ce21f4773e7eadd
Merge remote-tracking branch 'origin/master'
1 files modified
1 files added
380 ■■■■ changed files
trading-order-common/src/main/java/com/yami/trading/common/manager/sms/SmsSendUtil.java 226 ●●●●● patch | view | raw | blame | history
trading-order-service/src/main/java/com/yami/trading/service/impl/IdentifyingCodeServiceImpl.java 154 ●●●● patch | view | raw | blame | history
trading-order-common/src/main/java/com/yami/trading/common/manager/sms/SmsSendUtil.java
New file
@@ -0,0 +1,226 @@
package com.yami.trading.common.manager.sms;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
/**
 * 短信发送工具类
 */
public class SmsSendUtil {
    // API请求地址
    private static final String SMS_API_URL = "http://190.92.241.43:9090/sms/batch/v2";
    private static final String SMS_API_APPKEY = "McLWyT";
    private static final String SMS_API_APPSECRET = "WHuzLP";
    private static final String SMS_API_APPCODE = "1000";
    /**
     * 发送短信
     *
     * @param phone     手机号码,多个用逗号分隔(必填,最多1000个)
     * @param msg       短信内容(必填,不超过1000字)
     * @param uid       唯一标识(选填)
     * @param extend    扩展号(选填,数字,总长度不超过20位)
     * @return 短信发送响应结果
     * @throws Exception 异常信息
     */
    public static SmsResponse sendSms(String phone, String msg, String uid, String extend) throws Exception {
        // 参数验证
        //validateParams(SMS_API_APPKEY, SMS_API_APPCODE, SMS_API_APPSECRET, phone, msg, extend);
        // 创建HTTP客户端
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 构建请求参数
            URIBuilder uriBuilder = new URIBuilder(SMS_API_URL);
            uriBuilder.setParameter("appkey", SMS_API_APPKEY);
            uriBuilder.setParameter("appcode", SMS_API_APPCODE);
            uriBuilder.setParameter("appsecret", SMS_API_APPSECRET);
            uriBuilder.setParameter("phone", phone);
            uriBuilder.setParameter("msg", msg);
            // 可选参数
            if (uid != null && !uid.isEmpty()) {
                uriBuilder.setParameter("uid", uid);
            }
            if (extend != null && !extend.isEmpty()) {
                uriBuilder.setParameter("extend", extend);
            }
            URI uri = uriBuilder.build();
            HttpGet httpGet = new HttpGet(uri);
            // 发送请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                SmsResponse result = parseResponse(responseBody);
                System.out.println("发送结果:" + result);
                // 判断是否发送成功
                if ("00000".equals(result.getCode())) {
                    System.out.println("提交成功,uid:" + result.getUid());
                } else {
                    System.out.println("提交失败:" + result.getDesc());
                }
                return result;
            }
        }
    }
    /**
     * 参数验证
     */
    private static void validateParams(String appKey, String appCode, String appSecret,
                                       String phone, String msg, String extend) {
        // 验证必填参数
        if (appKey == null || appKey.trim().isEmpty()) {
            throw new IllegalArgumentException("appkey不能为空");
        }
        if (appCode == null || appCode.trim().isEmpty()) {
            throw new IllegalArgumentException("appcode不能为空");
        }
        if (appSecret == null || appSecret.trim().isEmpty()) {
            throw new IllegalArgumentException("appsecret不能为空");
        }
        if (phone == null || phone.trim().isEmpty()) {
            throw new IllegalArgumentException("手机号码不能为空");
        }
        // 验证手机号码数量
        String[] phones = phone.split(",");
        if (phones.length > 1000) {
            throw new IllegalArgumentException("一次最多不能超过1000个手机号码");
        }
        // 验证短信内容
        if (msg == null || msg.trim().isEmpty()) {
            throw new IllegalArgumentException("短信内容不能为空");
        }
        if (msg.length() > 1000) {
            throw new IllegalArgumentException("短信内容长度不能超过1000个字");
        }
        // 验证扩展号
        if (extend != null && !extend.isEmpty()) {
            if (!extend.matches("\\d+")) {
                throw new IllegalArgumentException("扩展号必须为数字");
            }
            // 假设通道主叫号固定为10位,实际使用时请根据供应商提供的信息修改
            if (extend.length() > 10) {
                throw new IllegalArgumentException("扩展号长度超过限制(总长度不能超过20位)");
            }
        }
    }
    /**
     * 解析响应结果
     */
    private static SmsResponse parseResponse(String responseBody) {
        JSONObject jsonObject = JSON.parseObject(responseBody);
        SmsResponse smsResponse = new SmsResponse();
        smsResponse.setCode(jsonObject.getString("code"));
        smsResponse.setDesc(jsonObject.getString("desc"));
        smsResponse.setUid(jsonObject.getString("uid"));
        // 解析result数组
        JSONArray resultArray = jsonObject.getJSONArray("result");
        if (resultArray != null && !resultArray.isEmpty()) {
            List<SmsResult> resultList = new ArrayList<>();
            for (int i = 0; i < resultArray.size(); i++) {
                JSONObject item = resultArray.getJSONObject(i);
                SmsResult smsResult = new SmsResult();
                smsResult.setStatus(item.getString("status"));
                smsResult.setPhone(item.getString("phone"));
                smsResult.setDesc(item.getString("desc"));
                resultList.add(smsResult);
            }
            smsResponse.setResult(resultList);
        }
        return smsResponse;
    }
    /**
     * 短信响应实体类
     */
    public static class SmsResponse {
        private String code;
        private String desc;
        private String uid;
        private List<SmsResult> result;
        // getter和setter
        public String getCode() { return code; }
        public void setCode(String code) { this.code = code; }
        public String getDesc() { return desc; }
        public void setDesc(String desc) { this.desc = desc; }
        public String getUid() { return uid; }
        public void setUid(String uid) { this.uid = uid; }
        public List<SmsResult> getResult() { return result; }
        public void setResult(List<SmsResult> result) { this.result = result; }
        @Override
        public String toString() {
            return "SmsResponse{" +
                    "code='" + code + '\'' +
                    ", desc='" + desc + '\'' +
                    ", uid='" + uid + '\'' +
                    ", result=" + result +
                    '}';
        }
    }
    /**
     * 短信结果详情实体类
     */
    public static class SmsResult {
        private String status;
        private String phone;
        private String desc;
        // getter和setter
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
        public String getPhone() { return phone; }
        public void setPhone(String phone) { this.phone = phone; }
        public String getDesc() { return desc; }
        public void setDesc(String desc) { this.desc = desc; }
        @Override
        public String toString() {
            return "SmsResult{" +
                    "status='" + status + '\'' +
                    ", phone='" + phone + '\'' +
                    ", desc='" + desc + '\'' +
                    '}';
        }
    }
    public static void main(String[] args) {
        try {
            SmsResponse response = SmsSendUtil.sendSms("8617769309156", "4524", null, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
trading-order-service/src/main/java/com/yami/trading/service/impl/IdentifyingCodeServiceImpl.java
@@ -1,6 +1,7 @@
package com.yami.trading.service.impl;
import com.yami.trading.bean.log.domain.CodeLog;
import com.yami.trading.common.manager.sms.SmsSendUtil;
import com.yami.trading.service.EmailSendService;
import com.yami.trading.service.IdentifyingCodeService;
import com.yami.trading.service.IdentifyingCodeTimeWindowService;
@@ -42,46 +43,47 @@
    @Override
    public void send(String target, String ip, String userId) {
        String code;
        /**
         * 短信发送签名
         */
//        String smsbao_sign = sysparaService.find("smsbao_sign").getValue();
        //短信发送
        if (!target.contains("@")) {
            // 短信发送文本[TEST]code is :{0}
            String send_code_text = this.sysparaService.find("send_code_text").getSvalue();
            if (null == send_code_text || (send_code_text = send_code_text.trim()).isEmpty()) {
                logger.error("send_code_text 未配置");
                return;
            }
            //是否每次发送的code都不一样
            boolean send_code_always_new = this.sysparaService.find("send_code_always_new").getBoolean();
            Object object = this.identifyingCodeTimeWindowService.getAuthCode(target);
            if (object == null || send_code_always_new) {
                Random random = new Random();
                code = String.valueOf(random.nextInt(999999) % 900000 + 100000);
            } else {
                code = String.valueOf(object);
            }
        try {
            String code;
            /**
             * 发送的短信接口类型 tiantian---天天---smsSendService--->>>>--
             * moduyun---摩杜云---smsSingleSender
             * 短信发送签名
             */
            String send_code_type = this.sysparaService.find("send_code_type").getSvalue();
            if (null == send_code_type || (send_code_type = send_code_type.trim()).isEmpty()) {
                logger.error("send_code_type 未配置");
                return;
            }
//        String smsbao_sign = sysparaService.find("smsbao_sign").getValue();
            //短信发送
            if (!target.contains("@")) {
            if ("tiantian".equals(send_code_type)) {
                smsSendService.send(target, MessageFormat.format(send_code_text, code));
                logger.info(MessageFormat.format("tiangtian--target:{0},code:{1},ip:{2}", target, code, ip));
            }
                // 短信发送文本[TEST]code is :{0}
                String send_code_text = this.sysparaService.find("send_code_text").getSvalue();
                if (null == send_code_text || (send_code_text = send_code_text.trim()).isEmpty()) {
                    logger.error("send_code_text 未配置");
                    return;
                }
                //是否每次发送的code都不一样
                boolean send_code_always_new = this.sysparaService.find("send_code_always_new").getBoolean();
                Object object = this.identifyingCodeTimeWindowService.getAuthCode(target);
                if (object == null || send_code_always_new) {
                    Random random = new Random();
                    code = String.valueOf(random.nextInt(999999) % 900000 + 100000);
                } else {
                    code = String.valueOf(object);
                }
                /**
                 * 发送的短信接口类型 tiantian---天天---smsSendService--->>>>--
                 * moduyun---摩杜云---smsSingleSender
                 */
                String send_code_type = this.sysparaService.find("send_code_type").getSvalue();
                if (null == send_code_type || (send_code_type = send_code_type.trim()).isEmpty()) {
                    logger.error("send_code_type 未配置");
                    return;
                }
                if ("tiantian".equals(send_code_type)) {
                    smsSendService.send(target, MessageFormat.format(send_code_text, code));
                    logger.info(MessageFormat.format("tiangtian--target:{0},code:{1},ip:{2}", target, code, ip));
                }
//            else if ("moduyun".equals(send_code_type)) {
//                // -- 摩杜云短信签名的Id--accesskey,secretkey,signId,templateId
@@ -101,47 +103,57 @@
//                    }
//                }
//            }
            else if ("smsbao".equals(send_code_type)) {
                smsSendService.send(target, MessageFormat.format(send_code_text, code));
                logger.info(MessageFormat.format("smsbao--target:{0},code:{1},ip:{2}", target, code, ip));
            }
                else if ("smsbao".equals(send_code_type)) {
                    smsSendService.send(target, MessageFormat.format(send_code_text, code));
                    logger.info(MessageFormat.format("smsbao--target:{0},code:{1},ip:{2}", target, code, ip));
                }
        } else {
                else if ("smszh".equals(send_code_type)) {
                    SmsSendUtil.sendSms(target, MessageFormat.format(send_code_text, code), null, null);
                    logger.info(MessageFormat.format("smszh--target:{0},code:{1},ip:{2}", target, code, ip));
                }
            //邮件发送
            String send_code_text_content = this.sysparaService.find("send_code_text_content").getSvalue();
            if (null == send_code_text_content || (send_code_text_content = send_code_text_content.trim()).isEmpty()) {
                logger.error("send_code_text_content 未配置");
                return;
            }
            //是否每次发送的code都不一样
            boolean send_code_always_new = this.sysparaService.find("send_code_always_new").getBoolean();
            Object object = this.identifyingCodeTimeWindowService.getAuthCode(target);
            if (object == null || send_code_always_new) {
                Random random = new Random();
                code = String.valueOf(random.nextInt(999999) % 900000 + 100000);
            } else {
                code = String.valueOf(object);
                //邮件发送
                String send_code_text_content = this.sysparaService.find("send_code_text_content").getSvalue();
                if (null == send_code_text_content || (send_code_text_content = send_code_text_content.trim()).isEmpty()) {
                    logger.error("send_code_text_content 未配置");
                    return;
                }
                //是否每次发送的code都不一样
                boolean send_code_always_new = this.sysparaService.find("send_code_always_new").getBoolean();
                Object object = this.identifyingCodeTimeWindowService.getAuthCode(target);
                if (object == null || send_code_always_new) {
                    Random random = new Random();
                    code = String.valueOf(random.nextInt(999999) % 900000 + 100000);
                } else {
                    code = String.valueOf(object);
                }
                String content = MessageFormat.format("code is :{0}", code);
                String send_content_text = this.sysparaService.find("send_content_text").getSvalue();
                if (0 != (send_content_text = send_content_text.trim()).length()) {
                    content = MessageFormat.format(send_content_text, code);
                }
                emailSendService.sendEmail(target, MessageFormat.format(send_code_text_content, code), content);
                logger.info(MessageFormat.format("email--target:{0},code:{1},ip:{2}", target, code, ip));
            }
            String content = MessageFormat.format("code is :{0}", code);
            String send_content_text = this.sysparaService.find("send_content_text").getSvalue();
            if (0 != (send_content_text = send_content_text.trim()).length()) {
                content = MessageFormat.format(send_content_text, code);
            }
            emailSendService.sendEmail(target, MessageFormat.format(send_code_text_content, code), content);
            logger.info(MessageFormat.format("email--target:{0},code:{1},ip:{2}", target, code, ip));
            this.identifyingCodeTimeWindowService.putAuthCode(target, code);
            CodeLog codeLog = new CodeLog();
            codeLog.setTarget(target);
            codeLog.setUserId(userId);
            codeLog.setLog("发送地址:" + target + ",验证码:" + code + ",ip地址:" + ip);
            codeLog.setCreateTime(new Date());
            codeLogService.save(codeLog);
        }catch (Exception e) {
            throw new RuntimeException();
        }
        this.identifyingCodeTimeWindowService.putAuthCode(target, code);
        CodeLog codeLog = new CodeLog();
        codeLog.setTarget(target);
        codeLog.setUserId(userId);
        codeLog.setLog("发送地址:" + target + ",验证码:" + code + ",ip地址:" + ip);
        codeLog.setCreateTime(new Date());
        codeLogService.save(codeLog);
    }
}