Exemple #1
0
  @Override
  public Qrcode httpQrcode(ReqTicket reqTicket, String scene) {
    Serializable sceneValue = reqTicket.generateScene();
    String actionName = reqTicket.getActionName().toString();

    Ticket ticket = weixinProxy.httpTicket(reqTicket); // 通过参数获取ticket
    String qrcodeUrl = String.format(QRCODE_URL, ticket.getTicket());
    byte[] results = HttpClientUtil.httpGet(qrcodeUrl);
    Qrcode qrcode = new Qrcode();
    qrcode.setTicketId(ticket.getId());
    qrcode.setActionName(actionName);
    qrcode.setSceneValue(sceneValue.toString());
    qrcode.setSceneType(sceneValue.getClass().getName());
    qrcode.setName(weixinConfig.getName());
    qrcode.setSuffix(weixinConfig.getSuffix());
    qrcode.setBytes(results);
    qrcode.setSceneName(scene);
    if (log.isInfoEnabled()) {
      log.info(
          "返回二维码信息:{}",
          JSON.toJSONString(
              qrcode, SerializerFeature.PrettyFormat, SerializerFeature.WriteClassName));
    }
    return qrcode;
  }
Exemple #2
0
 @Override
 public WxMsg testwhitelist(White white) {
   String jsonString = JSON.toJSONString(white);
   String url =
       String.format(CARD_TESTWHITELIST_URL, this.weixinProxy.httpToken().getAccessToken());
   byte[] bytes = HttpClientUtil.httpPost(url, jsonString);
   return JSON.parseObject(bytes, WxMsg.class);
 }
Exemple #3
0
 @Override
 public Card batchCard(ReqBatchCatch reqBatchCatch) {
   Utils.validate(reqBatchCatch);
   String jsonString = JSON.toJSONString(reqBatchCatch);
   String token = this.weixinProxy.httpToken().getAccessToken();
   String url = String.format(BATCH_CARD_URL, token);
   byte[] bytes = HttpClientUtil.httpPost(url, jsonString);
   return JSON.parseObject(bytes, Card.class);
 }
Exemple #4
0
 @Override
 public UserCard getUserCardList(ReqUserCard userCard) {
   Utils.validate(userCard);
   String jsonString = JSON.toJSONString(userCard);
   String token = this.weixinProxy.httpToken().getAccessToken();
   String url = String.format(USER_CARD_LIST_URL, token);
   byte[] bytes = HttpClientUtil.httpPost(url, jsonString);
   return JSON.parseObject(bytes, UserCard.class);
 }
Exemple #5
0
 @Override
 public Card getCard(JSONObject params) throws IOException {
   Object cardId = params.get("card_id");
   Assert.notNull(cardId, "卡券ID不能为空");
   String jsonString = params.toString();
   String token = this.weixinProxy.httpToken().getAccessToken();
   String url = String.format(CARD_URL, token);
   byte[] bytes = HttpClientUtil.httpPost(url, jsonString);
   return JSON.parseObject(bytes, Card.class);
 }
Exemple #6
0
  @Override
  public WxMsg createMenu(List<Button> buttons) {
    Map<String, List<Button>> btns = new HashMap<>();
    btns.put("button", buttons);
    String jsonString = JSON.toJSONString(btns);

    String token = this.weixinProxy.httpToken().getAccessToken();
    String url = String.format(CREATE_MENU_URL, token);
    byte[] bytes = HttpClientUtil.httpPost(url, jsonString);
    return JSON.parseObject(bytes, WxMsg.class);
  }
Exemple #7
0
 @Override
 public Message httpTemplateMsg(TemplateMsg templateMsg) {
   Token token = this.weixinProxy.httpToken();
   String templateUrl = String.format(TEMPLATE_URL, token.getAccessToken());
   String req = JSON.toJSONString(templateMsg);
   byte[] bytes = HttpClientUtil.httpPost(templateUrl, req);
   Message message = JSON.parseObject(bytes, Message.class);
   if (log.isInfoEnabled()) {
     log.info(
         "返回模板消息信息:{}",
         JSON.toJSONString(
             message, SerializerFeature.PrettyFormat, SerializerFeature.WriteClassName));
   }
   return message;
 }
Exemple #8
0
 @Override
 public Poi httpPoi(String poiId) {
   Assert.notNull(poiId, "获取门店的poi_id不能为空");
   JSONObject jsonObject = new JSONObject();
   jsonObject.put("poi_id", poiId);
   String req = jsonObject.toJSONString();
   String url = String.format(POI_URL, this.weixinProxy.httpToken().getAccessToken());
   byte[] bytes = HttpClientUtil.httpPost(url, req);
   Poi poi = JSON.parseObject(bytes, Poi.class);
   if (log.isInfoEnabled()) {
     log.info(
         "返回门店信息:{}",
         JSON.toJSONString(poi, SerializerFeature.PrettyFormat, SerializerFeature.WriteClassName));
   }
   return poi;
 }
