|
| 1 | +package cn.binarywang.wx.miniapp.api.impl; |
| 2 | + |
| 3 | +import cn.binarywang.wx.miniapp.api.WxMaJsapiService; |
| 4 | +import cn.binarywang.wx.miniapp.api.WxMaService; |
| 5 | +import com.google.gson.JsonElement; |
| 6 | +import com.google.gson.JsonObject; |
| 7 | +import com.google.gson.JsonParser; |
| 8 | +import me.chanjar.weixin.common.bean.WxJsapiSignature; |
| 9 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 10 | +import me.chanjar.weixin.common.util.RandomUtils; |
| 11 | +import me.chanjar.weixin.common.util.crypto.SHA1; |
| 12 | + |
| 13 | +import java.util.concurrent.locks.Lock; |
| 14 | + |
| 15 | +/** |
| 16 | + * <pre> |
| 17 | + * Created by BinaryWang on 2018/8/5. |
| 18 | + * </pre> |
| 19 | + * |
| 20 | + * @author <a href="https://github.com/binarywang">Binary Wang</a> |
| 21 | + */ |
| 22 | +public class WxMaJsapiServiceImpl implements WxMaJsapiService { |
| 23 | + private static final JsonParser JSON_PARSER = new JsonParser(); |
| 24 | + |
| 25 | + private WxMaService wxMaService; |
| 26 | + |
| 27 | + public WxMaJsapiServiceImpl(WxMaService wxMaService) { |
| 28 | + this.wxMaService = wxMaService; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public String getJsapiTicket() throws WxErrorException { |
| 33 | + return getJsapiTicket(false); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public String getJsapiTicket(boolean forceRefresh) throws WxErrorException { |
| 38 | + Lock lock = this.wxMaService.getWxMaConfig().getJsapiTicketLock(); |
| 39 | + try { |
| 40 | + lock.lock(); |
| 41 | + if (forceRefresh) { |
| 42 | + this.wxMaService.getWxMaConfig().expireJsapiTicket(); |
| 43 | + } |
| 44 | + |
| 45 | + if (this.wxMaService.getWxMaConfig().isJsapiTicketExpired()) { |
| 46 | + String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL, null); |
| 47 | + JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent); |
| 48 | + JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); |
| 49 | + String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); |
| 50 | + int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); |
| 51 | + this.wxMaService.getWxMaConfig().updateJsapiTicket(jsapiTicket, expiresInSeconds); |
| 52 | + } |
| 53 | + } finally { |
| 54 | + lock.unlock(); |
| 55 | + } |
| 56 | + return this.wxMaService.getWxMaConfig().getJsapiTicket(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException { |
| 61 | + long timestamp = System.currentTimeMillis() / 1000; |
| 62 | + String randomStr = RandomUtils.getRandomStr(); |
| 63 | + String jsapiTicket = getJsapiTicket(false); |
| 64 | + String signature = SHA1.genWithAmple("jsapi_ticket=" + jsapiTicket, |
| 65 | + "noncestr=" + randomStr, "timestamp=" + timestamp, "url=" + url); |
| 66 | + return WxJsapiSignature |
| 67 | + .builder() |
| 68 | + .appId(this.wxMaService.getWxMaConfig().getAppid()) |
| 69 | + .timestamp(timestamp) |
| 70 | + .nonceStr(randomStr) |
| 71 | + .url(url) |
| 72 | + .signature(signature) |
| 73 | + .build(); |
| 74 | + } |
| 75 | +} |
0 commit comments