diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaCustomserviceWorkService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaCustomserviceWorkService.java new file mode 100644 index 000000000..bf119bc59 --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaCustomserviceWorkService.java @@ -0,0 +1,57 @@ +package cn.binarywang.wx.miniapp.api; + +import cn.binarywang.wx.miniapp.bean.customservice.WxMaCustomserviceResult; +import me.chanjar.weixin.common.error.WxErrorException; + + +/** + *
+ *  小程序 - 微信客服 相关接口
+ *  负责处理 https://api.weixin.qq.com/customservice/work/**
+ *  文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/kf-work/getKfWorkBound.html
+ *  绑定的企业ID,需和小程序主体一致。
+ *  目前仅支持绑定非个人小程序。
+ *  Created by tryking123 on 2025/8/18.
+ * 
+ * + * @author tryking123 + */ +public interface WxMaCustomserviceWorkService { + + /** + * 查询小程序的微信客服绑定情况 + */ + String GET_CUSTOMSERVICE_URL = "https://api.weixin.qq.com/customservice/work/get"; + /** + * 为小程序绑定微信客服 注:此接口绑定的企业ID需完成企业认证 + */ + String BIND_CUSTOMSERVICE_URL = "https://api.weixin.qq.com/customservice/work/bind"; + /** + * 为小程序解除绑定微信客服 + */ + String UNBIND_CUSTOMSERVICE_URL = "https://api.weixin.qq.com/customservice/work/unbind"; + + /** + * 查询小程序的微信客服绑定情况 + * + * @return 成功示例json { "errcode": 0,"entityName": "XXXXX有限公司","corpid": "wwee11111xxxxxxx","bindTime": 1694611289 } + * @throws WxErrorException + */ + WxMaCustomserviceResult getCustomservice() throws WxErrorException; + + /** + * 绑定微信客服 + * @param corpid 企业ID,获取方式参考:https://developer.work.weixin.qq.com/document/path/90665#corpid + * @return 成功示例json { "errcode": 0 } + * @throws WxErrorException + */ + WxMaCustomserviceResult bindCustomservice(String corpid) throws WxErrorException; + + /** + * 解除绑定微信客服 + * @param corpid 企业ID,获取方式参考:https://developer.work.weixin.qq.com/document/path/90665#corpid + * @return 成功示例json { "errcode": 0 } + * @throws WxErrorException + */ + WxMaCustomserviceResult unbindCustomservice(String corpid) throws WxErrorException; +} diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java index 87405edd8..ef3a46bad 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java @@ -278,6 +278,13 @@ public interface WxMaService extends WxService { */ WxMaCodeService getCodeService(); + /** + * 获取小程序 - 微信客服。 + * + * @return 微信客服服务对象WxMaCustomserviceWorkService + */ + WxMaCustomserviceWorkService getCustomserviceWorkService(); + /** * 获取jsapi操作相关服务对象。 * diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java index 4a5ca1927..ec33dede0 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java @@ -112,6 +112,7 @@ public abstract class BaseWxMaServiceImpl implements WxMaService, RequestH private final WxMaSchemeService schemeService = new WxMaSchemeServiceImpl(this); private final WxMaAnalysisService analysisService = new WxMaAnalysisServiceImpl(this); private final WxMaCodeService codeService = new WxMaCodeServiceImpl(this); + private final WxMaCustomserviceWorkService customserviceWorkService = new WxMaCustomserviceWorkServiceImpl(this); private final WxMaInternetService internetService = new WxMaInternetServiceImpl(this); private final WxMaSettingService settingService = new WxMaSettingServiceImpl(this); private final WxMaJsapiService jsapiService = new WxMaJsapiServiceImpl(this); @@ -651,6 +652,11 @@ public abstract class BaseWxMaServiceImpl implements WxMaService, RequestH return this.codeService; } + @Override + public WxMaCustomserviceWorkService getCustomserviceWorkService() { + return this.customserviceWorkService; + } + @Override public WxMaJsapiService getJsapiService() { return this.jsapiService; diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaCustomserviceWorkServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaCustomserviceWorkServiceImpl.java new file mode 100644 index 000000000..0a30f86df --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaCustomserviceWorkServiceImpl.java @@ -0,0 +1,51 @@ +package cn.binarywang.wx.miniapp.api.impl; + +import cn.binarywang.wx.miniapp.api.WxMaCustomserviceWorkService; +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.bean.customservice.WxMaCustomserviceResult; +import com.google.gson.JsonObject; +import lombok.RequiredArgsConstructor; +import me.chanjar.weixin.common.error.WxErrorException; + + + +/** + *
+ *  小程序 - 微信客服 相关接口
+ *  负责处理 https://api.weixin.qq.com/customservice/work/**
+ *  文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/kf-work/getKfWorkBound.html
+ *  绑定的企业ID,需和小程序主体一致。
+ *  目前仅支持绑定非个人小程序。
+ *  Created by tryking123 on 2025/8/18.
+ * 
+ * + * @author tryking123 + */ +@RequiredArgsConstructor +public class WxMaCustomserviceWorkServiceImpl implements WxMaCustomserviceWorkService { + private static final String CORPID = "corpid"; + + private final WxMaService service; + + @Override + public WxMaCustomserviceResult getCustomservice() throws WxErrorException { + String responseContent = this.service.get(GET_CUSTOMSERVICE_URL, null); + return WxMaCustomserviceResult.fromJson(responseContent); + } + + @Override + public WxMaCustomserviceResult bindCustomservice(String corpid) throws WxErrorException { + JsonObject paramJson = new JsonObject(); + paramJson.addProperty(CORPID, corpid); + String response = this.service.post(BIND_CUSTOMSERVICE_URL, paramJson); + return WxMaCustomserviceResult.fromJson(response); + } + + @Override + public WxMaCustomserviceResult unbindCustomservice(String corpid) throws WxErrorException { + JsonObject paramJson = new JsonObject(); + paramJson.addProperty(CORPID, corpid); + String response = this.service.post(UNBIND_CUSTOMSERVICE_URL, paramJson); + return WxMaCustomserviceResult.fromJson(response); + } +} diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/customservice/WxMaCustomserviceResult.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/customservice/WxMaCustomserviceResult.java new file mode 100644 index 000000000..e7a9a46de --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/customservice/WxMaCustomserviceResult.java @@ -0,0 +1,56 @@ +package cn.binarywang.wx.miniapp.bean.customservice; + +import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.gson.annotations.SerializedName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.util.List; + +/** + * 客服绑定结果信息,包括错误码、主体名称、企业ID和绑定时间戳。 + *

+ * 字段说明: + *

+ * @author tryking123 + * @since 2025/8/18 17:40 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class WxMaCustomserviceResult implements Serializable { + private static final long serialVersionUID = 8854979405505241314L; + + @SerializedName("errcode") + private Integer errCode; + + /** + * 该小程序的主体名称,未绑定时不返回 + */ + @SerializedName("entityName") + private String entityName; + + /** + * 企业ID,未绑定时不返回 + */ + @SerializedName("corpid") + private String corpid; + + /** 接受绑定时间戳,ms */ + @JsonProperty("bindTime") + private Long bindTime; + + public static WxMaCustomserviceResult fromJson(String json) { + return WxMaGsonBuilder.create().fromJson(json, WxMaCustomserviceResult.class); + } +}