SystemRole.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. $res = app()->make(SystemMenusServices::class)->getColumn([['pid', 'in', $data['rules']]], 'id');
  76. $data['rules'] = array_merge($data['rules'], $res);
  77. $data['rules'] = implode(',', $data['rules']);
  78. if ($id) {
  79. if (!$this->services->update($id, $data)) return app('json')->fail(100007);
  80. $this->services->cacheDriver()->clear();
  81. return app('json')->success(100001);
  82. } else {
  83. $data['level'] = $this->adminInfo['level'] + 1;
  84. if (!$this->services->save($data)) return app('json')->fail(400223);
  85. $this->services->cacheDriver()->clear();
  86. return app('json')->success(400222);
  87. }
  88. }
  89. /**
  90. * 显示编辑资源表单页
  91. * @param SystemMenusServices $services
  92. * @param $id
  93. * @return mixed
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public function edit(SystemMenusServices $services, $id)
  99. {
  100. $role = $this->services->get($id);
  101. if (!$role) {
  102. return app('json')->fail(100100);
  103. }
  104. $menus = $services->getMenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles']);
  105. return app('json')->success(['role' => $role->toArray(), 'menus' => $menus]);
  106. }
  107. /**
  108. * 删除指定资源
  109. * @param SystemAdminServices $adminServices
  110. * @param $id
  111. * @return mixed
  112. */
  113. public function delete(SystemAdminServices $adminServices, $id)
  114. {
  115. if ($adminServices->checkRoleUse($id)) {
  116. return app('json')->fail(400754);
  117. }
  118. if (!$this->services->delete($id))
  119. return app('json')->fail(100008);
  120. else {
  121. $this->services->cacheDriver()->clear();
  122. return app('json')->success(100002);
  123. }
  124. }
  125. /**
  126. * 修改状态
  127. * @param $id
  128. * @param $status
  129. * @return mixed
  130. */
  131. public function set_status($id, $status)
  132. {
  133. if (!$id) {
  134. return app('json')->fail(100100);
  135. }
  136. $role = $this->services->get($id);
  137. if (!$role) {
  138. return app('json')->fail(400199);
  139. }
  140. $role->status = $status;
  141. if ($role->save()) {
  142. $this->services->cacheDriver()->clear();
  143. return app('json')->success(100001);
  144. } else {
  145. return app('json')->fail(100007);
  146. }
  147. }
  148. }