This commit is contained in:
weiz 2024-05-22 17:32:53 +08:00
parent 3e364fcafb
commit 2904adf3e9
6 changed files with 679 additions and 1 deletions

View File

@ -0,0 +1,111 @@
<?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\controller\works\rlzy;
use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\works\rlzy\OaAdminLists;
use app\adminapi\logic\works\rlzy\OaAdminLogic;
use app\adminapi\validate\works\rlzy\OaAdminValidate;
/**
* 企业员工控制器
* Class OaAdminController
* @package app\adminapi\controller\works\rlzy
*/
class OaAdminController extends BaseAdminController
{
/**
* @notes 获取企业员工列表
* @return \think\response\Json
* @author likeadmin
* @date 2024/05/22 15:49
*/
public function lists()
{
return $this->dataLists(new OaAdminLists());
}
/**
* @notes 添加企业员工
* @return \think\response\Json
* @author likeadmin
* @date 2024/05/22 15:49
*/
public function add()
{
$params = (new OaAdminValidate())->post()->goCheck('add');
$result = OaAdminLogic::add($params);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(OaAdminLogic::getError());
}
/**
* @notes 编辑企业员工
* @return \think\response\Json
* @author likeadmin
* @date 2024/05/22 15:49
*/
public function edit()
{
$params = (new OaAdminValidate())->post()->goCheck('edit');
$result = OaAdminLogic::edit($params);
if (true === $result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(OaAdminLogic::getError());
}
/**
* @notes 删除企业员工
* @return \think\response\Json
* @author likeadmin
* @date 2024/05/22 15:49
*/
public function delete()
{
$params = (new OaAdminValidate())->post()->goCheck('delete');
$result = OaAdminLogic::delete($params);
if (true === $result) {
return $this->success('删除成功', [], 1, 1);
}
return $this->fail(OaAdminLogic::getError());
}
/**
* @notes 获取企业员工详情
* @return \think\response\Json
* @author likeadmin
* @date 2024/05/22 15:49
*/
public function detail()
{
$params = (new OaAdminValidate())->goCheck('detail');
$result = OaAdminLogic::detail($params);
return $this->data($result);
}
}

View File

@ -0,0 +1,90 @@
<?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\lists\works\rlzy;
use app\adminapi\lists\BaseAdminDataLists;
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\lists\ListsSearchInterface;
/**
* 企业员工列表
* Class OaAdminLists
* @package app\adminapi\listsworks\rlzy
*/
class OaAdminLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件
* @return \string[][]
* @author likeadmin
* @date 2024/05/22 15:49
*/
public function setSearch(): array
{
return [
'=' => ['sex', 'org_id', 'dept_id', 'job_id', 'type', 'status'],
'%like%' => ['name', 'mobile'],
];
}
/**
* @notes 获取企业员工列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author likeadmin
* @date 2024/05/22 15:49
*/
public function lists(): array
{
return OaAdmin::where($this->searchWhere)
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()->each(function($data){
$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'] : '';
})
->toArray();
}
/**
* @notes 获取企业员工数量
* @return int
* @author likeadmin
* @date 2024/05/22 15:49
*/
public function count(): int
{
return OaAdmin::where($this->searchWhere)->count();
}
}

View File

@ -27,6 +27,7 @@ use app\common\cache\AdminTokenCache;
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\service\FileService;
use think\facade\Config;
use think\facade\Db;
@ -146,6 +147,7 @@ class AdminLogic extends BaseLogic
*/
public static function delete(array $params): bool
{
$oa_admin = OaAdmin::where('admin_id',$params['id'])->findOrEmpty();
Db::startTrans();
try {
$admin = Admin::findOrEmpty($params['id']);
@ -153,7 +155,7 @@ class AdminLogic extends BaseLogic
throw new \Exception("超级管理员不允许被删除");
}
Admin::destroy($params['id']);
OaAdmin::destroy($oa_admin['id']);
//设置token过期
$tokenArr = AdminSession::where('admin_id', $params['id'])->select()->toArray();
foreach ($tokenArr as $token) {

View File

@ -0,0 +1,247 @@
<?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\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);
}
}

View File

