On this page
粉丝列表api
controller层:application\api\controller\v1\User.php
php
// 粉丝列表
public function fens(){
(new UserValidate())->goCheck('getfens');
$list = (new UserModel())->getFensList();
return self::showResCode('获取成功',['list'=>$list]);
}
route层:route\route.php
php
// 用户操作(绑定手机)
Route::group('api/:v1/',function(){
// 粉丝列表
Route::get('fens/:page','api/v1.User/fens');
})->middleware(['ApiUserAuth','ApiUserBindPhone','ApiUserStatus']);
validate层:application\common\validate\UserValidate.php
php
protected $scene = [
'getfens'=>['page'],
];
model层:application\common\model\User.php
php
// 关联粉丝列表
public function fens(){
return $this->belongsToMany('User','Follow','user_id','follow_id');
}
// 获取当前用户粉丝列表
public function getFensList(){
// 获取所有参数
$params = request()->param();
// 获取用户id
$userid = request()->userId;
$fens = $this->get($userid)->fens()->page($params['page'],10)->select()->toArray();
return $this->filterReturn($fens);
}
// 关注和粉丝返回字段
public function filterReturn($param = []){
$arr = [];
$length = count($param);
for ($i=0; $i < $length; $i++) {
$arr[] = [
'id'=>$param[$i]['id'],
'username'=>$param[$i]['username'],
'userpic'=>$param[$i]['userpic'],
];
}
return $arr;
}