Manager.php 5.6 KB
<?php

namespace app\api\controller;

use app\admin\controller\Base;
use app\admin\model\Manager as ManagerModel;
use app\admin\model\Menu as MenuModel;
use app\admin\model\Role as RoleModel;
use think\Cache;
use think\Validate;

/**
 * @name:管理员管理
*/
class Manager extends Base
{
    /**
     * @name:获取管理员列表
     * @method:get
     * @param /p:页数 row:条数
    */
    public function lists(){
        $managerModel = new ManagerModel();
        $lists = $managerModel->lists($this->param,$this->p,$this->row,'sort desc');
        $this->allCount = $managerModel->allCount;
        $this->result($lists);
    }

    /**
     * @name:添加管理员
     * @method:post
     * @param:
    */
    public function add(){
        /*参数验证*/
        $rules = [
            ['username','require|max:25','名称必须|名称最多不能超过25个字符'],
            ['password','password|between:6,25','密码必须|年龄必须在6~26之间'],
            ['re_password','require||confirm:password','密码必须|两次密码不一致'],
        ];
        $validate = new Validate($rules);
        $res = $validate->check($this->param);
        if(!$res){
            $this->response($validate->getError(),400,[]);
        }
        $managerModel = new ManagerModel();
        $res = $managerModel->add($this->param);
        if($res === false){
            $this->response('添加失败',202,[]);
        }
        $this->response('添加成功',200);
    }

    /**
     * @name : 编辑管理员
     * @return void
     * @author :liyuhang
     * @method
     */

    public function edit(){
        $rules = [
            ['id','require','必须'],
        ];
        if(isset($this->param['password']) && !empty($this->param['password'])){
            array_push($rules,['password','password|between:6,25','密码必须|年龄必须在6~26之间']);
            array_push($rules,['re_password','require||confirm:password','密码必须|两次密码不一致']);
        }
        $validate = new Validate($rules);
        $res = $validate->check($this->param);
        if(!$res){
            $this->response($validate->getError(),400,[]);
        }
        $managerModel = new ManagerModel();
        if(isset($this->param['password'])){
            $this->param['password'] = think_center_md5($this->param['password']);
        }
        $res = $managerModel->edit($this->param,['id'=>$this->param['id']]);
        if($res === false){
            $this->response('编辑失败',202,[]);
        }
        $this->response('编辑成功',200);
    }

    /**
     * @name :修改当前管理员状态
     * @return void
     * @author :liyuhang
     * @method
     */
    public function status()
    {
        /*参数验证*/
        $rules = [
            ['id','require','必须'],
            ['status','require','必须'],
        ];
        $validate = new Validate($rules);
        $res = $validate->check($this->param);
        if(!$res){
            $this->response($validate->getError(),202,[]);
        }
        $managerModel = new ManagerModel();
        $info = $managerModel->read(['id'=>$this->param['id']]);
        if(empty($info)){
            $this->response('当前管理员不存在','');
        }
        $res = $managerModel->edit(['status'=>$this->param['status']],['id'=>$this->param['id']]);
        if ($res === false) {
            $this->response('修改状态失败',202,[]);
        } else {
            $this->response($this->param['status'] == 1 ? '启用成功': '禁用成功',200,[]);
        }
    }

    /**
     * @name:排序
     * @return void
     * @author :liyuhang
     * @method
     */
    public function sort()
    {
        /*参数验证*/
        $rules = [
            ['id','require','必须'],
            ['sort','require','必须'],
        ];
        $validate = new Validate($rules);
        $res = $validate->check($this->param);
        if(!$res){
            $this->response($validate->getError(),202,[]);
        }
        $res = ManagerModel::where('id', $this->param['id'])->update(['sort' => $this->param['sort']]);
        if ($res === false) {
            $this->response('排序失败',202,[]);
        } else {
            $this->response('成功',200,[]);;
        }
    }

    /**
     * @name
     * @return void
     * @author :liyuhang
     * @method
     */
    public function delete(){
        $rules = [
            ['id','require','必须'],
        ];
        $validate = new Validate($rules);
        $res = $validate->check($this->param);
        if(!$res){
            $this->response($validate->getError(),202,[]);
        }
        $managerModel = new ManagerModel();
        $res = $managerModel->edit(['deleted'=>0],['id'=>$this->param['id']]);
        if ($res === false) {
            $this->response('排序失败',202,[]);
        } else {
            $this->response('成功',200,[]);;
        }
    }

    /**
     * @name :获取当前用户菜单列表
     * @return void
     * @author :liyuhang
     * @method : get
     */
    public function get_menu(){
        //获取当前用户角色
        $roleModel = new RoleModel();
        $role_info = $roleModel->read(['id'=>$this->user['role_id']]);
        //根据当前角色获取当前菜单栏
        $menuModel = new MenuModel();
        $menu_lists = $menuModel->where(['pid'=>0,'status'=>1,'deleted'=>1])->where('id','in',$role_info['menu'])->select()->toArray();
        foreach ($menu_lists as $k =>  $v){
            $menu_lists[$k]['son'] = $menuModel->where(['pid'=>$v['id'],'status'=>1,'deleted'=>1])->where('id','in',$role_info['menu'])->select()->toArray();
        }
        $this->response('当前用户菜单列表',200,$menu_lists);
    }
}