cn.binarywang.wx.miniapp.api.WxMaService类的使用及代码示例

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

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

WxMaService介绍

暂无

代码示例

代码示例来源:origin: linlinjava/litemall

@Bean
public WxMaService wxMaService(WxMaConfig maConfig) {
  WxMaService service = new WxMaServiceImpl();
  service.setWxMaConfig(maConfig);
  return service;
}

代码示例来源:origin: linlinjava/litemall

@PostMapping("bindPhone")
public Object bindPhone(@LoginUser Integer userId, @RequestBody String body) {
  String sessionKey = UserTokenManager.getSessionKey(userId);
  String encryptedData = JacksonUtil.parseString(body, "encryptedData");
  String iv = JacksonUtil.parseString(body, "iv");
  WxMaPhoneNumberInfo phoneNumberInfo = this.wxService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
  String phone = phoneNumberInfo.getPhoneNumber();
  LitemallUser user = userService.findById(userId);
  user.setMobile(phone);
  if (userService.updateById(user) == 0) {
    return ResponseUtil.updatedDataFailed();
  }
  return ResponseUtil.ok();
}

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

@Bean
public Object services() {
  maServices = this.properties.getConfigs()
    .stream()
    .map(a -> {
      WxMaInMemoryConfig config = new WxMaInMemoryConfig();
      config.setAppid(a.getAppid());
      config.setSecret(a.getSecret());
      config.setToken(a.getToken());
      config.setAesKey(a.getAesKey());
      config.setMsgDataFormat(a.getMsgDataFormat());
      WxMaService service = new WxMaServiceImpl();
      service.setWxMaConfig(config);
      routers.put(a.getAppid(), this.newRouter(service));
      return service;
    }).collect(Collectors.toMap(s -> s.getWxMaConfig().getAppid(), a -> a));
  return Boolean.TRUE;
}

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

@Override
public boolean checkImage(File file) throws WxErrorException {
 //这里只是借用MediaUploadRequestExecutor,并不使用其返回值WxMediaUploadResult
 WxMediaUploadResult result = this.service.execute(MediaUploadRequestExecutor
  .create(this.service.getRequestHttp()), IMG_SEC_CHECK_URL, file);
 return result != null;
}

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

public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
 Lock lock = this.wxMaService.getWxMaConfig().getCardApiTicketLock();
 try {
  lock.lock();
  if (forceRefresh) {
   this.wxMaService.getWxMaConfig().expireCardApiTicket();
  }
  if (this.wxMaService.getWxMaConfig().isCardApiTicketExpired()) {
   String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=wx_card", null);
   JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
   JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
   String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
   int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
   this.wxMaService.getWxMaConfig().updateCardApiTicket(jsapiTicket, expiresInSeconds);
  }
 } finally {
  lock.unlock();
 }
 return this.wxMaService.getWxMaConfig().getJsapiTicket();
}

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

@Override
public byte[] getQrCode(String path) throws WxErrorException {
 String appId = this.wxMaService.getWxMaConfig().getAppid();
 Path qrCodeFilePath = null;
 try {
  RequestExecutor<File, String> executor = BaseMediaDownloadRequestExecutor
   .create(this.wxMaService.getRequestHttp(), Files.createTempDirectory("weixin-java-tools-ma-" + appId).toFile());
  final StringBuilder url = new StringBuilder(GET_QRCODE_URL);
  if (StringUtils.isNotBlank(path)) {
   url.append("?path=").append(URLEncoder.encode(path, StandardCharsets.UTF_8.name()));
  }
  qrCodeFilePath = this.wxMaService.execute(executor, url.toString(), null).toPath();
  return Files.readAllBytes(qrCodeFilePath);
 } catch (IOException e) {
  throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).build(), e);
 } finally {
  if (qrCodeFilePath != null) {
   try {
    // 及时删除二维码文件,避免积压过多缓存文件
    Files.delete(qrCodeFilePath);
   } catch (Exception ignored) {
   }
  }
 }
}

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

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

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

@Override
public void setUserStorage(Map<String, String> kvMap, String sessionKey, String openid) throws WxErrorException {
 final WxMaConfig config = this.service.getWxMaConfig();
 JsonObject param = new JsonObject();
 JsonArray array = new JsonArray();
 for (Map.Entry<String, String> e : kvMap.entrySet()) {
  JsonObject jsonObject = new JsonObject();
  jsonObject.addProperty("key", e.getKey());
  jsonObject.addProperty("value", e.getValue());
  array.add(jsonObject);
 }
 param.add("kv_list", array);
 String params = param.toString();
 String signature = SignUtils.createHmacSha256Sign(params, sessionKey);
 String url = String.format("https://api.weixin.qq.com/wxa/set_user_storage" +
   "?appid=%s&signature=%s&openid=%s&sig_method=%s",
  config.getAppid(), signature, openid, "hmac_sha256");
 String result = this.service.post(url, params);
 WxError error = WxError.fromJson(result);
 if (error.getErrorCode() != 0) {
  throw new WxErrorException(error);
 }
}

代码示例来源:origin: yjjdick/sdb-mall

/**
   * 下载临时素材
   */
  @GetMapping("/download/{mediaId}")
  public File getMedia(@PathVariable String mediaId) throws WxErrorException {
    return this.service.getMediaService().getMedia(mediaId);
  }
}

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

