dev_oa/app/home/controller/Position.php

189 lines
6.3 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2021 勾股工作室
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.gougucms.com
*/
declare (strict_types = 1);
namespace app\home\controller;
use app\home\BaseController;
use app\home\validate\PositionCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
class Position extends BaseController
{
public function index()
{
if (request()->isAjax()) {
$list = Db::name('Position')->where('status','>=',0)->order('create_time asc')->select()->toArray();
foreach ($list as &$val) {
$groupId = Db::name('PositionGroup')->where(['pid' => $val['id']])->column('group_id');
$groupName = Db::name('AdminGroup')->where('id', 'in', $groupId)->column('title');
$val['groupName'] = implode(',', $groupName);
}
$res['data'] = $list;
return table_assign(0, '', $res);
} else {
return view();
}
}
//添加
public function add()
{
$id = empty(get_params('id')) ? 0 : get_params('id');
$group = Db::name('AdminGroup')->order('create_time asc')->select()->toArray();
if($id > 0) {
$detail = Db::name('Position')->where(['id' => $id])->find();
$detail['group_id'] = Db::name('PositionGroup')->where(['pid' => $id])->column('group_id');
foreach ($group as &$val) {
if(in_array($val['id'],$detail['group_id'])){
$val['checked'] = 1;
}
else{
$val['checked'] = 0;
}
}
View::assign('detail', $detail);
}
View::assign('group', $group);
View::assign('id', $id);
return view();
}
//提交添加
public function post_submit()
{
if (request()->isAjax()) {
$param = get_params();
if (!empty($param['id']) && $param['id'] > 0) {
try {
validate(PositionCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
// 启动事务
Db::startTrans();
try {
Db::name('Position')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
Db::name('PositionGroup')->where(['pid' => $param['id']])->delete();
foreach ($param['group_id'] as $k => $v) {
$data[$k] = [
'pid' => $param['id'],
'group_id' => $v,
'create_time' => time()
];
}
Db::name('PositionGroup')->strict(false)->field(true)->insertAll($data);
add_log('edit', $param['id'], $param);
//清除菜单\权限缓存
clear_cache('adminMenu');
clear_cache('adminRules');
// 提交事务
Db::commit();
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return to_assign(1, '提交失败:' . $e->getMessage());
}
} else {
try {
validate(PositionCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
// 启动事务
Db::startTrans();
try {
$uid = Db::name('Position')->strict(false)->field(true)->insertGetId($param);
foreach ($param['group_id'] as $k => $v) {
$data[$k] = [
'pid' => $uid,
'group_id' => $v,
'create_time' => time()
];
}
Db::name('PositionGroup')->strict(false)->field(true)->insertAll($data);
add_log('add', $uid, $param);
// 提交事务
Db::commit();
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return to_assign(1, '提交失败:' . $e->getMessage());
}
}
return to_assign();
}
}
//查看
public function view()
{
$id = get_params('id');
$group = Db::name('AdminGroup')->order('create_time asc')->select()->toArray();
$detail = Db::name('Position')->where(['id' => $id])->find();
$detail['group_id'] = Db::name('PositionGroup')->where(['pid' => $id])->column('group_id');
foreach ($group as &$val) {
if(in_array($val['id'],$detail['group_id'])){
$val['checked'] = 1;
}
else{
$val['checked'] = 0;
}
}
$menu = get_admin_menu();
$rule = get_admin_rule();
$user_groups = Db::name('PositionGroup')
->alias('a')
->join("AdminGroup g", "a.group_id=g.id", 'LEFT')
->where("a.pid='{$id}' and g.status='1'")
->select()
->toArray();
$groups = $user_groups ?: [];
$menus = [];
$rules = [];
foreach ($groups as $g) {
$menus = array_merge($menus, explode(',', trim($g['menus'], ',')));
$rules = array_merge($rules, explode(',', trim($g['rules'], ',')));
}
$menus = array_unique($menus);
$rules = array_unique($rules);
$role_menu = create_tree_list(0, $menu, $menus);
$role_rule = create_tree_list(0, $rule, $rules);
View::assign('role_menu', $role_menu);
View::assign('role_rule', $role_rule);
View::assign('detail', $detail);
View::assign('group', $group);
add_log('view', $id);
return view();
}
//删除
public function delete()
{
$id = get_params("id");
if($id == 1){
return to_assign(0, "超级岗位,不能删除");
}
$data['status'] = '-1';
$data['id'] = $id;
$data['update_time'] = time();
if (Db::name('Position')->update($data) !== false) {
add_log('delete', $id);
return to_assign(0, "删除岗位成功");
} else {
return to_assign(1, "删除失败");
}
}
}