Files
nuttyreading_global/src/main/java/com/peanut/common/utils/SmsUtil.java
2025-08-18 14:58:11 +08:00

99 lines
3.9 KiB
Java

package com.peanut.common.utils;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.net.URLEncoder;
@Component
public class SmsUtil {
@Value("${sms.domestic.url}")
private String url;
@Value("${sms.domestic.account}")
private String account;
@Value("${sms.domestic.pswd}")
private String pswd;
@Value("${sms.domestic.content}")
private String content;
@Value("${sms.abroad.url}")
private String abroadUrl;
@Value("${sms.abroad.account}")
private String abroadAccount;
@Value("${sms.abroad.pswd}")
private String abroadPswd;
@Value("${sms.abroad.content}")
private String abroadContent;
public R sendSmsCode(String phone, String code) {
try {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity("" +
"account="+account+
"&pswd="+pswd+
"&mobile="+phone+
"&msg="+ URLEncoder.encode(content.replace("XXXXXX",code), "UTF-8")+
"&needstatus=false");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(entity);
String returnString = EntityUtils.toString(client.execute(httpPost).getEntity());
//20250416115415,0
String[] res = returnString.split(",");
if ("0".equals(res[1])){
return R.ok();
}else if ("104".equals(res[1])){
return R.error("短信平台系统忙,请稍后重试。");
}else if ("107".equals(res[1])){
return R.error("错误的手机号码。");
}else {
return R.error("系统错误,请联系管理员");
}
} catch (Exception e) {
e.printStackTrace();
return R.error("系统错误,请联系管理员");
}
}
public R sendSmsAbroad(String phone, String code){
try {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(abroadUrl);
StringEntity entity = new StringEntity("" +
"sp_id="+abroadAccount+
"&password="+MD5Utils.getStrrMD5(abroadPswd)+
"&mobile="+phone+
"&content="+abroadContent.replace("XXXXXX",code));
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
String responseBody = EntityUtils.toString(client.execute(httpPost).getEntity());
System.out.println(responseBody);
JSONObject object = JSONObject.parseObject(responseBody);
if ("0".equals(object.get("code").toString())){
return R.ok();
}else if ("10000".equals(object.get("code").toString())){
return R.error("短信平台系统忙,请稍后重试。");
}else {
if ("WL:CDQC".equals(object.get("data").toString())){
return R.error("地区不支持。");
}else if ("WL:CWHM".equals(object.get("data").toString())){
return R.error("错误的手机号码。");
}else {
return R.error("系统错误,请联系管理员");
}
}
} catch (Exception e) {
e.printStackTrace();
return R.error("系统错误,请联系管理员");
}
}
}