SystemRole.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\v1\system;
  12. use app\controller\admin\AuthController;
  13. use app\services\system\SystemMenusServices;
  14. use app\services\system\SystemRoleServices;
  15. use think\facade\App;
  16. /**
  17. * Class SystemRole
  18. * @package app\controller\admin\v1\setting
  19. */
  20. class SystemRole extends AuthController
  21. {
  22. /**
  23. * SystemRole constructor.
  24. * @param App $app
  25. * @param SystemRoleServices $services
  26. */
  27. public function __construct(App $app, SystemRoleServices $services)
  28. {
  29. parent::__construct($app);
  30. $this->services = $services;
  31. }
  32. /**
  33. * 显示资源列表
  34. *
  35. * @return \think\Response
  36. */
  37. public function index()
  38. {
  39. $where = $this->request->getMore([
  40. ['status', ''],
  41. ['role_name', ''],
  42. ]);
  43. $where['type'] = 0;
  44. $where['level'] = $this->adminInfo['level'] + 1;
  45. return $this->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'], 1, 0);
  55. return $this->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 $this->fail('请输入身份名称');
  70. if (!is_array($data['rules']) || !count($data['rules']))
  71. return $this->fail('请选择最少一个权限');
  72. $data['rules'] = implode(',', $data['rules']);
  73. if ($id) {
  74. if (!$this->services->update($id, $data)) return $this->fail('修改失败!');
  75. \crmeb\services\CacheService::clear();
  76. return $this->success('修改成功!');
  77. } else {
  78. $data['level'] = $this->adminInfo['level'] + 1;
  79. if (!$this->services->save($data)) return $this->fail('添加身份失败!');
  80. \crmeb\services\CacheService::clear();
  81. return $this->success('添加身份成功!');
  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 $this->fail('修改的角色不存在');
  95. }
  96. $menus = $services->getMenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles'], 1, 0);
  97. return $this->success(['role' => $role->toArray(), 'menus' => $menus]);
  98. }
  99. /**
  100. * 删除指定资源
  101. *
  102. * @param int $id
  103. * @return \think\Response
  104. */
  105. public function delete($id)
  106. {
  107. if (!$this->services->delete($id))
  108. return $this->fail('删除失败,请稍候再试!');
  109. else {
  110. \crmeb\services\CacheService::clear();
  111. return $this->success('删除成功!');
  112. }
  113. }
  114. /**
  115. * 修改状态
  116. * @param $id
  117. * @param $status
  118. * @return mixed
  119. */
  120. public function set_status($id, $status)
  121. {
  122. if (!$id) {
  123. return $this->fail('缺少参数');
  124. }
  125. $role = $this->services->get($id);
  126. if (!$role) {
  127. return $this->fail('没有查到此身份');
  128. }
  129. $role->status = $status;
  130. if ($role->save()) {
  131. \crmeb\services\CacheService::clear();
  132. return $this->success('修改成功');
  133. } else {
  134. return $this->fail('修改失败');
  135. }
  136. }
  137. }