com.zopen.wechat.mp.service.WechatJsSdkService Maven / Gradle / Ivy
Show all versions of zopen-ato-starter Show documentation
package com.zopen.wechat.mp.service;
import com.zcj.util.UtilRandom;
import com.zopen.wechat.exception.WechatAssert;
import com.zopen.wechat.mp.dto.jssdk.JsSdkConfig;
import com.zopen.wechat.mp.dto.jssdk.UtilSha1;
import com.zopen.wechat.mp.task.WechatInfo;
import org.springframework.stereotype.Component;
@Component("atoWechatJsSdkService")
public class WechatJsSdkService {
/**
* 生成前端需要的 JS-SDK 配置
*
* https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
*
* @param appId * app_id
* @param url * 当前网页的URL,不包含#及其后面部分
* @return com.zopen.wechat.mp.dto.jssdk.JsSdkConfig
*/
public JsSdkConfig getJsSdkConfig(String appId, String url) {
WechatAssert.notNullAndEmpty(appId, "app_id 不能为空");
WechatAssert.notNullAndEmpty(url, "url 不能为空");
String jsapi_ticket = WechatInfo.getJsapiTicket(appId);
WechatAssert.notNullAndEmpty(jsapi_ticket, "jsapi_ticket 不能为空");
String noncestr = UtilRandom.getRandomChar(10);
Long timestamp = System.currentTimeMillis();
String unSignString = String.format("jsapi_ticket=%s&noncestr=%s×tamp=%s&url=%s", jsapi_ticket, noncestr, timestamp, url);
String sign = UtilSha1.encode(unSignString);// sha1 签名
JsSdkConfig result = new JsSdkConfig();
result.setAppId(appId);
result.setNonceStr(noncestr);
result.setTimestamp(timestamp);
result.setSignature(sign);
return result;
}
}