ProjectRoleLogic.php 3.9 KB
<?php

namespace App\Http\Logic\Aside\User;

use App\Helper\Common;
use App\Http\Logic\Aside\BaseLogic;
use App\Models\Manage\Manage;
use App\Models\Project\Project;
use App\Models\User\ProjectMenu as ProjectMenuModel;
use App\Models\User\ProjectRole;
use App\Models\User\User as UserModel;

class ProjectRoleLogic extends BaseLogic
{
    const STATUS_ONE = 0;

    public function __construct()
    {
        parent::__construct();
        $this->model = new ProjectRole();
        $this->param = $this->requestAll;
    }

    /**
     * @name :菜单列表
     * @return void
     * @author :liyuhang
     * @method
     */
    public function role_get_menu(){
        $menuModel = new ProjectMenuModel();
        $this->param['status'] = $this::STATUS_ONE;
        $lists = $menuModel->list($this->param,'created_at');
        $menu = array();
        if(!empty($lists)){
            foreach ($lists as $v){
                $v = (array)$v;
                if ($v['pid'] == 0) {
                    $v['sub'] = _get_child($v['id'], $lists);
                    $menu[]   = $v;
                }
            }
        }
        return $this->success($menu);
    }
    /**
     * @name :详情
     * @return void
     * @author :liyuhang
     * @method
     */
    public function role_info(){
        $info = Common::get_user_cache($this->model,$this->param['id'],'A');
        if(empty($info)){
            $filed = ['id','name','project_id','operator_id','created_at','status','updated_at'];
            $info = $this->model->read($this->param,$filed);
            if($info === false){
                $this->fail('当前数据不存在');
            }
            $userModel = new UserModel();
            $info['operator_name'] = $userModel->read(['id'=>$info['operator_id']],['name'])['name'];
            $info['project_name'] = (new Project())->read(['id'=>$info['project_id']],['title'])['title'];
            Common::set_user_cache($info,$this->model,$this->param['id'],'A');
        }
        return $this->success($info);
    }

    /**
     * @name :添加角色
     * @return void
     * @author :liyuhang
     * @method
     */
    public function role_add(){
        //查询当前名称是否存在
        $info = $this->model->read([
            'name'=>$this->param['name'],
            'project_id'=>$this->param['project_id']]);
        if($info !== false){
            $this->fail('当前角色名称已存在');
        }
        $rs = $this->model->add($this->param);
        if($rs === false){
            $this->fail('添加失败');
        }
        return $this->success();
    }

    /**
     * @remark :便捷
     * @name   :role_edit
     * @author :lyh
     * @method :post
     * @time   :2023/8/29 16:30
     */
    public function role_edit(){
        //查询当前名称是否存在
        $info = $this->model->read([
                'name'=>$this->param['name'],
                'id'=>['!=',$this->param['id']],
                'project_id'=>$this->param['project_id']]);
        if($info !== false){
            $this->fail('当前菜单名称已存在');
        }
        $rs = $this->edit($this->param,['id'=>$this->param['id']]);
        if($rs === false){
            $this->fail('error');
        }
        Common::del_user_cache($this->model,$this->param['id'],'A');
        return $this->success();
    }

    /**
     * @name :删除角色
     * @return void
     * @author :liyuhang
     * @method
     */
    public function role_del(){
        //查询当前角色下是否有用户
        $userModel = new UserModel();
        $ids = $this->param['id'];
        foreach ($ids as $v){
            $user_info = $userModel->read(['role_id'=>$v]);
            if($user_info !== false){
                $this->fail('id为{'.$v.'}'.'的角色拥有用户'.$user_info['name'].',不允许删除');
            }
        }
        $this->param['id'] = ['in',$this->param['id']];
        $this->del($this->param,$ids);
        return $this->success();
    }
}