<?php

namespace Content\V1;

use Dever;
use Content\Lib\Base;

class News extends Base
{
    //*@ apiSampleRequest /v1/news.index 暂时不添加测试接口,这里需要域名和跨域支持
    /**
     * @api {get} v1/news.index 首页接口
     * @apiVersion 1.0.0
     * @apiName api.index
     * @apiGroup Data
     *
     * @apiDescription 小程序的首页接口
     *
     * apiSampleRequest /v1/news.index
     *
     * @apiParam {Number} appid 小程序id
     *
     * @apiSuccess {Object[]} info 小程序基本信息
     * @apiSuccess {Number}   info.id  小程序id
     * @apiSuccess {String}   info.name  小程序名称
     * @apiSuccess {Number}   info.cate_id  小程序所属栏目id
     * @apiSuccess {String}   info.logo  小程序logo
     * @apiSuccess {String}   info.title  首页标题
     * @apiSuccess {String}   info.pic  首页头图
     *
     * @apiSuccess {Object[]} cate 主栏目信息
     * @apiSuccess {Number}   cate.id  栏目id
     * @apiSuccess {String}   cate.name  栏目名称
     *
     * @apiSuccess {Object[]} cate_child 子栏目信息
     * @apiSuccess {Number}   cate_child.id  栏目id
     * @apiSuccess {String}   cate_child.name  栏目名称
     * @apiSuccess {String}   cate_child.pic  栏目图片
     * @apiSuccess {Number}   cate_child.cate_id  上级栏目id
     *
     * @apiSuccess {Object[]} cate_child.news 资讯信息
     * @apiSuccess {Number}   cate_child.news.id  资讯id
     * @apiSuccess {String}   cate_child.news.name  资讯标题
     * @apiSuccess {String}   cate_child.news.reading  资讯导读
     * @apiSuccess {String}   cate_child.news.content  资讯内容
     * @apiSuccess {String}   cate_child.news.num_view  阅读数
     * @apiSuccess {String}   cate_child.news.num_review  评论数
     * @apiSuccess {String}   cate_child.news.num_up  赞数
     * @apiSuccess {String}   cate_child.news.cdate  时间
     *
     * @apiSuccess {Object[]} cate_child.news.author 作者信息
     * @apiSuccess {Number}   cate_child.news.author.id  作者id
     * @apiSuccess {String}   cate_child.news.author.name  作者姓名
     * @apiSuccess {String}   cate_child.news.author.pic  作者头像
     *
     * @apiSuccessExample 操作成功
     * {"status":1,"code":1,"msg":"success","data":{"info":{"id":"1","name":"\u7279\u8272\u5c0f\u9547\u600e\u4e48\u641e","cate_id":"1","logo":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg","title":"test","pic":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/da31b5d54d15192fea10406f77f4b0cb.jpg","state":"1","cdate":"1521858466"},"cate":{"id":"1","name":"\u7279\u8272\u5c0f\u9547\u4e0d\u5f97\u4e0d\u5b66\u4e60\u7cfb\u5217","cate_id":"-1","reorder":"1","state":"1","cdate":"1521814022"},"cate_child":[{"id":"3","name":"\u5341\u5927\u6848\u4f8b","cate_id":"1","reorder":"1","state":"1","cdate":"1521858511","news":[{"id":"1","name":"\u4e07\u8fbe\u57ce\u521b\u65b0\u4e1a\u6001\u7814\u7a76\u62a5\u544a","pic":["http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg"],"top":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/da31b5d54d15192fea10406f77f4b0cb.jpg","cate_id":"3","info_id":"1","author_id":"1","content":"<p>\u5730\u65b9\u5927\u5e45\u5ea6\u53d1<\/p>","num_view":"0","cdate":"9\u5c0f\u65f6\u524d","author":{"id":"1","name":"\u7a0b\u8302","pic":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg","state":"1","cdate":"1521858481"}}]},{"id":"2","name":"\u5341\u5927\u680f\u76ee","cate_id":"1","reorder":"1","state":"1","cdate":"1521858505"}]},"page":{"total":"1","current_page":1,"total_page":1,"next_page":1,"prev_page":1,"html":"","status":0}}
     *
     * @apiErrorExample 操作失败
     * {"status":2,"code":1,"msg":"\u9519\u8bef\u7684\u5c0f\u7a0b\u5e8fid"}
     */
    public function index_api()
    {
        # 获取一条主栏目
        $this->data['cate'] = Dever::load('set/cate-one', $this->data['info']['cate_id']);

        if ($this->data['cate']) {
            # 获取焦点图
            //$this->focus($this->data['cate']['id']);

            $this->data['cate_child'] = Dever::load('set/cate-getAll', array('option_cate_id' => $this->data['cate']['id']));
            foreach($this->data['cate_child'] as $k => $v) {
                $this->data['cate_child'][$k]['pic'] = $this->replacePic($v['pic']);
            }
            if ($this->data['cate_child'] && isset($this->data['cate_child'][0])) {
                $news = Dever::load('content/news-getAll', array('option_info_id' => $this->data['info']['id'], 'option_cate_id' => $this->data['cate_child'][0]['id']));

                if ($news) {
                    foreach ($news as $k => $v) {
                        $v['cdate'] = date('m-d', $v['cdate']);
                        $this->data['cate_child'][0]['news'][$k] = $this->one($v, 2);
                    }
                }
            }
        }

        return $this->data;
    }

