SystemRole.php 4.8 KB

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