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

fe7e575ee99c

微信截图_20171122145820.png

fe7e575ee99c

微信截图_20171122153719.png

fe7e575ee99c

微信截图_20171122154723.png

fe7e575ee99c

image.png

route.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

/**

* 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

/**

* 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

/**

* 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;

}

fe7e575ee99c

image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值