Commit 539ef76d by 宁成龙

修复员工权限

parent 64f62566
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace App\Admin\Forms\User; namespace App\Admin\Forms\User;
use App\Admin\Service\PermService;
use App\Admin\Service\UserService; use App\Admin\Service\UserService;
use App\Models\Cms\CmsUser; use App\Models\Cms\CmsUser;
use App\Models\Inquiry; use App\Models\Inquiry;
...@@ -32,10 +33,41 @@ class AssignUserHandle extends Form implements LazyRenderable ...@@ -32,10 +33,41 @@ class AssignUserHandle extends Form implements LazyRenderable
->refresh(); ->refresh();
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
// var_dump((string)$throwable); // var_dump((string)$throwable);
return $this->response()->error(trans('admin.update_failed').": {$throwable->getMessage()}"); return $this->response()->error(trans('admin.update_failed') . ": {$throwable->getMessage()}");
} }
} }
public function getCmsUserList()
{
$where = [];
$where[] = [
function ($query) {
$adminUser = "userId";
$nowSaleId = getAdminUserId();
$role = PermService::getUserRoles($nowSaleId);
if ($nowSaleId == 1000) {
return $query;
}
if ($role[0] == PermService::ROLE_SALE_DIRECTOR) {//销售总监查看同部门
$userIds = PermService::getSubUserId($nowSaleId);
if (!empty($userIds)) {
return $query->whereIn($adminUser, $userIds);
}
} elseif ($role[0] == PermService::ROLE_SALE_LEADER) {//销售经理查看同组
//查看同组
$user_info = CmsUser::getInfoByUserId($nowSaleId);
if (empty($user_info) || !$user_info['department_id']) {
return $query->where($adminUser, $nowSaleId);
}
// 获取同组部门的人员
$sub_user_ids = CmsUser::getUserIdsByDepartmentIds([$user_info['department_id']]);
return $query->whereIn($adminUser, $sub_user_ids);
}
}
];
return CmsUser::where($where)->pluck('name', 'userId')->toArray();
}
/** /**
* Build a form here. * Build a form here.
*/ */
...@@ -43,7 +75,7 @@ class AssignUserHandle extends Form implements LazyRenderable ...@@ -43,7 +75,7 @@ class AssignUserHandle extends Form implements LazyRenderable
{ {
$this->disableResetButton(); $this->disableResetButton();
$this->hidden('id')->attribute('id', 'id'); $this->hidden('id')->attribute('id', 'id');
$this->select('sale_id')->options(CmsUser::getUserList())->default(0)->required(); $this->select('sale_id')->options($this->getCmsUserList())->default(0)->required();
} }
/** /**
......
...@@ -39,37 +39,31 @@ class TransferUserHandle extends Form implements LazyRenderable ...@@ -39,37 +39,31 @@ class TransferUserHandle extends Form implements LazyRenderable
public function getCmsUserList() public function getCmsUserList()
{ {
CmsUser::getUserList();
$where = []; $where = [];
$where[] = [ $where[] = [
function ($query) { function ($query) {
$viewList = User::$ruleViewList;
$adminUser = "userId"; $adminUser = "userId";
$nowSaleId = getAdminUserId(); $nowSaleId = getAdminUserId();
if ($nowSaleId == 1000) { $role = PermService::getUserRoles($nowSaleId);
if ($nowSaleId == 1000 || $role[0] == PermService::ROLE_SALE_DIRECTOR) {
return $query; return $query;
} }
//查看所有 if ($role[0] == PermService::ROLE_SALE_LEADER) {//销售经理可以查看同部门
if (checkPerm($viewList[0])) {
return $query;
}
//查看下级
if (checkPerm($viewList[1])) { //查看下级的权限//获取用户部门下的所有用户
$userIds = PermService::getSubUserId($nowSaleId); $userIds = PermService::getSubUserId($nowSaleId);
if (!empty($userIds)) { if (!empty($userIds)) {
return $query->whereIn($adminUser, $userIds); return $query->whereIn($adminUser, $userIds);
} }
} } elseif ($role[0] == PermService::ROLE_SALE) {//销售可以查看同组
//查看同组 //查看同组
$user_info = CmsUser::getInfoByUserId($nowSaleId); $user_info = CmsUser::getInfoByUserId($nowSaleId);
if (empty($user_info) || !$user_info['department_id']) { if (empty($user_info) || !$user_info['department_id']) {
return $query->where($adminUser, $nowSaleId); return $query->where($adminUser, $nowSaleId);
} }
// 获取下级部门的人员 // 获取同组部门的人员
$sub_user_ids = CmsUser::getUserIdsByDepartmentIds([$user_info['department_id']]); $sub_user_ids = CmsUser::getUserIdsByDepartmentIds([$user_info['department_id']]);
return $query->whereIn($adminUser, $sub_user_ids); return $query->whereIn($adminUser, $sub_user_ids);
} }
}
]; ];
return CmsUser::where($where)->pluck('name', 'userId')->toArray(); return CmsUser::where($where)->pluck('name', 'userId')->toArray();
} }
......
...@@ -22,22 +22,14 @@ class PermService ...@@ -22,22 +22,14 @@ class PermService
const ROLE_ADMIN = 1; // 管理员角色 const ROLE_ADMIN = 1; // 管理员角色
const ROLE_BUYER = 2; // 采购员 const ROLE_SALE = 200; // 普通销售
const ROLE_BUYER_LEADER = 3; // 采购经理 const ROLE_SALE_LEADER = 201; // 销售经理
const ROLE_BUYER_DIRECTOR = 4; // 采购总监 const ROLE_SALE_DIRECTOR = 202; // 销售总监
const ROLE_NULL = 0; // 未设置角色 const ROLE_NULL = 0; // 未设置角色
private static $role_name_map = [
"管理员" => self::ROLE_ADMIN,
"采购员" => self::ROLE_BUYER,
"采购经理" => self::ROLE_BUYER_LEADER,
"采购总监" => self::ROLE_BUYER_DIRECTOR
];
// 获取当前用户角色 // 获取当前用户角色
public static function getUserRoles($uid = 0, $email = "") public static function getUserRoles($uid = 0, $email = "")
{ {
......
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
namespace App\Models\Cms; namespace App\Models\Cms;
use App\Admin\Service\PermService;
use App\Models\BaseModel; use App\Models\BaseModel;
use App\Models\User;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class CmsUser extends BaseModel class CmsUser extends BaseModel
...@@ -11,6 +13,14 @@ class CmsUser extends BaseModel ...@@ -11,6 +13,14 @@ class CmsUser extends BaseModel
protected $table = 'user_info'; protected $table = 'user_info';
public $timestamps = false; public $timestamps = false;
//查看权限
public static $ruleViewList = [
self::CMS_USER_VIEW_ALL, //查看所有
self::CMS_USER_VIEW_SUB,//查看下级
];
const CMS_USER_VIEW_ALL = "cms_user_viewAllList";//查看所有
const CMS_USER_VIEW_SUB = "cms_user_viewSubList";//查看下级
public static function getInfoByUserId($userId) public static function getInfoByUserId($userId)
{ {
$res = self::where('userId', $userId)->first(); $res = self::where('userId', $userId)->first();
...@@ -19,7 +29,7 @@ class CmsUser extends BaseModel ...@@ -19,7 +29,7 @@ class CmsUser extends BaseModel
public static function getUserList() public static function getUserList()
{ {
return CmsUser::where([])->rule(\App\Models\User::$ruleViewList, 'userId')->pluck('name', 'userId')->toArray(); return CmsUser::where([])->rule(self::$ruleViewList, 'userId')->pluck('name', 'userId')->toArray();
} }
//根据部门id获取列表 //根据部门id获取列表
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment