<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/12/012
 * Time: 14:02
 */

namespace frontend\models;


class ArticleApi extends BaseApi
{
    const ARTICLE_CATEGORY_ID_HUODONG = 3; // 活动管理的文章分类id
    
    public $apiUrl = 'article';

    public function articleList($data = [])
    {
        $result = $this->get($this->apiUrl.'/article-list', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '获取文章列表成功');
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    public function articleSave($data = [])
    {
        $result = $this->post($this->apiUrl.'/article-save', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }
    
    public function articleDetail($data = [])
    {
        $result = $this->get($this->apiUrl.'/admin-article-detail', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }
    
    public function articleDelete($data = [])
    {
        $result = $this->post($this->apiUrl.'/article-delete', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 最新活动
     * @param array $options
     * @return array
     */
    public function getActivity($options = [])
    {
        $default = [
            'id' => self::ARTICLE_CATEGORY_ID_HUODONG,
            'is_publication' => 1,
            'pageSize' => 20,
            'orderBy' => 'is_top desc, create_date desc',
        ];
        
        $params = array_merge($default, $options);
        
        $articleList = $this->articleList($params)['data']['dataList'];

        foreach ($articleList as $k => $v) {
            $articleList[$k]['create_date'] = date('Y-m-d', strtotime($v['create_date']));
        }

        return $articleList;
    }
    
}


