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

com.tmsps.ne4weixin.api.JsAPI Maven / Gradle / Ivy

There is a newer version: 3.2.2.3
Show newest version
package com.tmsps.ne4weixin.api;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;

import com.tmsps.ne4weixin.config.JsAPIConfig;
import com.tmsps.ne4weixin.config.WxConfig;
import com.tmsps.ne4weixin.utils.JsSignUtil;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Title:  JsAPI.java
 * @Package com.tmsps.ne4weixin.api
 * @Description: 微信js校验相关API
 * @author: zhangwei
 * @date:   2020-04-18
 * @version  v1.0
 * @Copyright: 2020 TMSPS All rights reserved.
 */
@Slf4j
public class JsAPI extends BaseAPI {

	public JsAPI(WxConfig wxConfig) {
		super(wxConfig);
	}
	
	/**
	 * @Title: getJsAPIConfig
	 * @Description: 用户校验微信页面引用微信原生js的校验使用
	 * @author:zhangwei 
	 * @date: Apr 18, 2020 9:57:06 AM
	 * @param: @param url
	 * @param: @return      
	 * @return: JsAPIConfig      
	 * @throws
	 */
	public JsAPIConfig getJsAPIConfig(String url) {
		JsAPIConfig jsConfig = new JsAPIConfig();
		jsConfig.setAppId(wxConfig.getAppid());
		jsConfig.setTimestamp(System.currentTimeMillis()/1000);
		/**
		 * 随机字符串,不长于32位。
		 */
		jsConfig.setNoncestr(RandomUtil.randomString(16));
		String jsAccessToken = wxConfig.getJsAccessToken();
		
		Map params = new HashMap();
		params.put("noncestr", jsConfig.getNoncestr());
		params.put("jsapi_ticket", jsAccessToken);
		params.put("timestamp", Long.toString(jsConfig.getTimestamp()));
		params.put("url", url);
		
		log.info("packageSign:{}",params.toString());
		String packageSign = JsSignUtil.packageSign(params, false);
		String sign = DigestUtils.sha1Hex(packageSign);
		jsConfig.setSignature(sign);
		log.info("get jsConfig with sign :{}",jsConfig.toString());
		
		return jsConfig;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy