美文网首页
[PHP高可用后端]③⑤--文章排行接口开发

[PHP高可用后端]③⑤--文章排行接口开发

作者: 子木同 | 来源:发表于2017-11-24 11:18 被阅读18次
微信截图_20171124102504.png

route.php

<?php

use think\Route;

Route::get('test', 'api/test/index');
Route::put('test/:id', 'api/test/update');
Route::delete('test/:id', 'api/test/delete');

Route::resource('test', 'api/test');

Route::get('api/:ver/cat', 'api/:ver.cat/read');

Route::get('api/:ver/index', 'api/:ver.index/index');

Route::resource('api/:ver/news', 'api/:ver.news');

Route::get('api/:ver/rank','api/:ver.rank/index');

News.php(Model)

<?php
/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/20
 * Time: 16:34
 */

namespace app\common\model;

class News extends Base
{
    public function getNews($data = [])
    {
        $data['status'] = [
            'neq', config('code.status_delete'),
        ];
        $order = ['id' => 'desc'];

        $result = $this->where($data)
            ->order($order)
            ->paginate();
        return $result;
    }

    public function getNewsByCondition($condition = [], $from, $size = 5)
    {
        if (!isset($condition['status'])) {
            $condition['status'] = [
                'neq', config('code.status_delete')
            ];
        }
        $order = ['id' => 'desc'];

        $result = $this->where($condition)
            ->field($this->getListField())
            ->limit($from, $size)
            ->order($order)
            ->select();
        //echo $this->getLastSql();exit();
        return $result;
    }

    public function getNewsByCountCondition($condition = [])
    {
        if (!isset($condition['status'])) {
            $condition['status'] = [
                'neq', config('code.status_delete')];
        }
        return $this->where($condition)
            ->count();
    }

    public function getIndexHadNormalNews($num = 4)
    {
        $data = [
            'status' => 1,
            'is_head_figure' => 1,
        ];
        $order = [
            'id' => 'desc',
        ];
        return $this->where($data)
            ->field($this->getListField())
            ->order($order)
            ->limit($num)
            ->select();
    }

    public function getPositionNormalNews($num = 20)
    {
        $data = [
            'status' => 1,
            'is_position' => 1,
        ];
        $order = [
            'id' => 'desc',
        ];
        return $this->where($data)
            ->field($this->getListField())
            ->order($order)
            ->limit($num)
            ->select();
    }

    /**
     * 获取排行版数据
     * @param int $num
     */
    public function getRankNormalNews($num = 5)
    {
        $data = [
            'status' => 1,
        ];
        $order = [
            'read_count' => 'desc',
        ];
        return $this->where($data)
            ->field($this->getListField())
            ->order($order)
            ->limit($num)
            ->select();
    }


    private function getListField()
    {
        return [
            'id',
            'catid',
            'image',
            'title',
            'read_count',
            'status',
            'is_position',
            'update_time',
            'create_time',
        ];
    }
}
public function getRankNormalNews($num = 5)
    {
        $data = [
            'status' => 1,
        ];
        $order = [
            'read_count' => 'desc',
        ];
        return $this->where($data)
            ->field($this->getListField())
            ->order($order)
            ->limit($num)
            ->select();
    }

Rank.php(Controller)

<?php
/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/24
 * Time: 10:29
 */

namespace app\api\controller\v1;


use app\api\controller\Common;
use app\common\lib\exception\ApiException;

class Rank extends Common
{
    /*
     * 获取排行版数据列表
     * 1.获取数据库 read_count排序 5-10
     * 2.优化redis
     */
    public function index()
    {
        try {
            $rands = model('News')->getRankNormalNews();
            $rands = $this->getDealNews($rands);
        } catch (\Exception $e) {
            return new ApiException('error', 400);
        }
        return show(config('code.success'), 'OK', $rands, 200);
    }
}
image.png

相关文章

网友评论

      本文标题:[PHP高可用后端]③⑤--文章排行接口开发

      本文链接:https://www.haomeiwen.com/subject/qaexbxtx.html