SystemRole.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\setting;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\system\admin\SystemAdminServices;
  14. use app\services\system\admin\SystemRoleServices;
  15. use app\services\system\SystemMenusServices;
  16. use crmeb\services\CacheService;
  17. use think\facade\App;
  18. /**
  19. * Class SystemRole
  20. * @package app\adminapi\controller\v1\setting
  21. */
  22. class SystemRole extends AuthController
  23. {
  24. /**
  25. * SystemRole constructor.
  26. * @param App $app
  27. * @param SystemRoleServices $services
  28. */
  29. public function __construct(App $app, SystemRoleServices $services)
  30. {
  31. parent::__construct($app);
  32. $this->services = $services;
  33. }
  34. /**
  35. * 显示资源列表
  36. * @return mixed
  37. */
  38. public function index()
  39. {
  40. $where = $this->request->getMore([
  41. ['status', ''],
  42. ['role_name', ''],
  43. ]);
  44. $where['level'] = $this->adminInfo['level'] + 1;
  45. return app('json')->success($this->services->getRoleList($where));
  46. }
  47. /**
  48. * 显示创建资源表单页
  49. * @param SystemMenusServices $services
  50. * @return mixed
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function create(SystemMenusServices $services)
  56. {
  57. $menus = $services->getmenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles']);
  58. return app('json')->success(compact('menus'));
  59. }
  60. /**
  61. * 保存新建的资源
  62. *
  63. * @return \think\Response
  64. */
  65. public function save($id)
  66. {
  67. $data = $this->request->postMore([
  68. 'role_name',
  69. ['status', 0],
  70. ['checked_menus', [], '', 'rules']
  71. ]);
  72. if (!$data['role_name']) return app('json')->fail(400220);
  73. if (!is_array($data['rules']) || !count($data['rules']))
  74. return app('json')->fail(400221);
  75. $data['rules'] = implode(',', $data['rules']);
  76. if ($id) {
  77. if (!$this->services->update($id, $data)) return app('json')->fail(100007);
  78. CacheService::clear();
  79. return app('json')->success(100001);
  80. } else {
  81. $data['level'] = $this->adminInfo['level'] + 1;
  82. if (!$this->services->save($data)) return app('json')->fail(400223);
  83. CacheService::clear();
  84. return app('json')->success(400222);
  85. }
  86. }
  87. /**
  88. * 显示编辑资源表单页
  89. * @param SystemMenusServices $services
  90. * @param $id
  91. * @return mixed
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public function edit(SystemMenusServices $services, $id)
  97. {
  98. $role = $this->services->get($id);
  99. if (!$role) {
  100. return app('json')->fail(100100);
  101. }
  102. $menus = $services->getMenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles'], explode(',', $role['rules']));
  103. return app('json')->success(['role' => $role->toArray(), 'menus' => $menus]);
  104. }
  105. /**
  106. * 删除指定资源
  107. * @param SystemAdminServices $adminServices
  108. * @param $id
  109. * @return mixed
  110. */
  111. public function delete(SystemAdminServices $adminServices, $id)
  112. {
  113. if ($adminServices->checkRoleUse($id)) {
  114. return app('json')->fail(400754);
  115. }
  116. if (!$this->services->delete($id))
  117. return app('json')->fail(100008);
  118. else {
  119. CacheService::clear();
  120. return app('json')->success(100002);
  121. }
  122. }
  123. /**
  124. * 修改状态
  125. * @param $id
  126. * @param $status
  127. * @return mixed
  128. */
  129. public function set_status($id, $status)
  130. {
  131. if (!$id) {
  132. return app('json')->fail(100100);
  133. }
  134. $role = $this->services->get($id);
  135. if (!$role) {
  136. return app('json')->fail(400199);
  137. }
  138. $role->status = $status;
  139. if ($role->save()) {
  140. CacheService::clear();
  141. return app('json')->success(100001);
  142. } else {
  143. return app('json')->fail(100007);
  144. }
  145. }
  146. }