<?php

namespace app\api\controller;

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

class PlatformArticle extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = Db::name('platform_article');
    }

    /**
     * 分类列表
     * @return void
     */
    public function getTypeList()
    {
        try {
            $field = 'id,title';
            $where['status'] = 1;
            $result = Db::name('platform_article_type')->field($field)->where($where)->order('weigh asc')->select();

            $this->success('获取成功',$result);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 资讯列表
     * @return void
     */
    public function getList()
    {
        try {
            $typeId = $this->request->param('type_id',0);
            $field = 'id,title,desc,images,video,createtime';
            $where['status'] = 1;
            $where['type_id'] = $typeId;
            $result = Db::name('platform_article')->field($field)
                ->where($where)->order('createtime desc')->autopage()->select();
            $userName = config('site.article_name') ? config('site.article_name') : '';
            $avatar = config('site.article_avatar') ? cdnurl(config('site.article_avatar')) : '';
            if (!empty($result)) {
                foreach ($result as $key => &$value) {
                    $value['handeltime'] = weixinDate($value['createtime']);
                    $value['createtime'] = !empty($value['createtime']) ? date('Y-m-d H:i',$value['createtime']) : '';
                    $value['avatar'] = $avatar;
                    $value['name'] = $userName;

                }
                $result = list_domain_image($result,['images','video']);
            }
            $this->success('获取成功',$result);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 资讯详情
     * @return void
     */
    public function getInfo()
    {
        try {
            $id = $this->request->param('id',0);
            $field = 'id,title,desc,images,video,content,createtime';
            $where['status'] = 1;
            $where['id'] = $id;
            $result = Db::name('platform_article')->field($field)->where($where)->find();
            $userName = config('site.article_name') ? config('site.article_name') : '';
            $avatar = config('site.article_avatar') ? cdnurl(config('site.article_avatar')) : '';
            if (!empty($result)) {
                $result['handeltime'] = weixinDate($result['createtime']);
                $result['createtime'] = !empty($result['createtime']) ? date('Y.m.d H:i',$result['createtime']) : '';
                $result['avatar'] = $avatar;
                $result['name'] = $userName;
                $result = info_domain_image($result,['images','video']);
            }
            $this->success('获取成功',$result);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }
}