diff --git a/pom.xml b/pom.xml
index ada8d65a0..186ac3568 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
+ * 企业可通过此接口修改指定用户添加的客户的备注信息。 + * 请求方式: 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 + *+ * + * @param request 备注信息请求 + * @throws WxErrorException . + */ + void updateRemark(WxCpUpdateRemarkRequest request) throws WxErrorException; + /** * 获取客户列表. *
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java
index 007dff78f..b5a9579e0 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpGroupRobotService.java
@@ -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 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 mentionedList, List 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 articleList) throws WxErrorException;
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOaCalendarService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOaCalendarService.java
new file mode 100644
index 000000000..8dc0d3136
--- /dev/null
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOaCalendarService.java
@@ -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 Binary Wang
+ * @date 2020-09-20
+ */
+public interface WxCpOaCalendarService {
+ /**
+ * 创建日历.
+ *
+ * 该接口用于通过应用在企业内创建一个日历。
+ * 注: 企业微信需要更新到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
+ *
+ *
+ * @param calendar 日历对象
+ * @return 日历ID
+ * @throws WxErrorException .
+ */
+ String add(WxCpOaCalendar calendar) throws WxErrorException;
+}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java
index 3378f8862..465090f19 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java
@@ -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);
+
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java
index 97faa4c96..81c577eb9 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java
@@ -52,6 +52,7 @@ public abstract class BaseWxCpServiceImpl 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 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 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;
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImpl.java
index b64ec0e87..2fa5da03e 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImpl.java
@@ -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 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);
}
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java
index c5e71f5f2..996249eb3 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImpl.java
@@ -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 mentionedList, List 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 articleList) throws WxErrorException {
- WxCpGroupRobotMessage message = new WxCpGroupRobotMessage()
+ this.sendNews(this.getWebhookUrl(), articleList);
+ }
+
+ @Override
+ public void sendText(String webhookUrl, String content, List mentionedList, List 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 articleList) throws WxErrorException {
+ this.cpService.postWithoutToken(this.getWebhookUrl(), new WxCpGroupRobotMessage()
.setMsgType(GroupRobotMsgType.NEWS)
- .setArticles(articleList);
- cpService.postWithoutToken(this.getApiUrl(), message.toJson());
+ .setArticles(articleList).toJson());
}
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOaCalendarServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOaCalendarServiceImpl.java
new file mode 100644
index 000000000..c5b0a4aac
--- /dev/null
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOaCalendarServiceImpl.java
@@ -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 Binary Wang
+ * @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());
+ }
+}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOnTpImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOnTpImpl.java
index 35eab626a..aa30385d6 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOnTpImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOnTpImpl.java
@@ -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;
/**
*
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/WxCpUpdateRemarkRequest.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/WxCpUpdateRemarkRequest.java
new file mode 100644
index 000000000..678995590
--- /dev/null
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/external/WxCpUpdateRemarkRequest.java
@@ -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 Binary Wang
+ * @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);
+ }
+
+ /**
+ *
+ * 字段名:userid
+ * 是否必须:是
+ * 描述:企业成员的userid
+ *
+ */
+ @SerializedName("userid")
+ private String userId;
+
+ /**
+ *
+ * 字段名:external_userid
+ * 是否必须:是
+ * 描述:外部联系人userid
+ *
+ */
+ @SerializedName("external_userid")
+ private String externalUserId;
+
+ /**
+ *
+ * 字段名:remark
+ * 是否必须:否
+ * 描述:此用户对外部联系人的备注,最多20个字符
+ *
+ */
+ @SerializedName("remark")
+ private String remark;
+
+ /**
+ *
+ * 字段名:description
+ * 是否必须:否
+ * 描述:此用户对外部联系人的描述,最多150个字符
+ *
+ */
+ @SerializedName("description")
+ private String description;
+
+ /**
+ *
+ * 字段名:remark_company
+ * 是否必须:否
+ * 描述:此用户对外部联系人备注的所属公司名称,最多20个字符
+ *
+ */
+ @SerializedName("remark_company")
+ private String remarkCompany;
+
+ /**
+ *
+ * 字段名:remark_mobiles
+ * 是否必须:否
+ * 描述:此用户对外部联系人备注的手机号
+ *
+ */
+ @SerializedName("remark_mobiles")
+ private String[] remarkMobiles;
+
+ /**
+ *
+ * 字段名:remark_pic_mediaid
+ * 是否必须:否
+ * 描述:备注图片的mediaid,
+ *
+ */
+ @SerializedName("remark_pic_mediaid")
+ private String remarkPicMediaId;
+
+}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpXmlMessage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpXmlMessage.java
index a035f1d54..42f25add3 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpXmlMessage.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpXmlMessage.java
@@ -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;
+
/**
* 扩展属性.
*/
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/calendar/WxCpOaCalendar.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/calendar/WxCpOaCalendar.java
new file mode 100644
index 000000000..d4d9fd7f7
--- /dev/null
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/calendar/WxCpOaCalendar.java
@@ -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 Binary Wang
+ * @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 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));
+ }
+}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java
index 6fd219ff0..66132553b 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java
@@ -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";
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java
index 98bfd90ba..6e8fbb468 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java
@@ -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";
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageHandler.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageHandler.java
index 68e38d3bb..5d77444dd 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageHandler.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageHandler.java
@@ -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 context,
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageInterceptor.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageInterceptor.java
index 5bcca55ee..45d3976b7 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageInterceptor.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageInterceptor.java
@@ -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 context,
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageMatcher.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageMatcher.java
index 42db8f5dd..7fc758117 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageMatcher.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageMatcher.java
@@ -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);
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageRouter.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageRouter.java
index bf1ad6e5f..92de0c238 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageRouter.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageRouter.java
@@ -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.*;
/**
*
@@ -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 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(2));
+ return this.route(wxMessage, new HashMap<>(2));
}
private boolean isMsgDuplicated(WxCpXmlMessage wxMessage) {
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageRouterRule.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageRouterRule.java
index 3c7b5c66f..739bb0330 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageRouterRule.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageRouterRule.java
@@ -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 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 handlers) {
- this.handlers = handlers;
- }
-
- public void setInterceptors(List 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;
- }
}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageHandler.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageHandler.java
new file mode 100644
index 000000000..9ab718180
--- /dev/null
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageHandler.java
@@ -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 context,
+ WxCpTpService wxCpService,
+ WxSessionManager sessionManager) throws WxErrorException;
+
+}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageInterceptor.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageInterceptor.java
new file mode 100644
index 000000000..fe5ceefa0
--- /dev/null
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageInterceptor.java
@@ -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 context,
+ WxCpTpService wxCpService,
+ WxSessionManager sessionManager) throws WxErrorException;
+
+}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageMatcher.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageMatcher.java
new file mode 100644
index 000000000..8f7decf4b
--- /dev/null
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageMatcher.java
@@ -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);
+
+}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageRouter.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageRouter.java
new file mode 100644
index 000000000..147f324db
--- /dev/null
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageRouter.java
@@ -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.*;
+
+/**
+ *
+ * 微信消息路由器,通过代码化的配置,把来自微信的消息交给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);
+ *
+ *
+ *
+ * @author Daniel Qian
+ */
+@Slf4j
+public class WxCpTpMessageRouter {
+ private static final int DEFAULT_THREAD_POOL_SIZE = 100;
+ private final List 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();
+ }
+
+ /**
+ *
+ * 设置自定义的 {@link ExecutorService}
+ * 如果不调用该方法,默认使用 Executors.newFixedThreadPool(100)
+ *
+ */
+ public void setExecutorService(ExecutorService executorService) {
+ this.executorService = executorService;
+ }
+
+ /**
+ *
+ * 设置自定义的 {@link WxMessageDuplicateChecker}
+ * 如果不调用该方法,默认使用 {@link WxMessageInMemoryDuplicateChecker}
+ *
+ */
+ public void setMessageDuplicateChecker(WxMessageDuplicateChecker messageDuplicateChecker) {
+ this.messageDuplicateChecker = messageDuplicateChecker;
+ }
+
+ /**
+ *
+ * 设置自定义的{@link WxSessionManager}
+ * 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.session.StandardSessionManager}
+ *
+ */
+ public void setSessionManager(WxSessionManager sessionManager) {
+ this.sessionManager = sessionManager;
+ }
+
+ /**
+ *
+ * 设置自定义的{@link WxErrorExceptionHandler}
+ * 如果不调用该方法,默认使用 {@link LogExceptionHandler}
+ *
+ */
+ public void setExceptionHandler(WxErrorExceptionHandler exceptionHandler) {
+ this.exceptionHandler = exceptionHandler;
+ }
+
+ List getRules() {
+ return this.rules;
+ }
+
+ /**
+ * 开始一个新的Route规则.
+ */
+ public WxCpTpMessageRouterRule rule() {
+ return new WxCpTpMessageRouterRule(this);
+ }
+
+ /**
+ * 处理微信消息.
+ */
+ public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map context) {
+ if (isMsgDuplicated(wxMessage)) {
+ // 如果是重复消息,那么就不做处理
+ return null;
+ }
+
+ final List 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 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();
+ }
+
+ }
+}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageRouterRule.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageRouterRule.java
new file mode 100644
index 000000000..8494978e2
--- /dev/null
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageRouterRule.java
@@ -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 handlers = new ArrayList<>();
+
+ private List 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 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;
+
+ }
+
+
+}
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java
similarity index 72%
rename from weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java
rename to weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java
index ad2f403af..26da1ddd4 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java
@@ -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 {
*
*
* @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 {
*
*
* @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 {
* 原来的方法实现不全
*
*
- * @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 {
*
* 获取预授权链接
*
+ *
* @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}的实现方法
*
*
+ * @param
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java
index 9a0fbdbd3..8cace0fe3 100644
--- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java
@@ -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());
+ }
}
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImplTest.java
index 40597fa24..57bd9b750 100644
--- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImplTest.java
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpGroupRobotServiceImplTest.java
@@ -46,7 +46,7 @@ public class WxCpGroupRobotServiceImplTest {
">类型:用户反馈 \n" +
">普通用户反馈:117例 \n" +
">VIP用户反馈:15例";
- robotService.sendMarkDown(content);
+ robotService.sendMarkdown(content);
}
@Test
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpOaCalendarServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpOaCalendarServiceImplTest.java
new file mode 100644
index 000000000..905dc4995
--- /dev/null
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpOaCalendarServiceImplTest.java
@@ -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 Binary Wang
+ * @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());
+ }
+}
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpOaServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpOaServiceImplTest.java
index 6f3a76b0a..758f77970 100644
--- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpOaServiceImplTest.java
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpOaServiceImplTest.java
@@ -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 results = wxService.getOAService()
+ List 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 results = wxService.getOAService().getCheckinOption(now, Lists.newArrayList("binary"));
+ List 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
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/external/WxCpUpdateRemarkRequestTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/external/WxCpUpdateRemarkRequestTest.java
new file mode 100644
index 000000000..9564cdf9b
--- /dev/null
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/external/WxCpUpdateRemarkRequestTest.java
@@ -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 Binary Wang
+ * @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());
+ }
+}
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/oa/calendar/WxCpOaCalendarTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/oa/calendar/WxCpOaCalendarTest.java
new file mode 100644
index 000000000..761b0f8f9
--- /dev/null
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/oa/calendar/WxCpOaCalendarTest.java
@@ -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 Binary Wang
+ * @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());
+ }
+}
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoServer.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoServer.java
index 2067e03eb..52bc8e2ab 100644
--- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoServer.java
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoServer.java
@@ -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 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 context, WxCpService wxService,
- WxSessionManager sessionManager) {
- String href = "测试oauth2";
- return WxCpXmlOutMessage.TEXT().content(href)
- .fromUser(wxMessage.getToUserName())
- .toUser(wxMessage.getFromUserName()).build();
- }
+ WxCpMessageHandler oauth2handler = (wxMessage, context, wxService, sessionManager) -> {
+ String href = "测试oauth2";
+ 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 context, WxCpService wxCpService, WxSessionManager sessionManager) throws WxErrorException {
- System.out.println("通讯录发生变更");
- return null;
- }
+ .handler((wxMessage, context, wxCpService, sessionManager) -> {
+ System.out.println("通讯录发生变更");
+ return null;
})
.end();
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpEndpointServlet.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpEndpointServlet.java
index 3f48c3213..a5e785ffd 100644
--- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpEndpointServlet.java
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpEndpointServlet.java
@@ -61,7 +61,6 @@ public class WxCpEndpointServlet extends HttpServlet {
response.getWriter().write(outMessage.toEncryptedXml(this.wxCpConfigStorage));
}
- return;
}
}
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImplTest.java
similarity index 91%
rename from weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImplTest.java
rename to weixin-java-cp/src/test/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImplTest.java
index 9f7973561..83ace79f3 100644
--- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImplTest.java
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImplTest.java
@@ -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
diff --git a/weixin-java-miniapp/pom.xml b/weixin-java-miniapp/pom.xml
index 41ddb0a63..39925c366 100644
--- a/weixin-java-miniapp/pom.xml
+++ b/weixin-java-miniapp/pom.xml
@@ -7,7 +7,7 @@
com.github.binarywang
wx-java
- 3.9.2.B
+ 3.9.3.B
weixin-java-miniapp
diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageRouter.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageRouter.java
index e932da641..031c688c5 100644
--- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageRouter.java
+++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageRouter.java
@@ -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(), 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(2));
+ return this.route(wxMessage, new HashMap<>(2));
}
private boolean isMsgDuplicated(WxMaMessage wxMessage) {
diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageRouterRule.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageRouterRule.java
index 41f3e9957..99181e043 100644
--- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageRouterRule.java
+++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageRouterRule.java
@@ -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;
}
diff --git a/weixin-java-mp/pom.xml b/weixin-java-mp/pom.xml
index d1f1ac0fd..8a9e7b1f7 100644
--- a/weixin-java-mp/pom.xml
+++ b/weixin-java-mp/pom.xml
@@ -7,7 +7,7 @@
com.github.binarywang
wx-java
- 3.9.2.B
+ 3.9.3.B
weixin-java-mp
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMessageRouter.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMessageRouter.java
index 9b2ca03fe..36556d984 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMessageRouter.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMessageRouter.java
@@ -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.*;
/**
*
@@ -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();
diff --git a/weixin-java-open/pom.xml b/weixin-java-open/pom.xml
index 19d188ac1..2d35bc327 100644
--- a/weixin-java-open/pom.xml
+++ b/weixin-java-open/pom.xml
@@ -7,7 +7,7 @@
com.github.binarywang
wx-java
- 3.9.2.B
+ 3.9.3.B
weixin-java-open
diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenMpServiceImpl.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenMpServiceImpl.java
index 5efa429ad..ab0a9055b 100644
--- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenMpServiceImpl.java
+++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenMpServiceImpl.java
@@ -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;
/**
diff --git a/weixin-java-pay/pom.xml b/weixin-java-pay/pom.xml
index 629c6fc65..71f9458f5 100644
--- a/weixin-java-pay/pom.xml
+++ b/weixin-java-pay/pom.xml
@@ -5,7 +5,7 @@
com.github.binarywang
wx-java
- 3.9.2.B
+ 3.9.3.B
4.0.0
diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/FinishOrderRequest.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/FinishOrderRequest.java
new file mode 100644
index 000000000..1b09ba6ff
--- /dev/null
+++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/FinishOrderRequest.java
@@ -0,0 +1,81 @@
+package com.github.binarywang.wxpay.bean.ecommerce;
+
+import com.google.gson.annotations.SerializedName;
+import lombok.*;
+
+import java.io.Serializable;
+
+/**
+ * 完结分账 对象
+ *
+ * 文档地址:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/ecommerce/profitsharing/chapter3_5.shtml
+ *
+ * @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;
+
+ /**
+ *
+ * 字段名:二级商户号
+ * 变量名:sub_mchid
+ * 是否必填:是
+ * 类型:string(32)
+ * 描述:
+ * 分账出资的电商平台二级商户,填写微信支付分配的商户号。
+ * 示例值:1900000109
+ *
+ */
+ @SerializedName(value = "sub_mchid")
+ private String subMchid;
+
+ /**
+ *
+ * 字段名:微信订单号
+ * 变量名:transaction_id
+ * 是否必填:是
+ * 类型:string(32)
+ * 描述:
+ * 微信支付订单号。
+ * 示例值:4208450740201411110007820472
+ *
+ */
+ @SerializedName(value = "transaction_id")
+ private String transactionId;
+
+ /**
+ *
+ * 字段名:商户分账单号
+ * 变量名:out_order_no
+ * 是否必填:是
+ * 类型:string(64)
+ * 描述:
+ * 商户系统内部的分账单号,在商户系统内部唯一(单次分账、多次分账、完结分账应使用不同的商户分账单号),同一分账单号多次请求等同一次。
+ * 示例值:P20150806125346
+ *
+ */
+ @SerializedName(value = "out_order_no")
+ private String outOrderNo;
+
+ /**
+ *
+ * 字段名:分账描述
+ * 变量名:description
+ * 是否必填:是
+ * 类型:string(80)
+ * 描述:
+ * 分账的原因描述,分账账单中需要体现。
+ * 示例值:分给商户1900000109
+ *
+ */
+ @SerializedName(value = "description")
+ private String description;
+
+
+}
diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/RefundsRequest.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/RefundsRequest.java
new file mode 100644
index 000000000..9277b3557
--- /dev/null
+++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/RefundsRequest.java
@@ -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;
+
+/**
+ * 退款申请
+ * *
+ * * 文档地址:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/ecommerce/refunds/chapter3_1.shtml
+ * *
+ * @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;
+
+ /**
+ *
+ * 字段名:二级商户号
+ * 变量名:sub_mchid
+ * 是否必填:是
+ * 类型:string(32)
+ * 描述:
+ * 微信支付分配二级商户的商户号。
+ * 示例值:1900000109
+ *
+ */
+ @SerializedName(value = "sub_mchid")
+ private String subMchid;
+
+ /**
+ *
+ * 字段名:电商平台APPID
+ * 变量名:sp_appid
+ * 是否必填:是
+ * 类型:string(32)
+ * 描述:
+ * 电商平台在微信公众平台申请服务号对应的APPID,申请商户功能的时候微信支付会配置绑定关系。
+ * 示例值:wx8888888888888888
+ *
+ */
+ @SerializedName(value = "sp_appid")
+ private String spAppid;
+
+ /**
+ *
+ * 字段名:二级商户APPID
+ * 变量名:sub_appid
+ * 是否必填:否
+ * 类型:string(32)
+ * 描述:
+ * 二级商户在微信申请公众号成功后分配的帐号ID,需要电商平台侧配置绑定关系才能传参。
+ * 示例值:wxd678efh567hg6999
+ *
+ */
+ @SerializedName(value = "sub_appid")
+ private String subAppid;
+
+ /**
+ *
+ * 字段名:微信订单号
+ * 变量名:transaction_id
+ * 是否必填:与out_order_no二选一
+ * 类型:string(32)
+ * 描述:
+ * 微信支付订单号。
+ * 示例值:4208450740201411110007820472
+ *
+ */
+ @SerializedName(value = "transaction_id")
+ private String transactionId;
+
+ /**
+ *
+ * 字段名:商户订单号
+ * 变量名:out_order_no
+ * 是否必填:与transaction_id二选一
+ * 类型:string(64)
+ * 描述:
+ * 原支付交易对应的商户订单号。
+ * 示例值:P20150806125346
+ *
+ */
+ @SerializedName(value = "out_order_no")
+ private String outOrderNo;
+
+ /**
+ *
+ * 字段名:商户退款单号
+ * 变量名:out_refund_no
+ * 是否必填:是
+ * 类型:string(64)
+ * 描述:
+ * 商户系统内部的退款单号,商户系统内部唯一,只能是数字、大小写字母_-|*@,同一退款单号多次请求只退一笔。
+ * 示例值:1217752501201407033233368018
+ *
+ */
+ @SerializedName(value = "out_refund_no")
+ private String outRefundNo;
+
+ /**
+ *
+ * 字段名:退款原因
+ * 变量名:reason
+ * 是否必填:是
+ * 类型:string(80)
+ * 描述:
+ * 若商户传入,会在下发给用户的退款消息中体现退款原因。
+ * 注意:若订单退款金额≤1元,且属于部分退款,则不会在退款消息中体现退款原因
+ * 示例值:商品已售完
+ *
+ */
+ @SerializedName(value = "reason")
+ private String reason;
+
+ /**
+ *
+ * 字段名:订单金额
+ * 变量名:amount
+ * 是否必填:是
+ * 类型:object
+ * 描述:
+ * 订单金额信息
+ *
+ */
+ @SerializedName(value = "amount")
+ private Amount amount;
+
+ /**
+ *
+ * 字段名:退款结果回调url
+ * 变量名:notify_url
+ * 是否必填:是
+ * 类型:string(256)
+ * 描述:
+ * 异步接收微信支付退款结果通知的回调地址,通知url必须为外网可访问的url,不能携带参数。 如果参数中传了notify_url,则商户平台上配置的回调地址将不会生效,优先回调当前传的地址。
+ * 示例值:https://weixin.qq.com
+ *
+ */
+ @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;
+
+ /**
+ *
+ * 字段名:退款金额
+ * 变量名:refund
+ * 是否必填:是
+ * 类型:int
+ * 描述:
+ * 退款金额,币种的最小单位,只能为整数,不能超过原订单支付金额。
+ * 示例值:888
+ *
+ */
+ @SerializedName(value = "refund")
+ private Integer refund;
+
+ /**
+ *
+ * 字段名:原订单金额
+ * 变量名:total
+ * 是否必填:是
+ * 类型:int64
+ * 描述:
+ * 订单总金额,单位为分。
+ * 示例值:888
+ *
+ */
+ @SerializedName(value = "total")
+ private Integer total;
+
+ /**
+ *
+ * 字段名:币类型
+ * 变量名:currency
+ * 是否必填:否
+ * 类型:string(18)
+ * 描述:
+ * 符合ISO 4217标准的三位字母代码,目前只支持人民币:CNY。
+ * 示例值:CNY
+ *
+ */
+ @SerializedName(value = "currency")
+ private String currency;
+
+ }
+
+}
diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/RefundsResult.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/RefundsResult.java
new file mode 100644
index 000000000..9ba480104
--- /dev/null
+++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/RefundsResult.java
@@ -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;
+
+/**
+ * 退款结果
+ * *
+ * * 文档地址:https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/ecommerce/refunds/chapter3_1.shtml
+ * *
+ * @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;
+
+ /**
+ *
+ * 字段名:微信退款单号
+ * 变量名:refund_id
+ * 是否必填:是
+ * 类型:string(32)
+ * 描述:
+ * 微信支付退款订单号。
+ * 示例值:1217752501201407033233368018
+ *
+ */
+ @SerializedName(value = "refund_id")
+ private String refundId;
+
+ /**
+ *
+ * 字段名:商户订单号
+ * 变量名:out_order_no
+ * 是否必填:与transaction_id二选一
+ * 类型:string(64)
+ * 描述:
+ * 原支付交易对应的商户订单号。
+ * 示例值:P20150806125346
+ *
+ */
+ @SerializedName(value = "out_order_no")
+ private String outOrderNo;
+
+ /**
+ *
+ * 字段名:退款创建时间
+ * 变量名: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
+ *
+ */
+ @SerializedName(value = "create_time")
+ private Date createTime;
+
+ /**
+ *
+ * 字段名:订单金额
+ * 变量名:amount
+ * 是否必填:是
+ * 类型:object
+ * 描述:
+ * 订单金额信息
+ *
+ */
+ @SerializedName(value = "amount")
+ private Amount amount;
+
+ /**
+ *
+ * 字段名:优惠退款详情
+ * 变量名:promotion_detail
+ * 是否必填:否
+ * 类型:array
+ * 描述:
+ * 优惠退款功能信息
+ *
+ */
+ @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;
+
+ /**
+ *
+ * 字段名:退款金额
+ * 变量名:refund
+ * 是否必填:是
+ * 类型:int
+ * 描述:
+ * 退款金额,币种的最小单位,只能为整数,不能超过原订单支付金额。
+ * 示例值:888
+ *
+ */
+ @SerializedName(value = "refund")
+ private Integer refund;
+
+ /**
+ *
+ * 字段名:用户退款金额
+ * 变量名:payer_refund
+ * 是否必填:是
+ * 类型:int64
+ * 描述:
+ * 退款给用户的金额,不包含所有优惠券金额。
+ * 示例值:888
+ *
+ */
+ @SerializedName(value = "payer_refund")
+ private Integer payerRefund;
+
+ /**
+ *
+ * 字段名:优惠退款金额
+ * 变量名:discount_refund
+ * 是否必填:否
+ * 类型:int64
+ * 描述:
+ * 优惠券的退款金额,原支付单的优惠按比例退款。
+ * 示例值:888
+ *
+ */
+ @SerializedName(value = "discount_refund")
+ private Integer discountRefund;
+
+ /**
+ *
+ * 字段名:币类型
+ * 变量名:currency
+ * 是否必填:否
+ * 类型:string(18)
+ * 描述:
+ * 符合ISO 4217标准的三位字母代码,目前只支持人民币:CNY。
+ * 示例值:CNY
+ *
+ */
+ @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;
+
+ /**
+ *
+ * 字段名:券ID
+ * 变量名:promotion_id
+ * 是否必填:是
+ * 类型:string(32)
+ * 描述:
+ * 券或者立减优惠id。
+ * 示例值:109519
+ *
+ */
+ @SerializedName(value = "promotion_id")
+ private String promotionId;
+
+ /**
+ *
+ * 字段名:优惠范围
+ * 变量名:scope
+ * 是否必填:是
+ * 类型:string(32)
+ * 描述:
+ * 枚举值:
+ * GLOBAL:全场代金券
+ * SINGLE:单品优惠
+ * 示例值:SINGLE
+ *
+ */
+ @SerializedName(value = "scope")
+ private String scope;
+
+ /**
+ *
+ * 字段名:优惠类型
+ * 变量名:type
+ * 是否必填:是
+ * 类型:string(32)
+ * 描述:
+ * 枚举值:
+ * COUPON:充值型代金券,商户需要预先充值营销经费
+ * DISCOUNT:免充值型优惠券,商户不需要预先充值营销经费
+ * 示例值:DISCOUNT
+ *
+ */
+ @SerializedName(value = "type")
+ private String type;
+
+ /**
+ *
+ * 字段名:优惠券面额
+ * 变量名:amount
+ * 是否必填:是
+ * 类型:int
+ * 描述:
+ * 用户享受优惠的金额(优惠券面额=微信出资金额+商家出资金额+其他出资方金额 )。
+ * 示例值:5
+ *
+ */
+ @SerializedName(value = "amount")
+ private Integer amount;
+
+ /**
+ *
+ * 字段名:优惠退款金额
+ * 变量名:refund_amount
+ * 是否必填:是
+ * 类型:int
+ * 描述:
+ * 代金券退款金额<=退款金额,退款金额-代金券或立减优惠退款金额为现金,说明详见《代金券或立减优惠》https://pay.weixin.qq.com/wiki/doc/api/tools/sp_coupon.php?chapter=12_1 。
+ * 示例值:CNY
+ *
+ */
+ @SerializedName(value = "refund_amount")
+ private Integer refundAmount;
+
+ }
+
+}
diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/EcommerceService.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/EcommerceService.java
index 90997fb37..6d8160b00 100644
--- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/EcommerceService.java
+++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/EcommerceService.java
@@ -213,4 +213,28 @@ public interface EcommerceService {
*/
ReturnOrdersResult returnOrders(ReturnOrdersRequest request) throws WxPayException;
+ /**
+ *
+ * 完结分账API
+ * 文档地址: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/ecommerce/profitsharing/chapter3_5.shtml
+ *
+ *
+ * @param request 完结分账请求
+ * @return 返回数据 return orders result
+ * @throws WxPayException the wx pay exception
+ */
+ ProfitSharingResult finishOrder(FinishOrderRequest request) throws WxPayException;
+
+ /**
+ *
+ * 退款申请API
+ * 文档地址: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/ecommerce/refunds/chapter3_1.shtml
+ *
+ *
+ * @param request 退款请求
+ * @return 返回数据 return refunds result
+ * @throws WxPayException the wx pay exception
+ */
+ RefundsResult refunds(RefundsRequest request) throws WxPayException;
+
}
diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/EcommerceServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/EcommerceServiceImpl.java
index b792771a1..53ecb9ceb 100644
--- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/EcommerceServiceImpl.java
+++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/EcommerceServiceImpl.java
@@ -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(),