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
15
16
17
18
19 public class WeixinComponentPreCodeCreator extends TokenCreator {
20
21 private final TokenManager componentTokenManager;
22 private final String componentId;
23
24
25
26
27
28
29
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 }