SystemRole.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. *
  36. * @return \think\Response
  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. *
  50. * @return \think\Response
  51. */
  52. public function create(SystemMenusServices $services)
  53. {
  54. $menus = $services->getmenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles']);
  55. return app('json')->success(compact('menus'));
  56. }
  57. /**
  58. * 保存新建的资源
  59. *
  60. * @return \think\Response
  61. */
  62. public function save($id)
  63. {
  64. $data = $this->request->postMore([
  65. 'role_name',
  66. ['status', 0],
  67. ['checked_menus', [], '', 'rules']
  68. ]);
  69. if (!$data['role_name']) return app('json')->fail(400220);
  70. if (!is_array($data['rules']) || !count($data['rules']))
  71. return app('json')->fail(400221);
  72. $data['rules'] = implode(',', $data['rules']);
  73. if ($id) {
  74. if (!$this->services->update($id, $data)) return app('json')->fail(100007);
  75. \think\facade\Cache::clear();
  76. return app('json')->success(100001);
  77. } else {
  78. $data['level'] = $this->adminInfo['level'] + 1;
  79. if (!$this->services->save($data)) return app('json')->fail(400223);
  80. \think\facade\Cache::clear();
  81. return app('json')->success(400222);
  82. }
  83. }
  84. /**
  85. * 显示编辑资源表单页.
  86. *
  87. * @param int $id
  88. * @return \think\Response
  89. */
  90. public function edit(SystemMenusServices $services, $id)
  91. {
  92. $role = $this->services->get($id);
  93. if (!$role) {
  94. return app('json')->fail(100100);
  95. }
  96. $menus = $services->getMenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles']);
  97. return app('json')->success(['role' => $role->toArray(), 'menus' => $menus]);
  98. }
  99. /**
  100. * 删除指定资源
  101. * @param SystemAdminServices $adminServices
  102. * @param $id
  103. * @return mixed
  104. */
  105. public function delete(SystemAdminServices $adminServices, $id)
  106. {
  107. if ($adminServices->checkRoleUse($id)) {
  108. return app('json')->fail(400754);
  109. }
  110. if (!$this->services->delete($id))
  111. return app('json')->fail(100008);
  112. else {
  113. \think\facade\Cache::clear();
  114. return app('json')->success(100002);
  115. }
  116. }
  117. /**
  118. * 修改状态
  119. * @param $id
  120. * @param $status
  121. * @return mixed
  122. */
  123. public function set_status($id, $status)
  124. {
  125. if (!$id) {
  126. return app('json')->fail(100100);
  127. }
  128. $role = $this->services->get($id);
  129. if (!$role) {
  130. return app('json')->fail(400199);
  131. }
  132. $role->status = $status;
  133. if ($role->save()) {
  134. \think\facade\Cache::clear();
  135. return app('json')->success(100001);
  136. } else {
  137. return app('json')->fail(100007);
  138. }
  139. }
  140. }