247 lines
8.6 KiB
PHP
247 lines
8.6 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\oa\logic\works\rlzy;
|
||
|
||
|
||
use app\common\cache\AdminAuthCache;
|
||
use app\common\cache\AdminTokenCache;
|
||
use app\common\model\auth\Admin;
|
||
use app\common\model\auth\AdminRole;
|
||
use app\common\model\auth\AdminSession;
|
||
use app\common\model\dept\Dept;
|
||
use app\common\model\dept\Jobs;
|
||
use app\common\model\dept\Orgs;
|
||
use app\common\model\works\rlzy\OaAdmin;
|
||
use app\common\logic\BaseLogic;
|
||
use think\facade\Config;
|
||
use think\facade\Db;
|
||
|
||
|
||
/**
|
||
* 企业员工逻辑
|
||
* Class OaAdminLogic
|
||
* @package app\adminapi\logic\works\rlzy
|
||
*/
|
||
class OaAdminLogic extends BaseLogic
|
||
{
|
||
|
||
|
||
/**
|
||
* @notes 添加企业员工
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2024/05/22 15:49
|
||
*/
|
||
public static function add(array $params): bool
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
$admin = Admin::create([
|
||
'name' => $params['name'],
|
||
'account' => $params['mobile'],
|
||
'avatar' => $params['thumb'] ?? config('project.default_image.admin_avatar'),
|
||
'password' => create_password($params['password'], Config::get('project.unique_identification')),
|
||
'org_id' => $params['org_id'],
|
||
'dept_id' => $params['dept_id'],
|
||
'job_id' => $params['job_id'],
|
||
'disable' => $params['status'] == 2 ? 1 : 0,
|
||
]);
|
||
self::insertRole($admin['id'], $params['role_id'] ?? []);
|
||
OaAdmin::create([
|
||
'name' => $params['name'],
|
||
'email' => $params['email'] ?? '',
|
||
'mobile' => $params['mobile'] ?? '',
|
||
'sex' => $params['sex'] ?? 0,
|
||
'thumb' => $params['thumb'] ?? config('project.default_image.admin_avatar'),
|
||
'org_id' => $params['org_id'],
|
||
'dept_id' => $params['dept_id'],
|
||
'job_id' => $params['job_id'],
|
||
'type' => $params['type'],
|
||
'age' => $params['age'] ?? 0,
|
||
'native_place' => $params['native_place'] ?? '',
|
||
'idcard' => $params['idcard'] ?? '',
|
||
'education' => $params['education'] ?? '',
|
||
'bank_account' => $params['bank_account'] ?? '',
|
||
'bank_info' => $params['bank_info'] ?? '',
|
||
'desc' => $params['desc'] ?? null,
|
||
'entry_time' => !empty($params['entry_time']) ? strtotime($params['entry_time']) : 0,
|
||
'status' => $params['status'],
|
||
'admin_id' => $admin['id']
|
||
]);
|
||
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 编辑企业员工
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2024/05/22 15:49
|
||
*/
|
||
public static function edit(array $params): bool
|
||
{
|
||
$data = OaAdmin::where('id', $params['id'])->findOrEmpty();
|
||
Db::startTrans();
|
||
try {
|
||
OaAdmin::where('id', $params['id'])->update([
|
||
'name' => $params['name'],
|
||
'email' => $params['email'] ?? '',
|
||
'mobile' => $params['mobile'] ?? '',
|
||
'sex' => $params['sex'] ?? 0,
|
||
'thumb' => $params['thumb'] ?? config('project.default_image.admin_avatar'),
|
||
'org_id' => $params['org_id'],
|
||
'dept_id' => $params['dept_id'],
|
||
'job_id' => $params['job_id'],
|
||
'type' => $params['type'],
|
||
'age' => $params['age'] ?? 0,
|
||
'native_place' => $params['native_place'] ?? '',
|
||
'idcard' => $params['idcard'] ?? '',
|
||
'education' => $params['education'] ?? '',
|
||
'bank_account' => $params['bank_account'] ?? '',
|
||
'bank_info' => $params['bank_info'] ?? '',
|
||
'desc' => $params['desc'] ?? null,
|
||
'entry_time' => !empty($params['entry_time']) ? strtotime($params['entry_time']) : 0,
|
||
'status' => $params['status'],
|
||
]);
|
||
Admin::where('id',$data['admin_id'])->update([
|
||
'name' => $params['name'],
|
||
'account' => $params['name'],
|
||
'avatar' => $params['thumb'] ?? config('project.default_image.admin_avatar'),
|
||
'password' => create_password($params['password'], Config::get('project.unique_identification')),
|
||
'org_id' => $params['org_id'],
|
||
'dept_id' => $params['dept_id'],
|
||
'job_id' => $params['job_id'],
|
||
'disable' => $params['status'] == 2 ? 1 : 0,
|
||
]);
|
||
// 更换角色后.设置token过期
|
||
$roleId = AdminRole::where('admin_id', $data['admin_id'])->column('role_id');
|
||
if (!empty(array_diff_assoc($roleId, $params['role_id'])) || $params['status'] == 2) {
|
||
$tokenArr = AdminSession::where('admin_id', $data['admin_id'])->select()->toArray();
|
||
foreach ($tokenArr as $token) {
|
||
self::expireToken($token['token']);
|
||
}
|
||
}
|
||
(new AdminAuthCache($data['admin_id']))->clearAuthCache();
|
||
// 删除旧的关联信息
|
||
AdminRole::delByUserId($data['admin_id']);
|
||
// 角色
|
||
self::insertRole($data['admin_id'], $params['role_id']);
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 删除企业员工
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2024/05/22 15:49
|
||
*/
|
||
public static function delete(array $params): bool
|
||
{
|
||
$data = OaAdmin::where('id', $params['id'])->findOrEmpty();
|
||
Db::startTrans();
|
||
try {
|
||
OaAdmin::destroy($params['id']);
|
||
Admin::destroy($data['admin_id']);
|
||
//设置token过期
|
||
$tokenArr = AdminSession::where('admin_id', $data['admin_id'])->select()->toArray();
|
||
foreach ($tokenArr as $token) {
|
||
self::expireToken($token['token']);
|
||
}
|
||
(new AdminAuthCache($data['admin_id']))->clearAuthCache();
|
||
// 删除旧的关联信息
|
||
AdminRole::delByUserId($data['admin_id']);
|
||
Db::commit();
|
||
return true;
|
||
}catch (\Exception $e){
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 获取企业员工详情
|
||
* @param $params
|
||
* @return array
|
||
* @author likeadmin
|
||
* @date 2024/05/22 15:49
|
||
*/
|
||
public static function detail($params): array
|
||
{
|
||
$data = OaAdmin::findOrEmpty($params['id']);
|
||
$data['sex_text'] = $data->sex_text;
|
||
$data['type_text'] = $data->type_text;
|
||
$data['status_text'] = $data->status_text;
|
||
$org = Orgs::field('name')->where('id',$data['org_id'])->findOrEmpty();
|
||
$dept = Dept::field('name')->where('id',$data['dept_id'])->findOrEmpty();
|
||
$job = Jobs::field('name')->where('id',$data['job_id'])->findOrEmpty();
|
||
$data['org_name'] = !$org->isEmpty() ? $org['name'] : '';
|
||
$data['dept_name'] = !$dept->isEmpty() ? $dept['name'] : '';
|
||
$data['job_name'] = !$job->isEmpty() ? $job['name'] : '';
|
||
$data['role_id'] = AdminRole::where('admin_id', $data['admin_id'])->column('role_id');
|
||
return $data->toArray();
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|
||
|
||
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);
|
||
}
|
||
} |