puzhibing
4 天以前 a0e03b646f2c444b0f09687d24e9995112b99017
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package com.ruoyi.common.core.utils;
 
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.constant.MsgConstants;
import com.ruoyi.common.core.utils.req.SubmitTempletReq;
import org.apache.commons.codec.binary.Base64;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.concurrent.ThreadLocalRandom;
 
public class MsgUtil {
 
    public final static String ecName= "四川明星新能源科技有限公司";
    public final static String secretKey= "MX_xny2023?9";
    public final static String sign= "1PUAVuY2b";
 
 
 
    /**
     * 更换手机号:【签名】验证码:XXX,用于更换手机号。请勿转发。
     * @param phone 手机号
     * @param code 验证码
     * @return
     */
    public static String codeMsg(String phone,String code){
        SubmitTempletReq submitReq = new SubmitTempletReq();
        String[] paramss = {code};
        submitReq.setApId(MsgConstants.CODE_AP_ID);
        submitReq.setEcName(ecName);
        submitReq.setSecretKey(secretKey);
        submitReq.setParams(JSON.toJSONString(paramss));
        submitReq.setMobiles(phone);
        submitReq.setAddSerial("");
        submitReq.setSign(sign);
        submitReq.setTemplateId(MsgConstants.CODE_TEMPLATE_ID);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(submitReq.getEcName());
        stringBuffer.append(submitReq.getApId());
        stringBuffer.append(submitReq.getSecretKey());
        stringBuffer.append(submitReq.getTemplateId());
        stringBuffer.append(submitReq.getMobiles());
        stringBuffer.append(submitReq.getParams());
        stringBuffer.append(submitReq.getSign());
        stringBuffer.append(submitReq.getAddSerial());
        submitReq.setMac(MD5Util.getMD5(stringBuffer.toString()));
        String reqText = JSON.toJSONString(submitReq);
        //加密
        String encode = Base64.encodeBase64String(reqText.getBytes());
        System.err.println(encode);
        return encode;
    }
 
    /**
     * 更换手机号:【签名】验证码:XXX,用于更换手机号。请勿转发。
     * @param phone 手机号
     * @param code 验证码
     * @return
     */
    public static String applyCodeMsg(String phone,String code){
        SubmitTempletReq submitReq = new SubmitTempletReq();
        String[] paramss = {code};
        submitReq.setApId(MsgConstants.APPLY_AP_ID);
        submitReq.setEcName(ecName);
        submitReq.setSecretKey(secretKey);
        submitReq.setParams(JSON.toJSONString(paramss));
        submitReq.setMobiles(phone);
        submitReq.setAddSerial("");
        submitReq.setSign(sign);
        submitReq.setTemplateId(MsgConstants.APPLY_TEMPLATE_ID);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(submitReq.getEcName());
        stringBuffer.append(submitReq.getApId());
        stringBuffer.append(submitReq.getSecretKey());
        stringBuffer.append(submitReq.getTemplateId());
        stringBuffer.append(submitReq.getMobiles());
        stringBuffer.append(submitReq.getParams());
        stringBuffer.append(submitReq.getSign());
        stringBuffer.append(submitReq.getAddSerial());
        submitReq.setMac(MD5Util.getMD5(stringBuffer.toString()));
        String reqText = JSON.toJSONString(submitReq);
        //加密
        String encode = Base64.encodeBase64String(reqText.getBytes());
        System.err.println(encode);
        return encode;
    }
 
 
    /**
     * 停车占位提醒:车辆已在3小时前完成充电,请及时取车,避免长时间占用充电资源。
     * @param phone 手机号
     * @return
     */
    public static String stopMsg(String phone){
        SubmitTempletReq submitReq = new SubmitTempletReq();
        submitReq.setTemplateId(MsgConstants.STOP_TEMPLATE_ID);
        submitReq.setApId(MsgConstants.STOP_AP_ID);
        submitReq.setEcName(ecName);
        submitReq.setSecretKey(secretKey);
        submitReq.setMobiles(phone);
        submitReq.setAddSerial("");
        submitReq.setSign(sign);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(submitReq.getEcName());
        stringBuffer.append(submitReq.getApId());
        stringBuffer.append(submitReq.getSecretKey());
        stringBuffer.append(submitReq.getTemplateId());
        stringBuffer.append(submitReq.getMobiles());
        stringBuffer.append(submitReq.getParams());
        stringBuffer.append(submitReq.getSign());
        stringBuffer.append(submitReq.getAddSerial());
        submitReq.setMac(MD5Util.getMD5(stringBuffer.toString()));
        String reqText = JSON.toJSONString(submitReq);
        //加密
        String encode = Base64.encodeBase64String(reqText.getBytes());
        System.err.println(encode);
        return encode;
    }
 
