Skip to content

Commit

Permalink
🐛 #1109 修复企业微信第三方应用获取永久授权码解析代码错误的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Aug 18, 2019
1 parent 0b1baf0 commit 9750e9f
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* @author zhenjun cai
*/
public interface WxCpTpService {

/**
* <pre>
* 验证推送过来的消息的正确性
Expand Down Expand Up @@ -84,23 +83,23 @@ public interface WxCpTpService {
WxAccessToken getCorpToken(String authCorpid, String permanentCode) throws WxErrorException;

/**
* 获取企业永久授权码
* 获取企业永久授权码 .
*
* @param authCode
* @return
* @param authCode .
* @return .
*/
WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException;

/**
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求.
*
* @param url 接口地址
* @param queryParam 请求参数
*/
String get(String url, String queryParam) throws WxErrorException;

/**
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求.
*
* @param url 接口地址
* @param postData 请求body字符串
Expand All @@ -124,7 +123,7 @@ public interface WxCpTpService {

/**
* <pre>
* 设置当微信系统响应系统繁忙时,要等待多少 retrySleepMillis(ms) * 2^(重试次数 - 1) 再发起重试
* 设置当微信系统响应系统繁忙时,要等待多少 retrySleepMillis(ms) * 2^(重试次数 - 1) 再发起重试.
* 默认:1000ms
* </pre>
*
Expand All @@ -134,7 +133,7 @@ public interface WxCpTpService {

/**
* <pre>
* 设置当微信系统响应系统繁忙时,最大重试次数
* 设置当微信系统响应系统繁忙时,最大重试次数.
* 默认:5次
* </pre>
*
Expand All @@ -148,21 +147,21 @@ public interface WxCpTpService {
void initHttp();

/**
* 获取WxMpConfigStorage 对象
* 获取WxMpConfigStorage 对象.
*
* @return WxMpConfigStorage
*/
WxCpTpConfigStorage getWxCpTpConfigStorage();

/**
* 注入 {@link WxCpTpConfigStorage} 的实现
* 注入 {@link WxCpTpConfigStorage} 的实现.
*
* @param wxConfigProvider 配置对象
*/
void setWxCpTpConfigStorage(WxCpTpConfigStorage wxConfigProvider);

/**
* http请求对象
* http请求对象.
*/
RequestHttp<?, ?> getRequestHttp();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.HashMap;
import java.util.Map;

import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tp.*;

/**
* .
*
Expand Down Expand Up @@ -95,7 +97,7 @@ public WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorEx
params.put("js_code", jsCode);
params.put("grant_type", "authorization_code");

final String url = configStorage.getApiUrl(WxCpApiPathConsts.Tp.JSCODE_TO_SESSION);
final String url = configStorage.getApiUrl(JSCODE_TO_SESSION);
return WxCpMaJsCode2SessionResult.fromJson(this.get(url, Joiner.on("&").withKeyValueSeparator("=").join(params)));
}

Expand All @@ -105,20 +107,19 @@ public WxAccessToken getCorpToken(String authCorpid, String permanentCode) throw
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("auth_corpid", authCorpid);
jsonObject.addProperty("permanent_code", permanentCode);
String result = post(configStorage.getApiUrl(WxCpApiPathConsts.Tp.GET_CORP_TOKEN), jsonObject.toString());
String result = post(configStorage.getApiUrl(GET_CORP_TOKEN), jsonObject.toString());

return WxAccessToken.fromJson(result);
}


@Override
public WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("auth_code", authCode);

String result = post(configStorage.getApiUrl(WxCpApiPathConsts.Tp.GET_PERMANENT_CODE), jsonObject.toString());
String result = post(configStorage.getApiUrl(GET_PERMANENT_CODE), jsonObject.toString());
jsonObject = new JsonParser().parse(result).getAsJsonObject();
WxCpTpCorp wxCpTpCorp = WxCpTpCorp.fromJson(jsonObject.get("auth_corp_info").getAsString());
WxCpTpCorp wxCpTpCorp = WxCpTpCorp.fromJson(jsonObject.get("auth_corp_info").getAsJsonObject().toString());
wxCpTpCorp.setPermanentCode(jsonObject.get("permanent_code").getAsString());
return wxCpTpCorp;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package me.chanjar.weixin.cp.api.impl;

import com.google.gson.JsonObject;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpTpService;
import me.chanjar.weixin.cp.bean.WxCpTpCorp;
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
import org.testng.annotations.Test;

import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tp.GET_PERMANENT_CODE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;
import static org.testng.Assert.*;

/**
* 测试代码.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2019-08-18
*/
public class BaseWxCpTpServiceImplTest {
private WxCpTpService tpService = spy(new WxCpTpServiceImpl());

@Test
public void testCheckSignature() {
}

@Test
public void testGetSuiteAccessToken() {
}

@Test
public void testGetSuiteTicket() {
}

@Test
public void testTestGetSuiteTicket() {
}

@Test
public void testJsCode2Session() {
}

@Test
public void testGetCorpToken() {
}

@Test
public void testGetPermanentCode() throws WxErrorException {
String returnJson = "{\n" +
" \"errcode\":0 ,\n" +
" \"errmsg\":\"ok\" ,\n" +
" \"access_token\": \"xxxxxx\", \n" +
" \"expires_in\": 7200, \n" +
" \"permanent_code\": \"xxxx\", \n" +
" \"dealer_corp_info\": \n" +
" {\n" +
" \"corpid\": \"xxxx\",\n" +
" \"corp_name\": \"name\"\n" +
" },\n" +
" \"auth_corp_info\": \n" +
" {\n" +
" \"corpid\": \"xxxx\",\n" +
" \"corp_name\": \"name\",\n" +
" \"corp_type\": \"verified\",\n" +
" \"corp_square_logo_url\": \"yyyyy\",\n" +
" \"corp_user_max\": 50,\n" +
" \"corp_agent_max\": 30,\n" +
" \"corp_full_name\":\"full_name\",\n" +
" \"verified_end_time\":1431775834,\n" +
" \"subject_type\": 1,\n" +
" \"corp_wxqrcode\": \"zzzzz\",\n" +
" \"corp_scale\": \"1-50人\",\n" +
" \"corp_industry\": \"IT服务\",\n" +
" \"corp_sub_industry\": \"计算机软件/硬件/信息服务\",\n" +
" \"location\":\"广东省广州市\"\n" +
" },\n" +
" \"auth_info\":\n" +
" {\n" +
" \"agent\" :\n" +
" [\n" +
" {\n" +
" \"agentid\":1,\n" +
" \"name\":\"NAME\",\n" +
" \"round_logo_url\":\"xxxxxx\",\n" +
" \"square_logo_url\":\"yyyyyy\",\n" +
" \"appid\":1,\n" +
" \"privilege\":\n" +
" {\n" +
" \"level\":1,\n" +
" \"allow_party\":[1,2,3],\n" +
" \"allow_user\":[\"zhansan\",\"lisi\"],\n" +
" \"allow_tag\":[1,2,3],\n" +
" \"extra_party\":[4,5,6],\n" +
" \"extra_user\":[\"wangwu\"],\n" +
" \"extra_tag\":[4,5,6]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"agentid\":2,\n" +
" \"name\":\"NAME2\",\n" +
" \"round_logo_url\":\"xxxxxx\",\n" +
" \"square_logo_url\":\"yyyyyy\",\n" +
" \"appid\":5\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"auth_user_info\":\n" +
" {\n" +
" \"userid\":\"aa\",\n" +
" \"name\":\"xxx\",\n" +
" \"avatar\":\"http://xxx\"\n" +
" }\n" +
"}\n";

final WxCpTpConfigStorage configStorage = new WxCpTpDefaultConfigImpl();
tpService.setWxCpTpConfigStorage(configStorage);

JsonObject jsonObject = new JsonObject();
String authCode = "";
jsonObject.addProperty("auth_code", authCode);
doReturn(returnJson).when(tpService).post(configStorage.getApiUrl(GET_PERMANENT_CODE), jsonObject.toString());

final WxCpTpCorp tpCorp = tpService.getPermanentCode(authCode);
assertThat(tpCorp.getPermanentCode()).isEqualTo("xxxx");
}

@Test
public void testGet() {
}

@Test
public void testPost() {
}

@Test
public void testExecute() {
}

@Test
public void testExecuteInternal() {
}

@Test
public void testSetWxCpTpConfigStorage() {
}

@Test
public void testSetRetrySleepMillis() {
}

@Test
public void testSetMaxRetryTimes() {
}

@Test
public void testGetTmpDirFile() {
}

@Test
public void testSetTmpDirFile() {
}

@Test
public void testGetRequestHttp() {
}
}

0 comments on commit 9750e9f

Please sign in to comment.