初始代码
This commit is contained in:
53
yunzhupaas-boot-common/yunzhupaas-common-sms/pom.xml
Normal file
53
yunzhupaas-boot-common/yunzhupaas-common-sms/pom.xml
Normal file
@@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>yunzhupaas-boot-common</artifactId>
|
||||
<groupId>com.yunzhupaas</groupId>
|
||||
<version>5.2.0-RELEASE</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>yunzhupaas-common-sms</artifactId>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.tencentcloudapi</groupId>
|
||||
<artifactId>tencentcloud-sdk-java</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>dysmsapi20170525</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>bcpkix-jdk15on</artifactId>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcpkix-jdk18on</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>dingtalk-sdk-java</groupId>
|
||||
<artifactId>taobao-sdk-java</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>dingtalk-sdk-java</groupId>
|
||||
<artifactId>taobao-sdk-java-source</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yunzhupaas</groupId>
|
||||
<artifactId>yunzhupaas-common-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,143 @@
|
||||
package com.yunzhupaas.util.message;
|
||||
|
||||
import com.aliyun.dysmsapi20170525.Client;
|
||||
import com.aliyun.dysmsapi20170525.models.*;
|
||||
import com.aliyun.teaopenapi.models.*;
|
||||
import com.yunzhupaas.util.JsonUtil;
|
||||
import com.yunzhupaas.util.ParameterUtil;
|
||||
import com.yunzhupaas.util.StringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 阿里云发送短信
|
||||
*
|
||||
* @版本: V3.2.0
|
||||
* @版权: 深圳市乐程软件有限公司(http://www.szlecheng.cn)
|
||||
* @作者: 云筑产品开发平台组
|
||||
* @日期: 2021/4/21 11:45
|
||||
*/
|
||||
@Slf4j
|
||||
public class SmsAliYunUtil {
|
||||
|
||||
/**
|
||||
* 使用AK&SK初始化账号Client
|
||||
*
|
||||
* @param accessKeyId
|
||||
* @param accessKeySecret
|
||||
* @param endpoint
|
||||
* @return Client
|
||||
*/
|
||||
private static Client createClient(String accessKeyId, String accessKeySecret, String endpoint) {
|
||||
try {
|
||||
Config config = new Config()
|
||||
// 您的AccessKey ID
|
||||
.setAccessKeyId(accessKeyId)
|
||||
// 您的AccessKey Secret
|
||||
.setAccessKeySecret(accessKeySecret);
|
||||
// 访问的域名
|
||||
config.endpoint = endpoint;
|
||||
return new Client(config);
|
||||
} catch (Exception e) {
|
||||
log.error("创建阿里云短信客户端错误:" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询短信模板详情
|
||||
*
|
||||
* @param accessKeyId
|
||||
* @param accessKeySecret
|
||||
* @param endpoint
|
||||
* @param templateId
|
||||
*/
|
||||
public static List<String> querySmsTemplateRequest(String accessKeyId, String accessKeySecret, String endpoint, String templateId) {
|
||||
try {
|
||||
Client client = createClient(accessKeyId, accessKeySecret, endpoint);
|
||||
QuerySmsTemplateRequest querySmsTemplateRequest = new QuerySmsTemplateRequest()
|
||||
.setTemplateCode(templateId);
|
||||
QuerySmsTemplateResponse querySmsTemplateResponse = client.querySmsTemplate(querySmsTemplateRequest);
|
||||
String templateContent = querySmsTemplateResponse.getBody().templateContent;
|
||||
if (StringUtil.isNotEmpty(templateContent)) {
|
||||
List<String> list = new ArrayList<>();
|
||||
ParameterUtil.parse("${", "}", templateContent, list);
|
||||
return list;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("查询阿里云短信模板错误:" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询短信模板详情
|
||||
*
|
||||
* @param accessKeyId
|
||||
* @param accessKeySecret
|
||||
* @param endpoint
|
||||
* @param templateId
|
||||
*/
|
||||
public static String querySmsTemplateContent(String accessKeyId, String accessKeySecret, String endpoint, String templateId) {
|
||||
try {
|
||||
Client client = createClient(accessKeyId, accessKeySecret, endpoint);
|
||||
QuerySmsTemplateRequest querySmsTemplateRequest = new QuerySmsTemplateRequest()
|
||||
.setTemplateCode(templateId);
|
||||
QuerySmsTemplateResponse querySmsTemplateResponse = client.querySmsTemplate(querySmsTemplateRequest);
|
||||
String templateContent = querySmsTemplateResponse.getBody().templateContent;
|
||||
if (StringUtil.isNotEmpty(templateContent)) {
|
||||
return templateContent;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("查询阿里云短信模板错误:" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
*
|
||||
* @param accessKeyId
|
||||
* @param accessKeySecret
|
||||
* @param endpoint
|
||||
* @param phoneNumbers
|
||||
* @param signContent
|
||||
* @param templateId
|
||||
* @param map
|
||||
* @return
|
||||
*/
|
||||
public static String sentSms(String accessKeyId, String accessKeySecret, String endpoint, String phoneNumbers, String signContent, String templateId, Map<String, Object> map) {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
try {
|
||||
Client client = createClient(accessKeyId, accessKeySecret, endpoint);
|
||||
SendSmsRequest sendSmsRequest = new SendSmsRequest();
|
||||
// 接收者的号码
|
||||
sendSmsRequest.setPhoneNumbers(phoneNumbers);
|
||||
// 签名
|
||||
sendSmsRequest.setSignName(signContent);
|
||||
// 模板id
|
||||
sendSmsRequest.setTemplateCode(templateId);
|
||||
// 模板参数
|
||||
sendSmsRequest.setTemplateParam(JsonUtil.getObjectToString(map));
|
||||
SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
|
||||
if (!"Ok".equalsIgnoreCase(sendSmsResponse.body.code)) {
|
||||
log.error("发送短信失败:" + sendSmsResponse.getBody().message);
|
||||
return "发送短信失败:" + sendSmsResponse.getBody().message;
|
||||
}
|
||||
return "Ok";
|
||||
} catch (Exception e) {
|
||||
log.error("发送短信失败:" + e.getMessage());
|
||||
return "发送短信失败:" + e.getMessage();
|
||||
}
|
||||
// return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
package com.yunzhupaas.util.message;
|
||||
|
||||
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.*;
|
||||
import com.yunzhupaas.util.ParameterUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 腾讯云发送短信类
|
||||
*
|
||||
* @author YUNZHUPAAS
|
||||
* @版本: V3.2.0
|
||||
* @版权: 深圳市乐程软件有限公司(http://www.szlecheng.cn)
|
||||
* @作者: 云筑产品开发平台组
|
||||
* @日期: 2021/4/21 11:58
|
||||
*/
|
||||
@Slf4j
|
||||
public class SmsTenCentCloudUtil {
|
||||
|
||||
/**
|
||||
* 创建客户端
|
||||
*
|
||||
* @param accessKeyId
|
||||
* @param accessKeySecret
|
||||
* @return
|
||||
*/
|
||||
private static SmsClient createClient(String accessKeyId, String accessKeySecret, String endpoint, String region) {
|
||||
SmsClient smsClient = null;
|
||||
try {
|
||||
Credential cred = new Credential(accessKeyId, accessKeySecret);
|
||||
// 实例化一个http选项,可选的,没有特殊需求可以跳过
|
||||
HttpProfile httpProfile = new HttpProfile();
|
||||
httpProfile.setEndpoint(endpoint);
|
||||
// 实例化一个client选项,可选的,没有特殊需求可以跳过
|
||||
ClientProfile clientProfile = new ClientProfile();
|
||||
clientProfile.setHttpProfile(httpProfile);
|
||||
// 实例化要请求产品的client对象,clientProfile是可选的
|
||||
smsClient = new SmsClient(cred, region, clientProfile);
|
||||
} catch (Exception e) {
|
||||
log.error("创建客户端失败:" + e.getMessage());
|
||||
}
|
||||
return smsClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询短信模板详情
|
||||
*
|
||||
* @param accessKeyId
|
||||
* @param accessKeySecret
|
||||
* @param templateId
|
||||
*/
|
||||
public static List<String> querySmsTemplateRequest(String accessKeyId, String accessKeySecret, String endpoint,
|
||||
String region, String templateId) {
|
||||
try {
|
||||
SmsClient smsClient = createClient(accessKeyId, accessKeySecret, endpoint, region);
|
||||
// 实例化一个请求对象,每个接口都会对应一个request对象
|
||||
DescribeSmsTemplateListRequest req = new DescribeSmsTemplateListRequest();
|
||||
req.setTemplateIdSet(new Long[] { Long.valueOf(templateId) });
|
||||
req.setInternational(0L);
|
||||
// 返回的resp是一个DescribeSmsTemplateListResponse的实例,与请求对象对应
|
||||
DescribeSmsTemplateListResponse resp = smsClient.DescribeSmsTemplateList(req);
|
||||
// 输出json格式的字符串回包
|
||||
System.out.println(DescribeSmsTemplateListResponse.toJsonString(resp));
|
||||
DescribeTemplateListStatus[] describeTemplateStatusSet = resp.getDescribeTemplateStatusSet();
|
||||
for (DescribeTemplateListStatus describeTemplateListStatus : describeTemplateStatusSet) {
|
||||
String templateContent = describeTemplateListStatus.getTemplateContent();
|
||||
List<String> list = new ArrayList<>();
|
||||
ParameterUtil.parse("{", "}", templateContent, list);
|
||||
return list;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("查询短信模板参数失败:" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询短信模板详情
|
||||
*
|
||||
* @param accessKeyId
|
||||
* @param accessKeySecret
|
||||
* @param templateId
|
||||
*/
|
||||
public static String querySmsTemplateContent(String accessKeyId, String accessKeySecret, String endpoint,
|
||||
String region, String templateId) {
|
||||
try {
|
||||
SmsClient smsClient = createClient(accessKeyId, accessKeySecret, endpoint, region);
|
||||
// 实例化一个请求对象,每个接口都会对应一个request对象
|
||||
DescribeSmsTemplateListRequest req = new DescribeSmsTemplateListRequest();
|
||||
req.setTemplateIdSet(new Long[] { Long.valueOf(templateId) });
|
||||
req.setInternational(0L);
|
||||
// 返回的resp是一个DescribeSmsTemplateListResponse的实例,与请求对象对应
|
||||
DescribeSmsTemplateListResponse resp = smsClient.DescribeSmsTemplateList(req);
|
||||
// 输出json格式的字符串回包
|
||||
System.out.println(DescribeSmsTemplateListResponse.toJsonString(resp));
|
||||
DescribeTemplateListStatus[] describeTemplateStatusSet = resp.getDescribeTemplateStatusSet();
|
||||
for (DescribeTemplateListStatus describeTemplateListStatus : describeTemplateStatusSet) {
|
||||
String templateContent = describeTemplateListStatus.getTemplateContent();
|
||||
return templateContent;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("查询短信模板参数失败:" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
*
|
||||
* @param accessKeyId
|
||||
* @param accessKeySecret
|
||||
* @param phoneNumbers
|
||||
* @param appId
|
||||
* @param signContent
|
||||
* @param templateId
|
||||
* @param map
|
||||
* @return
|
||||
*/
|
||||
public static String sentSms(String accessKeyId, String accessKeySecret, String endpoint, String region,
|
||||
String phoneNumbers, String appId, String signContent, String templateId, Map<String, Object> map) {
|
||||
try {
|
||||
SmsClient client = createClient(accessKeyId, accessKeySecret, endpoint, region);
|
||||
// 实例化一个请求对象,每个接口都会对应一个request对象
|
||||
SendSmsRequest req = new SendSmsRequest();
|
||||
// 接收人
|
||||
String[] split = phoneNumbers.split(",");
|
||||
req.setPhoneNumberSet(split);
|
||||
// AppId
|
||||
req.setSmsSdkAppId(appId);
|
||||
// TemplateId
|
||||
req.setTemplateId(templateId);
|
||||
// SignName
|
||||
req.setSignName(signContent);
|
||||
// 参数
|
||||
List<String> list = new ArrayList<>();
|
||||
for (String key : map.keySet()) {
|
||||
String value = map.get(key) != null ? String.valueOf(map.get(key)) : "";
|
||||
list.add(value);
|
||||
}
|
||||
req.setTemplateParamSet(list.toArray(new String[list.size()]));
|
||||
// 返回的resp是一个SendSmsResponse的实例,与请求对象对应
|
||||
SendSmsResponse resp = client.SendSms(req);
|
||||
// 判断是否发送成功
|
||||
SendStatus[] sendStatusSet = resp.getSendStatusSet();
|
||||
for (SendStatus sendStatus : sendStatusSet) {
|
||||
String code = sendStatus.getCode();
|
||||
if ("Ok".equalsIgnoreCase(code)) {
|
||||
return "Ok";
|
||||
} else {
|
||||
log.error("发送短信失败:" + sendStatus.getMessage());
|
||||
return "发送短信失败:" + sendStatus.getMessage();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("发送短信失败:" + e.getMessage());
|
||||
return "发送短信失败:" + e.getMessage();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.yunzhupaas.util.message;
|
||||
|
||||
import com.yunzhupaas.base.SmsModel;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 短信工具类
|
||||
*
|
||||
* @author 云筑产品开发平台组
|
||||
* @version V3.1.0
|
||||
* @copyright 深圳市乐程软件有限公司(http://www.szlecheng.cn)
|
||||
* @date 2024-12-11
|
||||
*/
|
||||
@Component
|
||||
public class SmsUtil {
|
||||
|
||||
/**
|
||||
* 获取短信模板参数
|
||||
*
|
||||
* @param type
|
||||
* @param smsModel
|
||||
* @param templateId
|
||||
* @return
|
||||
*/
|
||||
public static List<String> querySmsTemplateRequest(Integer type, SmsModel smsModel, String endpoint, String region, String templateId) {
|
||||
if (type == 1) {
|
||||
return SmsAliYunUtil.querySmsTemplateRequest(smsModel.getAliAccessKey(), smsModel.getAliSecret(), endpoint, templateId);
|
||||
}
|
||||
return SmsTenCentCloudUtil.querySmsTemplateRequest(smsModel.getTencentSecretId(), smsModel.getTencentSecretKey(), endpoint , region, templateId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取短信模板内容
|
||||
*
|
||||
* @param type
|
||||
* @param smsModel
|
||||
* @param templateId
|
||||
* @return
|
||||
*/
|
||||
public static String querySmsTemplateContent(Integer type, SmsModel smsModel, String endpoint, String region, String templateId) {
|
||||
if (type == 1) {
|
||||
return SmsAliYunUtil.querySmsTemplateContent(smsModel.getAliAccessKey(), smsModel.getAliSecret(), endpoint, templateId);
|
||||
}
|
||||
return SmsTenCentCloudUtil.querySmsTemplateContent(smsModel.getTencentSecretId(), smsModel.getTencentSecretKey(), endpoint , region, templateId);
|
||||
}
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param type
|
||||
* @param smsModel
|
||||
* @param phoneNumbers
|
||||
* @param signContent
|
||||
* @param templateId
|
||||
* @param map
|
||||
* @return
|
||||
*/
|
||||
public static String sentSms(Integer type, SmsModel smsModel, String endpoint, String region, String phoneNumbers, String signContent, String templateId, Map<String, Object> map) {
|
||||
if (type == 1) {
|
||||
return SmsAliYunUtil.sentSms(smsModel.getAliAccessKey(), smsModel.getAliSecret(), endpoint, phoneNumbers, signContent, templateId, map);
|
||||
}
|
||||
return SmsTenCentCloudUtil.sentSms(smsModel.getTencentSecretId(), smsModel.getTencentSecretKey(), endpoint, region, phoneNumbers, smsModel.getTencentAppId(), signContent, templateId, map);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.yunzhupaas.util.third;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.dingtalk.api.DefaultDingTalkClient;
|
||||
import com.dingtalk.api.DingTalkClient;
|
||||
import com.dingtalk.api.request.OapiGettokenRequest;
|
||||
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
|
||||
import com.dingtalk.api.response.OapiGettokenResponse;
|
||||
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
|
||||
import com.taobao.api.ApiException;
|
||||
import com.yunzhupaas.util.RandomUtil;
|
||||
|
||||
|
||||
/**
|
||||
* 通过钉钉用户ID串进行发送消息,传入就是接收人的钉钉用户ID串
|
||||
*
|
||||
* @版本: V3.1.0
|
||||
* @版权: 深圳市乐程软件有限公司(http://www.szlecheng.cn)
|
||||
* @作者: 云筑产品开发平台组
|
||||
* @日期: 2021/4/21 16:10
|
||||
*/
|
||||
public class DingTalkUtil {
|
||||
|
||||
/**
|
||||
* 钉钉发送消息的接口路径
|
||||
*/
|
||||
public static final String SEND_MESSAGE = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";
|
||||
/**
|
||||
* 钉钉获取TOKEN的接口路径
|
||||
*/
|
||||
public static final String TOKEN = "https://oapi.dingtalk.com/gettoken";
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
* @param appkey
|
||||
* @param appsecret
|
||||
* @return
|
||||
*/
|
||||
// public static String getToken (String appkey,String appsecret){
|
||||
// DefaultDingTalkClient client = new
|
||||
// DefaultDingTalkClient(TOKEN);
|
||||
// OapiGettokenRequest request = new OapiGettokenRequest();
|
||||
// request.setAppkey(appkey);
|
||||
// request.setAppsecret(appsecret);
|
||||
// request.setHttpMethod("GET");
|
||||
// try {
|
||||
// OapiGettokenResponse response = client.execute(request);
|
||||
//// LocalCacheClient.set("access_token", response.getAccessToken(),7200*1000);
|
||||
// return response.getAccessToken();
|
||||
// } catch (ApiException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
* @param appkey
|
||||
* @param appsecret
|
||||
* @return
|
||||
*/
|
||||
public static JSONObject getAccessToken(String appkey, String appsecret){
|
||||
JSONObject retMsg = new JSONObject();
|
||||
retMsg.put("code",true);
|
||||
retMsg.put("error","");
|
||||
try{
|
||||
DingTalkClient client = new DefaultDingTalkClient(TOKEN);
|
||||
OapiGettokenRequest req = new OapiGettokenRequest();
|
||||
req.setAppkey(appkey);
|
||||
req.setAppsecret(appsecret);
|
||||
req.setHttpMethod("GET");
|
||||
OapiGettokenResponse rsp = client.execute(req);
|
||||
retMsg.put("access_token", rsp.getAccessToken());
|
||||
if (!rsp.isSuccess()) {
|
||||
retMsg.put("code", false);
|
||||
retMsg.put("error",rsp.getErrmsg());
|
||||
retMsg.put("access_token", "");
|
||||
}
|
||||
} catch (ApiException e) {
|
||||
retMsg.put("code", false);
|
||||
retMsg.put("error",e.toString());
|
||||
retMsg.put("access_token", "");
|
||||
}
|
||||
|
||||
return retMsg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给用户推送消息(文字消息)
|
||||
* @param appkey
|
||||
* @param appsecret
|
||||
* @param agentid
|
||||
* @param userIds
|
||||
* @param content
|
||||
* @return
|
||||
* 收到消息格式如下:
|
||||
* 发送的内容
|
||||
*/
|
||||
public static JSONObject sendDingMessage(String appkey, String appsecret, String agentid, String userIds, String content){
|
||||
JSONObject retMsg = new JSONObject();
|
||||
DingTalkClient client = new DefaultDingTalkClient(SEND_MESSAGE);
|
||||
|
||||
OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
|
||||
request.setUseridList(userIds);
|
||||
request.setAgentId(Long.parseLong(agentid));
|
||||
request.setToAllUser(false);
|
||||
|
||||
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
|
||||
msg.setMsgtype("text");
|
||||
msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
|
||||
String randomCode = "随机验证码:"+ RandomUtil.uuId();
|
||||
msg.getText().setContent(content+randomCode);
|
||||
request.setMsg(msg);
|
||||
|
||||
try {
|
||||
retMsg = getAccessToken(appkey,appsecret);
|
||||
if(retMsg.getBoolean("code")){
|
||||
// OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request,getToken(appkey,appsecret));
|
||||
OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request,retMsg.getString("access_token"));
|
||||
if(response.getErrcode()>0){
|
||||
retMsg.put("code",false);
|
||||
retMsg.put("error",response.getErrmsg());
|
||||
}else{
|
||||
retMsg.put("code",true);
|
||||
retMsg.put("error","");
|
||||
}
|
||||
}else{
|
||||
retMsg.put("code",false);
|
||||
retMsg.put("error","获取token失败:"+retMsg.getString("error"));
|
||||
}
|
||||
return retMsg;
|
||||
} catch (ApiException e) {
|
||||
retMsg.put("code",false);
|
||||
retMsg.put("error",e.toString());
|
||||
return retMsg;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.yunzhupaas.util.third;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yunzhupaas.util.wxutil.HttpUtil;
|
||||
|
||||
/**
|
||||
* 企业微信的接口类
|
||||
*
|
||||
* @版本: V3.1.0
|
||||
* @版权: 深圳市乐程软件有限公司(http://www.szlecheng.cn)
|
||||
* @作者: 云筑产品开发平台组
|
||||
* @日期: 2021/4/21 8:20
|
||||
*/
|
||||
public class QyWebChatUtil {
|
||||
|
||||
/**
|
||||
* 获取企业微信TOKEN的接口路径
|
||||
*/
|
||||
public static final String TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
|
||||
|
||||
/**
|
||||
* 往企业微信发送消息的接口路径
|
||||
*/
|
||||
public static final String SEND_MESSAGE = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s";
|
||||
|
||||
|
||||
/**
|
||||
* 获取接口访问凭证
|
||||
*/
|
||||
public static JSONObject getAccessToken(String corpId, String corpSecret) {
|
||||
JSONObject retMsg = new JSONObject();
|
||||
JSONObject rstObj = HttpUtil.httpRequest(String.format(TOKEN,corpId, corpSecret), "GET", null);
|
||||
// JSONObject rstObj = HttpUtil.httpsRequest(QyApi.getTokenUrl(corpId, corpSecret), "GET", null);
|
||||
return rstObj;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送消息 20210416 Add By GongXishan
|
||||
* 不抛出异常,返回Json
|
||||
*/
|
||||
public static JSONObject sendMessage(String message, String accessToken){
|
||||
JSONObject retMsg = new JSONObject();
|
||||
boolean codeFlag = true;
|
||||
String errorMsg = "";
|
||||
JSONObject rstObj = HttpUtil.httpRequest(String.format(SEND_MESSAGE, accessToken), "POST", message);
|
||||
// JSONObject rstObj = HttpUtil.httpsRequest(QyApi.sendMessage(accessToken), "POST", message);
|
||||
if (HttpUtil.isWxError(rstObj)) {
|
||||
codeFlag = false;
|
||||
errorMsg = rstObj.toString();
|
||||
}
|
||||
retMsg.put("code",codeFlag);
|
||||
retMsg.put("error",errorMsg);
|
||||
return retMsg;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 向企业微信发送信息
|
||||
* @param corpId
|
||||
* @param corpSecret
|
||||
* @param agentId
|
||||
* @param toUserId
|
||||
* @param contents
|
||||
* @return
|
||||
*/
|
||||
public static JSONObject sendWxMessage(String corpId,String corpSecret,String agentId,String toUserId,String contents) {
|
||||
JSONObject retMsg = null;
|
||||
JSONObject message = null;
|
||||
JSONObject tokenObject = null;
|
||||
JSONObject content = null;
|
||||
|
||||
message = new JSONObject();
|
||||
message.put("touser", toUserId);
|
||||
message.put("agentid", agentId);
|
||||
content = new JSONObject();
|
||||
content.put("content", contents);
|
||||
message.put("text", content);
|
||||
message.put("msgtype", "text");
|
||||
tokenObject = getAccessToken(corpId, corpSecret);
|
||||
if(tokenObject.getString("access_token")!=null && !"".equals(tokenObject.getString("access_token"))){
|
||||
retMsg = sendMessage(message.toJSONString(), tokenObject.getString("access_token"));
|
||||
}else
|
||||
{
|
||||
retMsg.put("code",false);
|
||||
retMsg.put("error","access_token值为空,不能发送信息!");
|
||||
}
|
||||
return retMsg;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user