    /**
     * 充电结束:车辆充电结束,请及时取车。
     * @param phone 手机号
     * @return
     */
    public static String chargeStopMsg(String phone){
        SubmitTempletReq submitReq = new SubmitTempletReq();
        submitReq.setTemplateId(MsgConstants.CHARGE_TEMPLATE_ID);
        submitReq.setApId(MsgConstants.CHARGE_AP_ID);
        submitReq.setEcName(ecName);
        submitReq.setSecretKey(secretKey);
        submitReq.setMobiles(phone);
        submitReq.setAddSerial("");
        submitReq.setSign(sign);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(submitReq.getEcName());
        stringBuffer.append(submitReq.getApId());
        stringBuffer.append(submitReq.getSecretKey());
        stringBuffer.append(submitReq.getTemplateId());
        stringBuffer.append(submitReq.getMobiles());
        stringBuffer.append(submitReq.getParams());
        stringBuffer.append(submitReq.getSign());
        stringBuffer.append(submitReq.getAddSerial());
        submitReq.setMac(MD5Util.getMD5(stringBuffer.toString()));
        String reqText = JSON.toJSONString(submitReq);
        //加密
        String encode = Base64.encodeBase64String(reqText.getBytes());
        System.err.println(encode);
        return encode;
    }
 
    /**
     * VIP充电完成:VIP 用户您好!您在{电站名称}的充电已完成,本次电量{充电度数}度,费用共计{充电费用}元。
     */
    public static String vipChargeFinishMsg(String phone, String site, String chargingCapacity, String chargingFee){
        SubmitTempletReq submitReq = new SubmitTempletReq();
        String[] paramss = {limitSiteName(site), limitNumber(chargingCapacity), limitNumber(chargingFee)};
        submitReq.setParams(JSON.toJSONString(paramss));
        submitReq.setTemplateId(MsgConstants.VIP_CHARGE_FINISH_TEMPLATE_ID);
        submitReq.setApId(MsgConstants.VIP_CHARGE_FINISH_AP_ID);
        submitReq.setEcName(ecName);
        submitReq.setSecretKey(secretKey);
        submitReq.setMobiles(phone);
        submitReq.setAddSerial("");
        submitReq.setSign(sign);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(submitReq.getEcName());
        stringBuffer.append(submitReq.getApId());
        stringBuffer.append(submitReq.getSecretKey());
        stringBuffer.append(submitReq.getTemplateId());
        stringBuffer.append(submitReq.getMobiles());
        stringBuffer.append(submitReq.getParams());
        stringBuffer.append(submitReq.getSign());
        stringBuffer.append(submitReq.getAddSerial());
        submitReq.setMac(MD5Util.getMD5(stringBuffer.toString()));
        String reqText = JSON.toJSONString(submitReq);
        return Base64.encodeBase64String(reqText.getBytes());
    }
 
    /**  phone  单词可拼接5000个号码
     * 桩故障(自动/手动):检测到【电站1】【编号】号桩设备离线,请及时查看处理!
     * @param phone 手机号
     * @param site 站点名称
     * @param chargeGun 桩编号
     * @return
     */
 
    public static String faultMsg(String phone,String site,String chargeGun){
        // 去除特殊字符 比如.-/{(等等
        site = site.replaceAll("[\\p{P}+~$`^=|<>~`$^+=|<>¥×]", "");
 
        SubmitTempletReq submitReq = new SubmitTempletReq();
        if(site.length()<=10){
            String[] paramss = {site,chargeGun};
            submitReq.setParams(JSON.toJSONString(paramss));
        }else{
            String work1 = site.substring(0,10);
            String[] paramss = {work1,chargeGun};
            submitReq.setParams(JSON.toJSONString(paramss));
        }
        submitReq.setTemplateId(MsgConstants.FAULT_TEMPLATE_ID);
        submitReq.setApId(MsgConstants.FAULT_AP_ID);
        submitReq.setEcName(ecName);
        submitReq.setSecretKey(secretKey);
        submitReq.setMobiles(phone);
        submitReq.setAddSerial("");
        submitReq.setSign(sign);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(submitReq.getEcName());
        stringBuffer.append(submitReq.getApId());
        stringBuffer.append(submitReq.getSecretKey());
        stringBuffer.append(submitReq.getTemplateId());
        stringBuffer.append(submitReq.getMobiles());
        stringBuffer.append(submitReq.getParams());
        stringBuffer.append(submitReq.getSign());
        stringBuffer.append(submitReq.getAddSerial());
        submitReq.setMac(MD5Util.getMD5(stringBuffer.toString()));
        String reqText = JSON.toJSONString(submitReq);
        //加密
        String encode = Base64.encodeBase64String(reqText.getBytes());
        return encode;
    }
    /**
     * 公司总账户余额不足提醒:尊敬的客户您好!您公司{[公司名称]}总账户余额不足,当前余额{[当前余额]}元,已低于预警线{[预警线]}元。为避免影响车辆正常充电,请及时充值。感谢配合!
     * @param phone 手机号
     * @param companyName 公司名称(纯中文,总长度10)
     * @param balance 当前余额(纯数字,总长度10)
     * @param warnAmount 预警线(纯数字,总长度10)
     * @return
     */
    public static String companyBalanceWarnMsg(String phone, String companyName, String balance, String warnAmount){
        return buildBalanceWarnMsg(phone, MsgConstants.COMPANY_BALANCE_WARN_TEMPLATE_ID,
                new String[]{limitChinese(companyName), limitDecimal(balance), limitDecimal(warnAmount)});
    }
 
