cn.binarywang.wx.miniapp.constant.WxMaConstants Java Examples

The following examples show how to use cn.binarywang.wx.miniapp.constant.WxMaConstants. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WxMaDemoServer.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(WxMaMessage wxMessage, Map<String, Object> context,
                   WxMaService service, WxSessionManager sessionManager) throws WxErrorException {
  try {
    WxMediaUploadResult uploadResult = service.getMediaService()
      .uploadMedia(WxMaConstants.MediaType.IMAGE, "png",
        ClassLoader.getSystemResourceAsStream("tmp.png"));
    service.getMsgService().sendKefuMsg(
      WxMaKefuMessage
        .newImageBuilder()
        .mediaId(uploadResult.getMediaId())
        .toUser(wxMessage.getFromUser())
        .build());
  } catch (WxErrorException e) {
    e.printStackTrace();
  }
}
 
Example #2
Source File: WxMaDemoServer.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(WxMaMessage wxMessage, Map<String, Object> context,
                   WxMaService service, WxSessionManager sessionManager) throws WxErrorException {
  try {
    final File file = service.getQrcodeService().createQrcode("123", 430);
    WxMediaUploadResult uploadResult = service.getMediaService().uploadMedia(WxMaConstants.MediaType.IMAGE, file);
    service.getMsgService().sendKefuMsg(
      WxMaKefuMessage
        .newImageBuilder()
        .mediaId(uploadResult.getMediaId())
        .toUser(wxMessage.getFromUser())
        .build());
  } catch (WxErrorException e) {
    e.printStackTrace();
  }
}
 
Example #3
Source File: WxMaMsgServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public void sendTemplateMsg(WxMaTemplateMessage templateMessage) throws WxErrorException {
  String responseContent = this.wxMaService.post(TEMPLATE_MSG_SEND_URL, templateMessage.toJson());
  JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
  if (jsonObject.get(WxMaConstants.ERRCODE).getAsInt() != 0) {
    throw new WxErrorException(WxError.fromJson(responseContent));
  }
}
 
Example #4
Source File: WxMaMediaController.java    From sdb-mall with Apache License 2.0 5 votes vote down vote up
/**
 * 上传临时素材
 *
 * @return 素材的media_id列表,实际上如果有的话,只会有一个
 */
@PostMapping("/upload")
public List<String> uploadMedia(HttpServletRequest request) throws WxErrorException {
    CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());

    if (!resolver.isMultipart(request)) {
        return Lists.newArrayList();
    }

    MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
    Iterator<String> it = multiRequest.getFileNames();
    List<String> result = Lists.newArrayList();
    while (it.hasNext()) {
        try {
            MultipartFile file = multiRequest.getFile(it.next());
            File newFile = new File(Files.createTempDir(), file.getOriginalFilename());
            this.logger.info("filePath is :" + newFile.toString());
            file.transferTo(newFile);
            WxMediaUploadResult uploadResult = this.service.getMediaService().uploadMedia(WxMaConstants.KefuMsgType.IMAGE, newFile);
            this.logger.info("media_id : " + uploadResult.getMediaId());
            result.add(uploadResult.getMediaId());
        } catch (IOException e) {
            this.logger.error(e.getMessage(), e);
        }
    }

    return result;
}