This commit is contained in:
parent
447dbcb88b
commit
b5edf7381f
|
|
@ -1,35 +1,32 @@
|
|||
package com.hxkj.admin.controller;
|
||||
|
||||
import com.hxkj.common.core.AjaxResult;
|
||||
import com.hxkj.common.plugin.storage.StorageDriver;
|
||||
import com.hxkj.common.plugin.storage.engine.Aliyun;
|
||||
import com.hxkj.common.plugin.storage.engine.Qiniu;
|
||||
import com.hxkj.common.utils.TimeUtil;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import com.hxkj.common.exception.OperateException;
|
||||
import com.hxkj.common.plugin.sms.SmsDriver;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartRequest;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@RestController
|
||||
public class IndexController {
|
||||
|
||||
@PostMapping("/aa")
|
||||
public AjaxResult aa(HttpServletRequest request) {
|
||||
MultipartFile multipartFile = ((MultipartRequest) request).getFile("file");
|
||||
if (multipartFile == null) {
|
||||
return AjaxResult.failed("请选择上传文件");
|
||||
}
|
||||
|
||||
|
||||
// Qiniu qiniu = new Qiniu();
|
||||
// qiniu.upload(multipartFile);
|
||||
|
||||
new StorageDriver();
|
||||
public AjaxResult aa() {
|
||||
try {
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
(new SmsDriver())
|
||||
.setMobile("15627119239")
|
||||
.setParam(params)
|
||||
.sendSms();
|
||||
|
||||
return AjaxResult.success();
|
||||
} catch (OperateException e) {
|
||||
return AjaxResult.failed(e.getMsg());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -102,6 +102,18 @@
|
|||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
</dependency>
|
||||
<!--腾讯云短信-->
|
||||
<dependency>
|
||||
<groupId>com.tencentcloudapi</groupId>
|
||||
<artifactId>tencentcloud-sdk-java</artifactId>
|
||||
<version>${tencentcloudapi.version}</version>
|
||||
</dependency>
|
||||
<!--阿里云短信-->
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-core</artifactId>
|
||||
<version>${aliyun-java.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
package com.hxkj.common.plugin.sms;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.hxkj.common.plugin.sms.engine.AliSms;
|
||||
import com.hxkj.common.plugin.sms.engine.TencentSms;
|
||||
import com.hxkj.common.utils.ConfigUtil;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class SmsDriver {
|
||||
|
||||
private String mobile; // 手机号码
|
||||
private String templateId; // 短信模板
|
||||
private String smsContent; // 短信内容
|
||||
private Map<String, String> param; // 短信参数
|
||||
private final String engine; // 短信引擎
|
||||
private final Map<String, String> config; // 短信配置
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public SmsDriver() {
|
||||
this.engine = ConfigUtil.get("sms", "default", "aliyun");
|
||||
this.config = ConfigUtil.getMap("sms", this.engine);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置手机号
|
||||
*
|
||||
* @author fzr
|
||||
* @param mobile 手机号
|
||||
* @return SmsDriver
|
||||
*/
|
||||
public SmsDriver setMobile(String mobile) {
|
||||
this.mobile = mobile;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置参数
|
||||
*
|
||||
* @author fzr
|
||||
* @param param 参数
|
||||
* @return SmsDriver
|
||||
*/
|
||||
public SmsDriver setParam(Map<String, String> param) {
|
||||
this.param = param;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
*
|
||||
* @author fzr
|
||||
*/
|
||||
public void sendSms() {
|
||||
String templateParam = JSON.toJSONString(param);
|
||||
switch (this.engine) {
|
||||
case "aliyun":
|
||||
AliSms aliSms = new AliSms(this.config);
|
||||
aliSms.setMobile(this.mobile)
|
||||
.setTemplateId(this.templateId)
|
||||
.setTemplateParams(templateParam)
|
||||
.send();
|
||||
break;
|
||||
case "tencent":
|
||||
TencentSms tencentSms = new TencentSms(this.config);
|
||||
tencentSms.setMobile(this.mobile)
|
||||
.setTemplateId(this.templateId)
|
||||
.setTemplateParams(templateParam.split(","))
|
||||
.send();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,106 @@
|
|||
package com.hxkj.common.plugin.sms.engine;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyuncs.CommonRequest;
|
||||
import com.aliyuncs.CommonResponse;
|
||||
import com.aliyuncs.DefaultAcsClient;
|
||||
import com.aliyuncs.IAcsClient;
|
||||
import com.aliyuncs.http.MethodType;
|
||||
import com.aliyuncs.profile.DefaultProfile;
|
||||
import com.hxkj.common.exception.OperateException;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 阿里云短信
|
||||
*/
|
||||
public class AliSms {
|
||||
|
||||
private String mobile; // 手机号码
|
||||
private String templateId; // 短信模板
|
||||
private String templateParams; // 短信参数
|
||||
private final Map<String, String> config; // 短信配置
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*
|
||||
* @author fzr
|
||||
* @param config 短信配置
|
||||
*/
|
||||
public AliSms(Map<String, String> config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置手机号
|
||||
*
|
||||
* @author fzr
|
||||
* @param mobile 手机号码
|
||||
* @return AliSms
|
||||
*/
|
||||
public AliSms setMobile(String mobile) {
|
||||
this.mobile = mobile;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置模板id
|
||||
*
|
||||
* @author fzr
|
||||
* @param templateId 模板id
|
||||
* @return AliSms
|
||||
*/
|
||||
public AliSms setTemplateId(String templateId) {
|
||||
this.templateId = templateId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置模板参数
|
||||
*
|
||||
* @author fzr
|
||||
* @param templateParams 模板参数
|
||||
* @return AliSms
|
||||
*/
|
||||
public AliSms setTemplateParams(String templateParams) {
|
||||
this.templateParams = templateParams;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
*
|
||||
* @author fzr
|
||||
* @return String
|
||||
*/
|
||||
public String send() {
|
||||
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", this.config.get("app_key"), this.config.get("secret_key"));
|
||||
IAcsClient client = new DefaultAcsClient(profile);
|
||||
|
||||
CommonRequest request = new CommonRequest();
|
||||
request.setSysMethod(MethodType.POST);
|
||||
request.setSysDomain("dysmsapi.aliyuncs.com");
|
||||
request.setSysVersion("2017-05-25");
|
||||
request.setSysAction("SendSms");
|
||||
request.putQueryParameter("PhoneNumbers", this.mobile);
|
||||
request.putQueryParameter("SignName", this.config.get("sign"));
|
||||
request.putQueryParameter("TemplateCode", this.templateId);
|
||||
request.putQueryParameter("TemplateParam", this.templateParams);
|
||||
try {
|
||||
System.out.println("来来来来来");
|
||||
CommonResponse response = client.getCommonResponse(request);
|
||||
System.out.println(response);
|
||||
JSONObject res = JSONObject.parseObject(response.getData());
|
||||
|
||||
if (!res.get("Code").equals("OK") || !res.get("Message").equals("OK")) {
|
||||
throw new OperateException(res.get("Message").toString());
|
||||
}
|
||||
|
||||
return response.getData();
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
throw new OperateException("短信发送异常:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,109 @@
|
|||
package com.hxkj.common.plugin.sms.engine;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hxkj.common.exception.OperateException;
|
||||
import com.tencentcloudapi.common.Credential;
|
||||
import com.tencentcloudapi.common.profile.ClientProfile;
|
||||
import com.tencentcloudapi.common.profile.HttpProfile;
|
||||
import com.tencentcloudapi.sms.v20210111.SmsClient;
|
||||
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
|
||||
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 腾讯短信驱
|
||||
*/
|
||||
public class TencentSms {
|
||||
|
||||
|
||||
private String mobile;
|
||||
private String templateId;
|
||||
private String[] templateParams;
|
||||
private final Map<String, String> config;
|
||||
|
||||
public TencentSms(Map<String, String> config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置手机号
|
||||
* @author fzr
|
||||
* @param mobile 手机号码
|
||||
* @return AliSms
|
||||
*/
|
||||
public TencentSms setMobile(String mobile) {
|
||||
this.mobile = mobile;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置模板id
|
||||
* @author fzr
|
||||
* @param templateId 模板id
|
||||
* @return AliSms
|
||||
*/
|
||||
public TencentSms setTemplateId(String templateId) {
|
||||
this.templateId = templateId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置模板参数
|
||||
* @author fzr
|
||||
* @param templateParams 模板参数
|
||||
* @return AliSms
|
||||
*/
|
||||
public TencentSms setTemplateParams(String[] templateParams) {
|
||||
this.templateParams = templateParams;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
*
|
||||
* @param config 配置
|
||||
* @return String
|
||||
*/
|
||||
public String send() {
|
||||
try {
|
||||
/*认证对象*/
|
||||
Credential cred = new Credential(this.config.get("secret_id").toString(), config.get("secret_key").toString());
|
||||
HttpProfile httpProfile = new HttpProfile();
|
||||
httpProfile.setReqMethod("POST");
|
||||
httpProfile.setConnTimeout(60);
|
||||
httpProfile.setEndpoint("sms.tencentcloudapi.com");
|
||||
|
||||
/*客户端配置*/
|
||||
ClientProfile clientProfile = new ClientProfile();
|
||||
clientProfile.setSignMethod("HmacSHA256");
|
||||
clientProfile.setHttpProfile(httpProfile);
|
||||
|
||||
/*参数配置*/
|
||||
SmsClient client = new SmsClient(cred, "ap-guangzhou",clientProfile);
|
||||
SendSmsRequest req = new SendSmsRequest();
|
||||
req.setSignName(config.get("sign").toString());
|
||||
req.setSmsSdkAppId(config.get("app_id").toString());
|
||||
req.setTemplateId(this.templateId);
|
||||
|
||||
/*手机号码*/
|
||||
String[] phoneNumberSet = {"+86"+this.mobile};
|
||||
req.setPhoneNumberSet(phoneNumberSet);
|
||||
|
||||
/*模板参数*/
|
||||
String[] templateParamSet = this.templateParams;
|
||||
req.setTemplateParamSet(templateParamSet);
|
||||
|
||||
/*发起请求*/
|
||||
SendSmsResponse res = client.SendSms(req);
|
||||
if (!res.getSendStatusSet()[0].getCode().equals("Ok")) {
|
||||
throw new Exception(res.getSendStatusSet()[0].getMessage());
|
||||
}
|
||||
|
||||
return res.getSendStatusSet()[0].getMessage();
|
||||
} catch (Exception e) {
|
||||
throw new OperateException("短信发送异常:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -2,13 +2,12 @@ package com.hxkj.common.plugin.storage;
|
|||
|
||||
import com.hxkj.common.config.GlobalConfig;
|
||||
import com.hxkj.common.exception.OperateException;
|
||||
import com.hxkj.common.plugin.storage.engine.Aliyun;
|
||||
import com.hxkj.common.plugin.storage.engine.Local;
|
||||
import com.hxkj.common.plugin.storage.engine.Qcloud;
|
||||
import com.hxkj.common.plugin.storage.engine.Qiniu;
|
||||
import com.hxkj.common.plugin.storage.engine.AliyunStorage;
|
||||
import com.hxkj.common.plugin.storage.engine.LocalStorage;
|
||||
import com.hxkj.common.plugin.storage.engine.QcloudStorage;
|
||||
import com.hxkj.common.plugin.storage.engine.QiniuStorage;
|
||||
import com.hxkj.common.utils.ConfigUtil;
|
||||
import com.hxkj.common.utils.TimeUtil;
|
||||
import com.hxkj.common.utils.ToolsUtil;
|
||||
import com.hxkj.common.utils.UrlUtil;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
|
|
@ -47,19 +46,19 @@ public class StorageDriver {
|
|||
String key = this.buildSaveName(multipartFile);
|
||||
switch (this.engine) {
|
||||
case "local":
|
||||
Local local = new Local();
|
||||
LocalStorage local = new LocalStorage();
|
||||
local.upload(multipartFile, key, folder);
|
||||
break;
|
||||
case "qiniu":
|
||||
Qiniu qiniu = new Qiniu(this.config);
|
||||
QiniuStorage qiniu = new QiniuStorage(this.config);
|
||||
qiniu.upload(multipartFile, folder + "/" + key);
|
||||
break;
|
||||
case "aliyun":
|
||||
Aliyun aliyun = new Aliyun(this.config);
|
||||
AliyunStorage aliyun = new AliyunStorage(this.config);
|
||||
aliyun.upload(multipartFile, folder + "/" + key);
|
||||
break;
|
||||
case "qcloud":
|
||||
Qcloud qcloud = new Qcloud(this.config);
|
||||
QcloudStorage qcloud = new QcloudStorage(this.config);
|
||||
qcloud.upload(multipartFile, folder + "/" + key);
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,19 @@
|
|||
package com.hxkj.common.plugin.storage.engine;
|
||||
|
||||
import com.aliyun.oss.ClientException;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.OSSException;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.hxkj.common.exception.OperateException;
|
||||
import com.qiniu.util.Auth;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class Aliyun {
|
||||
/**
|
||||
* 阿里云存储
|
||||
*/
|
||||
public class AliyunStorage {
|
||||
|
||||
/**
|
||||
* 存储配置
|
||||
|
|
@ -23,7 +23,7 @@ public class Aliyun {
|
|||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public Aliyun(Map<String, String> config) {
|
||||
public AliyunStorage(Map<String, String> config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
|
@ -6,9 +6,11 @@ import com.hxkj.common.utils.YmlUtil;
|
|||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Local {
|
||||
/**
|
||||
* 本地存储
|
||||
*/
|
||||
public class LocalStorage {
|
||||
|
||||
/**
|
||||
* 本地上传
|
||||
|
|
@ -15,7 +15,10 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
import java.io.*;
|
||||
import java.util.Map;
|
||||
|
||||
public class Qcloud {
|
||||
/**
|
||||
* 腾讯云存储
|
||||
*/
|
||||
public class QcloudStorage {
|
||||
|
||||
/**
|
||||
* 存储配置
|
||||
|
|
@ -25,7 +28,7 @@ public class Qcloud {
|
|||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public Qcloud(Map<String, String> config) {
|
||||
public QcloudStorage(Map<String, String> config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
|
@ -13,7 +13,10 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class Qiniu {
|
||||
/**
|
||||
* 七牛云存储
|
||||
*/
|
||||
public class QiniuStorage {
|
||||
|
||||
/**
|
||||
* 存储配置
|
||||
|
|
@ -23,7 +26,7 @@ public class Qiniu {
|
|||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public Qiniu(Map<String, String> config) {
|
||||
public QiniuStorage(Map<String, String> config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
14
pom.xml
14
pom.xml
|
|
@ -32,7 +32,9 @@
|
|||
<gson.version>2.9.0</gson.version>
|
||||
<qiniu.version>7.9.5</qiniu.version>
|
||||
<qcloud-version>5.6.54</qcloud-version>
|
||||
<tencentcloudapi.version>3.1.411</tencentcloudapi.version>
|
||||
<aliyun-oss.version>3.10.2</aliyun-oss.version>
|
||||
<aliyun-java.version>4.5.16</aliyun-java.version>
|
||||
</properties>
|
||||
|
||||
<!-- 依赖声明 -->
|
||||
|
|
@ -112,6 +114,18 @@
|
|||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>${aliyun-oss.version}</version>
|
||||
</dependency>
|
||||
<!--腾讯云短信-->
|
||||
<dependency>
|
||||
<groupId>com.tencentcloudapi</groupId>
|
||||
<artifactId>tencentcloud-sdk-java</artifactId>
|
||||
<version>${tencentcloudapi.version}</version>
|
||||
</dependency>
|
||||
<!--阿里云短信-->
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-core</artifactId>
|
||||
<version>${aliyun-java.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue