Commit 9f2dea45 by 杨树贤

初始化项目

parents
<?php
namespace App\Http\Models\Cms;
use Illuminate\Database\Eloquent\Model;
class CmsBusinessConfigModel extends Model
{
protected $connection = 'cms';
protected $table = 't_business_config';
public $timestamps = false;
public static function getInfoByUrl($url)
{
$res = self::where('url', $url)->first();
return ($res) ? $res->toArray() : [];
}
public static function getInfoByTitle($title)
{
$res = self::where('title', $title)->first();
return ($res) ? $res->toArray() : [];
}
}
<?php
namespace App\Http\Models\Cms;
use Illuminate\Database\Eloquent\Model;
class CmsDepartmentModel extends Model
{
protected $table = 'department';
public $timestamps = false;
protected $primaryKey = 'departmentId';
//采购部门的总ID
const PURCHASE_DEPARTMENT_ID = 8;
//销售部门的总ID
const SALES_DEPARTMENT_ID = 7;
// 获取部门人员
public static function getUsersByDepartmentId($departmentId, $status = '', $filter = '')
{
$departmentIds = [];
self::getSubDepartmentId($departmentId, $departmentIds);
return CmsUserInfoModel::whereIn('department_id', $departmentIds)
->where(function ($query) use ($status) {
if ($status !== '') {
$query->where('status', '=', $status);
}
})
->where(function ($query) use ($filter) {
if ($filter) {
$query->whereRaw($filter);
}
})
->get()->toArray();
}
// 获取下级部门ID
public static function getSubDepartmentId($departmentId, &$departmentIds)
{
// 获取下级部门
$sub_department = CmsUserDepartmentModel::where('parent_id', $departmentId)->select([
'department_id',
'department_name'
])->get();
if ($sub_department) {
foreach ($sub_department as $v) {
self::getSubDepartmentId($v['department_id'], $departmentIds);
}
}
$departmentIds[] = $departmentId;
return $departmentIds;
}
}
<?php
namespace App\Http\Models\Cms;
use Illuminate\Database\Eloquent\Model;
class CmsRolePermModel extends Model
{
protected $connection = 'cms';
protected $table = 't_role_perm';
public $timestamps = false;
public static function getRolePermInfosByRoleIds($role_ids, $bid)
{
$res = self::whereIn("roleId", $role_ids)->where("bid", $bid)->get();
return ($res) ? $res->toArray() : [];
}
}
<?php
namespace App\Http\Models\Cms;
use Illuminate\Database\Eloquent\Model;
class CmsUserDepartmentModel extends Model
{
protected $connection = 'cms';
protected $table = 'user_department';
public $timestamps = false;
public static function getDepartmentIdsParrentIds($parrent_ids = [])
{
$res = self::whereIn('parent_id', $parrent_ids)->pluck('department_id');
return ($res) ? $res->toArray() : [];
}
public static function getDepartmentInfo($department_id)
{
$res = self::where('department_id', $department_id)->first();
return ($res) ? $res->toArray() : [];
}
public static function getDepartmentList($departmentName, $departmentIds)
{
$res = self::select('department_name')->where('department_name', 'like',
'%' . $departmentName . '%')->whereIn('department_id', $departmentIds)->get();
return ($res) ? $res->toArray() : [];
}
public static function getDepartmentListByName($departmentName)
{
$res = self::select('department_name')->distinct('department_name')->where('department_name', 'like',
"%{$departmentName}%")->orderByDesc('department_id')->pluck('department_name');
return ($res) ? $res->toArray() : [];
}
public static function getDepartmentId($department)
{
$res = self::select('department_id')->distinct('department_name')->where('department_name',
$department)->pluck('department_id');
return ($res) ? $res->toArray() : [];
}
// 获取部门人员
public static function getUserByDepartmentId($departmentId, $status = '', $filter = '')
{
$departmentIds = [];
self::getSubDepartmentId($departmentId, $departmentIds);
return CmsUserInfoModel::whereIn('department_id', $departmentIds)
->where(function ($query) use ($status) {
if ($status !== '') {
$query->where('status', '=', $status);
}
})
->where(function ($query) use ($filter) {
if ($filter) {
$query->whereRaw($filter);
}
})
->select('userId', 'name', 'status')
->get();
}
// 获取下级部门ID
public static function getSubDepartmentId($departmentId, &$departmentIds)
{
// 获取下级部门
$sub_department = self::where('parent_id', $departmentId)->select('department_id',
'department_name')->get();
if ($sub_department) {
foreach ($sub_department as $v) {
self::getSubDepartmentId($v['department_id'], $departmentIds);
}
}
$departmentIds[] = $departmentId;
return $departmentIds;
}
/*
* 获取采购员部门的上级部门,只需要往上找一层
* 比如:联营一组=》联营一部
*/
public static function getParentDepName($dep_id){
$info = self::select("parent_id")->where("department_id",intval($dep_id))->first();
if(!$info){
return [];
}
$parentInfo = null;
if($info->parent_id != 0){
$parentInfo = self::select("department_id","department_name")->where("department_id",$info->parent_id)->first();
}
return $parentInfo ? $parentInfo->toArray():[];
}
}
<?php
namespace App\Http\Models\Cms;
use Illuminate\Database\Eloquent\Model;
class CmsUserInfoModel extends Model
{
protected $connection = 'cms';
protected $table = 'user_info';
const GENDER_TYPE_MAN = 1; // 男性
const GENDER_TYPE_WOMAN = 2; // 女性
const STATUS_ENABLE = 0; // 状态-正常
public $timestamps = false;
//获取下属用户ids
public static function getInferiorUserIds($userId)
{
$departmentId = self::where('userId', $userId)->value('department_id');
$users = CmsUserDepartmentModel::getUserByDepartmentId($departmentId);
return array_column($users, 'userId');
}
public static function getSalesBuyerList($where, $department_ids, $page = 1, $limit = 10)
{
return self::whereIn('department_id', $department_ids)->where($where)->orderBy('userId',
'desc')->paginate($limit, ['*'], 'p', $page)->toArray();
}
public static function getSalesByPrefixName($name, $department_id)
{
CmsDepartmentModel::getSubDepartmentId($department_id, $departmentIds);
return self::selectRaw("userId as id,name")->whereIn('department_id', $departmentIds)->where("name", "like",
"%" . trim($name) . "%")->orderBy('userId',
'desc')->get()->toArray();
}
public static function getUserListByPrefixName($name)
{
return self::selectRaw("userId as id,name")->where("name", "like",
"%" . trim($name) . "%")->orderBy('userId',
'desc')->get()->toArray();
}
public static function getUserInfoById($user_id)
{
$res = self::where('userId', $user_id)->first();
return ($res) ? $res->toArray() : [];
}
public static function getUserInfoByName($name)
{
$res = self::where('name', $name)->first();
return ($res) ? $res->toArray() : [];
}
public static function updateByWhere($data, $where)
{
if (!isset($data['mtime'])) {
$data['mtime'] = date("Y-m-d H:i:s");
}
return self::where($where)->update($data);
}
public static function getUsers($user_ids)
{
$res = self::whereIn('userId', $user_ids)->get();
return ($res) ? $res->toArray() : [];
}
public static function getUserInfo($user_id)
{
$res = self::select('department_name', 'name',"department_id")->where('userId', $user_id)->first();
return ($res) ? $res->toArray() : [];
}
public function getUserName($userId)
{
$res = $this->select('name')->where('userId', $userId)->get()->toArray();
if (!isset($res[0])) {
return -1;
}
return $res[0]['name'];
}
public static function getUsersByPositionId($position_id)
{
$res = self::where('position_id', $position_id)->where('status', self::STATUS_ENABLE)->get();
return ($res) ? $res->toArray() : [];
}
public static function getUserIdsByDepartmentIds($department_ids = [])
{
$res = self::whereIn('department_id', $department_ids)->pluck('userId');
return ($res) ? $res->toArray() : [];
}
public static function getUsersByDepartmentIdAndPositionNames($department_id, $position_names)
{
$res = self::where('department_id', $department_id)->where('status', self::STATUS_ENABLE)->whereIn('position_name', $position_names)->get();
return ($res) ? $res->toArray() : [];
}
public static function getCreatorUserInfo($create_name)
{
$creator_user_info = [];
$userInfo = self::getUserInfoByName($create_name);
if ($userInfo) {
$creator_user_info = [
"uid" => $userInfo['userId'],
"name" => $userInfo['name']
];
}
return $creator_user_info;
}
public static function getDepartmentList($user_ids)
{
$res = self::selectRaw('department_name as department , userId as user_id')->whereIn('userId',
$user_ids)->get();
return ($res) ? $res->toArray() : [];
}
// 批量获取名称
public static function getNameByUserIds($userIds)
{
return self::whereIn('userId', $userIds)->pluck('name', 'userId')->toArray();
}
public static function getUserList($status){
return self::where("status",$status)->pluck("name","userId")->toArray();
}
}
<?php
namespace App\Http\Models\Cms;
use Illuminate\Database\Eloquent\Model;
class CmsUserPermModel extends Model
{
protected $connection = 'cms';
protected $table = 't_user_perm';
public $timestamps = false;
public static function getUserPermByUidAndBid($uid, $bid)
{
$res = self::where(["userId" => $uid, "bid" => $bid])->first();
return ($res) ? $res->toArray() : [];
}
}
<?php
namespace App\Http\Models\Cms;
use Illuminate\Database\Eloquent\Model;
class ConfigModel extends Model
{
protected $connection = 'cms';
protected $table = 'config';
public $timestamps = false;
public static function getMenusByName($name)
{
$res = self::where('config_title', $name)->first();
$menus = [];
if ($res){
$menus = (json_decode($res['config_data'])) ? json_decode($res['config_data'], true) : [];
}
return $menus;
}
}
<?php
namespace App\Http\Models\Cms;
use Illuminate\Database\Eloquent\Model;
class UserDepartmentModel extends Model
{
protected $connection = 'cms';
protected $table = 'user_department';
protected $primaryKey = 'userId';
public $timestamps = false;
}
\ No newline at end of file
<?php
namespace App\Http\Models\Cms;
use Illuminate\Database\Eloquent\Model;
class UserInfoModel extends Model
{
protected $connection = 'cms';
protected $table = 'user_info';
protected $primaryKey = 'userId';
public $timestamps = false;
}
\ No newline at end of file
<?php
namespace App\Http\Services;
use App\Http\Models\Cms\CmsBusinessConfigModel;
use App\Http\Models\Cms\ConfigModel;
class MenuService
{
// 获取菜单
public static function getMenu()
{
// 获取系统名称
$name = PermService::getBusinessConfigName();
$user_menu_list = [];
$user = request()->user;
$userId = $user->userId;
$email = $user->email;
$role_list = PermService::getUserRoles($userId, $email);
if (PermService::hasRole(PermService::ROLE_ADMIN, $role_list)) {
return ConfigModel::getMenusByName($name);
} else {
$user_perms = PermService::getUserPerms();
if ($user_perms) {
$menu_list = ConfigModel::getMenusByName($name);
if ($menu_list) {
$user_menu_list = self::getUserMenuList($menu_list, $user_perms);
}
}
}
return $user_menu_list;
}
// 获取用户菜单列表,会获取全部菜单,然后遍历判断用户是否有权限,如果没有权限,那么会删除
public static function getUserMenuList($menu_list, $user_perms)
{
foreach ($menu_list as $i => &$menu_item) {
// 如果有子菜单,那么循环遍历判断
if ($menu_item['childs']) {
foreach ($menu_item['childs'] as $j => $child) {
$perm_id = PermService::getPermId($child['href']);
if (!in_array($perm_id, $user_perms)) {
unset($menu_item['childs'][$j]);
}
}
if (empty($menu_item['childs'])) {
unset($menu_list[$i]);
}
} else {
// 如果只是一级菜单,判断用户是否有权限
$perm_id = PermService::getPermId($menu_item['href']);
if (!in_array($perm_id, $user_perms)) {
unset($menu_list[$i]);
}
}
}
return array_values($menu_list);//保证索引从0开始
}
}
<?php
namespace App\Http\Services;
use App\Http\Models\Cms\CmsBusinessConfigModel;
use App\Http\Models\Cms\CmsRolePermModel;
use App\Http\Models\Cms\CmsUserDepartmentModel;
use App\Http\Models\Cms\CmsUserInfoModel;
use App\Http\Models\Cms\CmsUserPermModel;
use Illuminate\Support\Facades\Log;
class PermService
{
const ROLE_ADMIN = 1; // 管理员角色
const ROLE_ONLINE_ALL = 2; // 线上查看下级
const ROLE_ONLINE_SELF = 3; // 线上查看自己
const ROLE_OPERATORS = 4; // 运营
const ROLE_OFFLINE_ALL = 5; // 线下查看下级
const ROLE_OFFLINE_SELF = 6; // 线下查看自己
const ROLE_COMPANY_MANAGER = 7; // 公司管理员
const ROLE_KA_MAMAGER_ALL = 8; // 大客户查看下级
const ROLE_KA_MAMAGER_SELF = 9; // 大客户查看自己
const ROLE_SUZHOU_ALL = 10; // 苏州查看下级
const ROLE_SUZHOU_SELF = 11; // 苏州查看自己
const ROLE_NULL = 0; // 未设置角色
private static $role_name_map = [
'管理员' => self::ROLE_ADMIN,
'线上查看下级' => self::ROLE_ONLINE_ALL,
'线上查看下级' => self::ROLE_ONLINE_SELF,
'运营' => self::ROLE_OPERATORS,
'线下查看下级' => self::ROLE_OFFLINE_ALL,
'线下查看自己' => self::ROLE_OFFLINE_SELF,
'公司管理员' => self::ROLE_COMPANY_MANAGER,
'大客户查看下级' => self::ROLE_KA_MAMAGER_ALL,
'大客户查看自己' => self::ROLE_KA_MAMAGER_SELF,
'苏州查看下级' => self::ROLE_SUZHOU_ALL,
'苏州查看自己' => self::ROLE_SUZHOU_SELF,
];
// 获取当前用户角色
public static function getUserRoles($uid = 0, $email = "")
{
$admin = request()->get("user");
$uid = $uid ? $uid : $admin->userId;
$email = $email ? $email : $admin->email;
// 如果是管理员邮箱,直接返回管理员角色
if ($email == 'admin@ichunt.com') {
return [self::ROLE_ADMIN];
}
// 根据域名查询系统业务ID
$business = self::getBusinessInfo();
if (!$business) {
return [];
}
$bid = $business['bid'];
// 权限系统配置的管理帐号, 如果是权限系统中配置的管理员邮箱,直接返回管理员角色
$adminAccount = json_decode($business['admin'], true);
if (in_array($email, $adminAccount)) {
return [self::ROLE_ADMIN];
}
// 根据用户ID和业务ID查看角色
$userPerm = CmsUserPermModel::getUserPermByUidAndBid($uid, $bid);
if (empty($userPerm)) {
return [];
}
// 没有选择角色
if ($userPerm['roles'] == 'null') {
return [];
}
$role_list = [];
$role_ids = json_decode($userPerm['roles'], true);
if ($role_ids) {
$role_perm_list = CmsRolePermModel::getRolePermInfosByRoleIds($role_ids, $bid);
if ($role_perm_list) {
$role_name_list = array_column($role_perm_list, 'name');
foreach ($role_name_list as $role_name) {
if (isset(self::$role_name_map[$role_name])) {
$role_list[] = self::$role_name_map[$role_name];
}
}
}
}
return $role_list;
}
// 判断用户是否拥有某角色
public static function hasRole($role, $role_list)
{
return in_array($role, $role_list);
}
// 获取用户所有权限
public static function getUserPerms()
{
$userAllPermList = [];
$business = self::getBusinessInfo();
//获取用户单独的权限
if (isset($business['bid'])){
$bid = $business['bid'];
$userPerm = CmsUserPermModel::getUserPermByUidAndBid(request()->user->userId, $bid);//获取用户权限信息
if ($userPerm && isset($userPerm['perms'])){
$userPermList = json_decode($userPerm['perms']);
if (is_array($userPermList)){
$userAllPermList = array_merge($userAllPermList,$userPermList);
}
}
}
//获取用户所在角色的权限
$rolePermList = [];
if (isset($userPerm['roles'])) {
$roleIds = json_decode($userPerm['roles'], true);//用户所有的角色
if ($roleIds) {
$rolePermData = CmsRolePermModel::getRolePermInfosByRoleIds($roleIds, $bid);
if ($rolePermData) {
foreach ($rolePermData as $param) {//获取用户的角色权限
$arrParam = json_decode($param['perms']);
$rolePermList = array_merge($rolePermList, $arrParam);
}
}
}
}
$userAllPermList = array_merge($userAllPermList,$rolePermList);
if (empty($userAllPermList)){
Log::error("该用户未配置权限,请联系管理员,uid:" . request()->user->userId);
}
return $userAllPermList;
}
// 获取permId
public static function getPermId($url)
{
$perm_id = '';
$url_info = parse_url($url);
if (isset($url_info['path'])) {
$path_arr = explode("/", $url_info['path']);
$path_arr = array_filter($path_arr);
$perm_id = implode("_", $path_arr);
}
return $perm_id;
}
// 判断用户是否拥有某权限
public static function hasPerm($perm_id)
{
$user_perms = self::getUserPerms();
return in_array($perm_id, $user_perms);
}
// 系统名称
public static function getBusinessConfigName()
{
return get_resource_config_section('app', 'cube')['APP_CONFIG_NAME'] ?? '魔方-重构';
}
// 获取系统信息
public static function getBusinessInfo()
{
$app_config_name = self::getBusinessConfigName();
return CmsBusinessConfigModel::getInfoByTitle($app_config_name);
}
// 获取指定用户下级所有人员
public static function getSubUserId($userId)
{
$sub_user_ids = [];
array_unshift($sub_user_ids, $userId); // 将当前用户添加到数组
$user_info = CmsUserInfoModel::getUserInfoById($userId);
if (!$user_info['department_id']) {
return $sub_user_ids;
}
// 获取所有下级部门
$department_ids = self::_getDepartmentIds($user_info['department_id']);
// 获取下级部门的人员
$sub_user_ids = CmsUserInfoModel::getUserIdsByDepartmentIds($department_ids);
return array_unique($sub_user_ids);
}
// 获取查询的部门id,查询销售和采购部门下所有子部门的ids
// 这里要使用循环的查询方法,如果改部门下面还有子部门,那么一并查询,最终合并用户子部门id集
public static function _getDepartmentIds($top_department_id)
{
$all_department_ids = $next_department_ids = [$top_department_id];
while ($next_department_ids) {
$next_department_ids = CmsUserDepartmentModel::getDepartmentIdsParrentIds($next_department_ids);
$all_department_ids = array_merge($all_department_ids, $next_department_ids);
}
return $all_department_ids;
}
}
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