Skip to content
关注公众号,获取新课通知
【重要通知】uniapp实战社区交友交流群更换为:602180461,靓仔/靓女可以重新申请加入哦~

指定用户下的文章api


controller层:application\api\controller\v1\User.php

php
// 用户发布文章列表
public function post(){
    (new UserValidate())->goCheck('post'); 
    $list = (new UserModel())->getPostList();
    return self::showResCode('获取成功',['list'=>$list]);
}

route层:route\route.php

php
// 不需要验证token
Route::group('api/:version/',function(){
    ...
	// 获取指定用户下的文章
    Route::get('user/:id/post/:page', 'api/v1.User/post');
    ...
});

validate层:application\common\validate\UserValidate.php

php
protected $rule=[
	'id'=>'require|integer|>:0',
    'page'=>'require|integer|>:0',
];

protected $scene=[
	'post'=>['id','page']
];

model层:application\common\model\User.php

php
 // 关联文章
public function post(){
    return $this->hasMany('Post');
}
// 获取指定用户下文章
public function getPostList(){
    $params = request()->param();
    $user = $this->get($params['id']);
    if (!$user) TApiException('该用户不存在',10000);
    return $user->post()->with([
            'user'=>function($query){
                return $query->field('id,username,userpic');
            },'images'=>function($query){
                return $query->field('url');
            },'share'])->where('isopen',1)->page($params['page'],10)->select();
}