mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 03:25:35 +08:00 
			
		
		
		
	🐛 #1109 修复企业微信第三方应用获取永久授权码解析代码错误的问题
This commit is contained in:
		| @ -15,7 +15,6 @@ import me.chanjar.weixin.cp.config.WxCpTpConfigStorage; | ||||
|  * @author zhenjun cai | ||||
|  */ | ||||
| public interface WxCpTpService { | ||||
|  | ||||
|   /** | ||||
|    * <pre> | ||||
|    * 验证推送过来的消息的正确性 | ||||
| @ -84,15 +83,15 @@ 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 请求参数 | ||||
| @ -100,7 +99,7 @@ public interface WxCpTpService { | ||||
|   String get(String url, String queryParam) throws WxErrorException; | ||||
|  | ||||
|   /** | ||||
|    * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求 | ||||
|    * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求. | ||||
|    * | ||||
|    * @param url      接口地址 | ||||
|    * @param postData 请求body字符串 | ||||
| @ -124,7 +123,7 @@ public interface WxCpTpService { | ||||
|  | ||||
|   /** | ||||
|    * <pre> | ||||
|    * 设置当微信系统响应系统繁忙时,要等待多少 retrySleepMillis(ms) * 2^(重试次数 - 1) 再发起重试 | ||||
|    * 设置当微信系统响应系统繁忙时,要等待多少 retrySleepMillis(ms) * 2^(重试次数 - 1) 再发起重试. | ||||
|    * 默认:1000ms | ||||
|    * </pre> | ||||
|    * | ||||
| @ -134,7 +133,7 @@ public interface WxCpTpService { | ||||
|  | ||||
|   /** | ||||
|    * <pre> | ||||
|    * 设置当微信系统响应系统繁忙时,最大重试次数 | ||||
|    * 设置当微信系统响应系统繁忙时,最大重试次数. | ||||
|    * 默认:5次 | ||||
|    * </pre> | ||||
|    * | ||||
| @ -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(); | ||||
|  | ||||
|  | ||||
| @ -25,6 +25,8 @@ import java.io.IOException; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
|  | ||||
| import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tp.*; | ||||
|  | ||||
| /** | ||||
|  * . | ||||
|  * | ||||
| @ -95,7 +97,7 @@ public abstract class BaseWxCpTpServiceImpl<H, P> implements WxCpTpService, Requ | ||||
|     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))); | ||||
|   } | ||||
|  | ||||
| @ -105,20 +107,19 @@ public abstract class BaseWxCpTpServiceImpl<H, P> implements WxCpTpService, Requ | ||||
|     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; | ||||
|   } | ||||
|  | ||||
| @ -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() { | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang