package com.xzx.gc.common.utils.ali;
|
|
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.http.HttpException;
|
import cn.hutool.http.HttpRequest;
|
import cn.hutool.json.JSONUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import com.aliyuncs.CommonRequest;
|
import com.aliyuncs.CommonResponse;
|
import com.aliyuncs.DefaultAcsClient;
|
import com.aliyuncs.IAcsClient;
|
import com.aliyuncs.exceptions.ClientException;
|
import com.aliyuncs.http.MethodType;
|
import com.aliyuncs.profile.DefaultProfile;
|
import com.xzx.gc.common.Result;
|
import com.xzx.gc.common.constant.CommonEnum;
|
import com.xzx.gc.common.dto.AuthNameDto;
|
import com.xzx.gc.common.utils.sms.MixSmsUtil;
|
import lombok.experimental.UtilityClass;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.util.Map;
|
|
@Slf4j
|
@UtilityClass
|
public class SmsUtil {
|
|
|
private static final String AL_AUTH_NAME_URL="https://phone3.market.alicloudapi.com/phonethree";
|
private static final String AL_AUTH_NAME_CODE="decedbfd4e0c4a46b9165adc10d47bcf";
|
private static final String AL_KEY_ID="LTAI4GDj5oLisR1higZwHm1T";
|
private static final String AL_SECRET="DveOusgX8cIfeyWwKfLwCM87pfek3a";
|
|
/**
|
* 发送短信
|
* @param phoneNum
|
* @param map
|
* @throws ClientException
|
*/
|
public String send(String phoneNum,Map<String,String> map,String templateCode,String channel) throws ClientException {
|
if(CommonEnum.阿里云短信.getValue().equals(channel)) {
|
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AL_KEY_ID, AL_SECRET);
|
IAcsClient client = new DefaultAcsClient(profile);
|
|
CommonRequest request = new CommonRequest();
|
request.setMethod(MethodType.POST);
|
request.setDomain("dysmsapi.aliyuncs.com");
|
request.setVersion("2017-05-25");
|
request.setAction("SendSms");
|
request.putQueryParameter("RegionId", "cn-hangzhou");
|
request.putQueryParameter("PhoneNumbers", phoneNum);
|
request.putQueryParameter("SignName", "小棕熊到家");
|
// request.putQueryParameter("TemplateCode", "SMS_172840259");
|
request.putQueryParameter("TemplateCode", templateCode);
|
//设置短信内容 json字符串格式
|
String message = JSONObject.toJSONString(map);
|
request.putQueryParameter("TemplateParam", message);
|
CommonResponse response = client.getCommonResponse(request);
|
String data = response.getData();
|
cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(data);
|
String code = jsonObject.getStr("Code");
|
String msg = jsonObject.getStr("Message","短信发送失败");
|
if("OK".equals(code)){
|
log.info("阿里短信发送成功:" + data);
|
}else{
|
log.error("阿里短信发送失败:" + msg);
|
return msg;
|
}
|
}else if(CommonEnum.助通短信.getValue().equals(channel)){
|
|
String s = sendPointByTemplate(phoneNum, map, templateCode,1);
|
if(StrUtil.isNotBlank(s)){
|
return s;
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 助通单群发短信
|
* @param phoneNum
|
* @param content
|
*/
|
public int sendPoint(String phoneNum,String content) {
|
content=MixSmsUtil.sign+content;
|
String s = MixSmsUtil.sendSmsPoint(phoneNum, content);
|
cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(s);
|
if(jsonObject.getInt("code")==200){
|
log.info("助通单群发短信发送成功,手机号码:{},{}" ,phoneNum,s);
|
return 0;
|
}else{
|
log.error("助通单群发短信发送失败:" +jsonObject.getStr("msg"));
|
return -1;
|
}
|
}
|
|
/**
|
* 助通根据模板发送
|
* @param phoneNum
|
* @param templateCode
|
* @param map
|
* @param type
|
* @return
|
*/
|
public String sendPointByTemplate(String phoneNum,Map<String,String> map,String templateCode,int type) {
|
String s = MixSmsUtil.sendSms(phoneNum, map, templateCode,type);
|
cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(s);
|
String msg = jsonObject.getStr("msg","短信发送失败");
|
if(jsonObject.getInt("code")==200){
|
log.info("助通短信发送成功:" +s);
|
}else{
|
log.error("助通短信发送失败:" +msg);
|
return msg;
|
}
|
return null;
|
}
|
|
|
/**
|
* 实名认证
|
* @param authNameDto
|
* @return
|
*/
|
public Result authName(AuthNameDto authNameDto){
|
Result result=new Result();
|
try {
|
String authorization = HttpRequest.get(AL_AUTH_NAME_URL).form(BeanUtil.beanToMap(authNameDto)).header("Authorization", "APPCODE " + AL_AUTH_NAME_CODE).execute().body();
|
cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(authorization);
|
Integer code = jsonObject.getInt("code");
|
String msg = jsonObject.getStr("msg");
|
cn.hutool.json.JSONObject data = jsonObject.getJSONObject("data");
|
if(200==code){
|
result.setData(data);
|
return result;
|
}else{
|
return Result.error(-1,msg);
|
}
|
} catch (HttpException e) {
|
log.error("远程实名接口请求失败",e);
|
return Result.error(-1,"业务异常");
|
}
|
}
|
}
|