Position.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\user\controller;
  15. use app\base\BaseController;
  16. use app\user\validate\PositionCheck;
  17. use think\exception\ValidateException;
  18. use think\facade\Db;
  19. use think\facade\View;
  20. class Position extends BaseController
  21. {
  22. public function index()
  23. {
  24. if (request()->isAjax()) {
  25. $list = Db::name('Position')->where('status', '>=', 0)->order('create_time asc')->select()->toArray();
  26. foreach ($list as &$val) {
  27. $groupId = Db::name('PositionGroup')->where(['pid' => $val['id']])->column('group_id');
  28. $groupName = Db::name('AdminGroup')->where('id', 'in', $groupId)->column('title');
  29. $val['groupName'] = implode(',', $groupName);
  30. }
  31. $res['data'] = $list;
  32. return table_assign(0, '', $res);
  33. } else {
  34. return view();
  35. }
  36. }
  37. //添加&编辑
  38. public function add()
  39. {
  40. $param = get_params();
  41. if (request()->isAjax()) {
  42. if (!empty($param['id']) && $param['id'] > 0) {
  43. if($param['id']==1){
  44. return to_assign(1, '超级管理员不能编辑');
  45. }
  46. try {
  47. validate(PositionCheck::class)->scene('edit')->check($param);
  48. } catch (ValidateException $e) {
  49. // 验证失败 输出错误信息
  50. return to_assign(1, $e->getError());
  51. }
  52. // 启动事务
  53. Db::startTrans();
  54. try {
  55. Db::name('Position')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
  56. Db::name('PositionGroup')->where(['pid' => $param['id']])->delete();
  57. foreach ($param['group_id'] as $k => $v) {
  58. $data[$k] = [
  59. 'pid' => $param['id'],
  60. 'group_id' => $v,
  61. 'create_time' => time(),
  62. ];
  63. }
  64. Db::name('PositionGroup')->strict(false)->field(true)->insertAll($data);
  65. add_log('edit', $param['id'], $param);
  66. //清除菜单\权限缓存
  67. clear_cache('adminMenu');
  68. clear_cache('adminRules');
  69. // 提交事务
  70. Db::commit();
  71. } catch (\Exception $e) {
  72. // 回滚事务
  73. Db::rollback();
  74. return to_assign(1, '提交失败:' . $e->getMessage());
  75. }
  76. } else {
  77. try {
  78. validate(PositionCheck::class)->scene('add')->check($param);
  79. } catch (ValidateException $e) {
  80. // 验证失败 输出错误信息
  81. return to_assign(1, $e->getError());
  82. }
  83. // 启动事务
  84. Db::startTrans();
  85. try {
  86. $uid = Db::name('Position')->strict(false)->field(true)->insertGetId($param);
  87. foreach ($param['group_id'] as $k => $v) {
  88. $data[$k] = [
  89. 'pid' => $uid,
  90. 'group_id' => $v,
  91. 'create_time' => time(),
  92. ];
  93. }
  94. Db::name('PositionGroup')->strict(false)->field(true)->insertAll($data);
  95. add_log('add', $uid, $param);
  96. // 提交事务
  97. Db::commit();
  98. } catch (\Exception $e) {
  99. // 回滚事务
  100. Db::rollback();
  101. return to_assign(1, '提交失败:' . $e->getMessage());
  102. }
  103. }
  104. return to_assign();
  105. }
  106. else{
  107. $id = isset($param['id']) ? $param['id'] : 0;
  108. $group = Db::name('AdminGroup')->order('create_time asc')->select()->toArray();
  109. if ($id > 0) {
  110. $detail = Db::name('Position')->where(['id' => $id])->find();
  111. $detail['group_id'] = Db::name('PositionGroup')->where(['pid' => $id])->column('group_id');
  112. foreach ($group as &$val) {
  113. if (in_array($val['id'], $detail['group_id'])) {
  114. $val['checked'] = 1;
  115. } else {
  116. $val['checked'] = 0;
  117. }
  118. }
  119. View::assign('detail', $detail);
  120. }
  121. View::assign('group', $group);
  122. View::assign('id', $id);
  123. return view();
  124. }
  125. }
  126. //查看
  127. public function view()
  128. {
  129. $id = get_params('id');
  130. $group = Db::name('AdminGroup')->order('create_time asc')->select()->toArray();
  131. $detail = Db::name('Position')->where(['id' => $id])->find();
  132. $detail['group_id'] = Db::name('PositionGroup')->where(['pid' => $id])->column('group_id');
  133. foreach ($group as &$val) {
  134. if (in_array($val['id'], $detail['group_id'])) {
  135. $val['checked'] = 1;
  136. } else {
  137. $val['checked'] = 0;
  138. }
  139. }
  140. $rule = Db::name('AdminRule')->order('sort asc,id asc')->select()->toArray();
  141. $user_groups = Db::name('PositionGroup')
  142. ->alias('a')
  143. ->join("AdminGroup g", "a.group_id=g.id", 'LEFT')
  144. ->where("a.pid='{$id}' and g.status='1'")
  145. ->select()
  146. ->toArray();
  147. $groups = $user_groups ?: [];
  148. $rules = [];
  149. foreach ($groups as $g) {
  150. $rules = array_merge($rules, explode(',', trim($g['rules'], ',')));
  151. }
  152. $rules = array_unique($rules);
  153. $role_rule = create_tree_list(0, $rule, $rules);
  154. View::assign('role_rule', $role_rule);
  155. View::assign('detail', $detail);
  156. View::assign('group', $group);
  157. add_log('view', $id);
  158. return view();
  159. }
  160. //删除
  161. public function delete()
  162. {
  163. $id = get_params("id");
  164. if ($id == 1) {
  165. return to_assign(0, "超级岗位,不能删除");
  166. }
  167. $data['status'] = '-1';
  168. $data['id'] = $id;
  169. $data['update_time'] = time();
  170. if (Db::name('Position')->update($data) !== false) {
  171. add_log('delete', $id);
  172. return to_assign(0, "删除岗位成功");
  173. } else {
  174. return to_assign(1, "删除失败");
  175. }
  176. }
  177. }