    private function focus($cate)
    {
        $this->data['focus'] = Dever::load('content/focus-getAll', array('option_cate_id_parent' => $cate));

        if ($this->data['focus']) {
            foreach ($this->data['focus'] as $k => $v) {
                $this->data['focus'][$k]['pic'] = $this->replacePic($v['pic']);
            }
        } 
    }

    /**
     * @api {get} v1/news.list 资讯列表
     * @apiVersion 1.0.0
     * @apiName news.list
     * @apiGroup Data
     *
     * @apiDescription 小程序的资讯列表接口
     *
     * @apiParam {Number} appid 小程序id
     * @apiParam {Number} cateid 子栏目id
     *
     * @apiSuccess {Object[]} info 小程序基本信息
     * @apiSuccess {Number}   info.id  小程序id
     * @apiSuccess {String}   info.name  小程序名称
     * @apiSuccess {Number}   info.cate_id  小程序所属栏目id
     * @apiSuccess {String}   info.logo  小程序logo
     * @apiSuccess {String}   info.title  首页标题
     * @apiSuccess {String}   info.pic  首页头图
     *
     * @apiSuccess {Object[]} news 资讯信息
     * @apiSuccess {Number}   news.id  资讯id
     * @apiSuccess {String}   news.name  资讯标题
     * @apiSuccess {String}   news.reading  资讯导读
     * @apiSuccess {String}   news.name  资讯子标题
     * @apiSuccess {String}   news.content  资讯内容
     * @apiSuccess {String}   news.num_view  阅读数
     * @apiSuccess {String}   news.num_review  评论数
     * @apiSuccess {String}   news.num_up  赞数
     * @apiSuccess {String}   news.cdate  时间
     *
     * @apiSuccess {Object[]} news.author 作者信息
     * @apiSuccess {Number}   news.author.id  作者id
     * @apiSuccess {String}   news.author.name  作者姓名
     * @apiSuccess {String}   news.author.pic  作者头像
     *
     * @apiSuccessExample 操作成功
     * {"status":1,"code":1,"msg":"success","data":{"info":{"id":"1","name":"\u7279\u8272\u5c0f\u9547\u600e\u4e48\u641e","cate_id":"1","logo":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg","title":"test","pic":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/da31b5d54d15192fea10406f77f4b0cb.jpg","state":"1","cdate":"1521858466"},"news":[{"id":"1","name":"\u4e07\u8fbe\u57ce\u521b\u65b0\u4e1a\u6001\u7814\u7a76\u62a5\u544a","pic":["http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg"],"top":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/da31b5d54d15192fea10406f77f4b0cb.jpg","cate_id":"3","info_id":"1","author_id":"1","content":"<p>\u5730\u65b9\u5927\u5e45\u5ea6\u53d1<\/p>","num_view":"0","cdate":"9\u5c0f\u65f6\u524d","author":{"id":"1","name":"\u7a0b\u8302","pic":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg","state":"1","cdate":"1521858481"}}]},"page":{"total":"1","current_page":1,"total_page":1,"next_page":1,"prev_page":1,"html":"","status":0}}
     *
     * @apiErrorExample 操作失败
     * {"status":2,"code":1,"msg":"\u9519\u8bef\u7684\u5c0f\u7a0b\u5e8fid"}
     */
    public function list_api()
    {
        $cateid = Dever::input('cateid');

        if ($cateid > 0) {
            $cate = Dever::load('set/cate-one', $cateid);
            $this->data['news'] = array();
            if ($cate) {
                $this->data['news'] = Dever::load('content/news-getAll', array('option_info_id' => $this->data['info']['id'], 'option_cate_id' => $cate['id']));

                if ($this->data['news']) {
                    foreach ($this->data['news'] as $k => $v) {
                        $this->data['news'][$k] = $this->one($v, 2);
                    }
                }
            }
        }

        return $this->data;
    }

