Role.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\BaseController;
  5. use app\admin\model\AdminGroup;
  6. use app\admin\validate\GroupCheck;
  7. use think\exception\ValidateException;
  8. use think\facade\Db;
  9. use think\facade\View;
  10. use app\admin\model\Department as DepartmentModel;
  11. class Role extends BaseController
  12. {
  13. protected $login_admin;
  14. public function initialize(){
  15. parent::initialize();
  16. $this->login_admin = get_login_admin();
  17. }
  18. public function index()
  19. {
  20. if (request()->isAjax()) {
  21. $param = get_params();
  22. $where = array();
  23. if (!empty($param['keywords'])) {
  24. $where[] = ['id|title|desc', 'like', '%' . $param['keywords'] . '%'];
  25. }
  26. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  27. $unit_name = $this->login_admin['unit_name'];
  28. $permission = $this->login_admin['permission'];
  29. $group_id = Db::name('AdminGroupAccess')->where('uid', $this->login_admin['id'])->value('group_id');
  30. $where_unit = array();
  31. $whereor = array();
  32. $whereor_ = array();
  33. if($permission != 1){
  34. $where_unit[] = [
  35. ['unit_name', '=', $unit_name],
  36. ];
  37. }else{
  38. if(get_login_admin('user_type') == -1){
  39. $whereor[] = [
  40. ['pid', '=', $group_id],
  41. ];
  42. }
  43. }
  44. $group = AdminGroup::with(['department','admin'])
  45. ->where($where)
  46. ->where($whereor)
  47. ->where($where_unit)
  48. ->order('create_time asc')
  49. ->paginate($rows, false, ['query' => $param])->toArray();
  50. // $group_mine = Db::name('AdminGroup')->where('id', $group_id)->select()->toArray();
  51. // dump($group);
  52. if(get_login_admin('user_type') == -1){
  53. $group_ = AdminGroup::with(['department','admin'])
  54. ->where('id', $group_id)
  55. ->find()
  56. ->toArray();
  57. $group_n[] = $group_;
  58. $group['data'] = array_merge($group_n, $group['data']);
  59. }
  60. // $group = array_merge($group_mine, $group);
  61. // halt($group);
  62. return table_assign(0, '', $group);
  63. } else {
  64. return view();
  65. }
  66. }
  67. //添加&编辑
  68. public function add()
  69. {
  70. $param = get_params();
  71. // halt($param);
  72. if (request()->isAjax()) {
  73. $ruleData = isset($param['rule']) ? $param['rule'] : 0;
  74. $param['rules'] = implode(',', $ruleData);
  75. $param['operator'] = get_login_admin('nickname');
  76. if (!empty($param['id']) && $param['id'] > 0) { //编辑
  77. // halt($param);
  78. if(get_login_admin('permission') == 0 || get_login_admin('user_type') == -1){
  79. $login_group_id = Db::name('AdminGroupAccess')->where('uid', get_login_admin('id'))->value('group_id');
  80. if($login_group_id == $param['id']){
  81. return to_assign(0, '不可修改自身权限');
  82. }
  83. }
  84. try {
  85. validate(GroupCheck::class)->scene('edit')->check($param);
  86. } catch (ValidateException $e) {
  87. // 验证失败 输出错误信息
  88. return to_assign(1, $e->getError());
  89. }
  90. //为了系统安全id为1的系统所有者管理组不允许修改
  91. if ($param['id'] == 1) {
  92. return to_assign(1, '为了系统安全,该管理组不允许修改');
  93. }
  94. Db::name('AdminGroup')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
  95. $field_id = $param['field_id'];
  96. Db::name('FieldRules')->where('id', $field_id)->update(['group_id' => $param['id']]);
  97. add_log('edit', $param['id'], $param);
  98. } else { //添加
  99. try {
  100. validate(GroupCheck::class)->scene('add')->check($param);
  101. } catch (ValidateException $e) {
  102. // 验证失败 输出错误信息
  103. return to_assign(1, $e->getError());
  104. }
  105. $group_id = Db::name('AdminGroupAccess')->where('uid', get_login_admin('id'))->value('group_id');
  106. if($this->login_admin['permission'] == 1){ //系统方添加
  107. if($param['unit_name'] != 0){ //添加单位权限组
  108. if($this->login_admin['user_type'] == -1){
  109. $param['pid'] = $group_id;
  110. }else{
  111. $param['pid'] = 0;
  112. }
  113. }else{ //添加系统权限组
  114. $param['pid'] = $group_id;
  115. }
  116. }else{
  117. $param['unit_name'] = $this->login_admin['unit_name'];
  118. $param['pid'] = $group_id;
  119. }
  120. // halt($param);
  121. $gid = Db::name('AdminGroup')->strict(false)->field(true)->insertGetId($param);
  122. $field_id = $param['field_id'];
  123. if($field_id != 0){
  124. Db::name('FieldRules')->where('id', $field_id)->update(['group_id' => $gid]);
  125. }else{
  126. $field_rules = '{"see_auth":"1","project_status":"1","project_num":"1","project_name":"1","project_time":"1","entrust_unit":"1","entrust_head":"1","entrust_phone":"1","review_unit":"1","review_head":"1","review_head_phone":"1","sent_review_unit":"1","sent_review_head":"1","sent_review_phone":"1","construction_unit":"1","construction_head":"1","construction_phone":"1","project_scale":"1","project_region":"1","engineering_category":"1","engineering_type":"1","fiscal_nature":"1","sent_review_cost":"1","preparation_amount":"1","sent_review_amount":"1","sent_review_approver_amount":"1","construction_approver_amount":"1","authorize_amount":"1","review_add_reduce_amount":"1","review_add_reduce_rate":"1","report_time":"1","charge_standard":"1","invoicing__amount":"1","invoicing__num":"1","operate_name":"1","operate_team_names":"1","project_dead_time":"1","create_time":"1"}';
  127. $field_rules_edit = '{"project_status_edit":"1","project_num_edit":"1","project_name_edit":"1","project_time_edit":"1","entrust_unit_edit":"1","entrust_head_edit":"1","entrust_phone_edit":"1","review_unit_edit":"1","review_head_edit":"1","review_head_phone_edit":"1","sent_review_unit_edit":"1","sent_review_head_edit":"1","sent_review_phone_edit":"1","construction_unit_edit":"1","construction_head_edit":"1","construction_phone_edit":"1","project_scale_edit":"1","project_region_edit":"1","engineering_category_edit":"1","engineering_type_edit":"1","fiscal_nature_edit":"1","sent_review_cost_edit":"1","preparation_amount_edit":"1","sent_review_amount_edit":"1","sent_review_approver_amount_edit":"1","construction_approver_amount_edit":"1","authorize_amount_edit":"1","review_add_reduce_amount_edit":"1","review_add_reduce_rate_edit":"1","report_time_edit":"1","charge_standard_edit":"1","invoicing__amount_edit":"1","invoicing__num_edit":"1","operate_name_edit":"1","operate_team_names_edit":"1","project_dead_time_edit":"1","create_time_edit":"1"}';
  128. $data = [
  129. 'group_id' => $gid,
  130. 'field_rules' => $field_rules,
  131. 'field_edit_rules' => $field_rules_edit,
  132. 'operator' => get_login_admin('nickname'),
  133. ];
  134. $field_id = Db::name('FieldRules')->strict(false)->field(true)->insertGetId($data);
  135. }
  136. add_log('add', $gid, $param);
  137. }
  138. //清除菜单\权限缓存
  139. clear_cache('adminMenu');
  140. return to_assign();
  141. } else {
  142. $id = isset($param['id']) ? $param['id'] : 0; //id为编辑的权限组的表id
  143. $rule = get_admin_rule();
  144. $group_id = Db::name('admin_group_access')->where('uid', get_login_admin('id'))->value('group_id');
  145. $rules = get_admin_group_info($group_id); //登录账号的权限组
  146. $role_rule = create_tree_list(0, $rule, $rules);
  147. $role_rule = self::remove_checked_false($role_rule);
  148. if ($id > 0) { //编辑
  149. if($group_id == $id){
  150. // return to_assign(0, '不可修改自身权限');
  151. if(get_login_admin('user_type') != -2){
  152. echo '<div style="text-align:center;color:red;margin-top:20%;">不可修改自身权限!</div>';
  153. exit;
  154. }
  155. }
  156. $pid = Db::name('AdminGroup')->where('id', $group_id)->value('pid');
  157. if(get_login_admin('user_type' != -2)){
  158. if($pid == Db::name('AdminGroup')->where('id', $id)->value('pid')){
  159. echo '<div style="text-align:center;color:red;margin-top:20%;">不可修改同级权限组!</div>';
  160. exit;
  161. }
  162. }
  163. while($pid != 0){
  164. if($id == $pid){
  165. // return to_assign(0, '不可修改上级权限');
  166. echo '<div style="text-align:center;color:red;margin-top:20%;">不可修改上级权限!</div>';
  167. exit;
  168. // break;
  169. }
  170. $pid = Db::name('AdminGroup')->where('id', $pid)->value('pid');
  171. }
  172. // halt("@@@@");
  173. $rules = get_admin_group_info($id); //编辑的权限组全部权限
  174. $edit_role_rule = create_tree_list(0, $rule, $rules);
  175. $edit_role_rule = self::remove_checked_false($edit_role_rule);
  176. $login_role_rule = $role_rule;
  177. // dump($edit_role_rule);
  178. $role_rule = self::diff_role_rules($login_role_rule, $edit_role_rule);
  179. $role = Db::name('AdminGroup')->where(['id' => $id])->find();
  180. View::assign('role', $role);
  181. } else { //新增
  182. // $group_id = Db::name('admin_group_access')->where('uid', get_login_admin('id'))->value('group_id');
  183. // $rules = get_admin_group_info($group_id); //登录账号的权限组
  184. // $role_rule = create_tree_list(0, $rule, $rules);
  185. // $role_rule = self::remove_checked_false($role_rule);
  186. }
  187. if($this->login_admin['permission'] == 1){
  188. $department = Db::name('department')->where('pid', 0)->field('id,title')->select()->toArray();
  189. $system = [
  190. 'id' => 0,
  191. 'title' => '系统方',
  192. ];
  193. $department[count($department)] = $system;
  194. View::assign('department', $department);
  195. }
  196. $field_id = Db::name('field_rules')->where('group_id', $id)->value('id');
  197. // dump($field_id);
  198. view::assign('field_id', $field_id);
  199. // dump($role_rule);
  200. if(get_login_admin('permission') == 1 || get_login_admin('user_type') == -1){
  201. for($i = 0; $i < count($role_rule); $i++){
  202. // dump($role_rule[$i]['name']);
  203. if($role_rule[$i]['name'] == '公司项目管理'){
  204. $role_rule[$i]['title'] = '项目管理--公司';
  205. }else if($role_rule[$i]['name'] == '财政局项目管理'){
  206. $role_rule[$i]['title'] = '项目管理--财政局';
  207. }else if($role_rule[$i]['name'] == '业主项目管理'){
  208. $role_rule[$i]['title'] = '项目管理--业主';
  209. }
  210. // dump($role_rule[$i]['title']);
  211. if($role_rule[$i]['name'] == '项目看板'){
  212. for($j = 0; $j < count($role_rule[$i]['children']); $j++){
  213. if($role_rule[$i]['children'][$j]['name'] == '财政局项目看板'){
  214. $role_rule[$i]['children'][$j]['title'] = '项目看板--财政局';
  215. }else if($role_rule[$i]['children'][$j]['name'] == '公司项目看板'){
  216. $role_rule[$i]['children'][$j]['title'] = '项目看板--公司';
  217. }
  218. }
  219. }
  220. if($role_rule[$i]['name'] == '请款详情'){
  221. for($j = 0; $j < count($role_rule[$i]['children']); $j++){
  222. if($role_rule[$i]['children'][$j]['name'] == '财政局请款详情'){
  223. $role_rule[$i]['children'][$j]['title'] = '请款详情--财政局';
  224. }else if($role_rule[$i]['children'][$j]['name'] == '公司请款详情'){
  225. $role_rule[$i]['children'][$j]['title'] = '请款详情--公司';
  226. }
  227. }
  228. }
  229. }
  230. }
  231. // dump($role_rule);
  232. View::assign('permission', $this->login_admin['permission']);
  233. View::assign('role_rule', $role_rule);
  234. View::assign('id', $id);
  235. return view();
  236. }
  237. }
  238. //删除
  239. public function delete()
  240. {
  241. $id = get_params("id");
  242. if ($id == 1) {
  243. return to_assign(1, "该组是系统所有者,无法删除");
  244. }
  245. if (Db::name('AdminGroup')->delete($id) !== false) {
  246. add_log('delete', $id, []);
  247. return to_assign(0, "删除角色成功");
  248. } else {
  249. return to_assign(1, "删除失败");
  250. }
  251. }
  252. // 消除checked为false的权限
  253. public function remove_checked_false($role_rule){
  254. foreach($role_rule as $key => $value){
  255. if($role_rule[$key]['checked'] == false){
  256. unset($role_rule[$key]);
  257. continue;
  258. }
  259. // 只能用 $role_rule[$key]['children'] 不能用 $value['children]
  260. if(isset($role_rule[$key]['children'])){
  261. $role_rule[$key]['children'] = self::remove_checked_false($role_rule[$key]['children']);
  262. }
  263. }
  264. return $role_rule;
  265. }
  266. // 修改checked的值 true => false
  267. public function change_checked($arr){
  268. foreach($arr as $key => $value){
  269. $arr[$key]['checked'] = false;
  270. if(isset($arr[$key]['children'])){
  271. $arr[$key]['children'] = self::change_checked($arr[$key]['children']);
  272. }
  273. }
  274. return $arr;
  275. }
  276. // 对比两个权限组
  277. public function diff_role_rules($login_rules, $edit_rules){
  278. // dump($login_rules);
  279. // dump($edit_rules);
  280. $data_diff = array_diff_key($login_rules, $edit_rules);
  281. $data_diff = self::change_checked($data_diff);
  282. $login_rules = array_diff_key($login_rules, $data_diff);
  283. // dump($login_rules);
  284. try{
  285. foreach($edit_rules as $key => $value){
  286. if(isset($edit_rules[$key]['children'])){
  287. $edit_rules[$key]['children'] = self::diff_role_rules($login_rules[$key]['children'], $edit_rules[$key]['children']);
  288. }else if(isset($login_rules[$key]['children'])){
  289. $edit_rules[$key]['children'] = self::change_checked($login_rules[$key]['children']);
  290. }
  291. }
  292. }catch(\Exception $e){
  293. echo '<div style="text-align:center;color:red;margin-top:20%;">权限选择有误,没有相关权限!</div>';
  294. exit;
  295. }
  296. $edit_rules = $edit_rules + $data_diff;
  297. ksort($edit_rules);
  298. return $edit_rules;
  299. }
  300. }