Skip to content
关注公众号,获取新课通知

会员列表


控制器 app/controller/admin/User.php

php
public function index()
   {
   	$param = request()->param();
       $limit = intval(getValByKey('limit',$param,10));
       $keyword = getValByKey('keyword',$param,'');
       $user_level_id = getValByKey('user_level_id',$param,0);
       $method = $this->M->filterLoginMethod($keyword);
       $where = [
       	[ $method,'like','%'.$keyword.'%' ]
       ];
       
       if($user_level_id != 0){
       	$where[] = ['user_level_id','=',$user_level_id];
       }
       
       $totalCount = $this->M->where($where)->count();
       $list = $this->M->page($param['page'],$limit)
       		->where($where)
	        ->with(['userLevel'])
	        ->order([ 'id'=>'desc' ])
			->select()
			->hidden(['password']);
	$user_level = \app\model\UserLevel::field(['id','name'])->select();
       return showSuccess([
       	'list'=>$list,
       	'totalCount'=>$totalCount,
       	'user_level'=>$user_level
       ]);
   }

模型 app/model/User.php

php
// 获取当前用户等级
   public function userLevel(){
       return $this->belongsTo('UserLevel');
   }

// 验证用户名是什么格式,.../邮箱/手机号
   public function filterLoginMethod($data){
       // 验证是否是手机号码
       if(preg_match('^1(3|4|5|7|8)[0-9]\d{8}$^', $data)){
           return 'phone';
       }
       
       if (preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/', $data)) {
       	// 验证是否是邮箱
       	return 'email';
       }
       
       return 'username';
   }

验证器 app/validate/admin/User.php

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

路由 router/admin.php

php
Route::get('user/:page','admin.User/index');