更换短信验证码

This commit is contained in:
wuchunlei
2025-04-16 16:39:07 +08:00
parent 2b57f006a5
commit c6b7699e84
16 changed files with 174 additions and 21 deletions

View File

@@ -0,0 +1,91 @@
package com.peanut.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.bcloud.msg.http.HttpSender;
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.context.annotation.Configuration;
@Configuration
public class SmsUtil {
@Value("${sms.domestic.account}")
private String account;
@Value("${sms.domestic.pswd}")
private String pswd;
@Value("${sms.domestic.content}")
private String content;
@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) {
String uri = "http://116.62.212.142/msg/HttpBatchSendSM";//应用地址
String mobiles = phone;//手机号码,多个号码使用","分割
boolean needstatus = true;//是否需要状态报告需要true不需要false
String product = "";//产品ID
String extno = "";//扩展码
String respType = "json";//返回json格式响应
boolean encrypt = true;// 密码使用时间戳加密
try {
String returnString = HttpSender.send(uri, account, pswd, mobiles, content.replace("XXXXXX",code), needstatus, product, extno, respType, encrypt);
//{"result":108,"ts":"20250416115415"}
//{"result":0,"msgid":"2150416115442493200","ts":"20250416115442"}
JSONObject object = JSONObject.parseObject(returnString);
if ("0".equals(object.get("result").toString())){
return R.ok();
}else if ("104".equals(object.get("result").toString())){
return R.error("短信平台系统忙,请稍后重试。");
}else if ("107".equals(object.get("result").toString())){
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("http://www.onesnok.net:9511/api/send-sms-single");
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("系统错误,请联系管理员");
}
}
}