| New file |
| | |
| | | package com.yami.trading.common.manager.sms; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | 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.HttpGet; |
| | | import org.apache.http.client.utils.URIBuilder; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.message.BasicNameValuePair; |
| | | import org.apache.http.util.EntityUtils; |
| | | |
| | | import java.net.URI; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 短信发送工具类 |
| | | */ |
| | | public class SmsSendUtil { |
| | | |
| | | // API请求地址 |
| | | private static final String SMS_API_URL = "http://190.92.241.43:9090/sms/batch/v2"; |
| | | |
| | | private static final String SMS_API_APPKEY = "McLWyT"; |
| | | |
| | | private static final String SMS_API_APPSECRET = "WHuzLP"; |
| | | |
| | | private static final String SMS_API_APPCODE = "1000"; |
| | | |
| | | |
| | | /** |
| | | * 发送短信 |
| | | * |
| | | * @param phone 手机号码,多个用逗号分隔(必填,最多1000个) |
| | | * @param msg 短信内容(必填,不超过1000字) |
| | | * @param uid 唯一标识(选填) |
| | | * @param extend 扩展号(选填,数字,总长度不超过20位) |
| | | * @return 短信发送响应结果 |
| | | * @throws Exception 异常信息 |
| | | */ |
| | | public static SmsResponse sendSms(String phone, String msg, String uid, String extend) throws Exception { |
| | | // 参数验证 |
| | | //validateParams(SMS_API_APPKEY, SMS_API_APPCODE, SMS_API_APPSECRET, phone, msg, extend); |
| | | |
| | | // 创建HTTP客户端 |
| | | try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
| | | // 构建请求参数 |
| | | URIBuilder uriBuilder = new URIBuilder(SMS_API_URL); |
| | | uriBuilder.setParameter("appkey", SMS_API_APPKEY); |
| | | uriBuilder.setParameter("appcode", SMS_API_APPCODE); |
| | | uriBuilder.setParameter("appsecret", SMS_API_APPSECRET); |
| | | uriBuilder.setParameter("phone", phone); |
| | | uriBuilder.setParameter("msg", msg); |
| | | |
| | | // 可选参数 |
| | | if (uid != null && !uid.isEmpty()) { |
| | | uriBuilder.setParameter("uid", uid); |
| | | } |
| | | if (extend != null && !extend.isEmpty()) { |
| | | uriBuilder.setParameter("extend", extend); |
| | | } |
| | | |
| | | URI uri = uriBuilder.build(); |
| | | HttpGet httpGet = new HttpGet(uri); |
| | | |
| | | // 发送请求并获取响应 |
| | | try (CloseableHttpResponse response = httpClient.execute(httpGet)) { |
| | | String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); |
| | | SmsResponse result = parseResponse(responseBody); |
| | | System.out.println("发送结果:" + result); |
| | | // 判断是否发送成功 |
| | | if ("00000".equals(result.getCode())) { |
| | | System.out.println("提交成功,uid:" + result.getUid()); |
| | | } else { |
| | | System.out.println("提交失败:" + result.getDesc()); |
| | | } |
| | | return result; |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 参数验证 |
| | | */ |
| | | private static void validateParams(String appKey, String appCode, String appSecret, |
| | | String phone, String msg, String extend) { |
| | | // 验证必填参数 |
| | | if (appKey == null || appKey.trim().isEmpty()) { |
| | | throw new IllegalArgumentException("appkey不能为空"); |
| | | } |
| | | if (appCode == null || appCode.trim().isEmpty()) { |
| | | throw new IllegalArgumentException("appcode不能为空"); |
| | | } |
| | | if (appSecret == null || appSecret.trim().isEmpty()) { |
| | | throw new IllegalArgumentException("appsecret不能为空"); |
| | | } |
| | | if (phone == null || phone.trim().isEmpty()) { |
| | | throw new IllegalArgumentException("手机号码不能为空"); |
| | | } |
| | | |
| | | // 验证手机号码数量 |
| | | String[] phones = phone.split(","); |
| | | if (phones.length > 1000) { |
| | | throw new IllegalArgumentException("一次最多不能超过1000个手机号码"); |
| | | } |
| | | |
| | | // 验证短信内容 |
| | | if (msg == null || msg.trim().isEmpty()) { |
| | | throw new IllegalArgumentException("短信内容不能为空"); |
| | | } |
| | | if (msg.length() > 1000) { |
| | | throw new IllegalArgumentException("短信内容长度不能超过1000个字"); |
| | | } |
| | | |
| | | // 验证扩展号 |
| | | if (extend != null && !extend.isEmpty()) { |
| | | if (!extend.matches("\\d+")) { |
| | | throw new IllegalArgumentException("扩展号必须为数字"); |
| | | } |
| | | // 假设通道主叫号固定为10位,实际使用时请根据供应商提供的信息修改 |
| | | if (extend.length() > 10) { |
| | | throw new IllegalArgumentException("扩展号长度超过限制(总长度不能超过20位)"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 解析响应结果 |
| | | */ |
| | | private static SmsResponse parseResponse(String responseBody) { |
| | | JSONObject jsonObject = JSON.parseObject(responseBody); |
| | | SmsResponse smsResponse = new SmsResponse(); |
| | | smsResponse.setCode(jsonObject.getString("code")); |
| | | smsResponse.setDesc(jsonObject.getString("desc")); |
| | | smsResponse.setUid(jsonObject.getString("uid")); |
| | | |
| | | // 解析result数组 |
| | | JSONArray resultArray = jsonObject.getJSONArray("result"); |
| | | if (resultArray != null && !resultArray.isEmpty()) { |
| | | List<SmsResult> resultList = new ArrayList<>(); |
| | | for (int i = 0; i < resultArray.size(); i++) { |
| | | JSONObject item = resultArray.getJSONObject(i); |
| | | SmsResult smsResult = new SmsResult(); |
| | | smsResult.setStatus(item.getString("status")); |
| | | smsResult.setPhone(item.getString("phone")); |
| | | smsResult.setDesc(item.getString("desc")); |
| | | resultList.add(smsResult); |
| | | } |
| | | smsResponse.setResult(resultList); |
| | | } |
| | | |
| | | return smsResponse; |
| | | } |
| | | |
| | | /** |
| | | * 短信响应实体类 |
| | | */ |
| | | public static class SmsResponse { |
| | | private String code; |
| | | private String desc; |
| | | private String uid; |
| | | private List<SmsResult> result; |
| | | |
| | | // getter和setter |
| | | public String getCode() { return code; } |
| | | public void setCode(String code) { this.code = code; } |
| | | public String getDesc() { return desc; } |
| | | public void setDesc(String desc) { this.desc = desc; } |
| | | public String getUid() { return uid; } |
| | | public void setUid(String uid) { this.uid = uid; } |
| | | public List<SmsResult> getResult() { return result; } |
| | | public void setResult(List<SmsResult> result) { this.result = result; } |
| | | |
| | | @Override |
| | | public String toString() { |
| | | return "SmsResponse{" + |
| | | "code='" + code + '\'' + |
| | | ", desc='" + desc + '\'' + |
| | | ", uid='" + uid + '\'' + |
| | | ", result=" + result + |
| | | '}'; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 短信结果详情实体类 |
| | | */ |
| | | public static class SmsResult { |
| | | private String status; |
| | | private String phone; |
| | | private String desc; |
| | | |
| | | // getter和setter |
| | | public String getStatus() { return status; } |
| | | public void setStatus(String status) { this.status = status; } |
| | | public String getPhone() { return phone; } |
| | | public void setPhone(String phone) { this.phone = phone; } |
| | | public String getDesc() { return desc; } |
| | | public void setDesc(String desc) { this.desc = desc; } |
| | | |
| | | @Override |
| | | public String toString() { |
| | | return "SmsResult{" + |
| | | "status='" + status + '\'' + |
| | | ", phone='" + phone + '\'' + |
| | | ", desc='" + desc + '\'' + |
| | | '}'; |
| | | } |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | try { |
| | | SmsResponse response = SmsSendUtil.sendSms("8617769309156", "4524", null, null); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | } |