All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.foxinmy.weixin4j.jssdk.JSSDKConfigurator Maven / Gradle / Ivy

There is a newer version: 1.10.2
Show newest version
package com.foxinmy.weixin4j.jssdk;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.util.DateUtil;
import com.foxinmy.weixin4j.util.DigestUtil;
import com.foxinmy.weixin4j.util.MapUtil;
import com.foxinmy.weixin4j.util.RandomUtil;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;

/**
 * JSSDK配置类
 * 
 * @className JSSDKConfigurator
 * @author jy
 * @date 2015年12月23日
 * @since JDK 1.6
 * @see
 */
public class JSSDKConfigurator {
	private final TokenHolder ticketTokenHolder;
	private JSONObject config;
	private Set apis;

	/**
	 * ticket保存类 可调用WeixinProxy#getTicketHolder获取
	 * 
	 * @param ticketTokenHolder
	 */
	public JSSDKConfigurator(TokenHolder ticketTokenHolder) {
		this.ticketTokenHolder = ticketTokenHolder;
		this.config = new JSONObject();
		this.apis = new HashSet();
	}

	/**
	 * 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,
	 * 仅在pc端时才会打印。
	 * 
	 * @return
	 */
	public JSSDKConfigurator debugMode() {
		config.put("debug", true);
		return this;
	}

	/**
	 * 公众号的唯一标识 不填则获取weixin4j.properties#account中的id
	 * 
	 * @param appId
	 * @return
	 */
	public JSSDKConfigurator appId(String appId) {
		config.put("appId", appId);
		return this;
	}

	/**
	 * 需要使用的JS接口列表
	 * 
	 * @see JSSDKAPI
	 * @param apis
	 * @return
	 */
	public JSSDKConfigurator apis(JSSDKAPI... apis) {
		for (JSSDKAPI api : apis) {
			this.apis.add(api);
		}
		return this;
	}

	/**
	 * 需要使用的JS接口列表
	 * 
	 * @see JSSDKAPI
	 * @param apis
	 * @return
	 */
	public JSSDKConfigurator apis(JSSDKAPI[]... apis) {
		for (JSSDKAPI[] api : apis) {
			for (JSSDKAPI apii : api) {
				this.apis.add(apii);
			}
		}
		return this;
	}

	/**
	 * 生成config配置JSON串
	 * 
	 * @param url
	 *            当前网页的URL,不包含#及其后面部分
	 * @return
	 * @see 通过config接口注入权限验证配置
	 * @throws WeixinException
	 */
	public String toJSONConfig(String url) throws WeixinException {
		Map signMap = new HashMap();
		String timestamp = DateUtil.timestamp2string();
		String noncestr = RandomUtil.generateString(24);
		signMap.put("timestamp", timestamp);
		signMap.put("noncestr", noncestr);
		signMap.put("jsapi_ticket", this.ticketTokenHolder.getAccessToken());
		signMap.put("url", url);
		String sign = DigestUtil.SHA1(MapUtil.toJoinString(signMap, false,
				false));
		if (StringUtil.isBlank(config.getString("appId"))) {
			config.put("appId", Weixin4jConfigUtil.getWeixinAccount().getId());
		}
		if (StringUtil.isBlank(config.getString("debug"))) {
			config.put("debug", false);
		}
		if (apis.isEmpty()) {
			throw new WeixinException("jsapilist not be empty");
		}
		config.put("timestamp", timestamp);
		config.put("nonceStr", noncestr);
		config.put("signature", sign);
		config.put("jsApiList", apis.toArray());
		return config.toJSONString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy