SystemRole.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\adminapi\controller\v1\setting;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\system\admin\SystemRoleServices;
  14. use app\services\system\SystemMenusServices;
  15. use think\facade\App;
  16. /**
  17. * Class SystemRole
  18. * @package app\adminapi\controller\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['level'] = $this->adminInfo['level'] + 1;
  44. return app('json')->success($this->services->getRoleList($where));
  45. }
  46. /**
  47. * 显示创建资源表单页.
  48. *
  49. * @return \think\Response
  50. */
  51. public function create(SystemMenusServices $services)
  52. {
  53. $menus = $services->getmenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles']);
  54. return app('json')->success(compact('menus'));
  55. }
  56. /**
  57. * 保存新建的资源
  58. *
  59. * @return \think\Response
  60. */
  61. public function save($id)
  62. {
  63. $data = $this->request->postMore([
  64. 'role_name',
  65. ['status', 0],
  66. ['checked_menus', [], '', 'rules']
  67. ]);
  68. if (!$data['role_name']) return app('json')->fail('请输入身份名称');
  69. if (!is_array($data['rules']) || !count($data['rules']))
  70. return app('json')->fail('请选择最少一个权限');
  71. $data['rules'] = implode(',', $data['rules']);
  72. if ($id) {
  73. if (!$this->services->update($id, $data)) return app('json')->fail('修改失败!');
  74. \think\facade\Cache::clear();
  75. return app('json')->success('修改成功!');
  76. } else {
  77. $data['level'] = $this->adminInfo['level'] + 1;
  78. if (!$this->services->save($data)) return app('json')->fail('添加身份失败!');
  79. \think\facade\Cache::clear();
  80. return app('json')->success('添加身份成功!');
  81. }
  82. }
  83. /**
  84. * 显示编辑资源表单页.
  85. *
  86. * @param int $id
  87. * @return \think\Response
  88. */
  89. public function edit(SystemMenusServices $services, $id)
  90. {
  91. $role = $this->services->get($id);
  92. if (!$role) {
  93. return app('json')->fail('修改的规格不存在');
  94. }
  95. $menus = $services->getMenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles']);
  96. return app('json')->success(['role' => $role->toArray(), 'menus' => $menus]);
  97. }
  98. /**
  99. * 删除指定资源
  100. *
  101. * @param int $id
  102. * @return \think\Response
  103. */
  104. public function delete($id)
  105. {
  106. if (!$this->services->delete($id))
  107. return app('json')->fail('删除失败,请稍候再试!');
  108. else {
  109. \think\facade\Cache::clear();
  110. return app('json')->success('删除成功!');
  111. }
  112. }
  113. /**
  114. * 修改状态
  115. * @param $id
  116. * @param $status
  117. * @return mixed
  118. */
  119. public function set_status($id, $status)
  120. {
  121. if (!$id) {
  122. return app('json')->fail('缺少参数');
  123. }
  124. $role = $this->services->get($id);
  125. if (!$role) {
  126. return app('json')->fail('没有查到此身份');
  127. }
  128. $role->status = $status;
  129. if ($role->save()) {
  130. \think\facade\Cache::clear();
  131. return app('json')->success('修改成功');
  132. } else {
  133. return app('json')->fail('修改失败');
  134. }
  135. }
  136. }