    /**
     * 公司子账号余额不足提醒:尊敬的客户您好!您公司{[公司名称]}用户{[用户信息]}子账户余额不足,当前子账户余额{[当前余额]}元,已低于预警线{[预警线]}元。为避免影响车辆正常充电,请及时分配额度。感谢配合!
     * @param phone 手机号
     * @param companyName 公司名称(纯中文,总长度10)
     * @param userInfo 用户信息(中文+字母+数字,总长度10)
     * @param balance 当前余额(纯数字,总长度10)
     * @param warnAmount 预警线(纯数字,总长度10)
     * @return
     */
    public static String subBalanceWarnMsg(String phone, String companyName, String userInfo, String balance, String warnAmount){
        return buildBalanceWarnMsg(phone, MsgConstants.SUB_BALANCE_WARN_TEMPLATE_ID,
                new String[]{limitChinese(companyName), limitUserInfo(userInfo), limitDecimal(balance), limitDecimal(warnAmount)});
    }
 
    /**
     * 余额预警模板短信组包(公共逻辑)
     */
    private static String buildBalanceWarnMsg(String phone, String templateId, String[] params){
        SubmitTempletReq submitReq = new SubmitTempletReq();
        submitReq.setApId(MsgConstants.BALANCE_WARN_AP_ID);
        submitReq.setEcName(ecName);
        submitReq.setSecretKey(secretKey);
        submitReq.setParams(JSON.toJSONString(params));
        submitReq.setMobiles(phone);
        submitReq.setAddSerial("");
        submitReq.setSign(sign);
        submitReq.setTemplateId(templateId);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(submitReq.getEcName());
        stringBuffer.append(submitReq.getApId());
        stringBuffer.append(submitReq.getSecretKey());
        stringBuffer.append(submitReq.getTemplateId());
        stringBuffer.append(submitReq.getMobiles());
        stringBuffer.append(submitReq.getParams());
        stringBuffer.append(submitReq.getSign());
        stringBuffer.append(submitReq.getAddSerial());
        submitReq.setMac(MD5Util.getMD5(stringBuffer.toString()));
        String reqText = JSON.toJSONString(submitReq);
        return Base64.encodeBase64String(reqText.getBytes());
    }
 
    /**
     * 公司名称参数约束:纯中文,总长度10;剔除非法字符,空值兜底
     */
    private static String limitChinese(String value) {
        String v = value == null ? "" : value.replaceAll("[^\\u4e00-\\u9fa5]", "");
        if (v.length() == 0) {
            v = "公司";
        }
        return v.length() <= 10 ? v : v.substring(0, 10);
    }
 
    /**
     * 用户信息参数约束:中文+字母+数字,总长度10;剔除非法字符,空值兜底
     */
    private static String limitUserInfo(String value) {
        String v = value == null ? "" : value.replaceAll("[^\\u4e00-\\u9fa5A-Za-z0-9]", "");
        if (v.length() == 0) {
            v = "用户";
        }
        return v.length() <= 10 ? v : v.substring(0, 10);
    }
 
    /**
     * 金额参数约束:纯数字(允许小数点),总长度10;剔除非法字符,空值兜底
     */
    private static String limitDecimal(String value) {
        String v = value == null ? "" : value.replaceAll("[^\\d.]", "");
        if (v.length() == 0) {
            v = "0";
        }
        return v.length() <= 10 ? v : v.substring(0, 10);
    }
 
    /**
     * 生成验证码
     * @return
     */
    public static String createCode(){
        return String.valueOf(ThreadLocalRandom.current().nextInt(100000, 999999));
    }
 
    private static String limitSiteName(String site) {
        String value = site == null ? "充电站" : site.replaceAll("[^\\u4e00-\\u9fa5\\d,。!?、:;()《》【】]", "");
        if (value.length() == 0) {
            value = "充电站";
        }
        return value.length() <= 10 ? value : value.substring(0, 10);
    }
 
    private static String limitNumber(String value) {
        if (value == null) {
            return "0";
        }
        String number = value.replaceAll("[^\\d.]", "");
        if (number.length() == 0) {
            return "0";
        }
        try {
            number = new BigDecimal(number).setScale(0, RoundingMode.HALF_UP).toPlainString();
        } catch (NumberFormatException e) {
            number = number.replaceAll("\\D", "");
        }
        if (number.length() == 0) {
            return "0";
        }
        return number.length() <= 5 ? number : number.substring(0, 5);
    }
 
}