cn.binarywang.wx.miniapp.api.WxMaService.getWxMaConfig()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(161)

本文整理了Java中cn.binarywang.wx.miniapp.api.WxMaService.getWxMaConfig()方法的一些代码示例,展示了WxMaService.getWxMaConfig()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WxMaService.getWxMaConfig()方法的具体详情如下:
包路径:cn.binarywang.wx.miniapp.api.WxMaService
类名称:WxMaService
方法名:getWxMaConfig

WxMaService.getWxMaConfig介绍

[英]获取WxMaConfig 对象.
[中]获取WxMaConfig对象.

代码示例

代码示例来源:origin: com.github.binarywang/weixin-java-miniapp

  1. public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
  2. Lock lock = this.wxMaService.getWxMaConfig().getCardApiTicketLock();
  3. try {
  4. lock.lock();
  5. if (forceRefresh) {
  6. this.wxMaService.getWxMaConfig().expireCardApiTicket();
  7. }
  8. if (this.wxMaService.getWxMaConfig().isCardApiTicketExpired()) {
  9. String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=wx_card", null);
  10. JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
  11. JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
  12. String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
  13. int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
  14. this.wxMaService.getWxMaConfig().updateCardApiTicket(jsapiTicket, expiresInSeconds);
  15. }
  16. } finally {
  17. lock.unlock();
  18. }
  19. return this.wxMaService.getWxMaConfig().getJsapiTicket();
  20. }

代码示例来源:origin: binarywang/WxJava

  1. public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
  2. Lock lock = this.wxMaService.getWxMaConfig().getCardApiTicketLock();
  3. try {
  4. lock.lock();
  5. if (forceRefresh) {
  6. this.wxMaService.getWxMaConfig().expireCardApiTicket();
  7. }
  8. if (this.wxMaService.getWxMaConfig().isCardApiTicketExpired()) {
  9. String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=wx_card", null);
  10. JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
  11. JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
  12. String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
  13. int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
  14. this.wxMaService.getWxMaConfig().updateCardApiTicket(jsapiTicket, expiresInSeconds);
  15. }
  16. } finally {
  17. lock.unlock();
  18. }
  19. return this.wxMaService.getWxMaConfig().getJsapiTicket();
  20. }

代码示例来源:origin: com.github.binarywang/weixin-java-miniapp

  1. @Override
  2. public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
  3. Lock lock = this.wxMaService.getWxMaConfig().getJsapiTicketLock();
  4. try {
  5. lock.lock();
  6. if (forceRefresh) {
  7. this.wxMaService.getWxMaConfig().expireJsapiTicket();
  8. }
  9. if (this.wxMaService.getWxMaConfig().isJsapiTicketExpired()) {
  10. String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=jsapi", null);
  11. JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
  12. JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
  13. String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
  14. int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
  15. this.wxMaService.getWxMaConfig().updateJsapiTicket(jsapiTicket, expiresInSeconds);
  16. }
  17. } finally {
  18. lock.unlock();
  19. }
  20. return this.wxMaService.getWxMaConfig().getJsapiTicket();
  21. }

代码示例来源:origin: binarywang/WxJava

  1. @Override
  2. public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
  3. Lock lock = this.wxMaService.getWxMaConfig().getJsapiTicketLock();
  4. try {
  5. lock.lock();
  6. if (forceRefresh) {
  7. this.wxMaService.getWxMaConfig().expireJsapiTicket();
  8. }
  9. if (this.wxMaService.getWxMaConfig().isJsapiTicketExpired()) {
  10. String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=jsapi", null);
  11. JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
  12. JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
  13. String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
  14. int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
  15. this.wxMaService.getWxMaConfig().updateJsapiTicket(jsapiTicket, expiresInSeconds);
  16. }
  17. } finally {
  18. lock.unlock();
  19. }
  20. return this.wxMaService.getWxMaConfig().getJsapiTicket();
  21. }

