Share page java micro letter is signed

The first step: Get access_token public numbers, remember to access_token cache, this restriction only 2,000 visits a day, and need to replace their APPID secret.

/**
 * 获取微信access_token
 * @return
 * @throws BusinessException
 * @return Object ,true:已关注,false:未关注
 * @author tyg
 * @date   2019年1月14日上午11:51:05
 */
@Override
public String getAccessToken() {
	String access_token = null;
	try {
	//查询是否还有缓存
		BoundValueOperations<String, String> ops = redisTemplate.boundValueOps(CacheUnifiedEnum.accessToken.getKey());
		//缓存时长
		if(StringUtils.isBlank(ops.get()) || ops.getExpire().longValue() < 20L) {
			Map<String, String> params = new HashMap<String, String>();
			params.put("grant_type", "client_credential");
			params.put("appid", "wx12fdsf4a5f4d6");
			params.put("secret", "fdf456f46fd");
			String respData = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/token", params);
			System.out.println("getaccess_token=====\n"+respData);
			json = JSON.parseObject(respData);
			ops.set(json.getString("access_token"));
			ops.expire(CacheUnifiedEnum.accessToken.getTime(), TimeUnit.SECONDS);
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return access_token;
}

Step Two: Get the micro-channel bill signing.

/**
 * 获取微信jsapi_ticket
 * @return
 * @throws BusinessException
 * @return JSONObject
 * @author tyg
 * @date   2019年5月8日下午7:18:59
 */
public String getJsapiTicket() {
	String jsapi_ticket = null;
	try {
		String respData;
		JSONObject json = new JSONObject();
		BoundValueOperations<String, String> ops = redisTemplate.boundValueOps(CacheUnifiedEnum.jsapiTicket.getKey());
		//缓存时长
		if(StringUtils.isBlank(ops.get()) || ops.getExpire().longValue() < 20L) {
			Map<String, String> params = new HashMap<String, String>();
			params.put("access_token", this.getAccessToken());
			params.put("type", "jsapi");
			respData = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket", params);
			System.out.println("getjsapi_ticket=====\n"+respData);
			json = JSON.parseObject(respData);
			ops.set(json.getString("ticket"));
			ops.expire(CacheUnifiedEnum.jsapiTicket.getTime(), TimeUnit.SECONDS);
		}
		jsapi_ticket = ops.get();
	} catch (Exception e) {
		e.printStackTrace();
	}
	return jsapi_ticket;
}

The third step: take the current page address to sign.


/**
 * 微信分享进行签名
 * @param currentUrl
 * @return
 * @throws BusinessException
 * @return JSONObject
 * @author tyg
 * @date   2019年5月8日下午7:18:59
 */
@Override
public JSONObject getSignature(String currentUrl) throws BusinessException {
	JSONObject jssdk = null;
	try {
		String noncestr = UUID.randomUUID().toString();// 随机字符串
		String jsapi_ticket = getStoreDistributionJsapiTicket();// 调用微信JS接口的临时票据
		Long timestamp = new Date().getTime() / 1000;// 时间戳
		
		// 注意这里参数名必须全部小写,且必须有序
		String string1 = String.format("jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s", jsapi_ticket, noncestr, timestamp, currentUrl);
		
		MessageDigest crypt = MessageDigest.getInstance("SHA-1");
		crypt.reset();
		crypt.update(string1.getBytes("UTF-8"));
		String signature = byteToHex(crypt.digest());
		
		jssdk = new JSONObject();
		jssdk.put("appId", "wx13458f4d89sffdsfd");
		jssdk.put("timestamp", timestamp);
		jssdk.put("nonceStr", noncestr);
		jssdk.put("signature", signature);
	} catch (Exception e) {
		e.printStackTrace();
		logI.addErrorLog(currentUrl, ErrorLogEnum.微信信息, "获取微信签名信息", e);
		Assert.throwException("无法获取微信签名信息!");
	}
	return jssdk;
}

 

Guess you like

Origin blog.csdn.net/qq_26365837/article/details/89969237