package com.cftech.mp.article.web;


import com.alibaba.fastjson.JSONObject;
import com.cftech.core.util.MpTokenUtil;
import com.cftech.sys.security.UserUtils;
import org.mp.api.wxbase.wxmedia.model.WxNews;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lisw on 2017/4/18 0018.
 * 微信官方素材，管理Controller
 */
@RequestMapping("/a/wxArticle/article")
@RestController
public class WxArticleController {

    @Autowired
    private MpTokenUtil tokenUtil;


    @RequestMapping(value="newsList",method = {RequestMethod.POST,RequestMethod.GET})
    public JSONObject articleList(HttpServletRequest request,int iDisplayStart, int iDisplayLength){
        JSONObject rtnJson = new JSONObject();
        Long accountsId = UserUtils.getmpaccounts(request);
        String accessToken = tokenUtil.getToken(accountsId);
        WxNews wxNews = null;
//        try {
//            wxNews =  JwMediaAPI.queryArticlesByMaterial(accessToken,"news",iDisplayStart,iDisplayLength);
//        } catch (WexinReqException e) {
//            e.printStackTrace();
//        }
        rtnJson.put("iTotalRecords", wxNews.getTotal_count());
        rtnJson.put("iTotalDisplayRecords", wxNews.getTotal_count());
        rtnJson.put("aaData", wxNews.getItem());
        return  rtnJson;
    }
}
