SmsChineseHttpClient.java
2.19 KB
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
package com.diligrp.cashier.assistant.client;
import com.diligrp.cashier.assistant.exception.AssistantServiceException;
import com.diligrp.cashier.shared.ErrorCode;
import com.diligrp.cashier.shared.service.ServiceEndpointSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SmsChineseHttpClient extends ServiceEndpointSupport {
private static final Logger LOGGER = LoggerFactory.getLogger(SmsChineseHttpClient.class);
/**
* 接口地址
*/
private final String uri;
/**
* 用户名
*/
private final String uid;
/**
* 短信密钥
*/
private final String secretKey;
public SmsChineseHttpClient(String uri, String uid, String secretKey) {
this.uri = uri;
this.uid = uid;
this.secretKey = secretKey;
}
public String sendSmsMessage(String telephone, String message, String signature) {
HttpParam[] params = new HttpParam[4];
params[0] = HttpParam.create("Uid", uid);
params[1] = HttpParam.create("Key", secretKey);
params[2] = HttpParam.create("smsMob", telephone);
params[3] = HttpParam.create("smsText", String.format("【%s】%s", signature, message));
HttpResult result = send(uri, params);
if (result.statusCode != 200) {
throw new AssistantServiceException(ErrorCode.SERVICE_ACCESS_ERROR, "调用网建短信服务失败");
}
int code = Integer.parseInt(result.responseText);
if (code <= 0) {
LOGGER.error("发送短信失败, 错误码: {}", code);
throw new AssistantServiceException(ErrorCode.SERVICE_ACCESS_ERROR, "发送短信失败, " + errorMessage(code));
}
return null;
}
private String errorMessage(int code) {
return switch (code) {
case -1, -11 -> "账号不存在或被禁用";
case -2, -21 -> "接口密钥错误";
case -3 -> "短信数量不足";
case -4, -41 -> "手机号码为空或格式不正确";
case -14, -42 -> "短信内容为空或出现非法字符";
case -51, -52 -> "短信签名太长或格式不正确";
default -> "未知错误";
};
}
}