代码示例来源:origin: binarywang/WxJava

  1. @Override
  2. public void setUserStorage(Map<String, String> kvMap, String sessionKey, String openid) throws WxErrorException {
  3. final WxMaConfig config = this.service.getWxMaConfig();
  4. JsonObject param = new JsonObject();
  5. JsonArray array = new JsonArray();
  6. for (Map.Entry<String, String> e : kvMap.entrySet()) {
  7. JsonObject jsonObject = new JsonObject();
  8. jsonObject.addProperty("key", e.getKey());
  9. jsonObject.addProperty("value", e.getValue());
  10. array.add(jsonObject);
  11. }
  12. param.add("kv_list", array);
  13. String params = param.toString();
  14. String signature = SignUtils.createHmacSha256Sign(params, sessionKey);
  15. String url = String.format("https://api.weixin.qq.com/wxa/set_user_storage" +
  16. "?appid=%s&signature=%s&openid=%s&sig_method=%s",
  17. config.getAppid(), signature, openid, "hmac_sha256");
  18. String result = this.service.post(url, params);
  19. WxError error = WxError.fromJson(result);
  20. if (error.getErrorCode() != 0) {
  21. throw new WxErrorException(error);
  22. }
  23. }

代码示例来源:origin: com.github.binarywang/weixin-java-miniapp

  1. @Override
  2. public void setUserStorage(Map<String, String> kvMap, String sessionKey, String openid) throws WxErrorException {
  3. final WxMaConfig config = this.service.getWxMaConfig();
  4. JsonObject param = new JsonObject();
  5. JsonArray array = new JsonArray();
  6. for (Map.Entry<String, String> e : kvMap.entrySet()) {
  7. JsonObject jsonObject = new JsonObject();
  8. jsonObject.addProperty("key", e.getKey());
  9. jsonObject.addProperty("value", e.getValue());
  10. array.add(jsonObject);
  11. }
  12. param.add("kv_list", array);
  13. String params = param.toString();
  14. String signature = SignUtils.createHmacSha256Sign(params, sessionKey);
  15. String url = String.format("https://api.weixin.qq.com/wxa/set_user_storage" +
  16. "?appid=%s&signature=%s&openid=%s&sig_method=%s",
  17. config.getAppid(), signature, openid, "hmac_sha256");
  18. String result = this.service.post(url, params);
  19. WxError error = WxError.fromJson(result);
  20. if (error.getErrorCode() != 0) {
  21. throw new WxErrorException(error);
  22. }
  23. }

代码示例来源:origin: ustcwudi/springboot-seed

  1. @RequestParam("timestamp" ) String timestamp,
  2. @RequestParam("nonce" ) String nonce) {
  3. final boolean isJson = Objects.equals(this.wxService.getWxMaConfig().getMsgDataFormat(),
  4. WxMaConstants.MsgDataFormat.JSON);
  5. if (StringUtils.isBlank(encryptType)) {
  6. inMessage = WxMaMessage.fromEncryptedJson(requestBody, this.wxService.getWxMaConfig());
  7. } else {//xml
  8. inMessage = WxMaMessage.fromEncryptedXml(requestBody, this.wxService.getWxMaConfig(),
  9. timestamp, nonce, msgSignature);

代码示例来源:origin: binarywang/weixin-java-miniapp-demo

  1. final boolean isJson = Objects.equals(wxService.getWxMaConfig().getMsgDataFormat(),
  2. WxMaConstants.MsgDataFormat.JSON);
  3. if (StringUtils.isBlank(encryptType)) {
  4. inMessage = WxMaMessage.fromEncryptedJson(requestBody, wxService.getWxMaConfig());
  5. } else {//xml
  6. inMessage = WxMaMessage.fromEncryptedXml(requestBody, wxService.getWxMaConfig(),
  7. timestamp, nonce, msgSignature);

代码示例来源:origin: com.github.binarywang/weixin-java-miniapp

  1. @Override
  2. public byte[] getQrCode(String path) throws WxErrorException {
  3. String appId = this.wxMaService.getWxMaConfig().getAppid();
  4. Path qrCodeFilePath = null;
  5. try {
  6. RequestExecutor<File, String> executor = BaseMediaDownloadRequestExecutor
  7. .create(this.wxMaService.getRequestHttp(), Files.createTempDirectory("weixin-java-tools-ma-" + appId).toFile());
  8. final StringBuilder url = new StringBuilder(GET_QRCODE_URL);
  9. if (StringUtils.isNotBlank(path)) {
  10. url.append("?path=").append(URLEncoder.encode(path, StandardCharsets.UTF_8.name()));
  11. }
  12. qrCodeFilePath = this.wxMaService.execute(executor, url.toString(), null).toPath();
  13. return Files.readAllBytes(qrCodeFilePath);
  14. } catch (IOException e) {
  15. throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).build(), e);
  16. } finally {
  17. if (qrCodeFilePath != null) {
  18. try {
  19. // 及时删除二维码文件,避免积压过多缓存文件
  20. Files.delete(qrCodeFilePath);
  21. } catch (Exception ignored) {
  22. }
  23. }
  24. }
  25. }

代码示例来源:origin: binarywang/WxJava

  1. @Override
  2. public byte[] getQrCode(String path) throws WxErrorException {
  3. String appId = this.wxMaService.getWxMaConfig().getAppid();
  4. Path qrCodeFilePath = null;
  5. try {
  6. RequestExecutor<File, String> executor = BaseMediaDownloadRequestExecutor
  7. .create(this.wxMaService.getRequestHttp(), Files.createTempDirectory("weixin-java-tools-ma-" + appId).toFile());
  8. final StringBuilder url = new StringBuilder(GET_QRCODE_URL);
  9. if (StringUtils.isNotBlank(path)) {
  10. url.append("?path=").append(URLEncoder.encode(path, StandardCharsets.UTF_8.name()));
  11. }
  12. qrCodeFilePath = this.wxMaService.execute(executor, url.toString(), null).toPath();
  13. return Files.readAllBytes(qrCodeFilePath);
  14. } catch (IOException e) {
  15. throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).build(), e);
  16. } finally {
  17. if (qrCodeFilePath != null) {
  18. try {
  19. // 及时删除二维码文件,避免积压过多缓存文件
  20. Files.delete(qrCodeFilePath);
  21. } catch (Exception ignored) {
  22. }
  23. }
  24. }
  25. }

