Skip to content

Commit

Permalink
🆕 #1686 微信公众号增加对话能力(原导购助手)部分接口,如添加顾问、获取顾问信息等
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Oct 6, 2020
1 parent 64402ab commit 6948044
Show file tree
Hide file tree
Showing 15 changed files with 389 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public WxErrorException(WxError error, Throwable cause) {
this.error = error;
}

public WxErrorException(Throwable cause) {
super(cause.getMessage(), cause);
this.error = WxError.builder().errorCode(-1).errorMsg(cause.getMessage()).build();
}

public WxError getError() {
return this.error;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.chanjar.weixin.common.service;

import com.google.gson.JsonObject;
import me.chanjar.weixin.common.bean.ToJson;
import me.chanjar.weixin.common.error.WxErrorException;

/**
Expand Down Expand Up @@ -38,4 +40,24 @@ public interface WxService {
* @throws WxErrorException 异常
*/
String post(String url, Object obj) throws WxErrorException;

/**
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求.
*
* @param url 请求接口地址
* @param jsonObject 请求对象
* @return 接口响应字符串
* @throws WxErrorException 异常
*/
String post(String url, JsonObject jsonObject) throws WxErrorException;

/**
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求.
*
* @param url 请求接口地址
* @param obj 请求对象,实现了ToJson接口
* @return 接口响应字符串
* @throws WxErrorException 异常
*/
String post(String url, ToJson obj) throws WxErrorException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.chanjar.weixin.common.bean.ToJson;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.service.WxService;
import me.chanjar.weixin.common.session.WxSession;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
Expand All @@ -18,7 +19,7 @@
*
* @author chanjaster
*/
public interface WxCpService {
public interface WxCpService extends WxService {
/**
* <pre>
* 验证推送过来的消息的正确性
Expand Down Expand Up @@ -161,46 +162,6 @@ public interface WxCpService {
*/
WxCpProviderToken getProviderToken(String corpId, String providerSecret) 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;

/**
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求
*
* @param url 接口地址
* @param postData 请求body字符串
* @return the string
* @throws WxErrorException the wx error exception
*/
String post(String url, String postData) throws WxErrorException;

/**
* 内部使用.
*
* @param url 接口地址
* @param jsonObject 请求body的json对象
* @return the string
* @throws WxErrorException the wx error exception
*/
String post(String url, JsonObject jsonObject) throws WxErrorException;

/**
* 内部使用.
*
* @param url 接口地址
* @param obj 请求body的对象,实现了ToJson接口
* @return the string
* @throws WxErrorException the wx error exception
*/
String post(String url, ToJson obj) throws WxErrorException;

/**
* 当不需要自动带accessToken的时候,可以用这个发起post请求
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ public String post(String url, ToJson obj) throws WxErrorException {
return this.post(url, obj.toJson());
}

@Override
public String post(String url, Object obj) throws WxErrorException {
return this.post(url, obj.toString());
}

@Override
public String postWithoutToken(String url, String postData) throws WxErrorException {
return this.executeNormal(SimplePostRequestExecutor.create(this), url, postData);
Expand Down Expand Up @@ -319,7 +324,7 @@ private <T, E> T executeNormal(RequestExecutor<T, E> executor, String uri, E dat
return null;
} catch (IOException e) {
log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uri, data, e.getMessage());
throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).errorCode(-1).build(), e);
throw new WxErrorException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public void testUpdate() throws WxErrorException {

@Test
public void testGet() throws WxErrorException {
final List<WxCpOaCalendar> calendars = this.wxService.getOaCalendarService()
.get(Arrays.asList(calId));
final List<WxCpOaCalendar> calendars = this.wxService.getOaCalendarService().get(Arrays.asList(calId));
assertThat(calendars).isNotEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.api.WxImgProcService;
import me.chanjar.weixin.common.api.WxOcrService;
import me.chanjar.weixin.common.bean.ToJson;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
Expand Down Expand Up @@ -186,7 +187,15 @@ public String post(String url, String postData) throws WxErrorException {
public String post(String url, Object obj) throws WxErrorException {
return this.execute(SimplePostRequestExecutor.create(this), url, WxGsonBuilder.create().toJson(obj));
}
@Override
public String post(String url, ToJson obj) throws WxErrorException {
return this.post(url, obj.toJson());
}

@Override
public String post(String url, JsonObject jsonObject) throws WxErrorException {
return this.post(url, jsonObject.toString());
}
/**
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public WxOcrIdCardResult idCard(String imgUrl) throws WxErrorException {
// ignore cannot happen
}

final String result = this.mainService.post(String.format(IDCARD, imgUrl), null);
final String result = this.mainService.post(String.format(IDCARD, imgUrl), (String) null);
return WxOcrIdCardResult.fromJson(result);
}

Expand All @@ -62,7 +62,7 @@ public WxOcrBankCardResult bankCard(String imgUrl) throws WxErrorException {
// ignore cannot happen
}

final String result = this.mainService.post(String.format(BANK_CARD, imgUrl), null);
final String result = this.mainService.post(String.format(BANK_CARD, imgUrl), (String) null);
return WxOcrBankCardResult.fromJson(result);
}

Expand All @@ -81,7 +81,7 @@ public WxOcrDrivingResult driving(String imgUrl) throws WxErrorException {
// ignore cannot happen
}

final String result = this.mainService.post(String.format(DRIVING, imgUrl), null);
final String result = this.mainService.post(String.format(DRIVING, imgUrl), (String) null);
return WxOcrDrivingResult.fromJson(result);
}

Expand All @@ -100,7 +100,7 @@ public WxOcrDrivingLicenseResult drivingLicense(String imgUrl) throws WxErrorExc
// ignore cannot happen
}

final String result = this.mainService.post(String.format(DRIVING_LICENSE, imgUrl), null);
final String result = this.mainService.post(String.format(DRIVING_LICENSE, imgUrl), (String) null);
return WxOcrDrivingLicenseResult.fromJson(result);
}

Expand All @@ -119,7 +119,7 @@ public WxOcrBizLicenseResult bizLicense(String imgUrl) throws WxErrorException {
// ignore cannot happen
}

final String result = this.mainService.post(String.format(BIZ_LICENSE, imgUrl), null);
final String result = this.mainService.post(String.format(BIZ_LICENSE, imgUrl), (String) null);
return WxOcrBizLicenseResult.fromJson(result);
}

Expand All @@ -138,7 +138,7 @@ public WxOcrCommResult comm(String imgUrl) throws WxErrorException {
// ignore cannot happen
}

final String result = this.mainService.post(String.format(COMM, imgUrl), null);
final String result = this.mainService.post(String.format(COMM, imgUrl), (String) null);
return WxOcrCommResult.fromJson(result);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package me.chanjar.weixin.mp.api;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.guide.WxMpGuideInfo;

/**
* 微信导购助手(现在叫对话能力)接口.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020 -10-06
*/
public interface WxMpGuideService {
/**
* 为服务号添加顾问
* <pre>
* 请求地址: POST https://api.weixin.qq.com/cgi-bin/guide/addguideacct?access_token=ACCESS_TOKEN
* 文档地址:https://developers.weixin.qq.com/doc/offiaccount/Shopping_Guide/guide-account/shopping-guide.addGuideAcct.html
* </pre>
*
* @param account 顾问微信号(guide_account和guide_openid二选一,若同时请求,默认为guide_account)
* @param openid 顾问openid或者unionid(guide_account和guide_openid二选一)
* @param headImgUrl 顾问头像,头像url只能用《上传图文消息内的图片获取URL》 me.chanjar.weixin.mp.api.impl.WxMpMaterialServiceImpl#mediaImgUpload(java.io.File)
* @param nickName 顾问昵称
* @throws WxErrorException .
*/
void addGuide(String account, String openid, String headImgUrl, String nickName) throws WxErrorException;

/**
* 为服务号添加顾问
* <pre>
* 请求地址: POST https://api.weixin.qq.com/cgi-bin/guide/addguideacct?access_token=ACCESS_TOKEN
* 文档地址:https://developers.weixin.qq.com/doc/offiaccount/Shopping_Guide/guide-account/shopping-guide.addGuideAcct.html
* </pre>
*
* @param guideInfo 顾问信息
* @throws WxErrorException .
*/
void addGuide(WxMpGuideInfo guideInfo) throws WxErrorException;

/**
* 获取顾问信息
*
* <pre>
* 请求地址: POST https://api.weixin.qq.com/cgi-bin/guide/getguideacct?access_token=ACCESS_TOKEN
* 文档地址:https://developers.weixin.qq.com/doc/offiaccount/Shopping_Guide/guide-account/shopping-guide.getGuideAcct.html
* </pre>
*
* @param account 顾问微信号(guide_account和guide_openid二选一,若同时请求,默认为guide_account)
* @param openid 顾问openid或者unionid(guide_account和guide_openid二选一)
* @return 顾问信息
* @throws WxErrorException .
*/
WxMpGuideInfo getGuide(String account, String openid) throws WxErrorException;
}
Loading

0 comments on commit 6948044

Please sign in to comment.