RoleLogic.php 4.1 KB
<?php

namespace App\Http\Logic\Bside\User;

use App\Enums\Common\Code;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\User\ProjectMenu as ProjectMenuModel;
use App\Models\User\ProjectRole as ProjectRoleModel;
use App\Models\User\User as UserModel;

class RoleLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new ProjectRoleModel();
        $this->param = $this->requestAll;
    }

    /**
     * @remark :获取菜单
     * @name   :role_get_menu
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:40
     */
    public function role_get_menu(){
        $menuModel = new ProjectMenuModel();
        //根据当前登录用户角色返回用户菜单列表
        if($this->user['role_id'] != 0){
            $info = $this->model->read(['id'=>$this->user['role_id']]);
            $info['role_menu'] = trim($info['role_menu'],',');
            $lists = $menuModel->where(['status'=>0])->whereIn('id',explode(',',$info['role_menu']))->get()->toArray();
        }else{
            $lists = $menuModel->where(['status'=>0])->get()->toArray();
        }
        $menu = array();
        foreach ($lists as $v){
            $v = (array)$v;
            if ($v['pid'] == 0) {
                $v['sub'] = _get_child($v['id'], $lists);
                $menu[]   = $v;
            }
        }
        return $this->success($menu);
    }

    /**
     * @remark :获取详情
     * @name   :role_info
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:39
     */
    public function role_info(){
        $info = $this->info($this->param);
        return $this->success($info);
    }

    /**
     * @remark :添加角色
     * @name   :role_add
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:38
     */
    public function role_add(){
        $condition = [
            'name'=>$this->param['name'],
            'project_id'=>$this->user['project_id'],
        ];
        $info = $this->model->read($condition);
        if($info !== false){
            $this->fail('当前添加的角色已存在');
        }
        $this->param['create_id'] = $this->user['id'];
        $this->param['operator_id'] = $this->user['id'];
        $this->param['project_id'] = $this->user['project_id'];
        $rs = $this->model->add($this->param);
        if($rs === false){
            $this->fail('添加失败');
        }
        return $this->success();
    }

    /**
     * @remark :编辑角色
     * @name   :role_edit
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:38
     */
    public function role_edit(){
        //TODO::查询当前名称是否重复
        $condition = [
            'id'=>['!=',$this->param['id']],
            'name'=>$this->param['name'],
            'project_id'=>$this->user['project_id'],
        ];
        $info = $this->model->read($condition);
        if($info !== false){
            $this->fail('当前添加的角色已存在');
        }
        $this->param['operator_id'] = $this->user['id'];
        $this->edit($this->param,['id'=>$this->param['id']]);
        return $this->success();
    }

    /**
     * @remark :编辑角色状态
     * @name   :role_status
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:38
     */
    public function role_status(){
        $this->edit($this->param,['id'=>$this->param['id']]);
        return $this->success();
    }

    /**
     * @remark :删除角色
     * @name   :role_del
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:38
     */
    public function role_del(){
        //查询当前角色下是否有用户
        $userModel = new UserModel();
        //批量删除
        foreach ($this->param['id'] as $v){
            $user_info = $userModel->read(['role_id'=>$v]);
            if(!empty($user_info)){
                $this->fail('当前角色下有用户存在,不允许删除',Code::USER_ERROR);
            }
        }
        $ids = $this->param['id'];
        $this->param['id'] = ['in',$this->param['id']];
        $this->del($this->param,$ids);
        return $this->success();
    }

}