代码示例来源:origin: binarywang/weixin-java-miniapp-demo

  1. @Bean
  2. public Object services() {
  3. maServices = this.properties.getConfigs()
  4. .stream()
  5. .map(a -> {
  6. WxMaInMemoryConfig config = new WxMaInMemoryConfig();
  7. config.setAppid(a.getAppid());
  8. config.setSecret(a.getSecret());
  9. config.setToken(a.getToken());
  10. config.setAesKey(a.getAesKey());
  11. config.setMsgDataFormat(a.getMsgDataFormat());
  12. WxMaService service = new WxMaServiceImpl();
  13. service.setWxMaConfig(config);
  14. routers.put(a.getAppid(), this.newRouter(service));
  15. return service;
  16. }).collect(Collectors.toMap(s -> s.getWxMaConfig().getAppid(), a -> a));
  17. return Boolean.TRUE;
  18. }

代码示例来源:origin: com.github.binarywang/weixin-java-miniapp

  1. @Override
  2. public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException {
  3. long timestamp = System.currentTimeMillis() / 1000;
  4. String randomStr = RandomUtils.getRandomStr();
  5. String jsapiTicket = getJsapiTicket(false);
  6. String signature = SHA1.genWithAmple("jsapi_ticket=" + jsapiTicket,
  7. "noncestr=" + randomStr, "timestamp=" + timestamp, "url=" + url);
  8. return WxJsapiSignature
  9. .builder()
  10. .appId(this.wxMaService.getWxMaConfig().getAppid())
  11. .timestamp(timestamp)
  12. .nonceStr(randomStr)
  13. .url(url)
  14. .signature(signature)
  15. .build();
  16. }
  17. }

代码示例来源:origin: binarywang/WxJava

  1. @Override
  2. public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException {
  3. long timestamp = System.currentTimeMillis() / 1000;
  4. String randomStr = RandomUtils.getRandomStr();
  5. String jsapiTicket = getJsapiTicket(false);
  6. String signature = SHA1.genWithAmple("jsapi_ticket=" + jsapiTicket,
  7. "noncestr=" + randomStr, "timestamp=" + timestamp, "url=" + url);
  8. return WxJsapiSignature
  9. .builder()
  10. .appId(this.wxMaService.getWxMaConfig().getAppid())
  11. .timestamp(timestamp)
  12. .nonceStr(randomStr)
  13. .url(url)
  14. .signature(signature)
  15. .build();
  16. }
  17. }

相关文章