411 lines
14 KiB
PHP
411 lines
14 KiB
PHP
<?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\cache\AdminTokenCache;
|
||
use app\common\model\Company;
|
||
use app\common\model\contract\Contract;
|
||
use app\common\model\user\User;
|
||
use app\common\service\ConfigService;
|
||
use app\common\service\FileService;
|
||
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;
|
||
|
||
$params['avatar'] = $avatar;
|
||
$params['password'] = $password;
|
||
$params['create_time'] = time();
|
||
$params['company_id'] = $params['party_a'] ?? 0;
|
||
$admin_id = Admin::strict(false)->field(true)->insertGetId($params);
|
||
// 角色
|
||
self::insertRole($admin_id, explode(',',$params['role_id']) ?? []);
|
||
// 部门
|
||
// self::insertDept($admin_id, $params['dept_id'] ?? []);
|
||
// 岗位
|
||
// self::insertJobs($admin_id, $params['jobs_id'] ?? []);
|
||
// self::createUser($params, $password, $admin_id);
|
||
// if($params['party_a']>0){
|
||
// self::contract($admin_id, $params);//生成合同
|
||
// }
|
||
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 {
|
||
// 基础信息
|
||
if(is_array($params['role_id'])){
|
||
$role_id_arr=$params['role_id'];
|
||
}else{
|
||
$role_id_arr=explode(',',$params['role_id']) ?? [];
|
||
}
|
||
$data=$params;
|
||
// 头像
|
||
$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, $role_id_arr))) {
|
||
$editRole = true;
|
||
}
|
||
|
||
if ($params['disable'] == 1 || $editRole) {
|
||
$tokenArr = AdminSession::where('admin_id', $params['id'])->select()->toArray();
|
||
foreach ($tokenArr as $token) {
|
||
self::expireToken($token['token']);
|
||
}
|
||
}
|
||
$is_contract=false;
|
||
if(isset($params['party_a']) && $params['party_a']!=''){
|
||
$data['company_id'] = $params['party_a'] ?? 0;
|
||
$is_contract=true;
|
||
}
|
||
Admin::strict(false)->update($data);
|
||
(new AdminAuthCache($params['id']))->clearAuthCache();
|
||
|
||
// 删除旧的关联信息
|
||
AdminRole::delByUserId($params['id']);
|
||
AdminDept::delByUserId($params['id']);
|
||
AdminJobs::delByUserId($params['id']);
|
||
// 角色
|
||
self::insertRole($params['id'], $role_id_arr);
|
||
// if($is_contract){
|
||
// $datas=[
|
||
// 'party_a' => $params['party_a'],
|
||
// 'party_b' => $params['id'],
|
||
// 'contract_type' => $params['contract_type'],
|
||
// 'type' => 2,
|
||
// 'file' => $params['file'],
|
||
// ];
|
||
// self::contract($params['id'], $datas);//生成合同
|
||
// }
|
||
//修改用户同步信息
|
||
// self::createUser($params, $data['password'] ?? '', $data['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::name('user')->where(['admin_id'=>$params['id']])->update(['delete_time'=>time()]);
|
||
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', 'sex', 'province', 'city', 'area', 'street', 'address', 'qualification', 'is_contract', 'id_card,phone'
|
||
])->findOrEmpty($params['id'])->toArray();
|
||
$admin['qualification'] = $admin['qualification']!=null?json_decode($admin['qualification'], true):[];
|
||
$admin['company']=Company::where(['admin_id'=>$admin['id']])->field('id,company_name')->find();
|
||
// $admin['contract']=Db::name('contract')->where('party_b',$params['id'])
|
||
// ->withAttr('contract_type_name',function($value,$data){
|
||
// return Db::name('dict_data')->where('id',$data['contract_type'])->value('name');
|
||
// })
|
||
// ->find();
|
||
// if (empty($admin['contract']['party_a'])) {
|
||
// $admin['contract']['party_a_info'] = '';
|
||
// } else {
|
||
// $admin['contract']['party_a_info'] = Company::where(['id' => $admin['contract']['party_a']])->field('id,company_name,organization_code,master_name,master_phone,master_email')->find();
|
||
// }
|
||
if ($action == 'detail') {
|
||
return $admin;
|
||
}
|
||
|
||
$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);
|
||
}
|
||
}
|
||
|
||
public static function createUser($admin, $password, $adminId)
|
||
{
|
||
$user = User::where(['admin_id' => $adminId])->find();
|
||
unset($admin['id'],$admin['contract']);
|
||
if (empty($user)) {
|
||
$userSn = User::createUserSn();
|
||
User::create([
|
||
'sn' => $userSn,
|
||
'avatar' => $admin['avatar'],
|
||
'nickname' => $admin['name'],
|
||
'account' => $admin['account'],
|
||
'password' => $password,
|
||
'channel' => 3,
|
||
'admin_id' => $adminId,
|
||
'qualification'=>$admin['qualification'],
|
||
'province'=>$admin['province'],
|
||
'city'=>$admin['city'],
|
||
'area'=>$admin['area'],
|
||
'street'=>$admin['street'],
|
||
'address'=>$admin['address'],
|
||
]);
|
||
} else {
|
||
if(isset($admin['name']) && $admin['name']!=''){
|
||
$user->nickname = $admin['name'];
|
||
}
|
||
if(isset($admin['disable']) && $admin['disable']!=''){
|
||
$user->is_disable = $admin['disable'];
|
||
}
|
||
$user->save($admin);
|
||
}
|
||
}
|
||
|
||
public static function contract($adminId, $data)
|
||
{
|
||
$model = Contract::where(['party_b'=>$adminId])->find();
|
||
if (empty($model)) {
|
||
$model = new Contract();
|
||
$model->party_b = $adminId;
|
||
$model->contract_no = time();
|
||
$model->create_time = time();
|
||
$model->type = 2;
|
||
}
|
||
$model->update_time = time();
|
||
$model->setAttrs($data);
|
||
$model->save($data);
|
||
}
|
||
|
||
} |