ZQN
2024-06-22 d509e25f601c110427306543a0d17cd97cc6b194
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
package com.project.system.sms;
 
import com.project.common.constant.Constants;
import com.project.common.utils.SslUtils;
import com.project.system.service.ISysConfigService;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpEntity;
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.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import static com.project.common.utils.WxPayUtils.createIgnoreSSLHttpClient;
 
/**
 * 短信http接口的java代码调用示例
 * 基于Apache HttpClient 4.3
 * @author Mr.Zhao
 */
 
@Service
@RequiredArgsConstructor
public class YPSmsApi {
 
 
    private final ISysConfigService configService;
 
 
    /**
     * 智能匹配模板发送地址
     */
    private static final String YP_SMS_URI = "https://sms.yunpian.com/v2/sms/single_send.json";
 
    /**
     * 模板发送地址
     */
    private static final String YP_SMS_TMP_URI = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
 
    /**
     * KEY
     */
    private static final String API_KEY = "faf531146ca1e38abacd3862fb3fc32b";
 
    /**
     * 签名
     */
    private static final String SIGN = "【金明源】";
 
    /**
     * 执法单申请模板
     */
    public static final String APPLY_TMP = SIGN + "{} 提交了新的申请单,请您及时审批!";
 
    /**
     * 执法单审核后模板
     */
    public static final String CHECK_TMP = SIGN +  "您提交的申请单已被审批人 {}, 请您及时查看!";
 
    /**
     * 企业通知模板
     */
    public static final String COMPANY_TMP = SIGN +  "{} 单位预计将于 {} 到贵单位进行检查,请提前知晓!";
 
    /**
     * 验证码模板
     */
    public static final String CODE_TMP = SIGN + "您的验证码是{}";
 
 
 
 
 
    /**
     * 简单发送短信
     * @param mobile  接受的手机号
     * @param msg    短信内容
     */
    public String sendSms(String mobile, String msg)
    {
        String smsFlag = configService.selectConfigByKey("sys.sms.flag");
        if ("1".equals(smsFlag)) {
            Map<String, String> params = new HashMap<>(3);
            params.put("apikey", API_KEY);
            params.put("text", msg);
            params.put("mobile", mobile);
            return post(YP_SMS_URI, params);
        }
        return "";
    }
 
    /**
     * 基于HttpClient 4.3的通用POST方法
     *
     * @param url        提交的URL
     * @param paramsMap  提交<参数,值>Map
     * @return  提交响应
     */
    public static String post(String url, Map<String, String> paramsMap)
    {
        CloseableHttpClient client = createIgnoreSSLHttpClient();
 
        String responseText = "";
        CloseableHttpResponse response = null;
        try {
 
            HttpPost method = new HttpPost(url);
            if(method.getURI().getPath().contains("https")){
                SslUtils.ignoreSsl();
            }
            if (paramsMap != null) {
                List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
                    paramList.add(pair);
                }
                method.setEntity(new UrlEncodedFormEntity(paramList, Constants.UTF8));
            }
            if (client != null) {
                response = client.execute(method);
            }
            HttpEntity entity = null;
            if (response != null) {
                entity = response.getEntity();
            }
            if (entity != null) {
                responseText = EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(responseText);//此处打印在console后,会给出一个IP地址
        return responseText;
    }
}