OrgRole.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace app\admin\controller\base\org;
  3. /**
  4. * @title: 组织角色
  5. */
  6. use think\facade\Db;
  7. use app\admin\controller\Base;
  8. use app\common\model\base\org\OrgRole as OrgRoleModel;
  9. use app\common\model\base\org\Org as OrgModel;
  10. use app\admin\validate\base\org\OrgRole as OrgRoleValid;
  11. use app\common\model\base\user\UserRole;
  12. use app\common\model\base\menu\Menu;
  13. use app\common\model\base\menu\Menurequest;
  14. use app\common\model\base\menu\Frontmenus;
  15. class OrgRole extends Base
  16. {
  17. protected $OrgRoleModel = null;
  18. protected $orgModel = null;
  19. protected $userModel = null;
  20. protected $noNeedLogin = ['test'];
  21. public function initialize()
  22. {
  23. parent::initialize();
  24. $this->OrgRoleModel = new OrgRoleModel;
  25. $this->orgModel = new OrgModel;
  26. }
  27. /**
  28. * @title: 统一创建查询条件
  29. * @desc:
  30. * @param {*}
  31. * @return {*}
  32. * @Author: Rock
  33. * @Date: 2022-03-30 14:38:10
  34. * @LastEditTime: Do not edit
  35. */
  36. private function createWhere()
  37. {
  38. $data = $this->request->param();
  39. $where = [];
  40. if (!empty($data['keyword'])) {
  41. $keyword = $data['keyword'];
  42. $where[] = ['name|code|desc', 'LIKE', "%$keyword%"];
  43. }
  44. if (!empty($data['depart_id'])) {
  45. $where[] = ['depart_id', '=', $data['depart_id']];
  46. }
  47. $org_id = !empty($data['org_id']) ? $data['org_id'] : (!$this->userinfo['is_developer'] ? $this->userinfo['org_id'] : 0);
  48. if (!empty($org_id)) {
  49. if (!empty($data['child'])) {
  50. $ids = OrgModel::getChildrenIds($org_id, true);
  51. $where[] = ['org_id', 'IN', $ids];
  52. } else {
  53. $where[] = ['org_id', '=', $org_id];
  54. }
  55. }
  56. if (!$this->userinfo['is_developer']) {
  57. $where[] = ['code', 'not in', ['DEVELOPER', 'SUPERADMIN']];
  58. }
  59. if (!empty($data['status'])) {
  60. $where[] = ['status', '=', $data['status']];
  61. }
  62. return $where;
  63. }
  64. /**
  65. * @title: 组织角色列表
  66. * @param {int} {pageNo} {非必填,默认值为0} {页码,不传或传0则不分页}
  67. * @param {int} {pageSize} {非必填,默认值为10} {每页数量}
  68. * @param {int} {org_id} {非必填,自动获取} {组织id}
  69. * @param {int} {child} {非必填,0} {是否包含下级组织}
  70. * @param {int} {depart_id} {非必填} {部门id}
  71. * @param {string} {name} {非必填} {名称搜索}
  72. * @return array
  73. * @Author: wangkewei
  74. * @Date: 2021/5/18 9:57
  75. */
  76. public function getList($pageNo = 0, $pageSize = 10)
  77. {
  78. $where = $this->createWhere();
  79. if (empty($pageNo)) {
  80. $list = $this->OrgRoleModel->where($where)->with(['org'])->select();
  81. return res(1, "获取成功", $list, $where);
  82. } else {
  83. $list = $this->OrgRoleModel->where($where)->with(['org'])->paginate(['page' => $pageNo, 'list_rows' => $pageSize]);
  84. $list = FieldConverList($list);
  85. return pageRes(1, "获取成功", $list['total'], $list['data'], $where);
  86. }
  87. }
  88. /**
  89. * @title: 组织角色编辑
  90. * @param array
  91. * @return array
  92. * @Author: wangkewei
  93. * @Date: 2021/5/18 10:00
  94. */
  95. public function doEdit()
  96. {
  97. $data = $this->request->param();
  98. $check = validate(OrgRoleValid::class)->check($data);
  99. if (true !== $check) {
  100. return Res(2, $check);
  101. }
  102. // 管理员只允许有一个
  103. if ($data['code'] == 'ADMIN') {
  104. $where = [];
  105. if (!empty($data['depart_id'])) {
  106. $where[] = ['depart_id', '=', $data['depart_id']];
  107. } else {
  108. $where[] = ['org_id', '=', $data['org_id']];
  109. }
  110. $hasAdmin = $this->OrgRoleModel->where($where)->where('code', $data['code'])->find();
  111. if ($hasAdmin && !$hasAdmin->isEmpty() && !isset($data['role_id'])) {
  112. return res(2, "只允许存在一个管理员");
  113. }
  114. }
  115. $data['status'] = !empty($data['status']) ? $data['status'] : 1;
  116. $res = $this->OrgRoleModel->replace()->save($data);
  117. $role_id = $this->OrgRoleModel->role_id;
  118. $info = $this->OrgRoleModel->where('role_id', $role_id)->with(['org'])->find();
  119. slog(1, "编辑了" . $info->org->name . "的角色" . $info->name);
  120. return res(1, "保存成功");
  121. }
  122. /**
  123. * @title: 组织角色删除
  124. * @param {string} {ids} {必填} {多个id}
  125. * @return array
  126. * @Author: wangkewei
  127. * @Date: 2021/5/18 10:00
  128. */
  129. public function doDelete($ids = [])
  130. {
  131. if (empty($ids)) return res(2, '参数错误');
  132. if (is_string($ids)) {
  133. $ids = explode(',', $ids);
  134. } elseif (is_int($ids)) {
  135. $ids = [$ids];
  136. }
  137. $users = UserRole::where("role_id", "IN", $ids)->count();
  138. if (!empty($users)) {
  139. return res(2, "角色已有用户绑定,无法删除");
  140. }
  141. $this->OrgRoleModel->destroy(function ($query) use ($ids) {
  142. $query->where('role_id', 'IN', $ids);
  143. });
  144. slog(1, "删除了组织角色");
  145. return res(1, "删除成功");
  146. }
  147. /**
  148. * @title: 变更状态
  149. * @return array
  150. * @Author: wangkewei
  151. * @Date: 2021/5/18 10:01
  152. */
  153. public function changeStatus($ids = [], $status = 0)
  154. {
  155. if (empty($ids)) return res(2, "参数错误");
  156. if (is_string($ids)) {
  157. $ids = explode(',', $ids);
  158. } elseif (is_int($ids)) {
  159. $ids = [$ids];
  160. }
  161. // 变更角色状态
  162. $where = [];
  163. $where[] = ['role_id', 'IN', $ids];
  164. if (empty($status)) {
  165. $this->OrgRoleModel->where($where)->update(['status' => Db::raw('ABS(3 * `status` - 5)')]);
  166. } else {
  167. $this->OrgRoleModel->where($where)->update(['status' => $status]);
  168. }
  169. // 获取变更为禁用状态的角色ids
  170. $role_ids = $this->OrgRoleModel->where($where)->where('status', 2)->column('role_id');
  171. slog(1, "批量改变了角色状态");
  172. return res(1, "操作成功");
  173. }
  174. /**
  175. * @title: 获取角色已有的权限
  176. * @desc: 描述
  177. * @param {int} {role_id} {} {角色ID}
  178. * @return {*}
  179. * @author: Rock
  180. * @method: POST
  181. * @Date: 2023-02-06 17:46:53
  182. */
  183. public function getAuthList(int $role_id = 0)
  184. {
  185. try {
  186. $roleInfo = OrgRoleModel::find($role_id);
  187. if (empty($roleInfo) || $roleInfo->isEmpty()) {
  188. return error('未找到此角色');
  189. }
  190. $menu_ids = is_string($roleInfo->menu_ids) ? explode(',', $roleInfo->menu_ids) : $roleInfo->menu_ids;
  191. $request_ids = is_string($roleInfo->request_ids) ? explode(',', $roleInfo->request_ids) : $roleInfo->request_ids;
  192. $front_ids = is_string($roleInfo->front_ids) ? explode(',', $roleInfo->front_ids) : $roleInfo->front_ids;
  193. // 此角色的所有菜单权限
  194. $allMenuList = Menu::where('menu_id', 'IN', $menu_ids)->with(['menurequest'])->select()->toArray();
  195. $menuAuthList = [];
  196. foreach ($allMenuList as $menu) {
  197. $MenuItemRequestIds = array_column($menu['menurequest'], 'menu_request_id');
  198. $selected = array_intersect($request_ids, $MenuItemRequestIds);
  199. // 当所有请求权限都勾选了,菜单才会被选中
  200. if (count($selected) == count($MenuItemRequestIds)) {
  201. $menuAuthList[] = $menu['menu_id'];
  202. }
  203. }
  204. $menuAuthList = !empty($menuAuthList) ? array_filter($menuAuthList) : [];
  205. $request_ids = !empty($request_ids) ? array_filter($request_ids) : [];
  206. $front_ids = !empty($front_ids) ? array_filter($front_ids) : [];
  207. $request_ids = array_map('intval', $request_ids);
  208. $front_ids = array_map('intval', $front_ids);
  209. $menuAuthList = array_map('intval', $menuAuthList);
  210. return res(1, '获取成功', ['menuAuthList' => $menuAuthList, 'requestAuthList' => $request_ids, 'frontAuthList' => $front_ids]);
  211. } catch (\Exception $e) {
  212. return res(2, '获取失败', $e->getMessage(), $e->getTrace());
  213. }
  214. }
  215. /**
  216. * @title: 获取角色所在组织的管理员角色权限(没有管理员则获取组织的权限)
  217. * @desc: 描述
  218. * @param {int} {role_id} {} {角色ID}
  219. * @return {*}
  220. * @author: Rock
  221. * @method: POST
  222. * @Date: 2023-02-06 17:50:56
  223. */
  224. public function getAdminAuth(int $role_id = 0)
  225. {
  226. if ($this->userinfo['is_developer']) {
  227. $menuList = Menu::where('hidden',2)->with(['menurequest'])->order('menu_id asc')->select()->toArray();
  228. $menuList = array2tree($menuList, 'pid', 'menu_id');
  229. $frontList = Frontmenus::select();
  230. } else {
  231. //当前角色
  232. $roleInfo = OrgRoleModel::find($role_id);
  233. $where = [];
  234. $where[] = ['code', '=', 'ADMIN'];
  235. if ($roleInfo->depart_id) {
  236. $where[] = ['depart_id', '=', $roleInfo->depart_id];
  237. } else {
  238. $where[] = ['org_id', '=', $roleInfo->org_id];
  239. }
  240. // 管理员角色
  241. $adminInfo = OrgRoleModel::where($where)->find();
  242. if ($roleInfo->code == 'ADMIN' || empty($adminInfo) || $adminInfo->isEmpty()) {
  243. $orgInfo = OrgModel::where('org_id', $roleInfo->org_id)->find();
  244. $menu_ids = $orgInfo->menu_ids;
  245. $request_ids = $orgInfo->request_ids;
  246. $front_ids = $orgInfo->front_ids;
  247. } else {
  248. $menu_ids = $adminInfo->menu_ids;
  249. $request_ids = $adminInfo->request_ids;
  250. $front_ids = $adminInfo->front_ids;
  251. }
  252. $parentList = Menu::where('menu_id', 'IN', $menu_ids)->column('parent_path');
  253. $parentIds = explode(',', implode(',', $parentList));
  254. $menuList = Menu::where('menu_id', 'IN', $menu_ids)->whereOr('menu_id', 'IN', $parentIds)->with(['menurequest' => function ($query) use ($request_ids) {
  255. $query->where('menu_request_id', 'IN', $request_ids);
  256. }])->order('menu_id asc')->select()->toArray();
  257. $menuList = array2tree($menuList, 'pid', 'menu_id');
  258. $frontList = Frontmenus::where('menu_id', 'IN', $front_ids)->select();
  259. }
  260. return res(1, "获取成功", ['menuList' => $menuList, 'frontList' => $frontList]);
  261. }
  262. /**
  263. * @title: 设置角色权限
  264. * @desc: 描述
  265. * @param {int} {role_id} {} {角色ID}
  266. * @param {array} {menuAuthList} {} {菜单权限}
  267. * @param {array} {requestAuthList} {} {请求权限}
  268. * @param {array} {frontAuthList} {} {移动端权限}
  269. * @return {*}
  270. * @author: Rock
  271. * @method: POST
  272. * @Date: 2023-02-02 17:00:27
  273. */
  274. public function setAuth(int $role_id = 0, array $menuAuthList = [], array $requestAuthList = [], array $frontAuthList = [])
  275. {
  276. $menuAuthList = array_unique(array_filter($menuAuthList));
  277. $requestAuthList = array_unique(array_filter($requestAuthList));
  278. $frontAuthList = array_unique(array_filter($frontAuthList));
  279. try {
  280. // 事务开始
  281. OrgRoleModel::startTrans();
  282. $roleInfo = OrgRoleModel::find($role_id);
  283. $requestMenu = Menurequest::where('menu_request_id', 'IN', $requestAuthList)->column('menu_id');
  284. $menuAuthList = array_unique(array_merge($menuAuthList, $requestMenu));
  285. // 更新类型权限
  286. $roleInfo->menu_ids = $menuAuthList;//菜单权限
  287. $roleInfo->request_ids = $requestAuthList;//请求权限
  288. $roleInfo->front_ids = $frontAuthList;//小程序权限
  289. $roleInfo->save();
  290. OrgRoleModel::commit();
  291. return res(1, '保存成功');
  292. } catch (\Exception $e) {
  293. OrgRoleModel::rollback();
  294. return res(2, '保存失败', $e->getMessage(), $e->getTrace());
  295. }
  296. }
  297. }