@GetMapping(produces = "text/plain;charset=utf-8" )
public String authGet(@RequestParam(name = "signature", required = false) String signature,
           @RequestParam(name = "timestamp", required = false) String timestamp,
           @RequestParam(name = "nonce", required = false) String nonce,
           @RequestParam(name = "echostr", required = false) String echostr) {
  if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
    throw new IllegalArgumentException("invalid argument" );
  }
  if (this.wxService.checkSignature(timestamp, nonce, signature)) {
    return echostr;
  }
  return "非法请求";
}

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

@Override
public boolean checkImage(File file) throws WxErrorException {
 //这里只是借用MediaUploadRequestExecutor,并不使用其返回值WxMediaUploadResult
 WxMediaUploadResult result = this.service.execute(MediaUploadRequestExecutor
  .create(this.service.getRequestHttp()), IMG_SEC_CHECK_URL, file);
 return result != null;
}

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

@Override
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
 Lock lock = this.wxMaService.getWxMaConfig().getJsapiTicketLock();
 try {
  lock.lock();
  if (forceRefresh) {
   this.wxMaService.getWxMaConfig().expireJsapiTicket();
  }
  if (this.wxMaService.getWxMaConfig().isJsapiTicketExpired()) {
   String responseContent = this.wxMaService.get(GET_JSAPI_TICKET_URL + "?type=jsapi", null);
   JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
   JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
   String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
   int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
   this.wxMaService.getWxMaConfig().updateJsapiTicket(jsapiTicket, expiresInSeconds);
  }
 } finally {
  lock.unlock();
 }
 return this.wxMaService.getWxMaConfig().getJsapiTicket();
}

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

@Override
public byte[] getQrCode(String path) throws WxErrorException {
 String appId = this.wxMaService.getWxMaConfig().getAppid();
 Path qrCodeFilePath = null;
 try {
  RequestExecutor<File, String> executor = BaseMediaDownloadRequestExecutor
   .create(this.wxMaService.getRequestHttp(), Files.createTempDirectory("weixin-java-tools-ma-" + appId).toFile());
  final StringBuilder url = new StringBuilder(GET_QRCODE_URL);
  if (StringUtils.isNotBlank(path)) {
   url.append("?path=").append(URLEncoder.encode(path, StandardCharsets.UTF_8.name()));
  }
  qrCodeFilePath = this.wxMaService.execute(executor, url.toString(), null).toPath();
  return Files.readAllBytes(qrCodeFilePath);
 } catch (IOException e) {
  throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).build(), e);
 } finally {
  if (qrCodeFilePath != null) {
   try {
    // 及时删除二维码文件,避免积压过多缓存文件
    Files.delete(qrCodeFilePath);
   } catch (Exception ignored) {
   }
  }
 }
}

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

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

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

@Override
public void setUserStorage(Map<String, String> kvMap, String sessionKey, String openid) throws WxErrorException {
 final WxMaConfig config = this.service.getWxMaConfig();
 JsonObject param = new JsonObject();
 JsonArray array = new JsonArray();
 for (Map.Entry<String, String> e : kvMap.entrySet()) {
  JsonObject jsonObject = new JsonObject();
  jsonObject.addProperty("key", e.getKey());
  jsonObject.addProperty("value", e.getValue());
  array.add(jsonObject);
 }
 param.add("kv_list", array);
 String params = param.toString();
 String signature = SignUtils.createHmacSha256Sign(params, sessionKey);
 String url = String.format("https://api.weixin.qq.com/wxa/set_user_storage" +
   "?appid=%s&signature=%s&openid=%s&sig_method=%s",
  config.getAppid(), signature, openid, "hmac_sha256");
 String result = this.service.post(url, params);
 WxError error = WxError.fromJson(result);
 if (error.getErrorCode() != 0) {
  throw new WxErrorException(error);
 }
}

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

/**
   * 下载临时素材
   */
  @GetMapping("/download/{mediaId}")
  public File getMedia(@PathVariable String appid, @PathVariable String mediaId) throws WxErrorException {
    final WxMaService wxService = WxMaConfiguration.getMaService(appid);

    return wxService.getMediaService().getMedia(mediaId);
  }
}

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

@GetMapping(produces = "text/plain;charset=utf-8")
public String authGet(@PathVariable String appid,
           @RequestParam(name = "signature", required = false) String signature,
           @RequestParam(name = "timestamp", required = false) String timestamp,
           @RequestParam(name = "nonce", required = false) String nonce,
           @RequestParam(name = "echostr", required = false) String echostr) {
  this.logger.info("\n接收到来自微信服务器的认证消息:signature = [{}], timestamp = [{}], nonce = [{}], echostr = [{}]",
    signature, timestamp, nonce, echostr);
  if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
    throw new IllegalArgumentException("请求参数非法,请核实!");
  }
  final WxMaService wxService = WxMaConfiguration.getMaService(appid);
  if (wxService.checkSignature(timestamp, nonce, signature)) {
    return echostr;
  }
  return "非法请求";
}

代码示例来源:origin: linlinjava/litemall

WxMaJscode2SessionResult result = this.wxService.getUserService().getSessionInfo(wxCode);
  openId = result.getOpenid();
} catch (Exception e) {

代码示例来源:origin: lord-of-code/loc-framework

@Bean
 @ConditionalOnMissingBean
 public WxMaService wxMaService(WxMaConfig maConfig) {
  WxMaService service = new WxMaServiceImpl();
  service.setWxMaConfig(maConfig);
  return service;
 }
}

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

@Override
public WxMediaUploadResult uploadMedia(String mediaType, File file) throws WxErrorException {
 String url = String.format(MEDIA_UPLOAD_URL, mediaType);
 return this.wxMaService.execute(MediaUploadRequestExecutor.create(this.wxMaService.getRequestHttp()), url, file);
}

相关文章