first commit
This commit is contained in:
411
app/adminapi/logic/auth/AdminLogic.php
Normal file
411
app/adminapi/logic/auth/AdminLogic.php
Normal file
@@ -0,0 +1,411 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\adminapi\logic\auth;
|
||||
|
||||
use app\common\cache\AdminAuthCache;
|
||||
use app\common\enum\YesNoEnum;
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\model\auth\Admin;
|
||||
use app\common\model\auth\AdminDept;
|
||||
use app\common\model\auth\AdminJobs;
|
||||
use app\common\model\auth\AdminRole;
|
||||
use app\common\model\auth\AdminSession;
|
||||
use app\common\model\user\User;
|
||||
use app\common\cache\AdminTokenCache;
|
||||
use app\common\service\{FileService,UtilsService,ConfigService};
|
||||
use think\facade\Config;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 管理员逻辑
|
||||
* Class AdminLogic
|
||||
* @package app\adminapi\logic\auth
|
||||
*/
|
||||
class AdminLogic extends BaseLogic
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 添加管理员
|
||||
* @param array $params
|
||||
* @author 段誉
|
||||
* @date 2021/12/29 10:23
|
||||
*/
|
||||
public static function add(array $params)
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
$passwordSalt = Config::get('project.unique_identification');
|
||||
$password = create_password($params['password'], $passwordSalt);
|
||||
$defaultAvatar = config('project.default_image.admin_avatar');
|
||||
$avatar = !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : $defaultAvatar;
|
||||
|
||||
$admin = Admin::create([
|
||||
'name' => $params['name'],
|
||||
'account' => $params['account'],
|
||||
'avatar' => $avatar,
|
||||
'password' => $password,
|
||||
'create_time' => time(),
|
||||
'disable' => $params['disable'],
|
||||
'multipoint_login' => $params['multipoint_login'],
|
||||
]);
|
||||
|
||||
// 角色
|
||||
self::insertRole($admin['id'], $params['role_id'] ?? []);
|
||||
// 部门
|
||||
self::insertDept($admin['id'], $params['dept_id'] ?? []);
|
||||
// 岗位
|
||||
self::insertJobs($admin['id'], $params['jobs_id'] ?? []);
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 编辑管理员
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2021/12/29 10:43
|
||||
*/
|
||||
public static function edit(array $params): bool
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 基础信息
|
||||
$data = [
|
||||
'id' => $params['id'],
|
||||
'name' => $params['name'],
|
||||
'account' => $params['account'],
|
||||
'disable' => $params['disable'],
|
||||
'multipoint_login' => $params['multipoint_login']
|
||||
];
|
||||
|
||||
// 头像
|
||||
$data['avatar'] = !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : '';
|
||||
|
||||
// 密码
|
||||
if (!empty($params['password'])) {
|
||||
$passwordSalt = Config::get('project.unique_identification');
|
||||
$data['password'] = create_password($params['password'], $passwordSalt);
|
||||
}
|
||||
|
||||
// 禁用或更换角色后.设置token过期
|
||||
$roleId = AdminRole::where('admin_id', $params['id'])->column('role_id');
|
||||
$editRole = false;
|
||||
if (!empty(array_diff_assoc($roleId, $params['role_id']))) {
|
||||
$editRole = true;
|
||||
}
|
||||
|
||||
if ($params['disable'] == 1 || $editRole) {
|
||||
$tokenArr = AdminSession::where('admin_id', $params['id'])->select()->toArray();
|
||||
foreach ($tokenArr as $token) {
|
||||
self::expireToken($token['token']);
|
||||
}
|
||||
}
|
||||
|
||||
Admin::update($data);
|
||||
(new AdminAuthCache($params['id']))->clearAuthCache();
|
||||
|
||||
// 删除旧的关联信息
|
||||
AdminRole::delByUserId($params['id']);
|
||||
AdminDept::delByUserId($params['id']);
|
||||
AdminJobs::delByUserId($params['id']);
|
||||
// 角色
|
||||
self::insertRole($params['id'], $params['role_id']);
|
||||
// 部门
|
||||
self::insertDept($params['id'], $params['dept_id'] ?? []);
|
||||
// 岗位
|
||||
self::insertJobs($params['id'], $params['jobs_id'] ?? []);
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 删除管理员
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2021/12/29 10:45
|
||||
*/
|
||||
public static function delete(array $params): bool
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
$admin = Admin::findOrEmpty($params['id']);
|
||||
if ($admin->root == YesNoEnum::YES) {
|
||||
throw new \Exception("超级管理员不允许被删除");
|
||||
}
|
||||
Admin::destroy($params['id']);
|
||||
|
||||
//设置token过期
|
||||
$tokenArr = AdminSession::where('admin_id', $params['id'])->select()->toArray();
|
||||
foreach ($tokenArr as $token) {
|
||||
self::expireToken($token['token']);
|
||||
}
|
||||
(new AdminAuthCache($params['id']))->clearAuthCache();
|
||||
|
||||
// 删除旧的关联信息
|
||||
AdminRole::delByUserId($params['id']);
|
||||
AdminDept::delByUserId($params['id']);
|
||||
AdminJobs::delByUserId($params['id']);
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 过期token
|
||||
* @param $token
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author 段誉
|
||||
* @date 2021/12/29 10:46
|
||||
*/
|
||||
public static function expireToken($token): bool
|
||||
{
|
||||
$adminSession = AdminSession::where('token', '=', $token)
|
||||
->with('admin')
|
||||
->find();
|
||||
|
||||
if (empty($adminSession)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$time = time();
|
||||
$adminSession->expire_time = $time;
|
||||
$adminSession->update_time = $time;
|
||||
$adminSession->save();
|
||||
|
||||
return (new AdminTokenCache())->deleteAdminInfo($token);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 查看管理员详情
|
||||
* @param $params
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2021/12/29 11:07
|
||||
*/
|
||||
public static function detail($params, $action = 'detail'): array
|
||||
{
|
||||
$admin = Admin::field([
|
||||
'id', 'account', 'name', 'disable', 'root',
|
||||
'multipoint_login', 'avatar',
|
||||
])->findOrEmpty($params['id'])->toArray();
|
||||
|
||||
if ($action == 'detail') {
|
||||
return $admin;
|
||||
}
|
||||
|
||||
//代理登录,则用户名替换为代理账户
|
||||
$user = User::where(['agent_id' => $admin['id']])->findOrEmpty();
|
||||
|
||||
if (!$user->isEmpty()) {
|
||||
$admin['account'] = $user['account'];
|
||||
$admin['name'] = $user['account'];
|
||||
}
|
||||
|
||||
$result['user'] = $admin;
|
||||
// 当前管理员角色拥有的菜单
|
||||
$result['menu'] = MenuLogic::getMenuByAdminId($params['id']);
|
||||
// 当前管理员橘色拥有的按钮权限
|
||||
$result['permissions'] = AuthLogic::getBtnAuthByRoleId($admin);
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 编辑超级管理员
|
||||
* @param $params
|
||||
* @return Admin
|
||||
* @author 段誉
|
||||
* @date 2022/4/8 17:54
|
||||
*/
|
||||
public static function editSelf($params)
|
||||
{
|
||||
$data = [
|
||||
'id' => $params['admin_id'],
|
||||
'name' => $params['name'],
|
||||
'avatar' => FileService::setFileUrl($params['avatar']),
|
||||
];
|
||||
|
||||
if (!empty($params['password'])) {
|
||||
$passwordSalt = Config::get('project.unique_identification');
|
||||
$data['password'] = create_password($params['password'], $passwordSalt);
|
||||
}
|
||||
|
||||
return Admin::update($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 新增角色
|
||||
* @param $adminId
|
||||
* @param $roleIds
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2022/11/25 14:23
|
||||
*/
|
||||
public static function insertRole($adminId, $roleIds)
|
||||
{
|
||||
if (!empty($roleIds)) {
|
||||
// 角色
|
||||
$roleData = [];
|
||||
foreach ($roleIds as $roleId) {
|
||||
$roleData[] = [
|
||||
'admin_id' => $adminId,
|
||||
'role_id' => $roleId,
|
||||
];
|
||||
}
|
||||
(new AdminRole())->saveAll($roleData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 新增部门
|
||||
* @param $adminId
|
||||
* @param $deptIds
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2022/11/25 14:22
|
||||
*/
|
||||
public static function insertDept($adminId, $deptIds)
|
||||
{
|
||||
// 部门
|
||||
if (!empty($deptIds)) {
|
||||
$deptData = [];
|
||||
foreach ($deptIds as $deptId) {
|
||||
$deptData[] = [
|
||||
'admin_id' => $adminId,
|
||||
'dept_id' => $deptId
|
||||
];
|
||||
}
|
||||
(new AdminDept())->saveAll($deptData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 新增岗位
|
||||
* @param $adminId
|
||||
* @param $jobsIds
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2022/11/25 14:22
|
||||
*/
|
||||
public static function insertJobs($adminId, $jobsIds)
|
||||
{
|
||||
// 岗位
|
||||
if (!empty($jobsIds)) {
|
||||
$jobsData = [];
|
||||
foreach ($jobsIds as $jobsId) {
|
||||
$jobsData[] = [
|
||||
'admin_id' => $adminId,
|
||||
'jobs_id' => $jobsId
|
||||
];
|
||||
}
|
||||
(new AdminJobs())->saveAll($jobsData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 编辑超级管理员
|
||||
* @param $params
|
||||
* @return Admin
|
||||
* @author 段誉
|
||||
* @date 2022/4/8 17:54
|
||||
*/
|
||||
public static function google($params)
|
||||
{
|
||||
$admin = Admin::findOrEmpty($params['id'])->toArray();
|
||||
$show_google = false;
|
||||
|
||||
$google_auth = ConfigService::get('website', 'google_auth');
|
||||
|
||||
//代理登录
|
||||
$user = User::where(['agent_id' => $admin['id']])->findOrEmpty();
|
||||
if (!$user->isEmpty()) {
|
||||
$google_auth = ConfigService::get('website', 'agent_google_auth');
|
||||
}
|
||||
|
||||
if($google_auth == 0){
|
||||
$show_google = false;
|
||||
}else{
|
||||
if(!$admin['google_key']){
|
||||
$show_google = true;
|
||||
$secretKey = UtilsService::get_google_secretKey();
|
||||
Admin::update([
|
||||
'id' => $admin['id'],
|
||||
'google_key' => $secretKey,
|
||||
'google_qrcode' => UtilsService::get_google_qrcode($admin['account'],'',$secretKey),
|
||||
]);
|
||||
$admin = Admin::findOrEmpty($params['id'])->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
"show_google" => $show_google,
|
||||
"key" => $admin['google_key'],
|
||||
"qrcode" => $admin['google_qrcode'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 重置谷歌验证
|
||||
* @param $params
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2021/12/29 11:07
|
||||
*/
|
||||
public static function resetGoogle($params)
|
||||
{
|
||||
$admin = Admin::where(['id' => ($params['id'])])->findOrEmpty();
|
||||
|
||||
if ($admin->isEmpty()) {
|
||||
throw new \Exception("用户不存在");
|
||||
}
|
||||
|
||||
Admin::update([
|
||||
'id' => $admin['id'],
|
||||
'google_key' => "",
|
||||
'google_qrcode' => "",
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
105
app/adminapi/logic/auth/AuthLogic.php
Normal file
105
app/adminapi/logic/auth/AuthLogic.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\adminapi\logic\auth;
|
||||
|
||||
use app\common\model\auth\Admin;
|
||||
use app\common\model\auth\AdminRole;
|
||||
use app\common\model\auth\SystemMenu;
|
||||
use app\common\model\auth\SystemRoleMenu;
|
||||
|
||||
|
||||
/**
|
||||
* 权限功能类
|
||||
* Class AuthLogic
|
||||
* @package app\adminapi\logic\auth
|
||||
*/
|
||||
class AuthLogic
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 获取全部权限
|
||||
* @return mixed
|
||||
* @author 段誉
|
||||
* @date 2022/7/1 11:55
|
||||
*/
|
||||
public static function getAllAuth()
|
||||
{
|
||||
return SystemMenu::distinct(true)
|
||||
->where([
|
||||
['is_disable', '=', 0],
|
||||
['perms', '<>', '']
|
||||
])
|
||||
->column('perms');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取当前管理员角色按钮权限
|
||||
* @param $roleId
|
||||
* @return mixed
|
||||
* @author 段誉
|
||||
* @date 2022/7/1 16:10
|
||||
*/
|
||||
public static function getBtnAuthByRoleId($admin)
|
||||
{
|
||||
if ($admin['root']) {
|
||||
return ['*'];
|
||||
}
|
||||
|
||||
$menuId = SystemRoleMenu::whereIn('role_id', $admin['role_id'])
|
||||
->column('menu_id');
|
||||
|
||||
$where[] = ['is_disable', '=', 0];
|
||||
$where[] = ['perms', '<>', ''];
|
||||
|
||||
$roleAuth = SystemMenu::distinct(true)
|
||||
->where('id', 'in', $menuId)
|
||||
->where($where)
|
||||
->column('perms');
|
||||
|
||||
$allAuth = SystemMenu::distinct(true)
|
||||
->where($where)
|
||||
->column('perms');
|
||||
|
||||
$hasAllAuth = array_diff($allAuth, $roleAuth);
|
||||
if (empty($hasAllAuth)) {
|
||||
return ['*'];
|
||||
}
|
||||
|
||||
return $roleAuth;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取管理员角色关联的菜单id(菜单,权限)
|
||||
* @param int $adminId
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/7/1 15:56
|
||||
*/
|
||||
public static function getAuthByAdminId(int $adminId): array
|
||||
{
|
||||
$roleIds = AdminRole::where('admin_id', $adminId)->column('role_id');
|
||||
$menuId = SystemRoleMenu::whereIn('role_id', $roleIds)->column('menu_id');
|
||||
|
||||
return SystemMenu::distinct(true)
|
||||
->where([
|
||||
['is_disable', '=', 0],
|
||||
['perms', '<>', ''],
|
||||
['id', 'in', array_unique($menuId)],
|
||||
])
|
||||
->column('perms');
|
||||
}
|
||||
}
|
||||
199
app/adminapi/logic/auth/MenuLogic.php
Normal file
199
app/adminapi/logic/auth/MenuLogic.php
Normal file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\adminapi\logic\auth;
|
||||
|
||||
|
||||
use app\common\enum\YesNoEnum;
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\model\auth\Admin;
|
||||
use app\common\model\auth\SystemMenu;
|
||||
use app\common\model\auth\SystemRoleMenu;
|
||||
|
||||
|
||||
/**
|
||||
* 系统菜单
|
||||
* Class MenuLogic
|
||||
* @package app\adminapi\logic\auth
|
||||
*/
|
||||
class MenuLogic extends BaseLogic
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取管理员对应的角色菜单
|
||||
* @param $adminId
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author 段誉
|
||||
* @date 2022/7/1 10:50
|
||||
*/
|
||||
public static function getMenuByAdminId($adminId)
|
||||
{
|
||||
$admin = Admin::findOrEmpty($adminId);
|
||||
|
||||
$where = [];
|
||||
$where[] = ['type', 'in', ['M', 'C']];
|
||||
$where[] = ['is_disable', '=', 0];
|
||||
|
||||
if ($admin['root'] != 1) {
|
||||
$roleMenu = SystemRoleMenu::whereIn('role_id', $admin['role_id'])->column('menu_id');
|
||||
$where[] = ['id', 'in', $roleMenu];
|
||||
}
|
||||
|
||||
$menu = SystemMenu::where($where)
|
||||
->order(['sort' => 'desc', 'id' => 'asc'])
|
||||
->select();
|
||||
|
||||
return linear_to_tree($menu, 'children');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 添加菜单
|
||||
* @param array $params
|
||||
* @return SystemMenu|\think\Model
|
||||
* @author 段誉
|
||||
* @date 2022/6/30 10:06
|
||||
*/
|
||||
public static function add(array $params)
|
||||
{
|
||||
return SystemMenu::create([
|
||||
'pid' => $params['pid'],
|
||||
'type' => $params['type'],
|
||||
'name' => $params['name'],
|
||||
'icon' => $params['icon'] ?? '',
|
||||
'sort' => $params['sort'],
|
||||
'perms' => $params['perms'] ?? '',
|
||||
'paths' => $params['paths'] ?? '',
|
||||
'component' => $params['component'] ?? '',
|
||||
'selected' => $params['selected'] ?? '',
|
||||
'params' => $params['params'] ?? '',
|
||||
'is_cache' => $params['is_cache'],
|
||||
'is_show' => $params['is_show'],
|
||||
'is_disable' => $params['is_disable'],
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 编辑菜单
|
||||
* @param array $params
|
||||
* @return SystemMenu
|
||||
* @author 段誉
|
||||
* @date 2022/6/30 10:07
|
||||
*/
|
||||
public static function edit(array $params)
|
||||
{
|
||||
return SystemMenu::update([
|
||||
'id' => $params['id'],
|
||||
'pid' => $params['pid'],
|
||||
'type' => $params['type'],
|
||||
'name' => $params['name'],
|
||||
'icon' => $params['icon'] ?? '',
|
||||
'sort' => $params['sort'],
|
||||
'perms' => $params['perms'] ?? '',
|
||||
'paths' => $params['paths'] ?? '',
|
||||
'component' => $params['component'] ?? '',
|
||||
'selected' => $params['selected'] ?? '',
|
||||
'params' => $params['params'] ?? '',
|
||||
'is_cache' => $params['is_cache'],
|
||||
'is_show' => $params['is_show'],
|
||||
'is_disable' => $params['is_disable'],
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 详情
|
||||
* @param $params
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/6/30 9:54
|
||||
*/
|
||||
public static function detail($params)
|
||||
{
|
||||
return SystemMenu::findOrEmpty($params['id'])->toArray();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 删除菜单
|
||||
* @param $params
|
||||
* @author 段誉
|
||||
* @date 2022/6/30 9:47
|
||||
*/
|
||||
public static function delete($params)
|
||||
{
|
||||
// 删除菜单
|
||||
SystemMenu::destroy($params['id']);
|
||||
// 删除角色-菜单表中 与该菜单关联的记录
|
||||
SystemRoleMenu::where(['menu_id' => $params['id']])->delete();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 更新状态
|
||||
* @param array $params
|
||||
* @return SystemMenu
|
||||
* @author 段誉
|
||||
* @date 2022/7/6 17:02
|
||||
*/
|
||||
public static function updateStatus(array $params)
|
||||
{
|
||||
return SystemMenu::update([
|
||||
'id' => $params['id'],
|
||||
'is_disable' => $params['is_disable']
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 更新排序
|
||||
* @param array $params
|
||||
* @return SystemMenu
|
||||
* @author 段誉
|
||||
* @date 2022/7/6 17:02
|
||||
*/
|
||||
public static function updateSort(array $params)
|
||||
{
|
||||
return SystemMenu::update([
|
||||
'id' => $params['id'],
|
||||
'sort' => $params['sort']
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 全部数据
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author 段誉
|
||||
* @date 2022/10/13 11:03
|
||||
*/
|
||||
public static function getAllData()
|
||||
{
|
||||
$data = SystemMenu::where(['is_disable' => YesNoEnum::NO])
|
||||
->field('id,pid,name')
|
||||
->order(['sort' => 'desc', 'id' => 'asc'])
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
return linear_to_tree($data, 'children');
|
||||
}
|
||||
|
||||
}
|
||||
171
app/adminapi/logic/auth/RoleLogic.php
Normal file
171
app/adminapi/logic/auth/RoleLogic.php
Normal file
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\adminapi\logic\auth;
|
||||
|
||||
use app\common\{
|
||||
cache\AdminAuthCache,
|
||||
model\auth\SystemRole,
|
||||
logic\BaseLogic,
|
||||
model\auth\SystemRoleMenu
|
||||
};
|
||||
use think\facade\Db;
|
||||
|
||||
|
||||
/**
|
||||
* 角色逻辑层
|
||||
* Class RoleLogic
|
||||
* @package app\adminapi\logic\auth
|
||||
*/
|
||||
class RoleLogic extends BaseLogic
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 添加角色
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2021/12/29 11:50
|
||||
*/
|
||||
public static function add(array $params): bool
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
$menuId = !empty($params['menu_id']) ? $params['menu_id'] : [];
|
||||
|
||||
$role = SystemRole::create([
|
||||
'name' => $params['name'],
|
||||
'desc' => $params['desc'] ?? '',
|
||||
'sort' => $params['sort'] ?? 0,
|
||||
]);
|
||||
|
||||
$data = [];
|
||||
foreach ($menuId as $item) {
|
||||
if (empty($item)) {
|
||||
continue;
|
||||
}
|
||||
$data[] = [
|
||||
'role_id' => $role['id'],
|
||||
'menu_id' => $item,
|
||||
];
|
||||
}
|
||||
(new SystemRoleMenu)->insertAll($data);
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::$error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 编辑角色
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2021/12/29 14:16
|
||||
*/
|
||||
public static function edit(array $params): bool
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
$menuId = !empty($params['menu_id']) ? $params['menu_id'] : [];
|
||||
|
||||
SystemRole::update([
|
||||
'id' => $params['id'],
|
||||
'name' => $params['name'],
|
||||
'desc' => $params['desc'] ?? '',
|
||||
'sort' => $params['sort'] ?? 0,
|
||||
]);
|
||||
|
||||
if (!empty($menuId)) {
|
||||
SystemRoleMenu::where(['role_id' => $params['id']])->delete();
|
||||
$data = [];
|
||||
foreach ($menuId as $item) {
|
||||
$data[] = [
|
||||
'role_id' => $params['id'],
|
||||
'menu_id' => $item,
|
||||
];
|
||||
}
|
||||
(new SystemRoleMenu)->insertAll($data);
|
||||
}
|
||||
|
||||
(new AdminAuthCache())->deleteTag();
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::$error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 删除角色
|
||||
* @param int $id
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2021/12/29 14:16
|
||||
*/
|
||||
public static function delete(int $id)
|
||||
{
|
||||
SystemRole::destroy(['id' => $id]);
|
||||
(new AdminAuthCache())->deleteTag();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 角色详情
|
||||
* @param int $id
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author 段誉
|
||||
* @date 2021/12/29 14:17
|
||||
*/
|
||||
public static function detail(int $id): array
|
||||
{
|
||||
$detail = SystemRole::field('id,name,desc,sort')->find($id);
|
||||
$authList = $detail->roleMenuIndex()->select()->toArray();
|
||||
$menuId = array_column($authList, 'menu_id');
|
||||
$detail['menu_id'] = $menuId;
|
||||
return $detail->toArray();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 角色数据
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author 段誉
|
||||
* @date 2022/10/13 10:39
|
||||
*/
|
||||
public static function getAllData()
|
||||
{
|
||||
return SystemRole::order(['sort' => 'desc', 'id' => 'desc'])
|
||||
->where(' id <> 6')
|
||||
->select()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user