UserController.php 3.7 KB
<?php

namespace App\Http\Controllers\Bside;

use App\Enums\Common\Code;
use App\Http\Requests\Bside\UserRequest;
use App\Models\User as UserModel;
use App\Rules\Ids;
use Illuminate\Http\Request;

class UserController extends BaseController
{
    /**
     * @name :name
     * @return void
     * @author :liyuhang
     * @method
     */
    public function lists(UserModel $userModel){
        //TODO::搜索参数处理
        $this->map['project_id'] = $this->user['project_id'];
        $lists = $userModel->lists($this->map,$this->page,$this->row,$this->order,['id','name','mobile','created_at']);
        if(empty($lists)){
            $this->response('error',Code::USER_ERROR,[]);
        }
        $this->response('success',Code::SUCCESS,$lists);
    }

    /**
     * @name :添加管理员
     * @return void
     * @author :liyuhang
     * @method
     */
    public function add(UserRequest $request,UserModel $userModel){
        $request->validated();
        $this->param['project_id'] = $this->user['project_id'];
        $rs = $userModel->adds($this->param);
        if($rs === false){
            $this->response('当前添加用户已存在或参数错误,添加失败',Code::USER_REGISTER_ERROE,[]);
        }
        $this->response('success',Code::SUCCESS);
    }

    /**
     * @name : 编辑管理员
     * @return void
     * @author :liyuhang
     * @method
     */
    public function edit(UserRequest $request,UserModel $userModel){
        $request->validate([
            'id'=>['required']
        ],[
            'id.required' => 'ID不能为空'
        ]);
        $info = $userModel->where('id','<>',$this->param['id'])
            ->where(['mobile'=>$this->param['mobile']])->first();
        if(!empty($info)){
            $this->response('当前编辑的手机号码已存在',Code::USER_PARAMS_ERROE);
        }
        $rs = $userModel->edits($this->param);
        if($rs === false){
            $this->response('参数错误或其他服务器原因,编辑失败',Code::USER_ERROR,[]);
        }
        $this->response('编辑成功',Code::SUCCESS,[]);
    }

    /**
     * @name :修改当前用户状态
     * @return void
     * @author :liyuhang
     * @method
     */
    public function status(Request $request,UserModel $userModel){
        $request->validate([
            'id'=>['required'],
            'status'=>['required'],
        ],[
            'id.required' => 'ID不能为空',
            'status.required' => 'status不能为空'
        ]);
        $rs = $userModel->edit($this->param,['id'=>$this->param['id']]);
        if($rs === false){
            $this->response('error',Code::USER_ERROR);
        }
        $this->response($this->param['status'] == 0 ? '启用成功' : '禁用成功');
    }

    /**
     * @name :详情
     * @return json
     * @author :liyuhang
     * @method
     */
    public function info(Request $request,UserModel $userModel){
        $request->validate([
            'id'=>['required', new Ids()],
        ],[
            'id.required' => 'ID不能为空',
        ]);
        $rs = $userModel->read($this->param);
        if($rs === false){
            $this->response('error',Code::USER_ERROR);
        }
        $this->response('success');
    }
    /**
     * @name :删除管理员
     * @return json
     * @author :liyuhang
     * @method
     */
    public function del(Request $request,UserModel $userModel){
        $request->validate([
            'id'=>['required', new Ids()],
        ],[
            'id.required' => 'ID不能为空',
        ]);
        $rs = $userModel->del($this->param);
        if($rs === false){
            $this->response('error',Code::USER_ERROR);
        }
        $this->response('success');
    }
}