package com.stylefeng.guns.modular.system.util;
|
|
|
import com.alibaba.fastjson.JSON;
|
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.profile.DefaultProfile;
|
import com.google.gson.Gson;
|
import com.stylefeng.guns.core.util.DateUtil;
|
import org.springframework.stereotype.Component;
|
|
import java.math.BigDecimal;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.Map;
|
|
/**
|
* 阿里云短信工具类
|
*/
|
@Component
|
public class ALiSendSms {
|
|
// 设置鉴权参数,初始化客户端
|
private DefaultProfile profile = DefaultProfile.getProfile(
|
"cn-hangzhou",// 地域ID
|
"LTAI4G1BpRaihVRZp1RS8Stn",// 您的AccessKey ID
|
"PDR7uUEGNf0wIPPVCPCVdySgTwCr90");// 您的AccessKey Secret
|
private IAcsClient client = new DefaultAcsClient(profile);
|
|
private static void log_print(String functionName, Object result) {
|
Gson gson = new Gson();
|
System.out.println("-------------------------------" + functionName + "-------------------------------");
|
System.out.println(gson.toJson(result));
|
}
|
|
/**
|
* 添加短信模板
|
*/
|
public String addSmsTemplate() throws ClientException {
|
CommonRequest addSmsTemplateRequest = new CommonRequest();
|
addSmsTemplateRequest.setSysDomain("dysmsapi.aliyuncs.com");
|
addSmsTemplateRequest.setSysAction("AddSmsTemplate");
|
addSmsTemplateRequest.setSysVersion("2017-05-25");
|
// 短信类型。0:验证码;1:短信通知;2:推广短信;3:国际/港澳台消息
|
addSmsTemplateRequest.putQueryParameter("TemplateType", "0");
|
// 模板名称,长度为1~30个字符
|
addSmsTemplateRequest.putQueryParameter("TemplateName", "测试短信模板");
|
// 模板内容,长度为1~500个字符
|
addSmsTemplateRequest.putQueryParameter("TemplateContent", "您正在申请手机注册,验证码为:${code},5分钟内有效!");
|
// 短信模板申请说明
|
addSmsTemplateRequest.putQueryParameter("Remark", "测试");
|
CommonResponse addSmsTemplateResponse = client.getCommonResponse(addSmsTemplateRequest);
|
String data = addSmsTemplateResponse.getData();
|
// 消除返回文本中的反转义字符
|
String sData = data.replaceAll("'\'", "");
|
log_print("addSmsTemplate", sData);
|
Gson gson = new Gson();
|
// 将字符串转换为Map类型,取TemplateCode字段值
|
Map map = gson.fromJson(sData, Map.class);
|
Object templateCode = map.get("TemplateCode");
|
return templateCode.toString();
|
}
|
|
/**
|
* 发送短信
|
*/
|
public String sendSms(String phone, String templateCode, String json) throws ClientException {
|
CommonRequest request = new CommonRequest();
|
request.setSysDomain("dysmsapi.aliyuncs.com");
|
request.setSysVersion("2017-05-25");
|
request.setSysAction("SendSms");
|
// 接收短信的手机号码
|
request.putQueryParameter("PhoneNumbers", phone);
|
// 短信签名名称。请在控制台签名管理页面签名名称一列查看(必须是已添加、并通过审核的短信签名)。
|
request.putQueryParameter("SignName", "道奇乐科技");
|
// 短信模板ID
|
request.putQueryParameter("TemplateCode", templateCode);
|
// 短信模板变量对应的实际值,JSON格式。
|
request.putQueryParameter("TemplateParam", json);
|
CommonResponse commonResponse = client.getCommonResponse(request);
|
String data = commonResponse.getData();
|
String sData = data.replaceAll("'\'", "");
|
log_print("sendSms", sData);
|
return sData;
|
}
|
public String sendSms1(String phone, String templateCode, String json) throws ClientException {
|
CommonRequest request = new CommonRequest();
|
request.setSysDomain("dysmsapi.aliyuncs.com");
|
request.setSysVersion("2017-05-25");
|
request.setSysAction("SendSms");
|
// 接收短信的手机号码
|
request.putQueryParameter("PhoneNumbers", phone);
|
// 短信签名名称。请在控制台签名管理页面签名名称一列查看(必须是已添加、并通过审核的短信签名)。
|
request.putQueryParameter("SignName", "飞马打车");
|
// 短信模板ID
|
request.putQueryParameter("TemplateCode", templateCode);
|
// 短信模板变量对应的实际值,JSON格式。
|
request.putQueryParameter("TemplateParam", json);
|
CommonResponse commonResponse = client.getCommonResponse(request);
|
String data = commonResponse.getData();
|
String sData = data.replaceAll("'\'", "");
|
log_print("sendSms", sData);
|
return sData;
|
}
|
/**
|
* 查询发送详情
|
*/
|
private void querySendDetails(String bizId) throws ClientException {
|
CommonRequest request = new CommonRequest();
|
request.setSysDomain("dysmsapi.aliyuncs.com");
|
request.setSysVersion("2017-05-25");
|
request.setSysAction("QuerySendDetails");
|
// 接收短信的手机号码
|
request.putQueryParameter("PhoneNumber", "156xxxxxxxx");
|
// 短信发送日期,支持查询最近30天的记录。格式为yyyyMMdd,例如20191010。
|
request.putQueryParameter("SendDate", "20191010");
|
// 分页记录数量
|
request.putQueryParameter("PageSize", "10");
|
// 分页当前页码
|
request.putQueryParameter("CurrentPage", "1");
|
// 发送回执ID,即发送流水号。
|
request.putQueryParameter("BizId", bizId);
|
CommonResponse response = client.getCommonResponse(request);
|
log_print("querySendDetails", response.getData());
|
}
|
|
public static void main(String[] args) {
|
/*ALiSendSms sendSmsDemo = new ALiSendSms();
|
try {
|
// 创建短信模板
|
String templateCode = sendSmsDemo.addSmsTemplate();
|
// 使用刚创建的短信模板发送短信
|
String sData = sendSmsDemo.sendSms("156xxxxxxxx", templateCode, "{\"code\":\"8888\"}");
|
Gson gson = new Gson();
|
Map map = gson.fromJson(sData, Map.class);
|
String bizId = map.get("BizId").toString();
|
// 根据短信发送流水号查询短信发送情况
|
sendSmsDemo.querySendDetails(bizId);
|
} catch (ClientException e) {
|
e.printStackTrace();
|
}*/
|
//开始根据不同的方式计算金额
|
try {
|
ALiSendSms sendSmsDemo = new ALiSendSms();
|
String sData = sendSmsDemo.sendSms1("18380476116", "SMS_215342869", "{\"time\":\"" + DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss") + "\",\"money\":\"" + 1 + "\"}");
|
System.out.println(sData);
|
} catch (ClientException e) {
|
e.printStackTrace();
|
}
|
/*double amount = 0;
|
JSONObject jsonObject = JSON.parseObject("{\"num1\":\"6.4\",\"num2\":\"2.5\",\"num3\":\"4\",\"num4\":\"1.8\",\"num5\":\"0.25\",\"num6\":\"2\",\"num7\":\"0.5\",\"num8\":\"4\",\"num9\":\"10\",\"num10\":\"1.00\",\"num11\":\"10\",\"num12\":\"20\",\"num13\":\"1.00\",\"num14\":\"20\",\"num15\":\"1.00\",\"num16\":\"22:00 - 23:59\",\"num17\":\"7\",\"num18\":\"2.3\",\"num19\":\"0.5\",\"num20\":\"1\",\"num21\":\"1\",\"num22\":\"1\",\"num23\":\"00:00 - 02:59\",\"num24\":\"03:00 - 07:00\",\"num25\":\"7\",\"num26\":\"2.3\",\"num27\":\"0.5\",\"num28\":\"1\",\"num29\":\"1\",\"num30\":\"1\"}");
|
Double num1 = jsonObject.getDouble("num1");//起步价(元)
|
Double num2 = jsonObject.getDouble("num2");//起步公里(公里)
|
Double num3 = jsonObject.getDouble("num3");//起步时间(分钟)
|
Double num4 = jsonObject.getDouble("num4");//里程费(元)
|
Double num5 = jsonObject.getDouble("num5");//时长费(分钟)
|
Double num6 = jsonObject.getDouble("num6");//等待费(分钟)
|
Double num7 = jsonObject.getDouble("num7");//等待费(元)
|
Double num8 = jsonObject.getDouble("num8");//远途费(公里)
|
Double num9 = jsonObject.getDouble("num9");//远途费(公里)
|
Double num10 = jsonObject.getDouble("num10");//远途费(元)
|
Double num11 = jsonObject.getDouble("num11");//远途费(公里)
|
Double num12 = jsonObject.getDouble("num12");//远途费(公里)
|
Double num13 = jsonObject.getDouble("num13");//远途费(元)
|
Double num14 = jsonObject.getDouble("num14");//远途费(公里)
|
Double num15 = jsonObject.getDouble("num15");//远途费(元)
|
String num16 = jsonObject.getString("num16");//夜间费(开始时间)
|
Double num17 = jsonObject.getDouble("num17");//夜间费(元)
|
Double num18 = jsonObject.getDouble("num18");//夜间费(元)
|
Double num19 = jsonObject.getDouble("num19");//夜间费(元)
|
Double num20 = jsonObject.getDouble("num20");//夜间费(元)
|
Double num21 = jsonObject.getDouble("num21");//夜间费(元)
|
Double num22 = jsonObject.getDouble("num22");//夜间费(元)
|
String num23 = jsonObject.getString("num23");//高峰费(开始时间)
|
String num24 = jsonObject.getString("num24");//高峰费(开始时间)
|
Double num25 = jsonObject.getDouble("num25");//高峰费(元)
|
Double num26 = jsonObject.getDouble("num26");//高峰费(元)
|
Double num27 = jsonObject.getDouble("num27");//高峰费(元)
|
Double num28 = jsonObject.getDouble("num28");//高峰费(元)
|
Double num29 = jsonObject.getDouble("num29");//高峰费(元)
|
Double num30 = jsonObject.getDouble("num30");//高峰费(元)
|
|
Date date = DateUtil.parseTime("2021-03-31 23:07:29");
|
double d = 3606d / 1000;//实际公里
|
double d1 = (d - num2) < 0 ? 0 : d - num2;//超出起步里程的公里
|
double yt1 = 0;//远途1段
|
double yt2 = 0;//远途2段
|
double yt3 = 0;//远途3段
|
|
|
//夜间服务处理逻辑
|
Calendar s = Calendar.getInstance();
|
s.setTime(date);
|
s.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num16.split(" - ")[0].split(":")[0]));
|
s.set(Calendar.MINUTE, Integer.valueOf(num16.split(" - ")[0].split(":")[1]));
|
|
Calendar e = Calendar.getInstance();
|
e.setTime(date);
|
e.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num16.split(" - ")[1].split(":")[0]));
|
e.set(Calendar.MINUTE, Integer.valueOf(num16.split(" - ")[1].split(":")[1]));
|
|
if(date.getTime() > s.getTimeInMillis() && date.getTime() < e.getTimeInMillis()){
|
if(d > num8.doubleValue() && d <= num9.doubleValue()){
|
yt1 = num20 * (d - num8);
|
}
|
if(d > num9.doubleValue()){
|
yt1 = num20 * (num9 - num8);
|
}
|
if(d > num11.doubleValue() && d <= num12.doubleValue()){
|
yt2 = num21 * (d - num11);
|
}
|
if(d > num12.doubleValue()){
|
yt2 = num21 * (num12 - num11);
|
}
|
if(d > num14.doubleValue()){
|
yt3 = num22 * (d - num14);
|
}
|
System.out.println(new BigDecimal(yt1 + yt2 + yt3).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
|
}
|
|
|
//高峰时段处理逻辑
|
Calendar s1 = Calendar.getInstance();
|
s1.setTime(date);
|
s1.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num23.split(" - ")[0].split(":")[0]));
|
s1.set(Calendar.MINUTE, Integer.valueOf(num23.split(" - ")[0].split(":")[1]));
|
|
Calendar e1 = Calendar.getInstance();
|
e1.setTime(date);
|
e1.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num23.split(" - ")[1].split(":")[0]));
|
e1.set(Calendar.MINUTE, Integer.valueOf(num23.split(" - ")[1].split(":")[1]));
|
|
Calendar s2 = Calendar.getInstance();
|
s2.setTime(date);
|
s2.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num24.split(" - ")[0].split(":")[0]));
|
s2.set(Calendar.MINUTE, Integer.valueOf(num24.split(" - ")[0].split(":")[1]));
|
|
Calendar e2 = Calendar.getInstance();
|
e2.setTime(date);
|
e2.set(Calendar.HOUR_OF_DAY, Integer.valueOf(num24.split(" - ")[1].split(":")[0]));
|
e2.set(Calendar.MINUTE, Integer.valueOf(num24.split(" - ")[1].split(":")[1]));
|
|
if((date.getTime() > s1.getTimeInMillis() && date.getTime() < e1.getTimeInMillis()) || (date.getTime() > s2.getTimeInMillis() && date.getTime() < e2.getTimeInMillis())){
|
if(d > num8.doubleValue() && d <= num9.doubleValue()){
|
yt1 = num28 * (d - num8);
|
}
|
if(d > num9.doubleValue()){
|
yt1 = num28 * (num9 - num8);
|
}
|
if(d > num11.doubleValue() && d <= num12.doubleValue()){
|
yt2 = num29 * (d - num11);
|
}
|
if(d > num12.doubleValue()){
|
yt2 = num29 * (num12 - num11);
|
}
|
if(d > num14.doubleValue()){
|
yt3 = num30 * (d - num14);
|
}
|
System.out.println(new BigDecimal(yt1 + yt2 + yt3).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
|
}
|
|
//其他时间段的计算
|
if(d > num8.doubleValue() && d <= num9.doubleValue()){
|
yt1 = num10 * (d - num8);
|
}
|
if(d > num9.doubleValue()){
|
yt1 = num10 * (num9 - num8);
|
}
|
if(d > num11.doubleValue() && d <= num12.doubleValue()){
|
yt2 = num13 * (d - num11);
|
}
|
if(d > num12.doubleValue()){
|
yt2 = num13 * (num12 - num11);
|
}
|
if(d > num14.doubleValue()){
|
yt3 = num15 * (d - num14);
|
}
|
System.out.println(new BigDecimal(yt1 + yt2 + yt3).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());*/
|
}
|
}
|