Exemple #9
0
  @Override
  public Ticket httpTicket(ReqTicket reqTicket) {
    if (log.isInfoEnabled()) {
      log.info(
          JSON.toJSONString(
              reqTicket, SerializerFeature.PrettyFormat, SerializerFeature.WriteClassName));
    }
    Token token = weixinProxy.httpToken();
    String ticketUrl = null;
    TicketType ticketType = null;
    if (reqTicket.getActionName() == QrcodeType.QR_CARD) {
      ticketUrl = String.format(CARD_QRCODE_URL, token.getAccessToken()); // 卡券投放的二维码ticket路径
      ticketType = TicketType.QR_CARD_TICKET;
    } else {
      ticketUrl = String.format(TICKET_URL, token.getAccessToken()); // 二维码ticket路径
      ticketType = TicketType.QR_TICKET;
    }
    Assert.notNull(ticketUrl, "ticket url 不能为空");

    String jsonParam = JSON.toJSONString(reqTicket, SerializerFeature.WriteNonStringKeyAsString);
    byte[] results = HttpClientUtil.httpPost(ticketUrl, jsonParam);
    Ticket ticket = JSON.parseObject(results, Ticket.class);

    ticket.setType(ticketType);
    ticket.setCreateTime(System.currentTimeMillis());
    if (reqTicket.getActionName() == QrcodeType.QR_CARD) {
      ticket.setCode(reqTicket.getCode());
      ticket.setCardId(reqTicket.getCardId());
      ticket.setOpenid(reqTicket.getOpenid());
      ticket.setUniqueCode(reqTicket.getUniqueCode());
      ticket.setOuterId(reqTicket.getOuterId());
    }

    if (reqTicket.getActionName() == QrcodeType.QR_CARD) {}

    return ticket;
  }
Exemple #10
0
  @Override
  public Ticket httpTicket(TicketType ticketType) {
    Assert.notNull(ticketType, "请求的ticketType类型不能为空");
    if (log.isInfoEnabled()) {
      log.info(
          "请求获取ticket:{}",
          JSON.toJSONString(
              ticketType, SerializerFeature.PrettyFormat, SerializerFeature.WriteClassName));
    }
    if (ticketType == TicketType.QR_TICKET || ticketType == TicketType.QR_CARD_TICKET) {
      if (log.isErrorEnabled()) {
        log.error(
            "不能获取改类型{}的ticket,请调用public Ticket httpTicket(ReqTicket reqTicket) 方法获取", ticketType);
      }
      throw new RuntimeException("不能获取改类型(" + ticketType + ")的ticket");
    }
    Ticket result = null;
    if (ticketType == TicketType.JSAPI_TICKET) {
      result = this.jssapiTicket;
    } else if (ticketType == TicketType.JSAPI_CARD_TICKET) {
      result = this.jssapiCardTicket;
    }
    if (result == null) {
      result = this.ticketService.selectLastTicket(ticketType);
    }
    if (result != null && result.getCreateTime() != null) {
      Long currentTime = System.currentTimeMillis() / 1000;
      Long lastTokenCreateTime = result.getCreateTime() / 1000;
      Long gap = currentTime - lastTokenCreateTime;
      if (gap < result.getExpiresIn()) {

        if (ticketType == TicketType.JSAPI_TICKET) {
          this.jssapiTicket = result;
        } else if (ticketType == TicketType.JSAPI_CARD_TICKET) {
          this.jssapiCardTicket = result;
        }
        if (log.isInfoEnabled()) {
          log.info(
              "返回ticket:{}",
              JSON.toJSONString(
                  result, SerializerFeature.PrettyFormat, SerializerFeature.WriteClassName));
        }
        return result;
      }
    }
    String ticketUrl = null;

    if (ticketType == TicketType.JSAPI_TICKET) {
      ticketUrl = String.format(JSAPI_TICKET_URL, this.weixinProxy.httpToken().getAccessToken());
    } else if (ticketType == TicketType.JSAPI_CARD_TICKET) {
      ticketUrl =
          String.format(JSAPI_CARD_TICKET_URL, this.weixinProxy.httpToken().getAccessToken());
    }
    Assert.notNull(ticketUrl, "获取ticket的url不能为空");

    byte[] bytes = HttpClientUtil.httpGet(ticketUrl);
    result = JSON.parseObject(bytes, Ticket.class);
    result.setType(ticketType);
    result.setCreateTime(System.currentTimeMillis());
    if (log.isInfoEnabled()) {
      log.info(
          "返回ticket:{}",
          JSON.toJSONString(
              result, SerializerFeature.PrettyFormat, SerializerFeature.WriteClassName));
    }
    if (ticketType == TicketType.JSAPI_TICKET) {
      this.jssapiTicket = result;
    } else if (ticketType == TicketType.JSAPI_CARD_TICKET) {
      this.jssapiCardTicket = result;
    }
    return result;
  }
Exemple #11
0
 @Override
 public Fans httpFans(String url) {
   byte[] results = HttpClientUtil.httpGet(url);
   Fans fans = JSON.parseObject(results, Fans.class);
   return fans;
 }
Exemple #12
0
 /**
  * 请求服务器access_token
  *
  * @return
  */
 private Token httpToken(String url, TokeyType type) {
   byte[] result = HttpClientUtil.httpGet(url);
   Token token = JSON.parseObject(result, Token.class);
   token.setType(type);
   return token;
 }