    /**
     * @api {get} v1/news.view 资讯详情
     * @apiVersion 1.0.0
     * @apiName news.view
     * @apiGroup Data
     *
     * @apiDescription 小程序的资讯详情接口
     *
     * @apiParam {Number} appid 小程序id
     * @apiParam {Number} id 资讯id
     *
     * @apiSuccess {Object[]} info 小程序基本信息
     * @apiSuccess {Number}   info.id  小程序id
     * @apiSuccess {String}   info.name  小程序名称
     * @apiSuccess {Number}   info.cate_id  小程序所属栏目id
     * @apiSuccess {String}   info.logo  小程序logo
     * @apiSuccess {String}   info.title  首页标题
     * @apiSuccess {String}   info.pic  首页头图
     *
     * @apiSuccess {Object[]} news 资讯信息
     * @apiSuccess {Number}   news.id  资讯id
     * @apiSuccess {String}   news.name  资讯标题
     * @apiSuccess {String}   news.reading 资讯导读
     * @apiSuccess {String}   news.content  资讯内容
     * @apiSuccess {String}   news.num_view  阅读数
     * @apiSuccess {String}   news.num_review  评论数
     * @apiSuccess {String}   news.num_up  赞数
     * @apiSuccess {String}   news.cdate  时间
     *
     * @apiSuccess {Object[]} news.author 作者信息
     * @apiSuccess {Number}   news.author.id  作者id
     * @apiSuccess {String}   news.author.name  作者姓名
     * @apiSuccess {String}   news.author.pic  作者头像
     *
     * @apiSuccessExample 操作成功
     * {"status":1,"code":1,"msg":"success","data":{"info":{"id":"1","name":"\u7279\u8272\u5c0f\u9547\u600e\u4e48\u641e","cate_id":"1","logo":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg","title":"test","pic":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/da31b5d54d15192fea10406f77f4b0cb.jpg","state":"1","cdate":"1521858466"},"news":{"id":"1","name":"\u4e07\u8fbe\u57ce\u521b\u65b0\u4e1a\u6001\u7814\u7a76\u62a5\u544a","info_id":"1","author_id":"1","cate_id_parent":"1","cate_id":"3","num_add_view":"0","num_up":"0","num_view":"0","num_review":"0","pic":["http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg"],"top":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/da31b5d54d15192fea10406f77f4b0cb.jpg","content":"<p>\u5730\u65b9\u5927\u5e45\u5ea6\u53d1<\/p>","reorder":"1","state":"1","cdate":"9\u5c0f\u65f6\u524d","author":{"id":"1","name":"\u7a0b\u8302","pic":"http:\/\/192.168.33.10\/plant\/data\/upload\/1\/2018\/03\/24\/1e493a87d4cdddde7fd06ad9762694d2.jpg","state":"1","cdate":"1521858481"}}}}
     *
     * @apiErrorExample 操作失败
     * {"status":2,"code":1,"msg":"\u9519\u8bef\u7684\u5c0f\u7a0b\u5e8fid"}
     */
    public function view_api()
    {
        $id = Dever::input('id');

        if ($id > 0) {
            $this->data['news'] = Dever::load('content/news-one', $id);

            if ($this->data['news']) {
                $this->data['news'] = $this->one($this->data['news'], 2);
                # 增加阅读量
                Dever::load('content/news-addView', $id);
            } else {
                Dever::alert('错误的资讯id');
            }
        } else {
            Dever::alert('错误的资讯id');
        }

        $this->data['vote'] = $this->vote(1, $id);

        return $this->data;
    }
}