美文网首页
[PHP高可用后端]③②--首页接口开发

[PHP高可用后端]③②--首页接口开发

作者: 子木同 | 来源:发表于2017-11-22 17:24 被阅读107次
    微信截图_20171122145820.png 微信截图_20171122153719.png 微信截图_20171122154723.png image.png

    route.php

    <?php
    
    use think\Route;
    
    Route::get('test', 'api/test/index');
    Route::put('test/:id', 'api/test/update');
    
    Route::resource('test', 'api/test');
    
    Route::get('api/:ver/cat',  'api/:ver.cat/read');
    
    Route::get('api/:ver/index','api/:ver.index/index');
    

    News.php

    <?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)
        {
            $condition['status'] = [
                'neq', config('code.status_delete')
            ];
            $order = ['id' => 'desc'];
    
            $result = $this->where($condition)
                ->limit($from, $size)
                ->order($order)
                ->select();
            return $result;
        }
    
        public function getNewsByCountCondition($condition = [])
        {
            $condition['status'] = [
                'neq', config('code.status_delete')];
            return $this->where($condition)
                ->count();
        }
    
    
        /**
         * 获取首页数据
         * @param int $num
         * @return false|\PDOStatement|string|\think\Collection
         */
        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();
        }
    
        /**
         * 通用化获取参数的数据字段
         */
        private function getListField()
        {
            return ['id',
                'catid',
                'image',
                'title',
                'read_count'];
        }
    
    }
    
        /**
         * 获取首页数据
         * @param int $num
         * @return false|\PDOStatement|string|\think\Collection
         */
        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();
        }
    
        /**
         * 通用化获取参数的数据字段
         */
        private function getListField()
        {
            return ['id',
                'catid',
                'image',
                'title',
                'read_count'];
        }
    
    }
    

    Common.php

    <?php
    /**
     * Created by PhpStorm.
     * User: tong
     * Date: 2017/11/21
     * Time: 14:26
     */
    
    namespace app\api\controller;
    
    use app\common\lib\exception\ApiException;
    use app\common\lib\IAuth;
    use app\common\lib\Time;
    use think\Cache;
    use think\Controller;
    
    class Common extends Controller
    {
        /**
         * @var string
         */
        public $headers = '';
    
    
        protected function _initialize()
        {
            $this->checkRequestAuth();
        }
    
        public function checkRequestAuth()
        {
            $headers = request()->header();
            // $this->testAes();
    
            //sign加密需要客户端工程师 解密 服务端工程师
            // 1 headers body 仿照sign 做参数的加解密
            // 2 加密数据步骤客户端服务端工程师共同约定
            // 3
    
    
            //基础检验
            if (empty($headers['sign'])) {
                throw new ApiException('sign不存在', 400);
            }
            if (!in_array($headers['app_type'], config('app.apptypes'))) {
                throw new ApiException('app_type不合法', 400);
            }
    
            if (!IAuth::checkSignPass($headers)) {
                throw new ApiException('授权码sign失败', 401);
            }
            Cache::set($headers['sign'], 1, config('app.app_sign_cache_time'));
    
            //1.文件缓存(单一服务器) 2.mysql(分布式) 3 redis(分布式 数据量大)
            $this->headers = $headers;
    
    
        }
    
        public function testAes()
        {
            $data = [
                'did' => '12345dg',
                'version' => 1,
                'time' => Time::get13TimeStamp(),
            ];
    
            //col9j6cqegAKiiey3IrXWi5kTf508OkMmPu9zrTihQ8IVnKDb7Rin03dOqY2qLWP
            echo IAuth::setSign($data);
            exit;
            //exit;
        }
    
        /**
         * 获取处理的新闻数据
         * @param array $news
         * @return array
         */
        protected function getDealNews($news = [])
        {
            if (empty($news)) {
                return [];
            }
            $cats = config('cat.list');
    
            foreach ($news as $key => $new) {
                $news[$key]['catname'] = $cats[$new['catid']] ? $cats[$new['catid']] : '-';
            }
            return $news;
    
        }
    }
    

    Index.php

    <?php
    /**
     * Created by PhpStorm.
     * User: tong
     * Date: 2017/11/22
     * Time: 15:54
     */
    
    namespace app\api\controller\v1;
    
    use app\api\controller\Common;
    
    class Index extends Common
    {
    
        /**
         * 获取首页接口
         * 1.头图 4-6
         * 2.推荐位列表 默认40条
         */
        public function index()
        {
            $heads = model('News')->getIndexHadNormalNews();
            $heads = $this->getDealNews($heads);
    
            $positions = model('News')->getPositionNormalNews();
            $positions = $this->getDealNews($positions);
    
            $result = [
                'heads' => $heads,
                'positions' => $positions,
            ];
    
            return show(config('code.success'), 'OK', $result, 200);
    
        }
    }
    
    /**
      * 获取处理的新闻数据
      * @param array $news
      * @return array
      */
     protected function getDealNews($news = [])
     {
       if (empty($news)) {
           return [];
       }
       $cats = config('cat.list');
       foreach ($news as $key => $new) {
           $news[$key]['catname'] = $cats[$new['catid']] ? $cats[$new['catid']] : '-';
       }
       return $news;
    }
    
    image.png

    相关文章

      网友评论

          本文标题:[PHP高可用后端]③②--首页接口开发

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