View Javadoc
1   package com.foxinmy.weixin4j.jssdk;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.Map;
6   import java.util.Set;
7   
8   import com.alibaba.fastjson.JSONObject;
9   import com.foxinmy.weixin4j.exception.WeixinException;
10  import com.foxinmy.weixin4j.token.TokenManager;
11  import com.foxinmy.weixin4j.util.DateUtil;
12  import com.foxinmy.weixin4j.util.DigestUtil;
13  import com.foxinmy.weixin4j.util.MapUtil;
14  import com.foxinmy.weixin4j.util.RandomUtil;
15  import com.foxinmy.weixin4j.util.StringUtil;
16  
17  /**
18   * JSSDK配置类
19   *
20   * @className JSSDKConfigurator
21   * @author jinyu(foxinmy@gmail.com)
22   * @date 2015年12月23日
23   * @since JDK 1.6
24   * @see
25   */
26  public class JSSDKConfigurator {
27      private final TokenManager ticketTokenManager;
28      private JSONObject config;
29      private Set<JSSDKAPI> apis;
30  
31      /**
32       * ticket保存类 可调用WeixinProxy#getTicketManager获取
33       *
34       * @param ticketTokenManager
35       */
36      public JSSDKConfigurator(TokenManager ticketTokenManager) {
37          this.ticketTokenManager = ticketTokenManager;
38          this.config = new JSONObject();
39          this.apis = new HashSet<JSSDKAPI>();
40      }
41  
42      /**
43       * 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,
44       * 仅在pc端时才会打印。
45       *
46       * @return
47       */
48      public JSSDKConfigurator debugMode() {
49          config.put("debug", true);
50          return this;
51      }
52  
53      /**
54       * 需要使用的JS接口列表
55       *
56       * @see JSSDKAPI
57       * @param apis
58       * @return
59       */
60      public JSSDKConfigurator apis(JSSDKAPI... apis) {
61          for (JSSDKAPI api : apis) {
62              this.apis.add(api);
63          }
64          return this;
65      }
66  
67      /**
68       * 需要使用的JS接口列表
69       *
70       * @see JSSDKAPI
71       * @param apis
72       * @return
73       */
74      public JSSDKConfigurator apis(JSSDKAPI[]... apis) {
75          for (JSSDKAPI[] api : apis) {
76              apis(api);
77          }
78          return this;
79      }
80  
81      /**
82       * 生成config配置JSON串
83       *
84       * @param url
85       *            当前网页的URL,不包含#及其后面部分
86       * @return jssdk配置JSON字符串
87       * @see <a href=
88       *      "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN">公众号JSSDK</a>
89       * @see <a href=
90       *      "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BE%AE%E4%BF%A1JS-SDK%E6%8E%A5%E5%8F%A3">企业号JSSDK</a>
91       * @throws WeixinException
92       */
93      public String toJSONConfig(String url) throws WeixinException {
94          if (apis.isEmpty()) {
95              throw new WeixinException("jsapilist not be empty");
96          }
97          Map<String, String> signMap = new HashMap<String, String>();
98          String timestamp = DateUtil.timestamp2string();
99          String noncestr = RandomUtil.generateString(24);
100         signMap.put("timestamp", timestamp);
101         signMap.put("noncestr", noncestr);
102         signMap.put("jsapi_ticket", this.ticketTokenManager.getAccessToken());
103         signMap.put("url", url);
104         String sign = DigestUtil.SHA1(MapUtil.toJoinString(signMap, false, false));
105         config.put("appId", ticketTokenManager.getWeixinId());
106         if (StringUtil.isBlank(config.getString("debug"))) {
107             config.put("debug", false);
108         }
109         config.put("timestamp", timestamp);
110         config.put("nonceStr", noncestr);
111         config.put("signature", sign);
112         config.put("jsApiList", apis.toArray());
113         return config.toJSONString();
114     }
115 }