Helius
2021-06-29 5252d1396e21a16774be699a5ba1c8d39c14a22e
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
package com.xzx.gc.common.utils.sms;
 
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * @Description: 相关工具类
 * @author: wei
 * @date: 2019-04-16 22:08
 */
@Slf4j
public class MixSmsUtil {
 
 
    private static  final String USER_NAME="xzx888hy";
    //营销类账户
    private static  final String YX_USER_NAME="xzx888yx";
    private static  final String PWD="TRBiquIn65pO0tkY";
    public static String SEND_SMS_TP_URL = "http://api.mix2.zthysms.com/v2/sendSmsTp";
    public static String SEND_SMS_URL = "http://api.mix2.zthysms.com/v2/sendSms";
    public static String sign="【小棕熊到家】";
 
 
    /**
     * 单点群发
     * @return
     */
    public static String sendSmsPoint(String mobile,String content){
        String tKye = String.valueOf(DateUtil.currentSeconds());
        SendSmsRequestInfo sendSmsTpRequestInfo = new SendSmsRequestInfo();
        sendSmsTpRequestInfo.setUsername( USER_NAME );
        String pwd = SecureUtil.md5( SecureUtil.md5( PWD ) + tKye );
        sendSmsTpRequestInfo.setPassword( pwd );
        sendSmsTpRequestInfo.setMobile(mobile);
        sendSmsTpRequestInfo.setContent(content);
        sendSmsTpRequestInfo.setTKey( tKye );
        String json = new Gson().toJson( sendSmsTpRequestInfo);
 
        String result;
        try {
            result = HttpRequest.post( SEND_SMS_URL )
                    .timeout( 30000 )
                    .body( json, ContentType.JSON.toString() )
                    .execute()
                    .body();
        } catch (HttpException e) {
            result= getErrResult(e.getMessage());
        }
 
        return result;
    }
 
    /**
     * 模板发送
     * @param mobile
     * @param map
     * @param templateCode
     * @param type  1:通知类 2营销类
     * @return
     */
    public static String sendSms(String mobile,Map<String,String> map, String templateCode,int type) {
        String tKye = String.valueOf(DateUtil.currentSeconds());
        SendSmsTpRequestInfo sendSmsTpRequestInfo = new SendSmsTpRequestInfo();
        if(type==1) {
            sendSmsTpRequestInfo.setUsername(USER_NAME);
        }else{
            sendSmsTpRequestInfo.setUsername(YX_USER_NAME);
        }
        String pwd = SecureUtil.md5( SecureUtil.md5( PWD ) + tKye );
        sendSmsTpRequestInfo.setPassword( pwd );
        sendSmsTpRequestInfo.setTKey( tKye );
        sendSmsTpRequestInfo.setTpId( templateCode );
        sendSmsTpRequestInfo.setSignature( sign );
 
        List<ContentTp> records = new ArrayList<>();
 
        for (int i = 0; i < 1; i++) {
            ContentTp contentTp = new ContentTp();
            contentTp.setMobile(mobile);
            contentTp.setTpContent( map );
            records.add( contentTp );
        }
 
        sendSmsTpRequestInfo.setRecords( records );
        String json = new Gson().toJson( sendSmsTpRequestInfo);
 
        String result;
        try {
            result = HttpRequest.post( SEND_SMS_TP_URL )
                    .timeout( 30000 )
                    .body( json, ContentType.JSON.toString() )
                    .execute()
                    .body();
        } catch (HttpException e) {
            result= getErrResult(e.getMessage());
        }
 
        return result;
    }
 
 
    /**
     *
     * @return
     */
    private static  String getErrResult(String msg){
        JSONObject  jsonObject=new JSONObject();
        jsonObject.put("code",-1);
        jsonObject.put("msg",msg);
        return  JSONUtil.toJsonStr(jsonObject);
    }
}