lihai-oa/app/api/controller/UserDepartment.php

102 lines
3.2 KiB
PHP
Raw Normal View History

2023-10-28 15:07:04 +08:00
<?php
/**
* @copyright Copyright (c) 2021 勾股工作室
* @license https://opensource.org/licenses/GPL-3.0
* @link https://www.gougucms.com
*/
declare (strict_types = 1);
namespace app\api\controller;
use app\api\ApiController;
use app\api\middleware\Auth;
use app\user\validate\DepartmentCheck;
use think\exception\ValidateException;
use think\facade\Request;
use think\facade\Db;
class UserDepartment extends ApiController
{
2023-10-28 15:10:37 +08:00
protected $middleware = [
Auth::class => ['except' => []]
];
2023-10-28 15:58:35 +08:00
//获取部门架构
2023-10-28 15:07:04 +08:00
public function index()
{
2023-10-28 17:15:23 +08:00
$this->checkAuth();
$param = get_params();
if (!empty($param['tree']) && $param['tree'] == 1) {
$list = set_recursion(get_department());
} else {
$cate = Db::name('Department')
2023-10-28 15:07:04 +08:00
->field('d.*,a.name as leader')
->alias('d')
->join('Admin a', 'a.id = d.leader_id', 'LEFT')
->order('d.sort desc,d.id asc')
->select();
2023-10-28 17:15:23 +08:00
$list = generateTree($cate);
}
2023-10-28 15:07:04 +08:00
$this->apiSuccess('获取成功', $list);
}
//添加部门
2023-10-28 17:15:23 +08:00
public function add()
2023-10-28 15:07:04 +08:00
{
2023-10-28 17:15:23 +08:00
$this->checkAuth();
2023-10-28 15:07:04 +08:00
$param = get_params();
2023-10-28 15:58:35 +08:00
if (!empty($param['id']) && $param['id'] > 0) {
try {
validate(DepartmentCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
$this->apiError($e->getError());
}
$param['update_time'] = time();
$department_array = get_department_son($param['id']);
if (in_array($param['pid'], $department_array)) {
$this->apiError('上级部门不能是该部门本身或其下属部门');
2023-10-28 15:07:04 +08:00
} else {
2023-10-28 15:58:35 +08:00
Db::name('Department')->strict(false)->field(true)->update($param);
add_log('edit', $param['id'], $param);
$this->apiSuccess('操作成功');
2023-10-28 15:07:04 +08:00
}
} else {
2023-10-28 15:58:35 +08:00
try {
validate(DepartmentCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
$this->apiError($e->getError());
2023-10-28 15:07:04 +08:00
}
2023-10-28 15:58:35 +08:00
$did = Db::name('Department')->strict(false)->field(true)->insertGetId($param);
add_log('add', $did, $param);
$this->apiSuccess('操作成功');
2023-10-28 15:07:04 +08:00
}
2023-10-28 15:58:35 +08:00
2023-10-28 15:07:04 +08:00
}
//删除
public function delete()
{
2023-10-28 17:15:23 +08:00
$this->checkAuth();
2023-10-28 15:07:04 +08:00
$id = get_params("id");
2023-10-28 15:58:35 +08:00
if (empty($id)) {
$this->apiError('部门id不能为空');
}
2023-10-28 15:07:04 +08:00
$count = Db::name('Department')->where([['pid', '=', $id], ['status', '>=', 0]])->count();
if ($count > 0) {
2023-10-28 15:58:35 +08:00
$this->apiError('该部门下还有子部门,无法删除');
2023-10-28 15:07:04 +08:00
}
$users = Db::name('Admin')->where([['did', '=', $id], ['status', '>=', 0]])->count();
if ($users > 0) {
2023-10-28 15:58:35 +08:00
$this->apiError('该部门下还有员工,无法删除');
2023-10-28 15:07:04 +08:00
}
if (Db::name('Department')->delete($id) !== false) {
add_log('delete', $id);
2023-10-28 15:58:35 +08:00
$this->apiSuccess('删除部门成功');
2023-10-28 15:07:04 +08:00
} else {
2023-10-28 15:58:35 +08:00
$this->apiError('删除失败');
2023-10-28 15:07:04 +08:00
}
}
}