新疆油田短信功能

This commit is contained in:
2025-07-12 11:25:25 +08:00
parent df2b136b3e
commit 3169464045
2 changed files with 43 additions and 51 deletions
@@ -1,5 +1,6 @@
package org.jeecg.common.util;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHeaders;
@@ -9,12 +10,14 @@ import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.config.HttpclientConfig;
import org.springframework.util.Base64Utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@@ -26,8 +29,6 @@ import java.util.Map;
@Slf4j
public class CqSmsUtil {
private static final String SUCCESS = "00";
private static final String CODE = "code";
private static final String SEND = "send";
private static final String RETURN = "return";
private static String smsUrl;
@@ -35,24 +36,9 @@ public class CqSmsUtil {
private static String password;
private static String host;
private static int perSize;
private static final Map<String, String> CODE_TRANS_MAP = new HashMap<>();
private static String appId;
private static final String CHARSET = StandardCharsets.UTF_8.displayName();
static{
CODE_TRANS_MAP.put("00", "单条或者批量短信提交成功");
CODE_TRANS_MAP.put("01", "用户名错误或密码错误");
CODE_TRANS_MAP.put("03", "签名不正确");
CODE_TRANS_MAP.put("04", "信息内容中含有限制词(违禁词)");
CODE_TRANS_MAP.put("05", "信息内容为黑内容,或者乱码");
CODE_TRANS_MAP.put("06", "短信内容超过70个字符");
CODE_TRANS_MAP.put("07", "系统异常");
CODE_TRANS_MAP.put("08", "短信定时发送时间格式错误");
CODE_TRANS_MAP.put("09", "短信实效时间格式错误");
CODE_TRANS_MAP.put("10", "回复编号(replyId)超长");
CODE_TRANS_MAP.put("11", "replyId代表的实际短信发送人在短信网关平台没有注册");
CODE_TRANS_MAP.put("21", "未定义错误");
}
public static String getSmsUrl() {
return smsUrl;
}
@@ -93,46 +79,49 @@ public class CqSmsUtil {
CqSmsUtil.perSize = perSize;
}
private static String getSign(String phone) {
String pre = getHost() + getUsername() + phone;
return Md5Util.md5Encode(pre, CHARSET);
public static String getAppId() {
return appId;
}
public static Result<Map<String,String>> cqSmsSend(String phone, String content) {
Map<String,String> map = new HashMap<>();
public static void setAppId(String appId) {
CqSmsUtil.appId = appId;
}
public static Result<Map<String, String>> cqSmsSend(String phone, String content) {
CloseableHttpClient httpClient = HttpclientConfig.HTTP_CLIENT;
HttpPost post = new HttpPost(getSmsUrl());
post.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
post.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType());
post.addHeader(HttpHeaders.ACCEPT_CHARSET, CHARSET);
post.addHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType());
post.setConfig(RequestConfig.custom().setConnectTimeout(3000).build());
List<BasicNameValuePair> args = new ArrayList<>();
args.add(new BasicNameValuePair("password", Md5Util.md5Encode(getPassword(), CHARSET)));
args.add(new BasicNameValuePair("usrname", getUsername()));
args.add(new BasicNameValuePair("sendTime", ""));
args.add(new BasicNameValuePair("invalidTime", ""));
args.add(new BasicNameValuePair("mobiles", phone));
args.add(new BasicNameValuePair("content", content));
args.add(new BasicNameValuePair("sign", getSign(phone)));
String req = URLEncodedUtils.format(args, StandardCharsets.UTF_8);
log.info("短信请求接口发送信息:{}", req);
map.put(SEND, req);
post.setEntity(new UrlEncodedFormEntity(args, StandardCharsets.UTF_8));
try {
CloseableHttpResponse response = httpClient.execute(post);
String res = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
map.put(RETURN, res);
JSONObject json = JSONObject.parseObject(res);
if (SUCCESS.equals(json.getString(CODE))) {
return Result.OK(CODE_TRANS_MAP.get(json.getString(CODE)), map);
}else{
return Result.error(CODE_TRANS_MAP.get(json.getString(CODE)), map);
}
post.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils.encodeToString((getUsername() + ":" + getPassword()).getBytes()));
// 创建 JSON 请求体
JSONObject requestBody = new JSONObject();
requestBody.put("sismsID", IdUtil.getSnowflakeNextIdStr());
requestBody.put("destaddr", phone);
requestBody.put("messageContent", content);
requestBody.put("applictionId", "JKFW");//写死健康服务
// 设置请求体
post.setEntity(new StringEntity(requestBody.toJSONString(), StandardCharsets.UTF_8));
// 执行请求
try (CloseableHttpResponse response = httpClient.execute(post)) {
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
log.info("短信请求返回结果: {}", responseBody);
// 解析响应
//不确定有什么错误信息,这里返回的都给存了
Map<String, String> map = new HashMap<>();
map.put(SEND, requestBody.toJSONString());
map.put(RETURN, responseBody);
return Result.OK();
} catch (IOException e) {
log.error("短信平台连接失败", e);
Map<String, String> map = new HashMap<>();
map.put(SEND, requestBody.toJSONString());
map.put(RETURN, e.getMessage());
return Result.error(e.getMessage(),map);
return Result.error(e.getMessage(), map);
}
}
}
}
@@ -19,6 +19,8 @@ public class CqSmsConfig {
private String host;
@Value(value = "${cqsms.persize:10}")
private int perSize;
@Value(value = "${cqsms.appId:JKFW}")
private String appId;
@Bean
public void initSmsConfig() {
@@ -27,5 +29,6 @@ public class CqSmsConfig {
CqSmsUtil.setPassword(password);
CqSmsUtil.setHost(host);
CqSmsUtil.setPerSize(perSize);
CqSmsUtil.setAppId(appId);
}
}