@RequestMapping(method = RequestMethod.POST, value = "/charge")
  @ResponseBody
  public Charge charge(HttpServletRequest request) {
    // 获取请求body中的参数
    JSONObject params = Tools.getParams(request);
    // 文章的标识
    String articleId = params.getString("articleId");
    // 付款读者的标识
    String wechat = params.getString("wgateid");
    // 付款的渠道
    String channel = params.getString("channel");
    // 当前页面的URL
    String url = params.getString("url");
    // 付款的金额
    int amount = params.getInt("amount");
    // 获取请求的IP地址
    String ip = Tools.getIP(request);

    Deal deal = new Deal(wechat, articleId);
    deal.setDealPayment(amount);
    deal.setClientIp(ip);
    if (dealService.queryDealRecord(deal).getResponseCode() == ResponseCode.RESPONSE_NULL) {
      ResultData createMessage = dealService.createDealRecord(deal);
      if (createMessage.getResponseCode() == ResponseCode.RESPONSE_OK) {
        logger.debug("Insert a deal record successfully.");
      } else {
        logger.debug("Fail to insert a deal record.");
        return null;
      }
    }

    // 设置订单的ID
    String orderNo = deal.getDealId();

    // 根据文章的标识查询文章的信息
    Article article = new Article();
    article.setArticleId(articleId);
    article = (Article) articleService.queryArticle(article).getData();

    Reader reader = new Reader();
    if (!StringUtils.isEmpty(wechat)) {
      reader.setReaderWechat(wechat);
      ResultData readerExistMessage = readerService.queryReader(reader);
      if (readerExistMessage.getResponseCode() == ResponseCode.RESPONSE_NULL) {
        readerService.createReader(reader);
      } else if (readerExistMessage.getResponseCode() == ResponseCode.RESPONSE_ERROR) {
        return null;
      }
    }

    ChargeForm form = new ChargeForm(orderNo, article, reader, channel, amount, ip, url);
    ResultData result = dealService.charge(form);

    Charge charge = (Charge) result.getData();
    logger.debug(charge.toString());
    return charge;
  }
  @RequestMapping(method = RequestMethod.GET, value = "/{articleId}")
  public ModelAndView distribute(@PathVariable String articleId, HttpServletRequest request) {
    ModelAndView view = new ModelAndView();
    String wxopenid = request.getParameter("wxopenid");
    String wechat = request.getParameter("wgateid");
    String verify = request.getParameter("verify");
    String articleURL = "";
    if (!StringUtils.isEmpty(wechat)) {
      Reader reader = new Reader();
      reader.setReaderWechat(wechat);
      ResultData readerExistMessage = readerService.queryReader(reader);
      if (readerExistMessage.getResponseCode() == ResponseCode.RESPONSE_NULL) {
        readerService.createReader(reader);
      } else if (readerExistMessage.getResponseCode() == ResponseCode.RESPONSE_ERROR) {

      }
    }
    // 根据文章的ID查询图文信息
    ArticleViewVO vo = new ArticleViewVO();
    Article article = new Article();
    article.setArticleId(articleId);
    ResultData articleMessage = articleService.queryArticle(article);
    vo.setArticle((Article) articleMessage.getData());
    // 根据文章的ID和用户的微信标识查询当前用户是否已经打赏过此文章
    if (!StringUtils.isEmpty(wechat)) {
      Deal deal = new Deal(wechat, articleId);
      ResultData dealMessage = dealService.queryDealRecord(deal);
      if (dealMessage.getResponseCode() == ResponseCode.RESPONSE_OK) {
        vo.setPaymentStatus(ArticlePaymentStatus.ARTICLE_PAYED);
      } else {
        vo.setPaymentStatus(ArticlePaymentStatus.ARTICLE_NOT_PAYED);
      }
    } else {
      vo.setPaymentStatus(ArticlePaymentStatus.ARTICLE_NOT_PAYED);
    }
    view.setViewName("/distribute/view");
    view.addObject("vo", vo);
    if (!StringUtils.isEmpty(wechat)) {
      String url =
          "http://www.njuat.com/distribute/"
              + articleId
              + "?wxopenid="
              + wxopenid
              + "&wgateid="
              + wechat
              + "&verify="
              + verify;
      Configuration configuration = WechatConfig.config(url);
      try {
        String shareURL =
            "http://www.weixingate.com/gate.php?back="
                + URLEncoder.encode("http://www.njuat.com/distribute/" + articleId, "utf-8")
                + "&force=1";
        configuration.setShareLink(shareURL);
      } catch (Exception e) {

      }
      view.addObject("configuration", configuration);
      view.addObject("wgateid", wechat);
    }
    return view;
  }