RoleServer.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace app\admin\server\role;
  3. use app\admin\model\SystemRole;
  4. use app\admin\model\SystemMenu;
  5. use support\Redis;
  6. class RoleServer
  7. {
  8. /**
  9. * Notes:获取角色列表
  10. * @param string $keywords
  11. * @param int $page
  12. * @param int $limit
  13. * @return array
  14. * User: ZQ
  15. * Date: 2022/9/13
  16. */
  17. public static function getRoleList(int $page, int $limit, string $keywords)
  18. {
  19. [$list, $count] = SystemRole::getRoleList($page, $limit, $keywords);
  20. $menus = SystemMenu::getMenus(0);
  21. if (!empty($list) && $menus){
  22. for ($i=0;$i<count($menus);$i++){
  23. if ($menus[$i]['menu_is_show'] == 1){
  24. $menus[$i]['menu_is_show'] = true;
  25. }else{
  26. $menus[$i]['menu_is_show'] = false;
  27. }
  28. $menus[$i]['children'] = SystemMenu::getMenus($menus[$i]['menu_id']);
  29. if (!empty($menus[$i]['children'])){
  30. for ($s=0;$s<count($menus[$i]['children']);$s++){
  31. if ($menus[$i]['children'][$s]['menu_is_show'] == 1){
  32. $menus[$i]['children'][$s]['menu_is_show'] = true;
  33. }else{
  34. $menus[$i]['children'][$s]['menu_is_show'] = false;
  35. }
  36. $menus[$i]['children'][$s]['children'] = SystemMenu::getMenus($menus[$i]['children'][$s]['menu_id']);
  37. if (!empty($$menus[$i]['children'][$s]['children'])){
  38. for ($j=0;$j<count($menus[$i]['children'][$s]['children']);$j++){
  39. if ($menus[$i]['children'][$s]['children'][$j]['menu_is_show'] == 1){
  40. $menus[$i]['children'][$s]['children'][$j]['menu_is_show'] = true;
  41. }else{
  42. $menus[$i]['children'][$s]['children'][$j]['menu_is_show'] = false;
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. foreach ($list as $k => $v){
  50. if (!empty($v['role_rules'])){
  51. // $list[$k]['menus'] = $menus;
  52. $rules = explode(',',$v['role_rules']);
  53. $mation = SystemMenu::menusMation($rules);
  54. $name = [];
  55. for ($z=0;$z<count($mation);$z++){
  56. $name[] = $mation[$z]['menu_name'];
  57. }
  58. $list[$k]['menus'] = implode(',',$name);
  59. // for ($a=0;$a<count($list[$k]['menus']);$a++){
  60. // if (!in_array($list[$k]['menus'][$a]['menu_id'],$rules)){
  61. // unset($list[$k]['menus'][$a]);
  62. // continue;
  63. // }else{
  64. // for ($d=0;$d<count($list[$k]['menus'][$a]['children']);$d++){
  65. // if (!in_array($list[$k]['menus'][$a]['children'][$d]['menu_id'],$rules)){
  66. // unset($list[$k]['menus'][$a]['children'][$d]);
  67. // continue;
  68. // }else{
  69. // for ($f=0;$f<count($list[$k]['menus'][$a]['children'][$d]['children']);$f++){
  70. // if (!in_array($list[$k]['menus'][$a]['children'][$d]['children'][$f]['menu_id'],$rules)){
  71. // unset($list[$k]['menus'][$a]['children'][$d]['children'][$f]);
  72. // continue;
  73. // }
  74. // }
  75. // }
  76. // }
  77. // }
  78. // }
  79. }else{
  80. $list[$k]['menus'] = '';
  81. }
  82. }
  83. //$list = array_merge($list);
  84. }
  85. return compact('list', 'page', 'limit', 'count');
  86. }
  87. /**
  88. * Notes:获取所有角色
  89. * @return array
  90. * User: ZQ
  91. * Date: 2022/9/19
  92. */
  93. public static function getRoleAll()
  94. {
  95. return SystemRole::getRoleAll();
  96. }
  97. /**
  98. * Notes:修改角色
  99. * @param string $role_name
  100. * @param int $role_id
  101. * @param int $role_rules
  102. * @return int
  103. * User: ZQ
  104. * Date: 2022/9/3
  105. */
  106. public static function updateRole($role_id, $role_name, $role_rules, $role_info, $role_is_show)
  107. {
  108. SystemRole::affairBegin();
  109. try {
  110. $where = [];
  111. $where['role_id'] = $role_id;
  112. $data = [];
  113. $data['role_name'] = $role_name;
  114. if (!empty($role_rules)){
  115. $data['role_rules'] = implode(',',$role_rules);
  116. }
  117. if (!empty($role_info)){
  118. $data['role_info'] = $role_info;
  119. }
  120. if (!empty($role_is_show)){
  121. $data['role_is_show'] = $role_is_show;
  122. }
  123. $result = SystemRole::where($where)->update($data);
  124. if ($result !== false){
  125. Redis::del('roles_' . $role_id);
  126. SystemRole::affairCommit();
  127. return true;
  128. }
  129. throw new \Exception('操作失败!');
  130. }catch (\Exception $exception){
  131. SystemRole::affairRollback();
  132. throw new \Exception($exception->getMessage(), 500);
  133. }
  134. }
  135. /**
  136. * Notes:删除角色
  137. * @param int $role_id
  138. * @return int
  139. * User: ZQ
  140. * Date: 2022/9/13
  141. */
  142. public static function delRole($role_id)
  143. {
  144. SystemRole::affairBegin();
  145. try {
  146. $where = [];
  147. $where['role_id'] = $role_id;
  148. $data['role_status'] = 2;
  149. $result = SystemRole::where($where)->update($data);
  150. if (!empty($result)){
  151. Redis::del('roles_' . $role_id);
  152. SystemRole::affairCommit();
  153. return true;
  154. }else{
  155. return false;
  156. }
  157. }catch (\Exception $exception){
  158. SystemRole::affairRollback();
  159. throw new \Exception($exception->getMessage(), 500);
  160. }
  161. }
  162. /**
  163. * Notes: 添加角色
  164. * @param string $role_name
  165. * @param array $role_rules
  166. * @return int
  167. * User: ZQ
  168. * Date: 2022/9/13
  169. */
  170. public static function insertRole(string $role_name, $role_rules, $role_info, $role_is_show)
  171. {
  172. SystemRole::affairBegin();
  173. try {
  174. $data = [];
  175. $data['role_name'] = $role_name;
  176. if (!empty($role_rules)){
  177. $data['role_rules'] = implode(',',$role_rules);
  178. }else{
  179. $data['role_rules'] = '';
  180. }
  181. $data['role_is_show'] = $role_is_show;
  182. $data['role_info'] = $role_info;
  183. $result = SystemRole::insertGetId($data);
  184. if (!empty($result)){
  185. SystemRole::affairCommit();
  186. return $result;
  187. }
  188. throw new \Exception('操作失败!');
  189. }catch (\Exception $exception){
  190. SystemRole::affairRollback();
  191. throw new \Exception($exception->getMessage(), 500);
  192. }
  193. }
  194. /**
  195. * Notes:查询角色
  196. * @param int $role_id
  197. * @return int
  198. * User: ZQ
  199. * Date: 2022/9/13
  200. */
  201. public static function roleInfo($role_id)
  202. {
  203. $where = [];
  204. $where['role_id'] = $role_id;
  205. $result = SystemRole::where($where)->first();
  206. if (!empty($result)){
  207. $result['menu_is_menu'] = (string)$result['menu_is_menu'];
  208. if (!empty($result['role_rules'])){
  209. $result['role_rules'] = explode(',',$result['role_rules']);
  210. }else{
  211. $result['role_rules'] = [];
  212. }
  213. }
  214. return $result;
  215. }
  216. /**
  217. * Notes:修改角色
  218. * @param string $role_name
  219. * @param int $role_is_show
  220. * @return int
  221. * User: ZQ
  222. * Date: 2022/9/15
  223. */
  224. public static function updateStatus($role_id, $role_is_show)
  225. {
  226. SystemRole::affairBegin();
  227. try {
  228. $where = [];
  229. $where['role_id'] = $role_id;
  230. $data = [];
  231. $data['role_is_show'] = $role_is_show;
  232. $result = SystemRole::where($where)->update($data);
  233. if ($result !== false){
  234. SystemRole::affairCommit();
  235. return true;
  236. }
  237. throw new \Exception('操作失败!');
  238. }catch (\Exception $exception){
  239. SystemRole::affairRollback();
  240. throw new \Exception($exception->getMessage(), 500);
  241. }
  242. }
  243. }