예제 #1
0
  @RequestMapping(value = "/api/wechat/", method = RequestMethod.POST)
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, DocumentException, WxErrorException {
    // 将请求、响应的编码均设置为UTF-8(防止中文乱码)
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    logger.debug("点击按钮");
    /* 获取openId */
    // String code = request.getParameter("code");
    // WxMpOAuth2AccessToken wxMpOAuth2AccessToken = WeChatConnector
    // .getMpService().oauth2getAccessToken(code);
    // WxMpUser wxMpUser = WeChatConnector.getMpService()
    // .oauth2getUserInfo(wxMpOAuth2AccessToken, null);
    // logger.debug("openId"+wxMpUser.getOpenId());
    Map<String, String> requestMap = MessageUtil.parseXml(request);
    logger.debug(requestMap.toString());
    String toUserName = requestMap.get("FromUserName");
    String fromUserName = requestMap.get("ToUserName");
    String msgType = requestMap.get("MsgType");
    // logger.debug("msgType:"+msgType);
    // logger.debug("ToUserName"+fromUserName);
    // logger.debug("FromUserName"+toUserName);
    // logger.debug("Event:"+requestMap.get("Event"));
    // logger.debug("OrderId:"+requestMap.get("OrderId"));
    // logger.debug("OrderStatus:"+requestMap.get("OrderStatus"));
    // logger.debug("ProductId:"+requestMap.get("ProductId"));
    // logger.debug("SkuInfo:"+requestMap.get("SkuInfo"));
    if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
      String eventType = requestMap.get("Event");
      if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {
        // 事件KEY值,与创建自定义菜单时指定的KEY值对应
        String eventKey = requestMap.get("EventKey");
        if (eventKey.matches("activitys:\\d+")) {
          logger.debug("推送长图文消息");
          Long outNewsMessageId = Long.parseLong(eventKey.split(":")[1]);
          OutNewsMessage outNewsMessage = outNewsMessageRepository.findOne(outNewsMessageId);
          WxMpXmlOutNewsMessage m =
              WxMpXmlOutMessage.NEWS().fromUser(fromUserName).toUser(toUserName).build();
          if (outNewsMessage != null) {
            logger.debug("子图文size:" + outNewsMessage.getOutNewsMessageItems().size());
            logger.debug("遍历添加子图文");
            for (OutNewsMessageItem outNewsMessageItem : outNewsMessage.getOutNewsMessageItems()) {
              WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
              //							String url=outNewsMessageItem.getUrl();
              //							url=url.replace("/", "%2F");
              //							url=url.replace(":", "%3A");
              //							apiUrl=apiUrl.replace("{url}", url);
              item.setUrl(
                  apiUrl.replace("{outNewsMessageItem.id}", "" + outNewsMessageItem.getId()));
              logger.debug("apiUrl:" + apiUrl);
              item.setPicUrl(outNewsMessageItem.getPicUrl());
              item.setDescription(outNewsMessageItem.getContent());
              item.setTitle(outNewsMessageItem.getContent());
              m.addArticle(item);
            }
            // WxMpXmlOutNewsMessage m = WxMpXmlOutMessage.NEWS()
            // .fromUser(fromUserName).toUser(toUserName)
            // .addArticle(item).build();
            logger.info(m.toXml());
            response.getWriter().write(m.toXml());
            response.getWriter().close();
          }
          return;
        } else {

          OutMessage message = outMessageService.findOneById(Long.parseLong(eventKey));
          if (message == null) {
            return;
          }
          if ("news".equals(message.getType())) {
            logger.debug("推送图文消息");
            WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
            item.setUrl(message.getUrl());
            item.setPicUrl(message.getPicUrl());
            item.setDescription(message.getContent());
            item.setTitle(message.getTitle());
            WxMpXmlOutNewsMessage m =
                WxMpXmlOutMessage.NEWS()
                    .fromUser(fromUserName)
                    .toUser(toUserName)
                    .addArticle(item)
                    .build();
            logger.info(m.toXml());
            response.getWriter().write(m.toXml());
            response.getWriter().close();
          }
          if ("text".equals(message.getType())) {
            logger.debug("推送文本消息");
            WxMpXmlOutTextMessage text =
                WxMpXmlOutMessage.TEXT()
                    .fromUser(fromUserName)
                    .toUser(toUserName)
                    .content(message.getContent())
                    .build();
            response.getWriter().write(text.toXml());
            response.getWriter().close();
          }
        }
      } else if (eventType.equals("merchant_order")) {
        /*捕获微小店付款事件*/
        logger.debug("微小店付款");
        /*得到订单号*/
        String orderId = requestMap.get("OrderId");
        /*查看商品详情,得到商品名和商品数量*/
        Map<String, Object> orderInfo = RestTemplateUtil.getOrderInfo(orderId);
        Integer product_count = (Integer) orderInfo.get("product_count");
        String product_name = (String) orderInfo.get("product_name");
        Integer order_total_price = (Integer) orderInfo.get("order_total_price");
        String returnOrderPrice = "" + (order_total_price * 1.0 / 100) + "元";
        /*根据商品名确定是发哪个供应商的提货码,并且发同等数量*/
        Goods goods = goodsRepository.findOneByName(product_name);
        if (goods == null) {
          logger.debug("商品:" + product_name + ",未在管理台绑定对应的批次号");
          return;
        }
        List<NCoupon> nCoupons =
            nCouponService.getOneUnusedNCoupon(goods.getCouponBatch(), COUPONSTATUS.UNUSED);
        if (nCoupons == null || nCoupons.size() < product_count) {
          logger.debug("提货码不足");
          return;
        }
        String couponsInfo = "";
        for (int i = 0; i < product_count; i++) {
          NCoupon nCoupon = nCoupons.get(i);
          nCoupon.setStatus(COUPONSTATUS.SENT);
          nCouponService.save(nCoupon);
          couponsInfo += nCoupon.getCode() + ",";
        }
        couponsInfo = couponsInfo.substring(0, couponsInfo.length() - 1);
        logger.debug("couponsInfo:" + couponsInfo);
        RestTemplateUtil.sendTemplateInfo(
            toUserName, "https://www.baidu.com/", returnOrderPrice, couponsInfo);
      } else {
        logger.debug("eventType:" + eventType);
      }
    }
  }