add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.dobbinsoft.fw.support.sms;
 
import com.dobbinsoft.fw.core.exception.CoreExceptionDefinition;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.core.exception.ThirdPartServiceException;
import com.dobbinsoft.fw.support.properties.FwSMSProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: rize
 * Date: 2019/11/17
 * Time: 15:50
 */
public class ZjunSMSClient implements SMSClient, InitializingBean {
 
    private static final Logger logger = LoggerFactory.getLogger(ZjunSMSClient.class);
    
    @Autowired
    private FwSMSProperties properties;
    
    private String userName;
    
    private String password;
    
    private String content;
    private String url;
    
    @Override
    public void afterPropertiesSet() throws Exception {
        this.userName = properties.getZjunUserName();
        this.password = properties.getZjunPassword();
        this.content = properties.getZjunContent();
        this.url = "http://sms.izjun.com:8001/sms/api/sendMessageMass";
    }
    
    @Override
    public SMSResult sendRegisterVerify(String phone, String verifyCode) throws ServiceException {
        return send(phone, verifyCode);
    }
 
    @Override
    public SMSResult sendBindPhoneVerify(String phone, String verifyCode) throws ServiceException {
        return send(phone, verifyCode);
    }
 
    @Override
    public SMSResult sendResetPasswordVerify(String phone, String verifyCode) throws ServiceException {
        return send(phone, verifyCode);
    }
 
    @Override
    public SMSResult sendAdminLoginVerify(String phone, String verifyCode) throws ServiceException {
        return send(phone, verifyCode);
    }
 
    private SMSResult send(String phone, String verifyCode) throws ServiceException {
        logger.info("[掌骏短信发送] phone=" + phone + "; verifyCode=" + verifyCode);
        try {
            // 手机号
            List<String> phoneList = new ArrayList<>();
            phoneList.add(phone);
            // 替换内容中的验证码占位符
            String content = this.content.replace("${code}", verifyCode);
            // 时间戳
            long timestamp = System.currentTimeMillis();
            // 计算 sign 参数
            String sign = md5(this.userName + timestamp + md5(this.password));
            // 请求参数
            Map<String, Object> data = new HashMap<>();
            data.put("userName", this.userName);
            data.put("phoneList", phoneList);
            data.put("content", content);
            data.put("timestamp", timestamp);
            data.put("sign", sign);
            // 将字典转换为 JSON 字符串
            String json = new JSONObject(data).toString();
            logger.info("[掌骏短信发送] " + json);
            // 发送 POST 请求
            ZjunSMSResult zjunSMSResult = post(this.url, json);
            SMSResult smsResult = new SMSResult();
            if (zjunSMSResult.getCode() == 0) {
                smsResult.setSucc(true);
                smsResult.setMsg("OK");
            } else {
                smsResult.setSucc(false);
                smsResult.setMsg(zjunSMSResult.getMessage());
            }
            return smsResult;
        } catch (Exception e) {
            logger.error("[掌骏短信发送] 异常", e);
            throw new ThirdPartServiceException("掌骏短信发送未知异常", CoreExceptionDefinition.THIRD_PART_SERVICE_EXCEPTION.getCode());
        }
    }
 
    private static String md5(String str) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(str.getBytes(StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    }
 
    public static ZjunSMSResult post(String url, String data) throws IOException, ThirdPartServiceException {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        try (OutputStream os = connection.getOutputStream()) {
            os.write(data.getBytes(StandardCharsets.UTF_8));
            os.flush();
        }
        StringBuilder response = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            while ((line = br.readLine()) != null) {
                response.append(line);
            }
        }
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(response.toString(), ZjunSMSResult.class);
        } catch (Exception e) {
            logger.error("[掌骏短信发送JSON转换] 异常", e);
            throw new ThirdPartServiceException("掌骏短信发送JSON转换异常", CoreExceptionDefinition.THIRD_PART_SERVICE_EXCEPTION.getCode());
        }
    }
}