mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
Merge remote-tracking branch 'wechat/develop' into develop
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -6,7 +6,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>wx-java</artifactId>
|
||||
<version>3.9.2.B</version>
|
||||
<version>3.9.3.B</version>
|
||||
<packaging>pom</packaging>
|
||||
<name>WxJava - Weixin/Wechat Java SDK</name>
|
||||
<description>微信开发Java SDK</description>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>wx-java</artifactId>
|
||||
<version>3.9.2.B</version>
|
||||
<version>3.9.3.B</version>
|
||||
</parent>
|
||||
<packaging>pom</packaging>
|
||||
<artifactId>wx-java-spring-boot-starters</artifactId>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>wx-java-spring-boot-starters</artifactId>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<version>3.9.2.B</version>
|
||||
<version>3.9.3.B</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>wx-java-spring-boot-starters</artifactId>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<version>3.9.2.B</version>
|
||||
<version>3.9.3.B</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>wx-java-spring-boot-starters</artifactId>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<version>3.9.2.B</version>
|
||||
<version>3.9.3.B</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>wx-java-spring-boot-starters</artifactId>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<version>3.9.2.B</version>
|
||||
<version>3.9.3.B</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>wx-java</artifactId>
|
||||
<version>3.9.2.B</version>
|
||||
<version>3.9.3.B</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>weixin-graal</artifactId>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>wx-java</artifactId>
|
||||
<version>3.9.2.B</version>
|
||||
<version>3.9.3.B</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>weixin-java-common</artifactId>
|
||||
|
||||
@@ -464,11 +464,200 @@ public enum WxMaErrorMsgEnum {
|
||||
|
||||
CODE_85004(85004, "微信号已经绑定"),
|
||||
|
||||
/**
|
||||
* 53010
|
||||
* 名称格式不合法
|
||||
*/
|
||||
CODE_53010(53010, "名称格式不合法"),
|
||||
|
||||
/**
|
||||
* 53011
|
||||
* 名称检测命中频率限制
|
||||
*/
|
||||
CODE_53011(53011, "名称检测命中频率限制"),
|
||||
|
||||
/**
|
||||
* 53012
|
||||
* 禁止使用该名称
|
||||
*/
|
||||
CODE_53012(53012, "禁止使用该名称"),
|
||||
|
||||
/**
|
||||
* 53013
|
||||
* 公众号:名称与已有公众号名称重复;小程序:该名称与已有小程序名称重复
|
||||
*/
|
||||
CODE_53013(53013, "公众号:名称与已有公众号名称重复;小程序:该名称与已有小程序名称重复"),
|
||||
|
||||
/**
|
||||
* 53014
|
||||
* 公众号:公众号已有{名称 A+}时,需与该帐号相同主体才可申请{名称 A};小程序:小程序已有{名称 A+}时,需与该帐号相同主体才可申请{名称 A}
|
||||
*/
|
||||
CODE_53014(53014, "公众号:公众号已有{名称 A+}时,需与该帐号相同主体才可申请{名称 A};小程序:小程序已有{名称 A+}时,需与该帐号相同主体才可申请{名称 A}"),
|
||||
|
||||
/**
|
||||
* 53015
|
||||
* 公众号:该名称与已有小程序名称重复,需与该小程序帐号相同主体才可申请;小程序:该名称与已有公众号名称重复,需与该公众号帐号相同主体才可申请
|
||||
*/
|
||||
CODE_53015(53015, "公众号:该名称与已有小程序名称重复,需与该小程序帐号相同主体才可申请;小程序:该名称与已有公众号名称重复,需与该公众号帐号相同主体才可申请"),
|
||||
|
||||
/**
|
||||
* 53016
|
||||
* 公众号:该名称与已有多个小程序名称重复,暂不支持申请;小程序:该名称与已有多个公众号名称重复,暂不支持申请
|
||||
*/
|
||||
CODE_53016(53016, "公众号:该名称与已有多个小程序名称重复,暂不支持申请;小程序:该名称与已有多个公众号名称重复,暂不支持申请"),
|
||||
|
||||
/**
|
||||
* 53017
|
||||
* 公众号:小程序已有{名称 A+}时,需与该帐号相同主体才可申请{名称 A};小程序:公众号已有{名称 A+}时,需与该帐号相同主体才可申请{名称 A}
|
||||
*/
|
||||
CODE_53017(53017, "公众号:小程序已有{名称 A+}时,需与该帐号相同主体才可申请{名称 A};小程序:公众号已有{名称 A+}时,需与该帐号相同主体才可申请{名称 A}"),
|
||||
|
||||
/**
|
||||
* 53018
|
||||
* 名称命中微信号
|
||||
*/
|
||||
CODE_53018(53018, "名称命中微信号"),
|
||||
|
||||
/**
|
||||
* 53019
|
||||
* 名称在保护期内
|
||||
*/
|
||||
CODE_53019(53019, "名称在保护期内"),
|
||||
|
||||
/**
|
||||
* 61070
|
||||
* 法人姓名与微信号不一致 name, wechat name not in accordance
|
||||
*/
|
||||
CODE_61070(61070, "法人姓名与微信号不一致"),
|
||||
|
||||
/**
|
||||
* 85015
|
||||
* 该账号不是小程序账号
|
||||
*/
|
||||
CODE_85015(85015, "该账号不是小程序账号"),
|
||||
|
||||
/**
|
||||
* 85066
|
||||
* 链接错误
|
||||
*/
|
||||
CODE_85066(85066, "链接错误"),
|
||||
|
||||
/**
|
||||
* 85068
|
||||
* 测试链接不是子链接
|
||||
*/
|
||||
CODE_85068(85068, "测试链接不是子链接"),
|
||||
|
||||
/**
|
||||
* 85069
|
||||
* 校验文件失败
|
||||
*/
|
||||
CODE_85069(85069, "校验文件失败"),
|
||||
|
||||
/**
|
||||
* 85070
|
||||
* 个人类型小程序无法设置二维码规则
|
||||
*/
|
||||
CODE_85070(85070, "个人类型小程序无法设置二维码规则"),
|
||||
|
||||
/**
|
||||
* 85071
|
||||
* 已添加该链接,请勿重复添加
|
||||
*/
|
||||
CODE_85071(85071, "已添加该链接,请勿重复添加"),
|
||||
|
||||
/**
|
||||
* 85072
|
||||
* 该链接已被占用
|
||||
*/
|
||||
CODE_85072(85072, "该链接已被占用"),
|
||||
|
||||
/**
|
||||
* 85073
|
||||
* 二维码规则已满
|
||||
*/
|
||||
CODE_85073(85073, "二维码规则已满"),
|
||||
|
||||
/**
|
||||
* 85074
|
||||
* 小程序未发布, 小程序必须先发布代码才可以发布二维码跳转规则
|
||||
*/
|
||||
CODE_85074(85074, "小程序未发布, 小程序必须先发布代码才可以发布二维码跳转规则"),
|
||||
|
||||
/**
|
||||
* 85075
|
||||
* 个人类型小程序无法设置二维码规则
|
||||
*/
|
||||
CODE_85075(85075, "个人类型小程序无法设置二维码规则"),
|
||||
|
||||
/**
|
||||
* 86004
|
||||
* 无效微信号 invalid wechat
|
||||
*/
|
||||
CODE_86004(86004, "无效微信号"),
|
||||
|
||||
/**
|
||||
* 89247
|
||||
* 内部错误 inner error
|
||||
*/
|
||||
CODE_89247(89247, "内部错误"),
|
||||
|
||||
/**
|
||||
* 89248
|
||||
* 企业代码类型无效,请选择正确类型填写 invalid code_type type
|
||||
*/
|
||||
CODE_89248(89248, "企业代码类型无效,请选择正确类型填写"),
|
||||
|
||||
/**
|
||||
* 89249
|
||||
* 该主体已有任务执行中,距上次任务 24h 后再试 task running
|
||||
*/
|
||||
CODE_89249(89249, "该主体已有任务执行中,距上次任务 24h 后再试"),
|
||||
|
||||
/**
|
||||
* 89250
|
||||
* 未找到该任务 task not found
|
||||
*/
|
||||
CODE_89250(89250, "未找到该任务"),
|
||||
|
||||
|
||||
/**
|
||||
* 89251
|
||||
* 待法人人脸核身校验 legal person checking
|
||||
*/
|
||||
CODE_89251(89251, "待法人人脸核身校验"),
|
||||
|
||||
/**
|
||||
* 89252
|
||||
* 法人&企业信息一致性校验中 front checking
|
||||
*/
|
||||
CODE_89252(89252, "法人&企业信息一致性校验中"),
|
||||
|
||||
/**
|
||||
* 89253
|
||||
* 缺少参数 lack of some params
|
||||
*/
|
||||
CODE_89253(89253, "缺少参数s"),
|
||||
|
||||
|
||||
/**
|
||||
* 89254
|
||||
* 第三方权限集不全,补全权限集全网发布后生效 lack of some component rights
|
||||
*/
|
||||
CODE_89254(89254, "第三方权限集不全,补全权限集全网发布后生效"),
|
||||
|
||||
/**
|
||||
* 89255
|
||||
* code参数无效,请检查code长度以及内容是否正确 code参数无效,请检查code长度以及内容是否正确_;
|
||||
* 注意code_type的值不同需要传的code长度不一样 ;注意code_type的值不同需要传的code长度不一样 enterprise code_invalid invalid
|
||||
*/
|
||||
CODE_89255(89255, "code参数无效,请检查code长度以及内容是否正确_;注意code_type的值不同需要传的code长度不一样 ;注意code_type的值不同需要传的code长度不一样"),
|
||||
|
||||
// CODE_504002(-504002, "云函数未找到 Function not found"),
|
||||
;
|
||||
|
||||
private int code;
|
||||
private String msg;
|
||||
private final int code;
|
||||
private final String msg;
|
||||
|
||||
WxMaErrorMsgEnum(int code, String msg) {
|
||||
this.code = code;
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
package me.chanjar.weixin.common.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@Slf4j
|
||||
public class LogExceptionHandler implements WxErrorExceptionHandler {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(WxErrorExceptionHandler.class);
|
||||
|
||||
@Override
|
||||
public void handle(WxErrorException e) {
|
||||
|
||||
this.log.error("Error happens", e);
|
||||
|
||||
log.error("Error happens", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>wx-java</artifactId>
|
||||
<version>3.9.2.B</version>
|
||||
<version>3.9.3.B</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>weixin-java-cp</artifactId>
|
||||
|
||||
@@ -132,6 +132,20 @@ public interface WxCpExternalContactService {
|
||||
*/
|
||||
WxCpUserExternalContactInfo getContactDetail(String userId) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 修改客户备注信息.
|
||||
* <pre>
|
||||
* 企业可通过此接口修改指定用户添加的客户的备注信息。
|
||||
* 请求方式: POST(HTTP)
|
||||
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/externalcontact/remark?access_token=ACCESS_TOKEN
|
||||
* 文档地址:https://work.weixin.qq.com/api/doc/90000/90135/92115
|
||||
* </pre>
|
||||
*
|
||||
* @param request 备注信息请求
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
void updateRemark(WxCpUpdateRemarkRequest request) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 获取客户列表.
|
||||
* <pre>
|
||||
|
||||
@@ -31,7 +31,7 @@ public interface WxCpGroupRobotService {
|
||||
* @param content markdown内容,最长不超过4096个字节,必须是utf8编码
|
||||
* @throws WxErrorException 异常
|
||||
*/
|
||||
void sendMarkDown(String content) throws WxErrorException;
|
||||
void sendMarkdown(String content) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 发送image类型的消息
|
||||
@@ -49,4 +49,43 @@ public interface WxCpGroupRobotService {
|
||||
* @throws WxErrorException 异常
|
||||
*/
|
||||
void sendNews(List<NewArticle> articleList) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 发送text类型的消息
|
||||
*
|
||||
* @param webhookUrl webhook地址
|
||||
* @param content 文本内容,最长不超过2048个字节,必须是utf8编码
|
||||
* @param mentionedList userId的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userId,可以使用mentioned_mobile_list
|
||||
* @param mobileList 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人
|
||||
* @throws WxErrorException 异常
|
||||
*/
|
||||
void sendText(String webhookUrl, String content, List<String> mentionedList, List<String> mobileList) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 发送markdown类型的消息
|
||||
*
|
||||
* @param webhookUrl webhook地址
|
||||
* @param content markdown内容,最长不超过4096个字节,必须是utf8编码
|
||||
* @throws WxErrorException 异常
|
||||
*/
|
||||
void sendMarkdown(String webhookUrl, String content) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 发送image类型的消息
|
||||
*
|
||||
* @param webhookUrl webhook地址
|
||||
* @param base64 图片内容的base64编码
|
||||
* @param md5 图片内容(base64编码前)的md5值
|
||||
* @throws WxErrorException 异常
|
||||
*/
|
||||
void sendImage(String webhookUrl, String base64, String md5) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 发送news类型的消息
|
||||
*
|
||||
* @param webhookUrl webhook地址
|
||||
* @param articleList 图文消息,支持1到8条图文
|
||||
* @throws WxErrorException 异常
|
||||
*/
|
||||
void sendNews(String webhookUrl, List<NewArticle> articleList) throws WxErrorException;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.cp.bean.oa.calendar.WxCpOaCalendar;
|
||||
|
||||
/**
|
||||
* 企业微信日历接口.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @date 2020-09-20
|
||||
*/
|
||||
public interface WxCpOaCalendarService {
|
||||
/**
|
||||
* 创建日历.
|
||||
* <pre>
|
||||
* 该接口用于通过应用在企业内创建一个日历。
|
||||
* 注: 企业微信需要更新到3.0.2及以上版本
|
||||
* 请求方式: POST(HTTPS)
|
||||
* 请求地址: https://qyapi.weixin.qq.com/cgi-bin/oa/calendar/add?access_token=ACCESS_TOKEN
|
||||
*
|
||||
* 文档地址:https://work.weixin.qq.com/api/doc/90000/90135/92618
|
||||
* </pre>
|
||||
*
|
||||
* @param calendar 日历对象
|
||||
* @return 日历ID
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
String add(WxCpOaCalendar calendar) throws WxErrorException;
|
||||
}
|
||||
@@ -388,7 +388,14 @@ public interface WxCpService {
|
||||
*
|
||||
* @return the oa service
|
||||
*/
|
||||
WxCpOaService getOAService();
|
||||
WxCpOaService getOaService();
|
||||
|
||||
/**
|
||||
* 获取日历相关接口的服务类对象
|
||||
*
|
||||
* @return the menu service
|
||||
*/
|
||||
WxCpOaCalendarService getOaCalendarService();
|
||||
|
||||
/**
|
||||
* 获取群机器人消息推送服务
|
||||
@@ -445,4 +452,5 @@ public interface WxCpService {
|
||||
* @param tagService the tag service
|
||||
*/
|
||||
void setTagService(WxCpTagService tagService);
|
||||
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
|
||||
private WxCpExternalContactService externalContactService = new WxCpExternalContactServiceImpl(this);
|
||||
private WxCpGroupRobotService groupRobotService = new WxCpGroupRobotServiceImpl(this);
|
||||
private WxCpMessageService messageService = new WxCpMessageServiceImpl(this);
|
||||
private WxCpOaCalendarService oaCalendarService = new WxCpOaCalendarServiceImpl(this);
|
||||
|
||||
/**
|
||||
* 全局的是否正在刷新access token的锁.
|
||||
@@ -305,7 +306,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uri, data, e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).errorCode(-1).build(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -421,10 +422,15 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxCpOaService getOAService() {
|
||||
public WxCpOaService getOaService() {
|
||||
return oaService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxCpOaCalendarService getOaCalendarService() {
|
||||
return this.oaCalendarService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxCpGroupRobotService getGroupRobotService() {
|
||||
return groupRobotService;
|
||||
|
||||
@@ -8,7 +8,7 @@ import me.chanjar.weixin.common.error.WxCpErrorMsgEnum;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.cp.api.WxCpExternalContactService;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
import me.chanjar.weixin.cp.bean.*;
|
||||
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
|
||||
import me.chanjar.weixin.cp.bean.external.*;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -67,7 +67,7 @@ public class WxCpExternalContactServiceImpl implements WxCpExternalContactServic
|
||||
@Override
|
||||
public WxCpBaseResp deleteContactWay(@NonNull String configId) throws WxErrorException {
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("config_id",configId);
|
||||
json.addProperty("config_id", configId);
|
||||
|
||||
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(DEL_CONTACT_WAY);
|
||||
String responseContent = this.mainService.post(url, json.toString());
|
||||
@@ -79,8 +79,8 @@ public class WxCpExternalContactServiceImpl implements WxCpExternalContactServic
|
||||
public WxCpBaseResp closeTempChat(@NonNull String userId, @NonNull String externalUserId) throws WxErrorException {
|
||||
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("userid",userId);
|
||||
json.addProperty("external_userid",externalUserId);
|
||||
json.addProperty("userid", userId);
|
||||
json.addProperty("external_userid", externalUserId);
|
||||
|
||||
|
||||
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(CLOSE_TEMP_CHAT);
|
||||
@@ -103,6 +103,12 @@ public class WxCpExternalContactServiceImpl implements WxCpExternalContactServic
|
||||
return WxCpUserExternalContactInfo.fromJson(responseContent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateRemark(WxCpUpdateRemarkRequest request) throws WxErrorException {
|
||||
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(UPDATE_REMARK);
|
||||
this.mainService.post(url, request.toJson());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> listExternalContacts(String userId) throws WxErrorException {
|
||||
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(LIST_EXTERNAL_CONTACT + userId);
|
||||
@@ -233,66 +239,66 @@ public class WxCpExternalContactServiceImpl implements WxCpExternalContactServic
|
||||
@Override
|
||||
public WxCpUserExternalTagGroupList getCorpTagList(String[] tagId) throws WxErrorException {
|
||||
JsonObject json = new JsonObject();
|
||||
if(ArrayUtils.isNotEmpty(tagId)){
|
||||
json.add("tag_id",new Gson().toJsonTree(tagId).getAsJsonArray());
|
||||
if (ArrayUtils.isNotEmpty(tagId)) {
|
||||
json.add("tag_id", new Gson().toJsonTree(tagId).getAsJsonArray());
|
||||
}
|
||||
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(GET_CORP_TAG_LIST);
|
||||
final String result = this.mainService.post(url,json.toString());
|
||||
final String result = this.mainService.post(url, json.toString());
|
||||
return WxCpUserExternalTagGroupList.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxCpUserExternalTagGroupInfo addCorpTag(WxCpUserExternalTagGroupInfo tagGroup) throws WxErrorException{
|
||||
public WxCpUserExternalTagGroupInfo addCorpTag(WxCpUserExternalTagGroupInfo tagGroup) throws WxErrorException {
|
||||
|
||||
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(ADD_CORP_TAG);
|
||||
final String result = this.mainService.post(url,tagGroup.getTagGroup().toJson());
|
||||
final String result = this.mainService.post(url, tagGroup.getTagGroup().toJson());
|
||||
return WxCpUserExternalTagGroupInfo.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxCpBaseResp editCorpTag(String id, String name, Integer order) throws WxErrorException{
|
||||
public WxCpBaseResp editCorpTag(String id, String name, Integer order) throws WxErrorException {
|
||||
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("id",id);
|
||||
json.addProperty("name",name);
|
||||
json.addProperty("order",order);
|
||||
json.addProperty("id", id);
|
||||
json.addProperty("name", name);
|
||||
json.addProperty("order", order);
|
||||
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(EDIT_CORP_TAG);
|
||||
final String result = this.mainService.post(url,json.toString());
|
||||
final String result = this.mainService.post(url, json.toString());
|
||||
return WxCpBaseResp.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxCpBaseResp delCorpTag(String[] tagId, String[] groupId) throws WxErrorException{
|
||||
public WxCpBaseResp delCorpTag(String[] tagId, String[] groupId) throws WxErrorException {
|
||||
JsonObject json = new JsonObject();
|
||||
if(ArrayUtils.isNotEmpty(tagId)){
|
||||
json.add("tag_id",new Gson().toJsonTree(tagId).getAsJsonArray());
|
||||
if (ArrayUtils.isNotEmpty(tagId)) {
|
||||
json.add("tag_id", new Gson().toJsonTree(tagId).getAsJsonArray());
|
||||
}
|
||||
if(ArrayUtils.isNotEmpty(groupId)){
|
||||
json.add("group_id",new Gson().toJsonTree(groupId).getAsJsonArray());
|
||||
if (ArrayUtils.isNotEmpty(groupId)) {
|
||||
json.add("group_id", new Gson().toJsonTree(groupId).getAsJsonArray());
|
||||
}
|
||||
|
||||
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(DEL_CORP_TAG);
|
||||
final String result = this.mainService.post(url,json.toString());
|
||||
final String result = this.mainService.post(url, json.toString());
|
||||
return WxCpBaseResp.fromJson(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxCpBaseResp markTag(String userid, String externalUserid, String[] addTag, String[] removeTag)throws WxErrorException{
|
||||
public WxCpBaseResp markTag(String userid, String externalUserid, String[] addTag, String[] removeTag) throws WxErrorException {
|
||||
|
||||
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("userid",userid);
|
||||
json.addProperty("external_userid",externalUserid);
|
||||
json.addProperty("userid", userid);
|
||||
json.addProperty("external_userid", externalUserid);
|
||||
|
||||
if(ArrayUtils.isNotEmpty(addTag)){
|
||||
json.add("add_tag",new Gson().toJsonTree(addTag).getAsJsonArray());
|
||||
if (ArrayUtils.isNotEmpty(addTag)) {
|
||||
json.add("add_tag", new Gson().toJsonTree(addTag).getAsJsonArray());
|
||||
}
|
||||
if(ArrayUtils.isNotEmpty(removeTag)){
|
||||
json.add("remove_tag",new Gson().toJsonTree(removeTag).getAsJsonArray());
|
||||
if (ArrayUtils.isNotEmpty(removeTag)) {
|
||||
json.add("remove_tag", new Gson().toJsonTree(removeTag).getAsJsonArray());
|
||||
}
|
||||
|
||||
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(MARK_TAG);
|
||||
final String result = this.mainService.post(url,json.toString());
|
||||
final String result = this.mainService.post(url, json.toString());
|
||||
return WxCpBaseResp.fromJson(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package me.chanjar.weixin.cp.api.impl;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.cp.api.WxCpGroupRobotService;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
@@ -8,14 +9,16 @@ import me.chanjar.weixin.cp.bean.article.NewArticle;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpGroupRobotMessage;
|
||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
||||
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType;
|
||||
import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType.*;
|
||||
import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType.MARKDOWN;
|
||||
import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType.TEXT;
|
||||
|
||||
/**
|
||||
* 微信群机器人消息发送api 实现
|
||||
* 企业微信群机器人消息发送api 实现
|
||||
*
|
||||
* @author yr
|
||||
* @date 2020-08-20
|
||||
@@ -24,44 +27,66 @@ import static me.chanjar.weixin.cp.constant.WxCpConsts.GroupRobotMsgType.*;
|
||||
public class WxCpGroupRobotServiceImpl implements WxCpGroupRobotService {
|
||||
private final WxCpService cpService;
|
||||
|
||||
private String getApiUrl() {
|
||||
WxCpConfigStorage wxCpConfigStorage = cpService.getWxCpConfigStorage();
|
||||
return wxCpConfigStorage.getApiUrl(WxCpApiPathConsts.WEBHOOK_SEND) + wxCpConfigStorage.getWebhookKey();
|
||||
private String getWebhookUrl() throws WxErrorException {
|
||||
WxCpConfigStorage wxCpConfigStorage = this.cpService.getWxCpConfigStorage();
|
||||
final String webhookKey = wxCpConfigStorage.getWebhookKey();
|
||||
if (StringUtils.isEmpty(webhookKey)) {
|
||||
throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("请先设置WebhookKey").build());
|
||||
}
|
||||
return wxCpConfigStorage.getApiUrl(WxCpApiPathConsts.WEBHOOK_SEND) + webhookKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendText(String content, List<String> mentionedList, List<String> mobileList) throws WxErrorException {
|
||||
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage()
|
||||
.setMsgType(TEXT)
|
||||
.setContent(content)
|
||||
.setMentionedList(mentionedList)
|
||||
.setMentionedMobileList(mobileList);
|
||||
cpService.postWithoutToken(this.getApiUrl(), message.toJson());
|
||||
this.sendText(this.getWebhookUrl(), content, mentionedList, mobileList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMarkDown(String content) throws WxErrorException {
|
||||
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage()
|
||||
.setMsgType(MARKDOWN)
|
||||
.setContent(content);
|
||||
cpService.postWithoutToken(this.getApiUrl(), message.toJson());
|
||||
public void sendMarkdown(String content) throws WxErrorException {
|
||||
this.sendMarkdown(this.getWebhookUrl(), content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendImage(String base64, String md5) throws WxErrorException {
|
||||
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage()
|
||||
.setMsgType(GroupRobotMsgType.IMAGE)
|
||||
.setBase64(base64)
|
||||
.setMd5(md5);
|
||||
cpService.postWithoutToken(this.getApiUrl(), message.toJson());
|
||||
this.sendImage(this.getWebhookUrl(), base64, md5);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendNews(List<NewArticle> articleList) throws WxErrorException {
|
||||
WxCpGroupRobotMessage message = new WxCpGroupRobotMessage()
|
||||
this.sendNews(this.getWebhookUrl(), articleList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendText(String webhookUrl, String content, List<String> mentionedList, List<String> mobileList) throws WxErrorException {
|
||||
this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
|
||||
.setMsgType(TEXT)
|
||||
.setContent(content)
|
||||
.setMentionedList(mentionedList)
|
||||
.setMentionedMobileList(mobileList)
|
||||
.toJson());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMarkdown(String webhookUrl, String content) throws WxErrorException {
|
||||
this.cpService.postWithoutToken(webhookUrl, new WxCpGroupRobotMessage()
|
||||
.setMsgType(MARKDOWN)
|
||||
.setContent(content)
|
||||
.toJson());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendImage(String webhookUrl, String base64, String md5) throws WxErrorException {
|
||||
this.cpService.postWithoutToken(this.getWebhookUrl(), new WxCpGroupRobotMessage()
|
||||
.setMsgType(GroupRobotMsgType.IMAGE)
|
||||
.setBase64(base64)
|
||||
.setMd5(md5).toJson());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendNews(String webhookUrl, List<NewArticle> articleList) throws WxErrorException {
|
||||
this.cpService.postWithoutToken(this.getWebhookUrl(), new WxCpGroupRobotMessage()
|
||||
.setMsgType(GroupRobotMsgType.NEWS)
|
||||
.setArticles(articleList);
|
||||
cpService.postWithoutToken(this.getApiUrl(), message.toJson());
|
||||
.setArticles(articleList).toJson());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package me.chanjar.weixin.cp.api.impl;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.cp.api.WxCpOaCalendarService;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
import me.chanjar.weixin.cp.bean.oa.calendar.WxCpOaCalendar;
|
||||
|
||||
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Oa.CALENDAR_ADD;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @date 2020-09-20
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class WxCpOaCalendarServiceImpl implements WxCpOaCalendarService {
|
||||
private final WxCpService wxCpService;
|
||||
|
||||
@Override
|
||||
public String add(WxCpOaCalendar calendar) throws WxErrorException {
|
||||
return this.wxCpService.post(this.wxCpService.getWxCpConfigStorage().getApiUrl(CALENDAR_ADD),calendar.toJson());
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package me.chanjar.weixin.cp.api.impl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.cp.api.WxCpTpService;
|
||||
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
||||
101
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/WxCpUpdateRemarkRequest.java
vendored
Normal file
101
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/WxCpUpdateRemarkRequest.java
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
package me.chanjar.weixin.cp.bean.external;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 修改客户备注信息请求.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @date 2020-09-19
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Accessors(chain = true)
|
||||
public class WxCpUpdateRemarkRequest implements Serializable {
|
||||
private static final long serialVersionUID = -4960239393895754138L;
|
||||
|
||||
public String toJson() {
|
||||
return WxCpGsonBuilder.create().toJson(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:userid
|
||||
* 是否必须:是
|
||||
* 描述:企业成员的userid
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName("userid")
|
||||
private String userId;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:external_userid
|
||||
* 是否必须:是
|
||||
* 描述:外部联系人userid
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName("external_userid")
|
||||
private String externalUserId;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:remark
|
||||
* 是否必须:否
|
||||
* 描述:此用户对外部联系人的备注,最多20个字符
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName("remark")
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:description
|
||||
* 是否必须:否
|
||||
* 描述:此用户对外部联系人的描述,最多150个字符
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName("description")
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:remark_company
|
||||
* 是否必须:否
|
||||
* 描述:此用户对外部联系人备注的所属公司名称,最多20个字符
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName("remark_company")
|
||||
private String remarkCompany;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:remark_mobiles
|
||||
* 是否必须:否
|
||||
* 描述:此用户对外部联系人备注的手机号
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName("remark_mobiles")
|
||||
private String[] remarkMobiles;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:remark_pic_mediaid
|
||||
* 是否必须:否
|
||||
* 描述:备注图片的mediaid,
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName("remark_pic_mediaid")
|
||||
private String remarkPicMediaId;
|
||||
|
||||
}
|
||||
@@ -294,6 +294,20 @@ public class WxCpXmlMessage implements Serializable {
|
||||
@XStreamConverter(value = XStreamCDataConverter.class)
|
||||
private String address;
|
||||
|
||||
/**
|
||||
* 日程ID.
|
||||
*/
|
||||
@XStreamAlias("ScheduleId")
|
||||
@XStreamConverter(value = XStreamCDataConverter.class)
|
||||
private String scheduleId;
|
||||
|
||||
/**
|
||||
* 日历ID.
|
||||
*/
|
||||
@XStreamAlias("CalId")
|
||||
@XStreamConverter(value = XStreamCDataConverter.class)
|
||||
private String calId;
|
||||
|
||||
/**
|
||||
* 扩展属性.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package me.chanjar.weixin.cp.bean.oa.calendar;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 日历.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @date 2020-09-20
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Accessors(chain = true)
|
||||
public class WxCpOaCalendar implements Serializable {
|
||||
private static final long serialVersionUID = -817988838579546989L;
|
||||
|
||||
/**
|
||||
* 变量名:organizer
|
||||
* 是否必须:是
|
||||
* 描述:指定的组织者userid。注意该字段指定后不可更新
|
||||
*/
|
||||
@SerializedName("organizer")
|
||||
private String organizer;
|
||||
|
||||
/**
|
||||
* 变量名:readonly
|
||||
* 是否必须:否
|
||||
* 描述:日历组织者对日历是否只读权限(即不可编辑日历,不可在日历上添加日程,仅可作为组织者删除日历)。0-否;1-是。默认为1,即只读
|
||||
*/
|
||||
@SerializedName("readonly")
|
||||
private Integer readonly;
|
||||
|
||||
/**
|
||||
* 变量名:set_as_default
|
||||
* 是否必须:否
|
||||
* 描述:是否将该日历设置为组织者的默认日历。0-否;1-是。默认为0,即不设为默认日历
|
||||
*/
|
||||
@SerializedName("set_as_default")
|
||||
private Integer setAsDefault;
|
||||
|
||||
/**
|
||||
* 变量名:summary
|
||||
* 是否必须:是
|
||||
* 描述:日历标题。1 ~ 128 字符
|
||||
*/
|
||||
@SerializedName("summary")
|
||||
private String summary;
|
||||
|
||||
/**
|
||||
* 变量名:color
|
||||
* 是否必须:是
|
||||
* 描述:日历在终端上显示的颜色,RGB颜色编码16进制表示,例如:”#0000FF” 表示纯蓝色
|
||||
*/
|
||||
@SerializedName("color")
|
||||
private String color;
|
||||
|
||||
/**
|
||||
* 变量名:description
|
||||
* 是否必须:否
|
||||
* 描述:日历描述。0 ~ 512 字符
|
||||
*/
|
||||
@SerializedName("description")
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 变量名:shares
|
||||
* 是否必须:否
|
||||
* 描述:日历共享成员列表。最多2000人
|
||||
*/
|
||||
@SerializedName("shares")
|
||||
private List<ShareInfo> shares;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class ShareInfo implements Serializable {
|
||||
private static final long serialVersionUID = -4882781114860754679L;
|
||||
|
||||
/**
|
||||
* 日历共享成员的id
|
||||
*/
|
||||
private String userid;
|
||||
|
||||
/**
|
||||
* 共享成员对日历是否只读权限(即不可编辑日历,不可在日历上添加日程,仅可以退出日历)。
|
||||
* 0-否;1-是。默认为1,即只读
|
||||
*/
|
||||
private Integer readonly;
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
return WxCpGsonBuilder.create().toJson(ImmutableMap.of("calendar",this));
|
||||
}
|
||||
}
|
||||
@@ -101,6 +101,11 @@ public final class WxCpApiPathConsts {
|
||||
public static final String GET_DIAL_RECORD = "/cgi-bin/dial/get_dial_record";
|
||||
public static final String GET_TEMPLATE_DETAIL = "/cgi-bin/oa/gettemplatedetail";
|
||||
public static final String APPLY_EVENT = "/cgi-bin/oa/applyevent";
|
||||
|
||||
public static final String CALENDAR_ADD = "/cgi-bin/oa/calendar/add";
|
||||
public static final String CALENDAR_UPDATE = "/cgi-bin/oa/calendar/update";
|
||||
public static final String CALENDAR_GET = "/cgi-bin/oa/calendar/get";
|
||||
public static final String CALENDAR_DEL = "/cgi-bin/oa/calendar/del";
|
||||
}
|
||||
|
||||
@UtilityClass
|
||||
@@ -159,6 +164,7 @@ public final class WxCpApiPathConsts {
|
||||
public static final String CLOSE_TEMP_CHAT = "/cgi-bin/externalcontact/close_temp_chat";
|
||||
public static final String GET_FOLLOW_USER_LIST = "/cgi-bin/externalcontact/get_follow_user_list";
|
||||
public static final String GET_CONTACT_DETAIL = "/cgi-bin/externalcontact/get?external_userid=";
|
||||
public static final String UPDATE_REMARK = "/cgi-bin/externalcontact/remark";
|
||||
public static final String LIST_EXTERNAL_CONTACT = "/cgi-bin/externalcontact/list?userid=";
|
||||
public static final String LIST_UNASSIGNED_CONTACT = "/cgi-bin/externalcontact/get_unassigned_list";
|
||||
public static final String TRANSFER_UNASSIGNED_CONTACT = "/cgi-bin/externalcontact/transfer";
|
||||
|
||||
@@ -103,6 +103,30 @@ public class WxCpConsts {
|
||||
*/
|
||||
public static final String OPEN_APPROVAL_CHANGE = "open_approval_change";
|
||||
|
||||
/**
|
||||
* 修改日历事件
|
||||
*/
|
||||
public static final String MODIFY_CALENDAR = "modify_calendar";
|
||||
|
||||
/**
|
||||
* 删除日历事件
|
||||
*/
|
||||
public static final String DELETE_CALENDAR = "delete_calendar";
|
||||
|
||||
/**
|
||||
* 添加日程事件
|
||||
*/
|
||||
public static final String ADD_SCHEDULE = "add_schedule";
|
||||
|
||||
/**
|
||||
* 修改日程事件
|
||||
*/
|
||||
public static final String MODIFY_SCHEDULE = "modify_schedule";
|
||||
|
||||
/**
|
||||
* 删除日程事件
|
||||
*/
|
||||
public static final String DELETE_SCHEDULE = "delete_schedule";
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -16,11 +16,14 @@ import java.util.Map;
|
||||
public interface WxCpMessageHandler {
|
||||
|
||||
/**
|
||||
* @param wxMessage
|
||||
* Handle wx cp xml out message.
|
||||
*
|
||||
* @param wxMessage the wx message
|
||||
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
|
||||
* @param wxCpService
|
||||
* @param sessionManager
|
||||
* @return xml格式的消息,如果在异步规则里处理的话,可以返回null
|
||||
* @param wxCpService the wx cp service
|
||||
* @param sessionManager the session manager
|
||||
* @return xml格式的消息 ,如果在异步规则里处理的话,可以返回null
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
|
||||
Map<String, Object> context,
|
||||
|
||||
@@ -17,11 +17,12 @@ public interface WxCpMessageInterceptor {
|
||||
/**
|
||||
* 拦截微信消息
|
||||
*
|
||||
* @param wxMessage
|
||||
* @param wxMessage the wx message
|
||||
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
|
||||
* @param wxCpService
|
||||
* @param sessionManager
|
||||
* @return true代表OK,false代表不OK
|
||||
* @param wxCpService the wx cp service
|
||||
* @param sessionManager the session manager
|
||||
* @return true代表OK ,false代表不OK
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
boolean intercept(WxCpXmlMessage wxMessage,
|
||||
Map<String, Object> context,
|
||||
|
||||
@@ -4,11 +4,16 @@ import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
|
||||
|
||||
/**
|
||||
* 消息匹配器,用在消息路由的时候
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public interface WxCpMessageMatcher {
|
||||
|
||||
/**
|
||||
* 消息是否匹配某种模式
|
||||
*
|
||||
* @param message the message
|
||||
* @return the boolean
|
||||
*/
|
||||
boolean match(WxCpXmlMessage message);
|
||||
|
||||
|
||||
@@ -1,15 +1,7 @@
|
||||
package me.chanjar.weixin.cp.message;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
|
||||
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker;
|
||||
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
|
||||
@@ -20,6 +12,13 @@ import me.chanjar.weixin.common.util.LogExceptionHandler;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
@@ -49,9 +48,9 @@ import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@Slf4j
|
||||
public class WxCpMessageRouter {
|
||||
private static final int DEFAULT_THREAD_POOL_SIZE = 100;
|
||||
private final Logger log = LoggerFactory.getLogger(WxCpMessageRouter.class);
|
||||
private final List<WxCpMessageRouterRule> rules = new ArrayList<>();
|
||||
|
||||
private final WxCpService wxCpService;
|
||||
@@ -69,7 +68,9 @@ public class WxCpMessageRouter {
|
||||
*/
|
||||
public WxCpMessageRouter(WxCpService wxCpService) {
|
||||
this.wxCpService = wxCpService;
|
||||
this.executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE);
|
||||
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("WxCpMessageRouter-pool-%d").build();
|
||||
this.executorService = new ThreadPoolExecutor(DEFAULT_THREAD_POOL_SIZE, DEFAULT_THREAD_POOL_SIZE,
|
||||
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), namedThreadFactory);
|
||||
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
|
||||
this.sessionManager = wxCpService.getSessionManager();
|
||||
this.exceptionHandler = new LogExceptionHandler();
|
||||
@@ -156,37 +157,31 @@ public class WxCpMessageRouter {
|
||||
// 返回最后一个非异步的rule的执行结果
|
||||
if (rule.isAsync()) {
|
||||
futures.add(
|
||||
this.executorService.submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
rule.service(wxMessage, context, WxCpMessageRouter.this.wxCpService, WxCpMessageRouter.this.sessionManager, WxCpMessageRouter.this.exceptionHandler);
|
||||
}
|
||||
this.executorService.submit(() -> {
|
||||
rule.service(wxMessage, context, WxCpMessageRouter.this.wxCpService, WxCpMessageRouter.this.sessionManager, WxCpMessageRouter.this.exceptionHandler);
|
||||
})
|
||||
);
|
||||
} else {
|
||||
res = rule.service(wxMessage, context, this.wxCpService, this.sessionManager, this.exceptionHandler);
|
||||
// 在同步操作结束,session访问结束
|
||||
this.log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName());
|
||||
log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName());
|
||||
sessionEndAccess(wxMessage);
|
||||
}
|
||||
}
|
||||
|
||||
if (futures.size() > 0) {
|
||||
this.executorService.submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (Future future : futures) {
|
||||
try {
|
||||
future.get();
|
||||
WxCpMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName());
|
||||
// 异步操作结束,session访问结束
|
||||
sessionEndAccess(wxMessage);
|
||||
} catch (InterruptedException e) {
|
||||
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (ExecutionException e) {
|
||||
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e);
|
||||
}
|
||||
this.executorService.submit(() -> {
|
||||
for (Future future : futures) {
|
||||
try {
|
||||
future.get();
|
||||
log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName());
|
||||
// 异步操作结束,session访问结束
|
||||
sessionEndAccess(wxMessage);
|
||||
} catch (InterruptedException e) {
|
||||
log.error("Error happened when wait task finish", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (ExecutionException e) {
|
||||
log.error("Error happened when wait task finish", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -198,7 +193,7 @@ public class WxCpMessageRouter {
|
||||
* 处理微信消息.
|
||||
*/
|
||||
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
|
||||
return this.route(wxMessage, new HashMap<String, Object>(2));
|
||||
return this.route(wxMessage, new HashMap<>(2));
|
||||
}
|
||||
|
||||
private boolean isMsgDuplicated(WxCpXmlMessage wxMessage) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package me.chanjar.weixin.cp.message;
|
||||
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
@@ -8,14 +9,16 @@ import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* The type Wx cp message router rule.
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@Data
|
||||
public class WxCpMessageRouterRule {
|
||||
|
||||
private final WxCpMessageRouter routerBuilder;
|
||||
|
||||
private boolean async = true;
|
||||
@@ -44,6 +47,11 @@ public class WxCpMessageRouterRule {
|
||||
|
||||
private List<WxCpMessageInterceptor> interceptors = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Instantiates a new Wx cp message router rule.
|
||||
*
|
||||
* @param routerBuilder the router builder
|
||||
*/
|
||||
protected WxCpMessageRouterRule(WxCpMessageRouter routerBuilder) {
|
||||
this.routerBuilder = routerBuilder;
|
||||
}
|
||||
@@ -51,7 +59,8 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 设置是否异步执行,默认是true
|
||||
*
|
||||
* @param async
|
||||
* @param async the async
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule async(boolean async) {
|
||||
this.async = async;
|
||||
@@ -61,7 +70,8 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 如果agentId匹配
|
||||
*
|
||||
* @param agentId
|
||||
* @param agentId the agent id
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule agentId(Integer agentId) {
|
||||
this.agentId = agentId;
|
||||
@@ -71,7 +81,8 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 如果msgType等于某值
|
||||
*
|
||||
* @param msgType
|
||||
* @param msgType the msg type
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule msgType(String msgType) {
|
||||
this.msgType = msgType;
|
||||
@@ -81,7 +92,8 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 如果event等于某值
|
||||
*
|
||||
* @param event
|
||||
* @param event the event
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule event(String event) {
|
||||
this.event = event;
|
||||
@@ -91,7 +103,8 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 如果eventKey等于某值
|
||||
*
|
||||
* @param eventKey
|
||||
* @param eventKey the event key
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule eventKey(String eventKey) {
|
||||
this.eventKey = eventKey;
|
||||
@@ -100,6 +113,9 @@ public class WxCpMessageRouterRule {
|
||||
|
||||
/**
|
||||
* 如果eventKey匹配该正则表达式
|
||||
*
|
||||
* @param regex the regex
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule eventKeyRegex(String regex) {
|
||||
this.eventKeyRegex = regex;
|
||||
@@ -109,7 +125,8 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 如果content等于某值
|
||||
*
|
||||
* @param content
|
||||
* @param content the content
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule content(String content) {
|
||||
this.content = content;
|
||||
@@ -119,7 +136,8 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 如果content匹配该正则表达式
|
||||
*
|
||||
* @param regex
|
||||
* @param regex the regex
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule rContent(String regex) {
|
||||
this.rContent = regex;
|
||||
@@ -129,7 +147,8 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 如果fromUser等于某值
|
||||
*
|
||||
* @param fromUser
|
||||
* @param fromUser the from user
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule fromUser(String fromUser) {
|
||||
this.fromUser = fromUser;
|
||||
@@ -139,7 +158,8 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 如果消息匹配某个matcher,用在用户需要自定义更复杂的匹配规则的时候
|
||||
*
|
||||
* @param matcher
|
||||
* @param matcher the matcher
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule matcher(WxCpMessageMatcher matcher) {
|
||||
this.matcher = matcher;
|
||||
@@ -149,7 +169,8 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 设置微信消息拦截器
|
||||
*
|
||||
* @param interceptor
|
||||
* @param interceptor the interceptor
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule interceptor(WxCpMessageInterceptor interceptor) {
|
||||
return interceptor(interceptor, (WxCpMessageInterceptor[]) null);
|
||||
@@ -158,15 +179,14 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 设置微信消息拦截器
|
||||
*
|
||||
* @param interceptor
|
||||
* @param otherInterceptors
|
||||
* @param interceptor the interceptor
|
||||
* @param otherInterceptors the other interceptors
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule interceptor(WxCpMessageInterceptor interceptor, WxCpMessageInterceptor... otherInterceptors) {
|
||||
this.interceptors.add(interceptor);
|
||||
if (otherInterceptors != null && otherInterceptors.length > 0) {
|
||||
for (WxCpMessageInterceptor i : otherInterceptors) {
|
||||
this.interceptors.add(i);
|
||||
}
|
||||
Collections.addAll(this.interceptors, otherInterceptors);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -174,7 +194,8 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 设置微信消息处理器
|
||||
*
|
||||
* @param handler
|
||||
* @param handler the handler
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule handler(WxCpMessageHandler handler) {
|
||||
return handler(handler, (WxCpMessageHandler[]) null);
|
||||
@@ -183,21 +204,22 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 设置微信消息处理器
|
||||
*
|
||||
* @param handler
|
||||
* @param otherHandlers
|
||||
* @param handler the handler
|
||||
* @param otherHandlers the other handlers
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpMessageRouterRule handler(WxCpMessageHandler handler, WxCpMessageHandler... otherHandlers) {
|
||||
this.handlers.add(handler);
|
||||
if (otherHandlers != null && otherHandlers.length > 0) {
|
||||
for (WxCpMessageHandler i : otherHandlers) {
|
||||
this.handlers.add(i);
|
||||
}
|
||||
Collections.addAll(this.handlers, otherHandlers);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 规则结束,代表如果一个消息匹配该规则,那么它将不再会进入其他规则
|
||||
*
|
||||
* @return the wx cp message router
|
||||
*/
|
||||
public WxCpMessageRouter end() {
|
||||
this.routerBuilder.getRules().add(this);
|
||||
@@ -206,12 +228,20 @@ public class WxCpMessageRouterRule {
|
||||
|
||||
/**
|
||||
* 规则结束,但是消息还会进入其他规则
|
||||
*
|
||||
* @return the wx cp message router
|
||||
*/
|
||||
public WxCpMessageRouter next() {
|
||||
this.reEnter = true;
|
||||
return end();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test boolean.
|
||||
*
|
||||
* @param wxMessage the wx message
|
||||
* @return the boolean
|
||||
*/
|
||||
protected boolean test(WxCpXmlMessage wxMessage) {
|
||||
return
|
||||
(this.fromUser == null || this.fromUser.equals(wxMessage.getFromUserName()))
|
||||
@@ -237,7 +267,11 @@ public class WxCpMessageRouterRule {
|
||||
/**
|
||||
* 处理微信推送过来的消息
|
||||
*
|
||||
* @param wxMessage
|
||||
* @param wxMessage the wx message
|
||||
* @param context the context
|
||||
* @param wxCpService the wx cp service
|
||||
* @param sessionManager the session manager
|
||||
* @param exceptionHandler the exception handler
|
||||
* @return true 代表继续执行别的router,false 代表停止执行别的router
|
||||
*/
|
||||
protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage,
|
||||
@@ -274,60 +308,5 @@ public class WxCpMessageRouterRule {
|
||||
|
||||
}
|
||||
|
||||
public void setFromUser(String fromUser) {
|
||||
this.fromUser = fromUser;
|
||||
}
|
||||
|
||||
public void setMsgType(String msgType) {
|
||||
this.msgType = msgType;
|
||||
}
|
||||
|
||||
public void setEvent(String event) {
|
||||
this.event = event;
|
||||
}
|
||||
|
||||
public void setEventKey(String eventKey) {
|
||||
this.eventKey = eventKey;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public void setrContent(String rContent) {
|
||||
this.rContent = rContent;
|
||||
}
|
||||
|
||||
public void setMatcher(WxCpMessageMatcher matcher) {
|
||||
this.matcher = matcher;
|
||||
}
|
||||
|
||||
public void setAgentId(Integer agentId) {
|
||||
this.agentId = agentId;
|
||||
}
|
||||
|
||||
public void setHandlers(List<WxCpMessageHandler> handlers) {
|
||||
this.handlers = handlers;
|
||||
}
|
||||
|
||||
public void setInterceptors(List<WxCpMessageInterceptor> interceptors) {
|
||||
this.interceptors = interceptors;
|
||||
}
|
||||
|
||||
public boolean isAsync() {
|
||||
return this.async;
|
||||
}
|
||||
|
||||
public void setAsync(boolean async) {
|
||||
this.async = async;
|
||||
}
|
||||
|
||||
public boolean isReEnter() {
|
||||
return this.reEnter;
|
||||
}
|
||||
|
||||
public void setReEnter(boolean reEnter) {
|
||||
this.reEnter = reEnter;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package me.chanjar.weixin.cp.tp.message;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
|
||||
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 处理微信推送消息的处理器接口
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public interface WxCpTpMessageHandler {
|
||||
|
||||
/**
|
||||
* Handle wx cp xml out message.
|
||||
*
|
||||
* @param wxMessage the wx message
|
||||
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
|
||||
* @param wxCpService the wx cp service
|
||||
* @param sessionManager the session manager
|
||||
* @return xml格式的消息 ,如果在异步规则里处理的话,可以返回null
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
|
||||
Map<String, Object> context,
|
||||
WxCpTpService wxCpService,
|
||||
WxSessionManager sessionManager) throws WxErrorException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package me.chanjar.weixin.cp.tp.message;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
|
||||
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 微信消息拦截器,可以用来做验证
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public interface WxCpTpMessageInterceptor {
|
||||
|
||||
/**
|
||||
* 拦截微信消息
|
||||
*
|
||||
* @param wxMessage the wx message
|
||||
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
|
||||
* @param wxCpService the wx cp service
|
||||
* @param sessionManager the session manager
|
||||
* @return true代表OK ,false代表不OK
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
boolean intercept(WxCpXmlMessage wxMessage,
|
||||
Map<String, Object> context,
|
||||
WxCpTpService wxCpService,
|
||||
WxSessionManager sessionManager) throws WxErrorException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package me.chanjar.weixin.cp.tp.message;
|
||||
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
|
||||
|
||||
/**
|
||||
* 消息匹配器,用在消息路由的时候
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public interface WxCpTpMessageMatcher {
|
||||
|
||||
/**
|
||||
* 消息是否匹配某种模式
|
||||
*
|
||||
* @param message the message
|
||||
* @return the boolean
|
||||
*/
|
||||
boolean match(WxCpXmlMessage message);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
package me.chanjar.weixin.cp.tp.message;
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
|
||||
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker;
|
||||
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
|
||||
import me.chanjar.weixin.common.session.InternalSession;
|
||||
import me.chanjar.weixin.common.session.InternalSessionManager;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.common.util.LogExceptionHandler;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
|
||||
import me.chanjar.weixin.cp.message.WxCpMessageRouterRule;
|
||||
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 微信消息路由器,通过代码化的配置,把来自微信的消息交给handler处理
|
||||
*
|
||||
* 说明:
|
||||
* 1. 配置路由规则时要按照从细到粗的原则,否则可能消息可能会被提前处理
|
||||
* 2. 默认情况下消息只会被处理一次,除非使用 {@link WxCpMessageRouterRule#next()}
|
||||
* 3. 规则的结束必须用{@link WxCpMessageRouterRule#end()}或者{@link WxCpMessageRouterRule#next()},否则不会生效
|
||||
*
|
||||
* 使用方法:
|
||||
* WxCpMessageRouter router = new WxCpMessageRouter();
|
||||
* router
|
||||
* .rule()
|
||||
* .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT")
|
||||
* .interceptor(interceptor, ...).handler(handler, ...)
|
||||
* .end()
|
||||
* .rule()
|
||||
* // 另外一个匹配规则
|
||||
* .end()
|
||||
* ;
|
||||
*
|
||||
* // 将WxXmlMessage交给消息路由器
|
||||
* router.route(message);
|
||||
*
|
||||
* </pre>
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@Slf4j
|
||||
public class WxCpTpMessageRouter {
|
||||
private static final int DEFAULT_THREAD_POOL_SIZE = 100;
|
||||
private final List<WxCpTpMessageRouterRule> rules = new ArrayList<>();
|
||||
|
||||
private final WxCpTpService wxCpService;
|
||||
|
||||
private ExecutorService executorService;
|
||||
|
||||
private WxMessageDuplicateChecker messageDuplicateChecker;
|
||||
|
||||
private WxSessionManager sessionManager;
|
||||
|
||||
private WxErrorExceptionHandler exceptionHandler;
|
||||
|
||||
/**
|
||||
* 构造方法.
|
||||
*/
|
||||
public WxCpTpMessageRouter(WxCpTpService wxCpService) {
|
||||
this.wxCpService = wxCpService;
|
||||
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("WxCpTpMessageRouter-pool-%d").build();
|
||||
this.executorService = new ThreadPoolExecutor(DEFAULT_THREAD_POOL_SIZE, DEFAULT_THREAD_POOL_SIZE,
|
||||
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), namedThreadFactory);
|
||||
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
|
||||
this.sessionManager = wxCpService.getSessionManager();
|
||||
this.exceptionHandler = new LogExceptionHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 设置自定义的 {@link ExecutorService}
|
||||
* 如果不调用该方法,默认使用 Executors.newFixedThreadPool(100)
|
||||
* </pre>
|
||||
*/
|
||||
public void setExecutorService(ExecutorService executorService) {
|
||||
this.executorService = executorService;
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 设置自定义的 {@link WxMessageDuplicateChecker}
|
||||
* 如果不调用该方法,默认使用 {@link WxMessageInMemoryDuplicateChecker}
|
||||
* </pre>
|
||||
*/
|
||||
public void setMessageDuplicateChecker(WxMessageDuplicateChecker messageDuplicateChecker) {
|
||||
this.messageDuplicateChecker = messageDuplicateChecker;
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 设置自定义的{@link WxSessionManager}
|
||||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.session.StandardSessionManager}
|
||||
* </pre>
|
||||
*/
|
||||
public void setSessionManager(WxSessionManager sessionManager) {
|
||||
this.sessionManager = sessionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 设置自定义的{@link WxErrorExceptionHandler}
|
||||
* 如果不调用该方法,默认使用 {@link LogExceptionHandler}
|
||||
* </pre>
|
||||
*/
|
||||
public void setExceptionHandler(WxErrorExceptionHandler exceptionHandler) {
|
||||
this.exceptionHandler = exceptionHandler;
|
||||
}
|
||||
|
||||
List<WxCpTpMessageRouterRule> getRules() {
|
||||
return this.rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始一个新的Route规则.
|
||||
*/
|
||||
public WxCpTpMessageRouterRule rule() {
|
||||
return new WxCpTpMessageRouterRule(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理微信消息.
|
||||
*/
|
||||
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String, Object> context) {
|
||||
if (isMsgDuplicated(wxMessage)) {
|
||||
// 如果是重复消息,那么就不做处理
|
||||
return null;
|
||||
}
|
||||
|
||||
final List<WxCpTpMessageRouterRule> matchRules = new ArrayList<>();
|
||||
// 收集匹配的规则
|
||||
for (final WxCpTpMessageRouterRule rule : this.rules) {
|
||||
if (rule.test(wxMessage)) {
|
||||
matchRules.add(rule);
|
||||
if (!rule.isReEnter()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (matchRules.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
WxCpXmlOutMessage res = null;
|
||||
final List<Future> futures = new ArrayList<>();
|
||||
for (final WxCpTpMessageRouterRule rule : matchRules) {
|
||||
// 返回最后一个非异步的rule的执行结果
|
||||
if (rule.isAsync()) {
|
||||
futures.add(
|
||||
this.executorService.submit(() -> {
|
||||
rule.service(wxMessage, context, WxCpTpMessageRouter.this.wxCpService, WxCpTpMessageRouter.this.sessionManager, WxCpTpMessageRouter.this.exceptionHandler);
|
||||
})
|
||||
);
|
||||
} else {
|
||||
res = rule.service(wxMessage, context, this.wxCpService, this.sessionManager, this.exceptionHandler);
|
||||
// 在同步操作结束,session访问结束
|
||||
log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName());
|
||||
sessionEndAccess(wxMessage);
|
||||
}
|
||||
}
|
||||
|
||||
if (futures.size() > 0) {
|
||||
this.executorService.submit(() -> {
|
||||
for (Future future : futures) {
|
||||
try {
|
||||
future.get();
|
||||
log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName());
|
||||
// 异步操作结束,session访问结束
|
||||
sessionEndAccess(wxMessage);
|
||||
} catch (InterruptedException e) {
|
||||
log.error("Error happened when wait task finish", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (ExecutionException e) {
|
||||
log.error("Error happened when wait task finish", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理微信消息.
|
||||
*/
|
||||
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
|
||||
return this.route(wxMessage, new HashMap<>(2));
|
||||
}
|
||||
|
||||
private boolean isMsgDuplicated(WxCpXmlMessage wxMessage) {
|
||||
StringBuilder messageId = new StringBuilder();
|
||||
if (wxMessage.getMsgId() == null) {
|
||||
messageId.append(wxMessage.getCreateTime())
|
||||
.append("-").append(StringUtils.trimToEmpty(String.valueOf(wxMessage.getAgentId())))
|
||||
.append("-").append(wxMessage.getFromUserName())
|
||||
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEventKey()))
|
||||
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEvent()));
|
||||
} else {
|
||||
messageId.append(wxMessage.getMsgId())
|
||||
.append("-").append(wxMessage.getCreateTime())
|
||||
.append("-").append(wxMessage.getFromUserName());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotEmpty(wxMessage.getUserId())) {
|
||||
messageId.append("-").append(wxMessage.getUserId());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotEmpty(wxMessage.getChangeType())) {
|
||||
messageId.append("-").append(wxMessage.getChangeType());
|
||||
}
|
||||
|
||||
return this.messageDuplicateChecker.isDuplicate(messageId.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 对session的访问结束.
|
||||
*/
|
||||
private void sessionEndAccess(WxCpXmlMessage wxMessage) {
|
||||
InternalSession session = ((InternalSessionManager) this.sessionManager).findSession(wxMessage.getFromUserName());
|
||||
if (session != null) {
|
||||
session.endAccess();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
package me.chanjar.weixin.cp.tp.message;
|
||||
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
|
||||
import me.chanjar.weixin.cp.message.WxCpMessageMatcher;
|
||||
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* The type Wx cp message router rule.
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@Data
|
||||
public class WxCpTpMessageRouterRule {
|
||||
private final WxCpTpMessageRouter routerBuilder;
|
||||
|
||||
private boolean async = true;
|
||||
|
||||
private String fromUser;
|
||||
|
||||
private String msgType;
|
||||
|
||||
private String event;
|
||||
|
||||
private String eventKey;
|
||||
|
||||
private String eventKeyRegex;
|
||||
|
||||
private String content;
|
||||
|
||||
private String rContent;
|
||||
|
||||
private WxCpMessageMatcher matcher;
|
||||
|
||||
private boolean reEnter = false;
|
||||
|
||||
private Integer agentId;
|
||||
|
||||
private List<WxCpTpMessageHandler> handlers = new ArrayList<>();
|
||||
|
||||
private List<WxCpTpMessageInterceptor> interceptors = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Instantiates a new Wx cp message router rule.
|
||||
*
|
||||
* @param routerBuilder the router builder
|
||||
*/
|
||||
protected WxCpTpMessageRouterRule(WxCpTpMessageRouter routerBuilder) {
|
||||
this.routerBuilder = routerBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否异步执行,默认是true
|
||||
*
|
||||
* @param async the async
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule async(boolean async) {
|
||||
this.async = async;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果agentId匹配
|
||||
*
|
||||
* @param agentId the agent id
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule agentId(Integer agentId) {
|
||||
this.agentId = agentId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果msgType等于某值
|
||||
*
|
||||
* @param msgType the msg type
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule msgType(String msgType) {
|
||||
this.msgType = msgType;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果event等于某值
|
||||
*
|
||||
* @param event the event
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule event(String event) {
|
||||
this.event = event;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果eventKey等于某值
|
||||
*
|
||||
* @param eventKey the event key
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule eventKey(String eventKey) {
|
||||
this.eventKey = eventKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果eventKey匹配该正则表达式
|
||||
*
|
||||
* @param regex the regex
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule eventKeyRegex(String regex) {
|
||||
this.eventKeyRegex = regex;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果content等于某值
|
||||
*
|
||||
* @param content the content
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule content(String content) {
|
||||
this.content = content;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果content匹配该正则表达式
|
||||
*
|
||||
* @param regex the regex
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule rContent(String regex) {
|
||||
this.rContent = regex;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果fromUser等于某值
|
||||
*
|
||||
* @param fromUser the from user
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule fromUser(String fromUser) {
|
||||
this.fromUser = fromUser;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果消息匹配某个matcher,用在用户需要自定义更复杂的匹配规则的时候
|
||||
*
|
||||
* @param matcher the matcher
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule matcher(WxCpMessageMatcher matcher) {
|
||||
this.matcher = matcher;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置微信消息拦截器
|
||||
*
|
||||
* @param interceptor the interceptor
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule interceptor(WxCpTpMessageInterceptor interceptor) {
|
||||
return interceptor(interceptor, (WxCpTpMessageInterceptor[]) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置微信消息拦截器
|
||||
*
|
||||
* @param interceptor the interceptor
|
||||
* @param otherInterceptors the other interceptors
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule interceptor(WxCpTpMessageInterceptor interceptor, WxCpTpMessageInterceptor... otherInterceptors) {
|
||||
this.interceptors.add(interceptor);
|
||||
if (otherInterceptors != null && otherInterceptors.length > 0) {
|
||||
Collections.addAll(this.interceptors, otherInterceptors);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置微信消息处理器
|
||||
*
|
||||
* @param handler the handler
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule handler(WxCpTpMessageHandler handler) {
|
||||
return handler(handler, (WxCpTpMessageHandler[]) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置微信消息处理器
|
||||
*
|
||||
* @param handler the handler
|
||||
* @param otherHandlers the other handlers
|
||||
* @return the wx cp message router rule
|
||||
*/
|
||||
public WxCpTpMessageRouterRule handler(WxCpTpMessageHandler handler, WxCpTpMessageHandler... otherHandlers) {
|
||||
this.handlers.add(handler);
|
||||
if (otherHandlers != null && otherHandlers.length > 0) {
|
||||
Collections.addAll(this.handlers, otherHandlers);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 规则结束,代表如果一个消息匹配该规则,那么它将不再会进入其他规则
|
||||
*
|
||||
* @return the wx cp message router
|
||||
*/
|
||||
public WxCpTpMessageRouter end() {
|
||||
this.routerBuilder.getRules().add(this);
|
||||
return this.routerBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* 规则结束,但是消息还会进入其他规则
|
||||
*
|
||||
* @return the wx cp message router
|
||||
*/
|
||||
public WxCpTpMessageRouter next() {
|
||||
this.reEnter = true;
|
||||
return end();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test boolean.
|
||||
*
|
||||
* @param wxMessage the wx message
|
||||
* @return the boolean
|
||||
*/
|
||||
protected boolean test(WxCpXmlMessage wxMessage) {
|
||||
return
|
||||
(this.fromUser == null || this.fromUser.equals(wxMessage.getFromUserName()))
|
||||
&&
|
||||
(this.agentId == null || this.agentId.equals(wxMessage.getAgentId()))
|
||||
&&
|
||||
(this.msgType == null || this.msgType.equalsIgnoreCase(wxMessage.getMsgType()))
|
||||
&&
|
||||
(this.event == null || this.event.equalsIgnoreCase(wxMessage.getEvent()))
|
||||
&&
|
||||
(this.eventKey == null || this.eventKey.equalsIgnoreCase(wxMessage.getEventKey()))
|
||||
&&
|
||||
(this.eventKeyRegex == null || Pattern.matches(this.eventKeyRegex, StringUtils.trimToEmpty(wxMessage.getEventKey())))
|
||||
&&
|
||||
(this.content == null || this.content.equals(StringUtils.trimToNull(wxMessage.getContent())))
|
||||
&&
|
||||
(this.rContent == null || Pattern.matches(this.rContent, StringUtils.trimToEmpty(wxMessage.getContent())))
|
||||
&&
|
||||
(this.matcher == null || this.matcher.match(wxMessage))
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理微信推送过来的消息
|
||||
*
|
||||
* @param wxMessage the wx message
|
||||
* @param context the context
|
||||
* @param wxCpService the wx cp service
|
||||
* @param sessionManager the session manager
|
||||
* @param exceptionHandler the exception handler
|
||||
* @return true 代表继续执行别的router,false 代表停止执行别的router
|
||||
*/
|
||||
protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage,
|
||||
Map<String, Object> context,
|
||||
WxCpTpService wxCpService,
|
||||
WxSessionManager sessionManager,
|
||||
WxErrorExceptionHandler exceptionHandler) {
|
||||
|
||||
if (context == null) {
|
||||
context = new HashMap<>(2);
|
||||
}
|
||||
|
||||
try {
|
||||
// 如果拦截器不通过
|
||||
for (WxCpTpMessageInterceptor interceptor : this.interceptors) {
|
||||
if (!interceptor.intercept(wxMessage, context, wxCpService, sessionManager)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 交给handler处理
|
||||
WxCpXmlOutMessage res = null;
|
||||
for (WxCpTpMessageHandler handler : this.handlers) {
|
||||
// 返回最后handler的结果
|
||||
res = handler.handle(wxMessage, context, wxCpService, sessionManager);
|
||||
}
|
||||
return res;
|
||||
|
||||
} catch (WxErrorException e) {
|
||||
exceptionHandler.handle(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
package me.chanjar.weixin.cp.tp.service;
|
||||
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
@@ -12,7 +13,7 @@ import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo;
|
||||
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
|
||||
|
||||
/**
|
||||
* 微信第三方应用API的Service.
|
||||
* 企业微信第三方应用API的Service.
|
||||
*
|
||||
* @author zhenjun cai
|
||||
*/
|
||||
@@ -27,13 +28,16 @@ public interface WxCpTpService {
|
||||
* @param timestamp 时间戳
|
||||
* @param nonce 随机数
|
||||
* @param data 微信传输过来的数据,有可能是echoStr,有可能是xml消息
|
||||
* @return the boolean
|
||||
*/
|
||||
boolean checkSignature(String msgSignature, String timestamp, String nonce, String data);
|
||||
|
||||
/**
|
||||
* 获取suite_access_token, 不强制刷新suite_access_token
|
||||
*
|
||||
* @see #getSuiteAccessToken(boolean)
|
||||
* @return the suite access token
|
||||
* @throws WxErrorException the wx error exception
|
||||
* @see #getSuiteAccessToken(boolean) #getSuiteAccessToken(boolean)
|
||||
*/
|
||||
String getSuiteAccessToken() throws WxErrorException;
|
||||
|
||||
@@ -47,13 +51,17 @@ public interface WxCpTpService {
|
||||
* </pre>
|
||||
*
|
||||
* @param forceRefresh 强制刷新
|
||||
* @return the suite access token
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
String getSuiteAccessToken(boolean forceRefresh) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 获得suite_ticket,不强制刷新suite_ticket
|
||||
*
|
||||
* @see #getSuiteTicket(boolean)
|
||||
* @return the suite ticket
|
||||
* @throws WxErrorException the wx error exception
|
||||
* @see #getSuiteTicket(boolean) #getSuiteTicket(boolean)
|
||||
*/
|
||||
String getSuiteTicket() throws WxErrorException;
|
||||
|
||||
@@ -66,6 +74,8 @@ public interface WxCpTpService {
|
||||
* </pre>
|
||||
*
|
||||
* @param forceRefresh 强制刷新
|
||||
* @return the suite ticket
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
String getSuiteTicket(boolean forceRefresh) throws WxErrorException;
|
||||
|
||||
@@ -73,6 +83,8 @@ public interface WxCpTpService {
|
||||
* 小程序登录凭证校验
|
||||
*
|
||||
* @param jsCode 登录时获取的 code
|
||||
* @return the wx cp ma js code 2 session result
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorException;
|
||||
|
||||
@@ -81,6 +93,8 @@ public interface WxCpTpService {
|
||||
*
|
||||
* @param authCorpid 授权方corpid
|
||||
* @param permanentCode 永久授权码,通过get_permanent_code获取
|
||||
* @return the corp token
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
WxAccessToken getCorpToken(String authCorpid, String permanentCode) throws WxErrorException;
|
||||
|
||||
@@ -88,7 +102,8 @@ public interface WxCpTpService {
|
||||
* 获取企业永久授权码 .
|
||||
*
|
||||
* @param authCode .
|
||||
* @return .
|
||||
* @return . permanent code
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
@Deprecated
|
||||
WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException;
|
||||
@@ -99,13 +114,11 @@ public interface WxCpTpService {
|
||||
* 原来的方法实现不全
|
||||
* </pre>
|
||||
*
|
||||
* @param authCode
|
||||
* @return
|
||||
*
|
||||
* @param authCode the auth code
|
||||
* @return permanent code info
|
||||
* @throws WxErrorException the wx error exception
|
||||
* @author yuan
|
||||
* @since 2020-03-18
|
||||
*
|
||||
* @throws WxErrorException
|
||||
* @since 2020 -03-18
|
||||
*/
|
||||
WxCpTpPermanentCodeInfo getPermanentCodeInfo(String authCode) throws WxErrorException;
|
||||
|
||||
@@ -113,28 +126,31 @@ public interface WxCpTpService {
|
||||
* <pre>
|
||||
* 获取预授权链接
|
||||
* </pre>
|
||||
*
|
||||
* @param redirectUri 授权完成后的回调网址
|
||||
* @param state a-zA-Z0-9的参数值(不超过128个字节),用于第三方自行校验session,防止跨域攻击
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @param state a-zA-Z0-9的参数值(不超过128个字节),用于第三方自行校验session,防止跨域攻击
|
||||
* @return pre auth url
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
String getPreAuthUrl(String redirectUri,String state) throws WxErrorException;
|
||||
String getPreAuthUrl(String redirectUri, String state) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 获取企业的授权信息
|
||||
*
|
||||
* @param authCorpId 授权企业的corpId
|
||||
* @param authCorpId 授权企业的corpId
|
||||
* @param permanentCode 授权企业的永久授权码
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @return auth info
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
WxCpTpAuthInfo getAuthInfo(String authCorpId,String permanentCode) throws WxErrorException;
|
||||
WxCpTpAuthInfo getAuthInfo(String authCorpId, String permanentCode) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求.
|
||||
*
|
||||
* @param url 接口地址
|
||||
* @param queryParam 请求参数
|
||||
* @return the string
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
String get(String url, String queryParam) throws WxErrorException;
|
||||
|
||||
@@ -143,6 +159,8 @@ public interface WxCpTpService {
|
||||
*
|
||||
* @param url 接口地址
|
||||
* @param postData 请求body字符串
|
||||
* @return the string
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
String post(String url, String postData) throws WxErrorException;
|
||||
|
||||
@@ -153,11 +171,13 @@ public interface WxCpTpService {
|
||||
* 可以参考,{@link MediaUploadRequestExecutor}的实现方法
|
||||
* </pre>
|
||||
*
|
||||
* @param <T> 请求值类型
|
||||
* @param <E> 返回值类型
|
||||
* @param executor 执行器
|
||||
* @param uri 请求地址
|
||||
* @param data 参数
|
||||
* @param <T> 请求值类型
|
||||
* @param <E> 返回值类型
|
||||
* @return the t
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
<T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException;
|
||||
|
||||
@@ -189,7 +209,7 @@ public interface WxCpTpService {
|
||||
/**
|
||||
* 获取WxMpConfigStorage 对象.
|
||||
*
|
||||
* @return WxMpConfigStorage
|
||||
* @return WxMpConfigStorage wx cp tp config storage
|
||||
*/
|
||||
WxCpTpConfigStorage getWxCpTpConfigStorage();
|
||||
|
||||
@@ -202,7 +222,15 @@ public interface WxCpTpService {
|
||||
|
||||
/**
|
||||
* http请求对象.
|
||||
*
|
||||
* @return the request http
|
||||
*/
|
||||
RequestHttp<?, ?> getRequestHttp();
|
||||
|
||||
/**
|
||||
* 获取WxSessionManager 对象
|
||||
*
|
||||
* @return WxSessionManager session manager
|
||||
*/
|
||||
WxSessionManager getSessionManager();
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
package me.chanjar.weixin.cp.api.impl;
|
||||
package me.chanjar.weixin.cp.tp.service.impl;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxCpErrorMsgEnum;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.StandardSessionManager;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.common.util.DataUtils;
|
||||
import me.chanjar.weixin.common.util.crypto.SHA1;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
@@ -16,9 +18,9 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
import me.chanjar.weixin.cp.api.WxCpTpService;
|
||||
import me.chanjar.weixin.cp.bean.*;
|
||||
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
|
||||
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
@@ -49,6 +51,8 @@ public abstract class BaseWxCpTpServiceImpl<H, P> implements WxCpTpService, Requ
|
||||
|
||||
protected WxCpTpConfigStorage configStorage;
|
||||
|
||||
private WxSessionManager sessionManager = new StandardSessionManager();
|
||||
|
||||
/**
|
||||
* 临时文件目录.
|
||||
*/
|
||||
@@ -127,7 +131,7 @@ public abstract class BaseWxCpTpServiceImpl<H, P> implements WxCpTpService, Requ
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxCpTpPermanentCodeInfo getPermanentCodeInfo(String authCode) throws WxErrorException{
|
||||
public WxCpTpPermanentCodeInfo getPermanentCodeInfo(String authCode) throws WxErrorException {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("auth_code", authCode);
|
||||
String result = post(configStorage.getApiUrl(GET_PERMANENT_CODE), jsonObject.toString());
|
||||
@@ -136,18 +140,19 @@ public abstract class BaseWxCpTpServiceImpl<H, P> implements WxCpTpService, Requ
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public String getPreAuthUrl(String redirectUri,String state) throws WxErrorException{
|
||||
String result = get(configStorage.getApiUrl(GET_PREAUTH_CODE),null);
|
||||
WxCpTpPreauthCode preauthCode = WxCpTpPreauthCode.fromJson(result);
|
||||
String preAuthUrl = "https://open.work.weixin.qq.com/3rdapp/install?suite_id="+configStorage.getSuiteId()+
|
||||
"&pre_auth_code="+preauthCode.getPreAuthCode()+"&redirect_uri="+ URLEncoder.encode(redirectUri,"utf-8");
|
||||
if(StringUtils.isNotBlank(state))
|
||||
preAuthUrl += "&state="+state;
|
||||
public String getPreAuthUrl(String redirectUri, String state) throws WxErrorException {
|
||||
String result = get(configStorage.getApiUrl(GET_PREAUTH_CODE), null);
|
||||
WxCpTpPreauthCode preAuthCode = WxCpTpPreauthCode.fromJson(result);
|
||||
String preAuthUrl = "https://open.work.weixin.qq.com/3rdapp/install?suite_id=" + configStorage.getSuiteId() +
|
||||
"&pre_auth_code=" + preAuthCode.getPreAuthCode() + "&redirect_uri=" + URLEncoder.encode(redirectUri, "utf-8");
|
||||
if (StringUtils.isNotBlank(state)) {
|
||||
preAuthUrl += "&state=" + state;
|
||||
}
|
||||
return preAuthUrl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxCpTpAuthInfo getAuthInfo(String authCorpId, String permanentCode) throws WxErrorException{
|
||||
public WxCpTpAuthInfo getAuthInfo(String authCorpId, String permanentCode) throws WxErrorException {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("auth_corpid", authCorpId);
|
||||
jsonObject.addProperty("permanent_code", permanentCode);
|
||||
@@ -273,4 +278,9 @@ public abstract class BaseWxCpTpServiceImpl<H, P> implements WxCpTpService, Requ
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxSessionManager getSessionManager() {
|
||||
return this.sessionManager;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.cp.api.impl;
|
||||
package me.chanjar.weixin.cp.tp.service.impl;
|
||||
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
@@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.cp.api.impl;
|
||||
package me.chanjar.weixin.cp.tp.service.impl;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
@@ -217,4 +217,17 @@ public class WxCpExternalContactServiceImplTest {
|
||||
.welcomeCode("abc")
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateRemark() throws WxErrorException {
|
||||
this.wxCpService.getExternalContactService().updateRemark(WxCpUpdateRemarkRequest.builder()
|
||||
.description("abc")
|
||||
.userId("aaa")
|
||||
.externalUserId("aaa")
|
||||
.remark("aa")
|
||||
.remarkCompany("aaa")
|
||||
.remarkMobiles(new String[]{"111","222"})
|
||||
.remarkPicMediaId("aaa")
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public class WxCpGroupRobotServiceImplTest {
|
||||
">类型:<font color=\"comment\">用户反馈</font> \n" +
|
||||
">普通用户反馈:<font color=\"comment\">117例</font> \n" +
|
||||
">VIP用户反馈:<font color=\"comment\">15例</font>";
|
||||
robotService.sendMarkDown(content);
|
||||
robotService.sendMarkdown(content);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package me.chanjar.weixin.cp.api.impl;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.cp.api.ApiTestModule;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
import me.chanjar.weixin.cp.bean.oa.calendar.WxCpOaCalendar;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 单元测试.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @date 2020-09-20
|
||||
*/
|
||||
|
||||
@Test
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxCpOaCalendarServiceImplTest {
|
||||
@Inject
|
||||
protected WxCpService wxService;
|
||||
|
||||
@Test
|
||||
public void testAdd() throws WxErrorException {
|
||||
this.wxService.getOaCalendarService().add(WxCpOaCalendar.builder()
|
||||
.organizer("userid1")
|
||||
.readonly(1)
|
||||
.setAsDefault(1)
|
||||
.summary("test_summary")
|
||||
.color("#FF3030")
|
||||
.description("test_describe")
|
||||
.shares(Arrays.asList(new WxCpOaCalendar.ShareInfo("userid2", null),
|
||||
new WxCpOaCalendar.ShareInfo("userid3", 1)))
|
||||
.build());
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public class WxCpOaServiceImplTest {
|
||||
Date startTime = DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.parse("2019-04-11");
|
||||
Date endTime = DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.parse("2019-05-10");
|
||||
|
||||
List<WxCpCheckinData> results = wxService.getOAService()
|
||||
List<WxCpCheckinData> results = wxService.getOaService()
|
||||
.getCheckinData(1, startTime, endTime, Lists.newArrayList("binary"));
|
||||
|
||||
assertThat(results).isNotNull();
|
||||
@@ -51,7 +51,7 @@ public class WxCpOaServiceImplTest {
|
||||
public void testGetCheckinOption() throws WxErrorException {
|
||||
|
||||
Date now = new Date();
|
||||
List<WxCpCheckinOption> results = wxService.getOAService().getCheckinOption(now, Lists.newArrayList("binary"));
|
||||
List<WxCpCheckinOption> results = wxService.getOaService().getCheckinOption(now, Lists.newArrayList("binary"));
|
||||
assertThat(results).isNotNull();
|
||||
System.out.println("results ");
|
||||
System.out.println(gson.toJson(results));
|
||||
@@ -61,7 +61,7 @@ public class WxCpOaServiceImplTest {
|
||||
public void testGetApprovalInfo() throws WxErrorException, ParseException {
|
||||
Date startTime = DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.parse("2019-12-01");
|
||||
Date endTime = DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.parse("2019-12-31");
|
||||
WxCpApprovalInfo result = wxService.getOAService().getApprovalInfo(startTime, endTime);
|
||||
WxCpApprovalInfo result = wxService.getOaService().getApprovalInfo(startTime, endTime);
|
||||
|
||||
assertThat(result).isNotNull();
|
||||
|
||||
@@ -72,7 +72,7 @@ public class WxCpOaServiceImplTest {
|
||||
@Test
|
||||
public void testGetApprovalDetail() throws WxErrorException {
|
||||
String spNo = "201912020001";
|
||||
WxCpApprovalDetailResult result = wxService.getOAService().getApprovalDetail(spNo);
|
||||
WxCpApprovalDetailResult result = wxService.getOaService().getApprovalDetail(spNo);
|
||||
|
||||
assertThat(result).isNotNull();
|
||||
|
||||
@@ -83,7 +83,7 @@ public class WxCpOaServiceImplTest {
|
||||
@Test
|
||||
public void testGetTemplateDetail() throws WxErrorException {
|
||||
String templateId = "3TkZjxugodbqpEMk9j7X6h6zKqYkc7MxQrrFmT7H";
|
||||
WxCpTemplateResult result = wxService.getOAService().getTemplateDetail(templateId);
|
||||
WxCpTemplateResult result = wxService.getOaService().getTemplateDetail(templateId);
|
||||
assertThat(result).isNotNull();
|
||||
System.out.println("result ");
|
||||
System.out.println(gson.toJson(result));
|
||||
@@ -91,7 +91,7 @@ public class WxCpOaServiceImplTest {
|
||||
|
||||
@Test
|
||||
public void testApply() throws WxErrorException {
|
||||
this.wxService.getOAService().apply(new WxCpOaApplyEventRequest().setCreatorUserId("123"));
|
||||
this.wxService.getOaService().apply(new WxCpOaApplyEventRequest().setCreatorUserId("123"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
42
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/external/WxCpUpdateRemarkRequestTest.java
vendored
Normal file
42
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/external/WxCpUpdateRemarkRequestTest.java
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
package me.chanjar.weixin.cp.bean.external;
|
||||
|
||||
import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* 单元测试.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @date 2020-09-20
|
||||
*/
|
||||
public class WxCpUpdateRemarkRequestTest {
|
||||
|
||||
@Test
|
||||
public void testToJson() {
|
||||
String json = "{\n" +
|
||||
" \"userid\":\"zhangsan\",\n" +
|
||||
" \"external_userid\":\"woAJ2GCAAAd1asdasdjO4wKmE8Aabj9AAA\",\n" +
|
||||
" \"remark\":\"备注信息\",\n" +
|
||||
" \"description\":\"描述信息\",\n" +
|
||||
" \"remark_company\":\"腾讯科技\",\n" +
|
||||
" \"remark_mobiles\":[\n" +
|
||||
" \"13800000001\",\n" +
|
||||
" \"13800000002\"\n" +
|
||||
" ],\n" +
|
||||
" \"remark_pic_mediaid\":\"MEDIAID\"\n" +
|
||||
"}\n";
|
||||
|
||||
WxCpUpdateRemarkRequest request = WxCpUpdateRemarkRequest.builder()
|
||||
.description("描述信息")
|
||||
.userId("zhangsan")
|
||||
.externalUserId("woAJ2GCAAAd1asdasdjO4wKmE8Aabj9AAA")
|
||||
.remark("备注信息")
|
||||
.remarkCompany("腾讯科技")
|
||||
.remarkMobiles(new String[]{"13800000001","13800000002"})
|
||||
.remarkPicMediaId("MEDIAID")
|
||||
.build();
|
||||
assertThat(request.toJson()).isEqualTo(GsonParser.parse(json).toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package me.chanjar.weixin.cp.bean.oa.calendar;
|
||||
|
||||
import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* 单元测试.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @date 2020-09-20
|
||||
*/
|
||||
public class WxCpOaCalendarTest {
|
||||
|
||||
@Test
|
||||
public void testToJson() {
|
||||
String json = "{\n" +
|
||||
" \"calendar\" : {\n" +
|
||||
" \"organizer\" : \"userid1\",\n" +
|
||||
" \"readonly\" : 1,\n" +
|
||||
" \"set_as_default\" : 1,\n" +
|
||||
" \"summary\" : \"test_summary\",\n" +
|
||||
" \"color\" : \"#FF3030\",\n" +
|
||||
" \"description\" : \"test_describe\",\n" +
|
||||
" \"shares\" : [\n" +
|
||||
" {\n" +
|
||||
" \"userid\" : \"userid2\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"userid\" : \"userid3\",\n" +
|
||||
" \"readonly\" : 1\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" }\n" +
|
||||
"}\n";
|
||||
|
||||
assertThat(WxCpOaCalendar.builder()
|
||||
.organizer("userid1")
|
||||
.readonly(1)
|
||||
.setAsDefault(1)
|
||||
.summary("test_summary")
|
||||
.color("#FF3030")
|
||||
.description("test_describe")
|
||||
.shares(Arrays.asList(new WxCpOaCalendar.ShareInfo("userid2", null),
|
||||
new WxCpOaCalendar.ShareInfo("userid3", 1)))
|
||||
.build().toJson())
|
||||
.isEqualTo(GsonParser.parse(json).toString());
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,19 @@
|
||||
package me.chanjar.weixin.cp.demo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutTextMessage;
|
||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
||||
import me.chanjar.weixin.cp.constant.WxCpConsts;
|
||||
import me.chanjar.weixin.cp.message.WxCpMessageHandler;
|
||||
import me.chanjar.weixin.cp.message.WxCpMessageRouter;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.servlet.ServletHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.cp.constant.WxCpConsts;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
|
||||
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutTextMessage;
|
||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
||||
import me.chanjar.weixin.cp.message.WxCpMessageHandler;
|
||||
import me.chanjar.weixin.cp.message.WxCpMessageRouter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class WxCpDemoServer {
|
||||
|
||||
@@ -54,30 +49,20 @@ public class WxCpDemoServer {
|
||||
wxCpService = new WxCpServiceImpl();
|
||||
wxCpService.setWxCpConfigStorage(config);
|
||||
|
||||
WxCpMessageHandler handler = new WxCpMessageHandler() {
|
||||
@Override
|
||||
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
|
||||
Map<String, Object> context, WxCpService wxService,
|
||||
WxSessionManager sessionManager) {
|
||||
WxCpXmlOutTextMessage m = WxCpXmlOutMessage.TEXT().content("测试加密消息")
|
||||
.fromUser(wxMessage.getToUserName())
|
||||
.toUser(wxMessage.getFromUserName()).build();
|
||||
return m;
|
||||
}
|
||||
WxCpMessageHandler handler = (wxMessage, context, wxService, sessionManager) -> {
|
||||
WxCpXmlOutTextMessage m = WxCpXmlOutMessage.TEXT().content("测试加密消息")
|
||||
.fromUser(wxMessage.getToUserName())
|
||||
.toUser(wxMessage.getFromUserName()).build();
|
||||
return m;
|
||||
};
|
||||
|
||||
WxCpMessageHandler oauth2handler = new WxCpMessageHandler() {
|
||||
@Override
|
||||
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
|
||||
Map<String, Object> context, WxCpService wxService,
|
||||
WxSessionManager sessionManager) {
|
||||
String href = "<a href=\""
|
||||
+ wxService.getOauth2Service().buildAuthorizationUrl(wxCpConfigStorage.getOauth2redirectUri(), null)
|
||||
+ "\">测试oauth2</a>";
|
||||
return WxCpXmlOutMessage.TEXT().content(href)
|
||||
.fromUser(wxMessage.getToUserName())
|
||||
.toUser(wxMessage.getFromUserName()).build();
|
||||
}
|
||||
WxCpMessageHandler oauth2handler = (wxMessage, context, wxService, sessionManager) -> {
|
||||
String href = "<a href=\""
|
||||
+ wxService.getOauth2Service().buildAuthorizationUrl(wxCpConfigStorage.getOauth2redirectUri(), null)
|
||||
+ "\">测试oauth2</a>";
|
||||
return WxCpXmlOutMessage.TEXT().content(href)
|
||||
.fromUser(wxMessage.getToUserName())
|
||||
.toUser(wxMessage.getFromUserName()).build();
|
||||
};
|
||||
|
||||
wxCpMessageRouter = new WxCpMessageRouter(wxCpService);
|
||||
@@ -93,12 +78,9 @@ public class WxCpDemoServer {
|
||||
.end()
|
||||
.rule()
|
||||
.event(WxCpConsts.EventType.CHANGE_CONTACT)
|
||||
.handler(new WxCpMessageHandler() {
|
||||
@Override
|
||||
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService, WxSessionManager sessionManager) throws WxErrorException {
|
||||
System.out.println("通讯录发生变更");
|
||||
return null;
|
||||
}
|
||||
.handler((wxMessage, context, wxCpService, sessionManager) -> {
|
||||
System.out.println("通讯录发生变更");
|
||||
return null;
|
||||
})
|
||||
.end();
|
||||
|
||||
|
||||
@@ -61,7 +61,6 @@ public class WxCpEndpointServlet extends HttpServlet {
|
||||
response.getWriter().write(outMessage.toEncryptedXml(this.wxCpConfigStorage));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
package me.chanjar.weixin.cp.api.impl;
|
||||
package me.chanjar.weixin.cp.tp.service.impl;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.cp.api.WxCpTpService;
|
||||
import me.chanjar.weixin.cp.bean.WxCpTpAuthInfo;
|
||||
import me.chanjar.weixin.cp.bean.WxCpTpCorp;
|
||||
import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo;
|
||||
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
|
||||
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
|
||||
import org.mockito.Mockito;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tp.GET_AUTH_INFO;
|
||||
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tp.GET_PERMANENT_CODE;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
/**
|
||||
* 测试代码.
|
||||
@@ -23,7 +23,7 @@ import static org.testng.Assert.*;
|
||||
* @date 2019-08-18
|
||||
*/
|
||||
public class BaseWxCpTpServiceImplTest {
|
||||
private WxCpTpService tpService = spy(new WxCpTpServiceImpl());
|
||||
private final WxCpTpService tpService = Mockito.spy(new WxCpTpServiceImpl());
|
||||
|
||||
@Test
|
||||
public void testCheckSignature() {
|
||||
@@ -123,7 +123,7 @@ public class BaseWxCpTpServiceImplTest {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
String authCode = "";
|
||||
jsonObject.addProperty("auth_code", authCode);
|
||||
doReturn(returnJson).when(tpService).post(configStorage.getApiUrl(GET_PERMANENT_CODE), jsonObject.toString());
|
||||
Mockito.doReturn(returnJson).when(tpService).post(configStorage.getApiUrl(GET_PERMANENT_CODE), jsonObject.toString());
|
||||
|
||||
final WxCpTpCorp tpCorp = tpService.getPermanentCode(authCode);
|
||||
assertThat(tpCorp.getPermanentCode()).isEqualTo("xxxx");
|
||||
@@ -134,7 +134,7 @@ public class BaseWxCpTpServiceImplTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPermanentCodeInfo() throws WxErrorException{
|
||||
public void testGetPermanentCodeInfo() throws WxErrorException {
|
||||
String returnJson = "{\n" +
|
||||
" \"access_token\": \"u6SoEWyrEmworJ1uNzddbiXh42mCLNU_mdd6b01Afo2LKmyi-WdaaYqhEGFZjB1RGZ-rhjLcAJ86ger7b7Q0gowSw9iIDR8dm49aVH_MztzmQttP3XFG7np1Dxs_VQkVwhhRmfRpEonAmK1_JWIFqayJXXiPUS3LsFd3tWpE7rxmsRa7Ev2ml2htbRp_qGUjtFTErKoDsnNGSka6_RqFPA\", \n" +
|
||||
" \"expires_in\": 7200, \n" +
|
||||
@@ -187,15 +187,15 @@ public class BaseWxCpTpServiceImplTest {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
String authCode = "";
|
||||
jsonObject.addProperty("auth_code", authCode);
|
||||
doReturn(returnJson).when(tpService).post(configStorage.getApiUrl(GET_PERMANENT_CODE), jsonObject.toString());
|
||||
Mockito.doReturn(returnJson).when(tpService).post(configStorage.getApiUrl(GET_PERMANENT_CODE), jsonObject.toString());
|
||||
final WxCpTpPermanentCodeInfo tpPermanentCodeInfo = tpService.getPermanentCodeInfo(authCode);
|
||||
assertThat(tpPermanentCodeInfo.getAuthInfo().getAgents().get(0).getAgentId()).isEqualTo(1000012);
|
||||
assertNotNull(tpPermanentCodeInfo.getAuthInfo().getAgents().get(0).getSquareLogoUrl());
|
||||
assertNotNull(tpPermanentCodeInfo.getAuthCorpInfo().getCorpSquareLogoUrl());
|
||||
Assert.assertNotNull(tpPermanentCodeInfo.getAuthInfo().getAgents().get(0).getSquareLogoUrl());
|
||||
Assert.assertNotNull(tpPermanentCodeInfo.getAuthCorpInfo().getCorpSquareLogoUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAuthInfo() throws WxErrorException{
|
||||
public void testGetAuthInfo() throws WxErrorException {
|
||||
String returnJson = "{\n" +
|
||||
" \"errcode\":0 ,\n" +
|
||||
" \"errmsg\":\"ok\" ,\n" +
|
||||
@@ -260,9 +260,9 @@ public class BaseWxCpTpServiceImplTest {
|
||||
String permanentCode = "xxxxx";
|
||||
jsonObject.addProperty("auth_corpid", authCorpId);
|
||||
jsonObject.addProperty("permanent_code", permanentCode);
|
||||
doReturn(returnJson).when(tpService).post(configStorage.getApiUrl(GET_AUTH_INFO), jsonObject.toString());
|
||||
WxCpTpAuthInfo authInfo = tpService.getAuthInfo(authCorpId,permanentCode);
|
||||
assertNotNull(authInfo.getAuthCorpInfo().getCorpId());
|
||||
Mockito.doReturn(returnJson).when(tpService).post(configStorage.getApiUrl(GET_AUTH_INFO), jsonObject.toString());
|
||||
WxCpTpAuthInfo authInfo = tpService.getAuthInfo(authCorpId, permanentCode);
|
||||
Assert.assertNotNull(authInfo.getAuthCorpInfo().getCorpId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>wx-java</artifactId>
|
||||
<version>3.9.2.B</version>
|
||||
<version>3.9.3.B</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>weixin-java-miniapp</artifactId>
|
||||
|
||||
@@ -45,7 +45,7 @@ public class WxMaMessageRouter {
|
||||
this.wxMaService = wxMaService;
|
||||
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("WxMaMessageRouter-pool-%d").build();
|
||||
this.executorService = new ThreadPoolExecutor(DEFAULT_THREAD_POOL_SIZE, DEFAULT_THREAD_POOL_SIZE,
|
||||
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), namedThreadFactory);
|
||||
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), namedThreadFactory);
|
||||
this.sessionManager = new StandardSessionManager();
|
||||
this.exceptionHandler = new LogExceptionHandler();
|
||||
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
|
||||
@@ -88,11 +88,8 @@ public class WxMaMessageRouter {
|
||||
// 返回最后一个非异步的rule的执行结果
|
||||
if (rule.isAsync()) {
|
||||
futures.add(
|
||||
this.executorService.submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
rule.service(wxMessage, context, WxMaMessageRouter.this.wxMaService, WxMaMessageRouter.this.sessionManager, WxMaMessageRouter.this.exceptionHandler);
|
||||
}
|
||||
this.executorService.submit(() -> {
|
||||
rule.service(wxMessage, context, WxMaMessageRouter.this.wxMaService, WxMaMessageRouter.this.sessionManager, WxMaMessageRouter.this.exceptionHandler);
|
||||
})
|
||||
);
|
||||
} else {
|
||||
@@ -104,18 +101,15 @@ public class WxMaMessageRouter {
|
||||
}
|
||||
|
||||
if (futures.size() > 0) {
|
||||
this.executorService.submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (Future<?> future : futures) {
|
||||
try {
|
||||
future.get();
|
||||
WxMaMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUser());
|
||||
// 异步操作结束,session访问结束
|
||||
sessionEndAccess(wxMessage);
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
WxMaMessageRouter.this.log.error("Error happened when wait task finish", e);
|
||||
}
|
||||
this.executorService.submit(() -> {
|
||||
for (Future<?> future : futures) {
|
||||
try {
|
||||
future.get();
|
||||
WxMaMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUser());
|
||||
// 异步操作结束,session访问结束
|
||||
sessionEndAccess(wxMessage);
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
WxMaMessageRouter.this.log.error("Error happened when wait task finish", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -124,7 +118,7 @@ public class WxMaMessageRouter {
|
||||
}
|
||||
|
||||
public WxMaXmlOutMessage route(final WxMaMessage wxMessage) {
|
||||
return this.route(wxMessage, new HashMap<String, Object>(2));
|
||||
return this.route(wxMessage, new HashMap<>(2));
|
||||
}
|
||||
|
||||
private boolean isMsgDuplicated(WxMaMessage wxMessage) {
|
||||
|
||||
@@ -6,10 +6,7 @@ import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -135,9 +132,7 @@ public class WxMaMessageRouterRule {
|
||||
public WxMaMessageRouterRule interceptor(WxMaMessageInterceptor interceptor, WxMaMessageInterceptor... otherInterceptors) {
|
||||
this.interceptors.add(interceptor);
|
||||
if (otherInterceptors != null && otherInterceptors.length > 0) {
|
||||
for (WxMaMessageInterceptor i : otherInterceptors) {
|
||||
this.interceptors.add(i);
|
||||
}
|
||||
Collections.addAll(this.interceptors, otherInterceptors);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -155,9 +150,7 @@ public class WxMaMessageRouterRule {
|
||||
public WxMaMessageRouterRule handler(WxMaMessageHandler handler, WxMaMessageHandler... otherHandlers) {
|
||||
this.handlers.add(handler);
|
||||
if (otherHandlers != null && otherHandlers.length > 0) {
|
||||
for (WxMaMessageHandler i : otherHandlers) {
|
||||
this.handlers.add(i);
|
||||
}
|
||||
Collections.addAll(this.handlers, otherHandlers);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>wx-java</artifactId>
|
||||
<version>3.9.2.B</version>
|
||||
<version>3.9.3.B</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>weixin-java-mp</artifactId>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
|
||||
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker;
|
||||
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
|
||||
@@ -18,10 +19,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
@@ -68,7 +66,9 @@ public class WxMpMessageRouter {
|
||||
|
||||
public WxMpMessageRouter(WxMpService wxMpService) {
|
||||
this.wxMpService = wxMpService;
|
||||
this.executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE);
|
||||
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("WxMpMessageRouter-pool-%d").build();
|
||||
this.executorService = new ThreadPoolExecutor(DEFAULT_THREAD_POOL_SIZE, DEFAULT_THREAD_POOL_SIZE,
|
||||
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), namedThreadFactory);
|
||||
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker();
|
||||
this.sessionManager = new StandardSessionManager();
|
||||
this.exceptionHandler = new LogExceptionHandler();
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>wx-java</artifactId>
|
||||
<version>3.9.2.B</version>
|
||||
<version>3.9.3.B</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>weixin-java-open</artifactId>
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package me.chanjar.weixin.open.api.impl;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
|
||||
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.open.api.WxOpenComponentService;
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>wx-java</artifactId>
|
||||
<version>3.9.2.B</version>
|
||||
<version>3.9.3.B</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.github.binarywang.wxpay.bean.ecommerce;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 完结分账 对象
|
||||
* <pre>
|
||||
* 文档地址:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/ecommerce/profitsharing/chapter3_5.shtml
|
||||
* </pre>
|
||||
* @author: f00lish
|
||||
* @date: 2020/09/12
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class FinishOrderRequest implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -8662837652326828377L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:二级商户号
|
||||
* 变量名:sub_mchid
|
||||
* 是否必填:是
|
||||
* 类型:string(32)
|
||||
* 描述:
|
||||
* 分账出资的电商平台二级商户,填写微信支付分配的商户号。
|
||||
* 示例值:1900000109
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "sub_mchid")
|
||||
private String subMchid;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:微信订单号
|
||||
* 变量名:transaction_id
|
||||
* 是否必填:是
|
||||
* 类型:string(32)
|
||||
* 描述:
|
||||
* 微信支付订单号。
|
||||
* 示例值:4208450740201411110007820472
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "transaction_id")
|
||||
private String transactionId;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:商户分账单号
|
||||
* 变量名:out_order_no
|
||||
* 是否必填:是
|
||||
* 类型:string(64)
|
||||
* 描述:
|
||||
* 商户系统内部的分账单号,在商户系统内部唯一(单次分账、多次分账、完结分账应使用不同的商户分账单号),同一分账单号多次请求等同一次。
|
||||
* 示例值:P20150806125346
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "out_order_no")
|
||||
private String outOrderNo;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:分账描述
|
||||
* 变量名:description
|
||||
* 是否必填:是
|
||||
* 类型:string(80)
|
||||
* 描述:
|
||||
* 分账的原因描述,分账账单中需要体现。
|
||||
* 示例值:分给商户1900000109
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "description")
|
||||
private String description;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
package com.github.binarywang.wxpay.bean.ecommerce;
|
||||
|
||||
/**
|
||||
* @author: f00lish
|
||||
* @date: 2020/09/17
|
||||
*/
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 退款申请
|
||||
* * <pre>
|
||||
* * 文档地址:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/ecommerce/refunds/chapter3_1.shtml
|
||||
* * </pre>
|
||||
* @author: f00lish
|
||||
* @date: 2020/09/14
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class RefundsRequest implements Serializable {
|
||||
private static final long serialVersionUID = -3186851559004865784L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:二级商户号
|
||||
* 变量名:sub_mchid
|
||||
* 是否必填:是
|
||||
* 类型:string(32)
|
||||
* 描述:
|
||||
* 微信支付分配二级商户的商户号。
|
||||
* 示例值:1900000109
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "sub_mchid")
|
||||
private String subMchid;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:电商平台APPID
|
||||
* 变量名:sp_appid
|
||||
* 是否必填:是
|
||||
* 类型:string(32)
|
||||
* 描述:
|
||||
* 电商平台在微信公众平台申请服务号对应的APPID,申请商户功能的时候微信支付会配置绑定关系。
|
||||
* 示例值:wx8888888888888888
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "sp_appid")
|
||||
private String spAppid;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:二级商户APPID
|
||||
* 变量名:sub_appid
|
||||
* 是否必填:否
|
||||
* 类型:string(32)
|
||||
* 描述:
|
||||
* 二级商户在微信申请公众号成功后分配的帐号ID,需要电商平台侧配置绑定关系才能传参。
|
||||
* 示例值:wxd678efh567hg6999
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "sub_appid")
|
||||
private String subAppid;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:微信订单号
|
||||
* 变量名:transaction_id
|
||||
* 是否必填:与out_order_no二选一
|
||||
* 类型:string(32)
|
||||
* 描述:
|
||||
* 微信支付订单号。
|
||||
* 示例值:4208450740201411110007820472
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "transaction_id")
|
||||
private String transactionId;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:商户订单号
|
||||
* 变量名:out_order_no
|
||||
* 是否必填:与transaction_id二选一
|
||||
* 类型:string(64)
|
||||
* 描述:
|
||||
* 原支付交易对应的商户订单号。
|
||||
* 示例值:P20150806125346
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "out_order_no")
|
||||
private String outOrderNo;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:商户退款单号
|
||||
* 变量名:out_refund_no
|
||||
* 是否必填:是
|
||||
* 类型:string(64)
|
||||
* 描述:
|
||||
* 商户系统内部的退款单号,商户系统内部唯一,只能是数字、大小写字母_-|*@,同一退款单号多次请求只退一笔。
|
||||
* 示例值:1217752501201407033233368018
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "out_refund_no")
|
||||
private String outRefundNo;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:退款原因
|
||||
* 变量名:reason
|
||||
* 是否必填:是
|
||||
* 类型:string(80)
|
||||
* 描述:
|
||||
* 若商户传入,会在下发给用户的退款消息中体现退款原因。
|
||||
* 注意:若订单退款金额≤1元,且属于部分退款,则不会在退款消息中体现退款原因
|
||||
* 示例值:商品已售完
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "reason")
|
||||
private String reason;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:订单金额
|
||||
* 变量名:amount
|
||||
* 是否必填:是
|
||||
* 类型:object
|
||||
* 描述:
|
||||
* 订单金额信息
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "amount")
|
||||
private Amount amount;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:退款结果回调url
|
||||
* 变量名:notify_url
|
||||
* 是否必填:是
|
||||
* 类型:string(256)
|
||||
* 描述:
|
||||
* 异步接收微信支付退款结果通知的回调地址,通知url必须为外网可访问的url,不能携带参数。 如果参数中传了notify_url,则商户平台上配置的回调地址将不会生效,优先回调当前传的地址。
|
||||
* 示例值:https://weixin.qq.com
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "notify_url")
|
||||
private String notifyUrl;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public static class Amount implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 7383027142329410399L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:退款金额
|
||||
* 变量名:refund
|
||||
* 是否必填:是
|
||||
* 类型:int
|
||||
* 描述:
|
||||
* 退款金额,币种的最小单位,只能为整数,不能超过原订单支付金额。
|
||||
* 示例值:888
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "refund")
|
||||
private Integer refund;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:原订单金额
|
||||
* 变量名:total
|
||||
* 是否必填:是
|
||||
* 类型:int64
|
||||
* 描述:
|
||||
* 订单总金额,单位为分。
|
||||
* 示例值:888
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "total")
|
||||
private Integer total;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:币类型
|
||||
* 变量名:currency
|
||||
* 是否必填:否
|
||||
* 类型:string(18)
|
||||
* 描述:
|
||||
* 符合ISO 4217标准的三位字母代码,目前只支持人民币:CNY。
|
||||
* 示例值:CNY
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "currency")
|
||||
private String currency;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
package com.github.binarywang.wxpay.bean.ecommerce;
|
||||
|
||||
/**
|
||||
* @author: f00lish
|
||||
* @date: 2020/09/17
|
||||
*/
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 退款结果
|
||||
* * <pre>
|
||||
* * 文档地址:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/ecommerce/refunds/chapter3_1.shtml
|
||||
* * </pre>
|
||||
* @author: f00lish
|
||||
* @date: 2020/09/14
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class RefundsResult implements Serializable {
|
||||
private static final long serialVersionUID = -3186851559004865784L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:微信退款单号
|
||||
* 变量名:refund_id
|
||||
* 是否必填:是
|
||||
* 类型:string(32)
|
||||
* 描述:
|
||||
* 微信支付退款订单号。
|
||||
* 示例值:1217752501201407033233368018
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "refund_id")
|
||||
private String refundId;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:商户订单号
|
||||
* 变量名:out_order_no
|
||||
* 是否必填:与transaction_id二选一
|
||||
* 类型:string(64)
|
||||
* 描述:
|
||||
* 原支付交易对应的商户订单号。
|
||||
* 示例值:P20150806125346
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "out_order_no")
|
||||
private String outOrderNo;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:退款创建时间
|
||||
* 变量名:create_time
|
||||
* 是否必填:是
|
||||
* 类型:string(64)
|
||||
* 描述:
|
||||
* 退款受理时间,遵循rfc3339标准格式,格式为YYYY-MM-DDTHH:mm:ss+TIMEZONE,YYYY-MM-DD表示年月日,T出现在字符串中,表示time元素的开头,HH:mm:ss表示时分秒,TIMEZONE表示时区(+08:00表示东八区时间,领先UTC 8小时,即北京时间)。例如:2015-05-20T13:29:35+08:00表示,北京时间2015年5月20日13点29分35秒。
|
||||
* 示例值:2018-06-08T10:34:56+08:00
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "create_time")
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:订单金额
|
||||
* 变量名:amount
|
||||
* 是否必填:是
|
||||
* 类型:object
|
||||
* 描述:
|
||||
* 订单金额信息
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "amount")
|
||||
private Amount amount;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:优惠退款详情
|
||||
* 变量名:promotion_detail
|
||||
* 是否必填:否
|
||||
* 类型:array
|
||||
* 描述:
|
||||
* 优惠退款功能信息
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "promotion_detail")
|
||||
private PromotionDetail[] promotionDetail;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public static class Amount implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 7383027142329410399L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:退款金额
|
||||
* 变量名:refund
|
||||
* 是否必填:是
|
||||
* 类型:int
|
||||
* 描述:
|
||||
* 退款金额,币种的最小单位,只能为整数,不能超过原订单支付金额。
|
||||
* 示例值:888
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "refund")
|
||||
private Integer refund;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:用户退款金额
|
||||
* 变量名:payer_refund
|
||||
* 是否必填:是
|
||||
* 类型:int64
|
||||
* 描述:
|
||||
* 退款给用户的金额,不包含所有优惠券金额。
|
||||
* 示例值:888
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "payer_refund")
|
||||
private Integer payerRefund;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:优惠退款金额
|
||||
* 变量名:discount_refund
|
||||
* 是否必填:否
|
||||
* 类型:int64
|
||||
* 描述:
|
||||
* 优惠券的退款金额,原支付单的优惠按比例退款。
|
||||
* 示例值:888
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "discount_refund")
|
||||
private Integer discountRefund;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:币类型
|
||||
* 变量名:currency
|
||||
* 是否必填:否
|
||||
* 类型:string(18)
|
||||
* 描述:
|
||||
* 符合ISO 4217标准的三位字母代码,目前只支持人民币:CNY。
|
||||
* 示例值:CNY
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "currency")
|
||||
private String currency;
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public static class PromotionDetail implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 7383027142329410399L;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:券ID
|
||||
* 变量名:promotion_id
|
||||
* 是否必填:是
|
||||
* 类型:string(32)
|
||||
* 描述:
|
||||
* 券或者立减优惠id。
|
||||
* 示例值:109519
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "promotion_id")
|
||||
private String promotionId;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:优惠范围
|
||||
* 变量名:scope
|
||||
* 是否必填:是
|
||||
* 类型:string(32)
|
||||
* 描述:
|
||||
* 枚举值:
|
||||
* GLOBAL:全场代金券
|
||||
* SINGLE:单品优惠
|
||||
* 示例值:SINGLE
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "scope")
|
||||
private String scope;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:优惠类型
|
||||
* 变量名:type
|
||||
* 是否必填:是
|
||||
* 类型:string(32)
|
||||
* 描述:
|
||||
* 枚举值:
|
||||
* COUPON:充值型代金券,商户需要预先充值营销经费
|
||||
* DISCOUNT:免充值型优惠券,商户不需要预先充值营销经费
|
||||
* 示例值:DISCOUNT
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "type")
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:优惠券面额
|
||||
* 变量名:amount
|
||||
* 是否必填:是
|
||||
* 类型:int
|
||||
* 描述:
|
||||
* 用户享受优惠的金额(优惠券面额=微信出资金额+商家出资金额+其他出资方金额 )。
|
||||
* 示例值:5
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "amount")
|
||||
private Integer amount;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 字段名:优惠退款金额
|
||||
* 变量名:refund_amount
|
||||
* 是否必填:是
|
||||
* 类型:int
|
||||
* 描述:
|
||||
* 代金券退款金额<=退款金额,退款金额-代金券或立减优惠退款金额为现金,说明详见《代金券或立减优惠》https://pay.weixin.qq.com/wiki/doc/api/tools/sp_coupon.php?chapter=12_1 。
|
||||
* 示例值:CNY
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName(value = "refund_amount")
|
||||
private Integer refundAmount;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -213,4 +213,28 @@ public interface EcommerceService {
|
||||
*/
|
||||
ReturnOrdersResult returnOrders(ReturnOrdersRequest request) throws WxPayException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 完结分账API
|
||||
* 文档地址: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/ecommerce/profitsharing/chapter3_5.shtml
|
||||
* </pre>
|
||||
*
|
||||
* @param request 完结分账请求
|
||||
* @return 返回数据 return orders result
|
||||
* @throws WxPayException the wx pay exception
|
||||
*/
|
||||
ProfitSharingResult finishOrder(FinishOrderRequest request) throws WxPayException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 退款申请API
|
||||
* 文档地址: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/ecommerce/refunds/chapter3_1.shtml
|
||||
* </pre>
|
||||
*
|
||||
* @param request 退款请求
|
||||
* @return 返回数据 return refunds result
|
||||
* @throws WxPayException the wx pay exception
|
||||
*/
|
||||
RefundsResult refunds(RefundsRequest request) throws WxPayException;
|
||||
|
||||
}
|
||||
|
||||
@@ -162,6 +162,20 @@ public class EcommerceServiceImpl implements EcommerceService {
|
||||
return GSON.fromJson(response, ReturnOrdersResult.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProfitSharingResult finishOrder(FinishOrderRequest request) throws WxPayException {
|
||||
String url = String.format("%s/v3/ecommerce/profitsharing/finish-order", this.payService.getPayBaseUrl());
|
||||
String response = this.payService.postV3(url, GSON.toJson(request));
|
||||
return GSON.fromJson(response, ProfitSharingResult.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RefundsResult refunds(RefundsRequest request) throws WxPayException {
|
||||
String url = String.format("%s/v3/ecommerce/refunds/apply", this.payService.getPayBaseUrl());
|
||||
String response = this.payService.postV3(url, GSON.toJson(request));
|
||||
return GSON.fromJson(response, RefundsResult.class);
|
||||
}
|
||||
|
||||
private boolean verifyNotifySign(SignatureHeader header, String data) {
|
||||
String beforeSign = String.format("%s\n%s\n%s\n",
|
||||
header.getTimeStamp(),
|
||||
|
||||
Reference in New Issue
Block a user