<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Exception;

/**
 * 文章资讯接口
 */
class News extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 文章资讯列表
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNewsList(){

        $list = Db::name("unishop_news")->field("id,title,look,image,createtime")->where("status",'1')->whereNull("deletetime")->order("weigh desc")->select();

        if(!empty($list)){
            foreach ($list as $key=>$value){
                $list[$key]['createtime'] = date("Y-m-d H:i:s",$value['createtime']);
            }
        }

        $this->success("文章资讯列表",$list);
    }

    /**
     * 文章资讯详情
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNewsDetails(){

        try {

            $nid = input("nid",'0');

            $res = Db::name("unishop_news")->where("id",$nid)->where("status",'1')->whereNull("deletetime")->find();

            if(empty($res)){
                $this->error("文章已删除");
            }

            $res['createtime'] = date("Y-m-d H:i:s",$res['createtime']);

            //增加文章阅读量
            Db::name("unishop_news")->where("id",$nid)->setInc('look');

            $this->success("文章资讯详情",$res);

        } catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

}
