View Javadoc
1   package com.foxinmy.weixin4j.mp.component;
2   
3   import com.alibaba.fastjson.JSONObject;
4   import com.foxinmy.weixin4j.exception.WeixinException;
5   import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
6   import com.foxinmy.weixin4j.model.Token;
7   import com.foxinmy.weixin4j.mp.type.URLConsts;
8   import com.foxinmy.weixin4j.token.TokenCreator;
9   import com.foxinmy.weixin4j.token.TokenManager;
10  
11  /**
12   * 微信开放平台应用组件预授权码创建
13   *
14   * @className WeixinComponentPreCodeCreator
15   * @author jinyu(foxinmy@gmail.com)
16   * @date 2016年7月5日
17   * @since JDK 1.6
18   */
19  public class WeixinComponentPreCodeCreator extends TokenCreator {
20  
21      private final TokenManager componentTokenManager;
22      private final String componentId;
23  
24      /**
25       *
26       * @param componentTokenManager
27       *            应用套件的token
28       * @param componentId
29       *            应用组件ID
30       */
31      public WeixinComponentPreCodeCreator(TokenManager componentTokenManager, String componentId) {
32          this.componentTokenManager = componentTokenManager;
33          this.componentId = componentId;
34      }
35  
36      @Override
37      public String name() {
38          return "mp_component_precode";
39      }
40  
41      @Override
42      public String uniqueid() {
43          return componentId;
44      }
45  
46      @Override
47      public Token create() throws WeixinException {
48          WeixinResponse response = weixinExecutor.post(
49                  String.format(URLConsts.COMPONENET_PRE_CODE_URL, componentTokenManager.getAccessToken()),
50                  String.format("{\"component_appid\":\"%s\"}", componentId));
51          JSONObject result = response.getAsJson();
52          return new Token(result.getString("pre_auth_code"), result.getLongValue("expires_in") * 1000l);
53      }
54  
55  }