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

指定用户下的所有文章api


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

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

route层:route\route.php

php
// 用户操作(绑定手机)
Route::group('api/:v1/',function(){
    ...
	// 获取指定用户下的所有文章(含隐私)
    Route::get('user/post/:page', 'api/v1.User/Allpost');
    ...
})->middleware(['ApiUserAuth','ApiUserBindPhone','ApiUserStatus']);

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

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

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

php
// 获取指定用户下所有文章
public function getAllPostList(){
    $params = request()->param();
    // 获取用户id
    $user_id=request()->userId;
    return $this->get($user_id)->post()->with([
        'user'=>function($query){
            return $query->field('id,username,userpic');
        },'images'=>function($query){
            return $query->field('url');
        },'share'])->page($params['page'],10)->select();
}