@ -0,0 +1,178 @@
<?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\validate\works\rlzy;
use app\common\model\auth\SystemRole;
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\validate\BaseValidate;
/**
* 企业员工验证器
* Class OaAdminValidate
* @package app\adminapi\validate\works\rlzy
*/
class OaAdminValidate extends BaseValidate
{
/**
* 设置校验规则
* @var string[]
*/
protected $rule = [
'id' => 'require|checkData',
'name' => 'require',
'email' => 'email',
'mobile' => 'require|mobile|unique:oa_admin',
'sex' => 'integer|in:0,1,2',
'org_id' => 'require|checkOrg',
'dept_id' => 'require|checkDept',
'job_id' => 'require|checkJob',
'type' => 'require|integer|in:0,1,2',
'age' => 'integer|gt:0',
'idcard' => 'idCard',
'entry_time' => 'require|dateFormat:Y-m-d',
'status' => 'require|integer|in:0,1,2',
'password' => 'require|length:6,32',
'role_id' => 'require|checkRole',
];
/**
* 参数描述
* @var string[]
*/
protected $field = [
'id' => 'id',
'name' => '员工姓名',
'email' => '电子邮箱',
'mobile' => '手机号码',
'sex' => '性别',
'org_id' => '组织',
'dept_id' => '部门',
'job_id' => '职位',
'type' => '员工类型',
'age' => '年龄',
'idcard' => '身份证',
'entry_time' => '员工入职日期',
'status' => '状态',
'password' => '登录密码',
'role_id' => '角色权限',
];
/**
* @notes 添加场景
* @return OaAdminValidate
* @author likeadmin
* @date 2024/05/22 15:49
*/
public function sceneAdd()
{
return $this->remove('id',true);
}
/**
* @notes 编辑场景
* @return OaAdminValidate
* @author likeadmin
* @date 2024/05/22 15:49
*/
public function sceneEdit()
{}
/**
* @notes 删除场景
* @return OaAdminValidate
* @author likeadmin
* @date 2024/05/22 15:49
*/
public function sceneDelete()
{
return $this->only(['id']);
}
/**
* @notes 详情场景
* @return OaAdminValidate
* @author likeadmin
* @date 2024/05/22 15:49
*/
public function sceneDetail()
{
return $this->only(['id']);
}
public function checkData($value): bool|string
{
$data = OaAdmin::field('id')->where('id',$value)->findOrEmpty();
return $data->isEmpty() ? '数据不存在' : true;
}
public function checkOrg($value): bool|string
{
$org = Orgs::where('id', $value)->findOrEmpty();
if ($org->isEmpty()) {
return '组织不存在';
}
return true;
}
public function checkDept($value, $rule, $data): bool|string
{
$dept = Dept::where('id', $value)->findOrEmpty();
if ($dept->isEmpty()) {
return '部门不存在';
}
if ($dept['org_id'] != $data['org_id']) {
return '该部门不属于当前选择的组织';
}
return true;
}
public function checkJob($value, $rule, $data): bool|string
{
$job = Jobs::where('id', $value)->findOrEmpty();
if ($job->isEmpty()) {
return '岗位不存在';
}
if ($job['dept_id'] != $data['dept_id']) {
return '该岗位不属于当前选择的部门';
}
return true;
}
public function checkRole($value): bool|string
{
if(empty($value) || !is_array($value)){
return '角色数据格式错误';
}
foreach ($value as $item) {
$res = SystemRole::where('id',$item)->findOrEmpty();
if($res->isEmpty()){
return '角色数据不存在';
}
}
return true;
}
}

View File

@ -0,0 +1,50 @@
<?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\common\model\works\rlzy;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;
/**
* 企业员工模型
* Class OaAdmin
* @package app\common\model\works\rlzy
*/
class OaAdmin extends BaseModel
{
use SoftDelete;
protected $name = 'oa_admin';
protected $deleteTime = 'delete_time';
public function getSexTextAttr($value,$data): string
{
$arr = [0=>'未设置', 1=>'男', 2=>'女'];
return $arr[$data['sex']];
}
public function getTypeTextAttr($value,$data): string
{
$arr = [0=>'正式', 1=>'试用', 2=>'实习'];
return $arr[$data['type']];
}
public function getStatusTextAttr($value,$data): string
{
$arr = [0=>'禁用', 1=>'正常', 2=>'离职'];
return $arr[$data['status']];
}
}