Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SystemRole.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\controller\store\system;
  3. use app\controller\store\AuthController;
  4. use app\services\store\SystemStoreStaffServices;
  5. use app\services\system\SystemRoleServices;
  6. use app\services\system\SystemMenusServices;
  7. use think\facade\App;
  8. /**
  9. * Class SystemRole
  10. * @package app\controller\store\system
  11. */
  12. class SystemRole extends AuthController
  13. {
  14. /**
  15. * 管理员|员工
  16. * @var null
  17. */
  18. protected $storeStaffServices = null;
  19. /**
  20. * SystemRole constructor.
  21. * @param App $app
  22. * @param SystemRoleServices $services
  23. */
  24. public function __construct(App $app, SystemRoleServices $services, SystemStoreStaffServices $storeStaffServices)
  25. {
  26. parent::__construct($app);
  27. $this->services = $services;
  28. $this->storeStaffServices = $storeStaffServices;
  29. }
  30. /**
  31. * 显示资源列表
  32. *
  33. * @return \think\Response
  34. */
  35. public function index()
  36. {
  37. $where = $this->request->getMore([
  38. ['status', ''],
  39. ['role_name', ''],
  40. ]);
  41. $where['type'] = $this->type;
  42. $where['store_id'] = $this->storeId;
  43. $where['level'] = $this->storeStaffInfo['level'] + 1;
  44. $result = $this->services->getRoleList($where);
  45. if (isset($result['list']) && $list = $result['list']) {
  46. foreach ($list as &$item) {
  47. $item['count'] = $this->storeStaffServices->count(['is_del' => 0, 'roles' => $item['id'], 'is_admin' => 1]);
  48. }
  49. $result['list'] = $list;
  50. }
  51. return app('json')->success($result);
  52. }
  53. /**
  54. * 显示创建资源表单页.
  55. *
  56. * @return \think\Response
  57. */
  58. public function create(SystemMenusServices $services)
  59. {
  60. $menus = $services->getmenus($this->storeStaffInfo['level'] == 0 ? [] : $this->storeStaffInfo['roles']);
  61. return app('json')->success(compact('menus'));
  62. }
  63. /**
  64. * 保存新建的资源
  65. *
  66. * @return \think\Response
  67. */
  68. public function save($id)
  69. {
  70. $data = $this->request->postMore([
  71. 'role_name',
  72. ['status', 0],
  73. ['checked_menus', [], '', 'rules']
  74. ]);
  75. if (!$data['role_name']) return app('json')->fail('请输入身份名称');
  76. if (!is_array($data['rules']) || !count($data['rules']))
  77. return app('json')->fail('请选择最少一个权限');
  78. $data['rules'] = implode(',', $data['rules']);
  79. $data['type'] = $this->type;
  80. $data['store_id'] = $this->storeId;
  81. if ($id) {
  82. $id = (int)$id;
  83. $role = $this->services->get($id);
  84. if (!$role) return app('json')->fail('角色不存在!');
  85. if (!$this->services->update($id, $data)) return app('json')->fail('修改失败!');
  86. //更改角色下店员状态
  87. $this->services->setStaffStatus((int)$role['store_id'], $id, $data['status']);
  88. \crmeb\services\CacheService::clear();
  89. return app('json')->success('修改成功!');
  90. } else {
  91. $data['level'] = $this->storeStaffInfo['level'] + 1;
  92. if (!$this->services->save($data)) return app('json')->fail('添加身份失败!');
  93. \crmeb\services\CacheService::clear();
  94. return app('json')->success('添加身份成功!');
  95. }
  96. }
  97. /**
  98. * 显示编辑资源表单页.
  99. *
  100. * @param int $id
  101. * @return \think\Response
  102. */
  103. public function edit(SystemMenusServices $services, $id)
  104. {
  105. $role = $this->services->get($id);
  106. if (!$role) {
  107. return app('json')->fail('修改的角色不存在');
  108. }
  109. $role = $role->toArray();
  110. if ($role['rules']) {
  111. $role['rules'] = is_string($role['rules']) ? explode(',', $role['rules']) : $role['rules'];
  112. foreach ($role['rules'] as &$item) {
  113. $item = (int)$item;
  114. }
  115. }
  116. /** @var SystemMenusServices $systemMenusServices */
  117. $systemMenusServices = app()->make(SystemMenusServices::class);
  118. return app('json')->success(['role' => $role, 'menus' => $systemMenusServices->getList(['type' => 2])]);
  119. }
  120. /**
  121. * 删除指定资源
  122. *
  123. * @param int $id
  124. * @return \think\Response
  125. */
  126. public function delete($id)
  127. {
  128. $role = $this->services->get($id);
  129. if (!$role) {
  130. return app('json')->fail('没有查到此身份');
  131. }
  132. if ($this->storeStaffServices->count(['is_del' => 0, 'roles' => $id, 'is_admin' => 1])){
  133. return app('json')->fail('该角色存在管理员,请先删除管理员');
  134. }
  135. if (!$this->services->delete($id))
  136. return app('json')->fail('删除失败,请稍候再试!');
  137. else {
  138. //更改角色下店员状态
  139. $this->services->setStaffStatus((int)$role['store_id'], (int)$id, 0);
  140. \crmeb\services\CacheService::clear();
  141. return app('json')->success('删除成功!');
  142. }
  143. }
  144. /**
  145. * 修改状态
  146. * @param $id
  147. * @param $status
  148. * @return mixed
  149. */
  150. public function set_status($id, $status)
  151. {
  152. if (!$id) {
  153. return app('json')->fail('缺少参数');
  154. }
  155. $role = $this->services->get($id);
  156. if (!$role) {
  157. return app('json')->fail('没有查到此身份');
  158. }
  159. $role->status = $status;
  160. if ($role->save()) {
  161. //更改角色下店员状态
  162. $this->services->setStaffStatus((int)$role['store_id'], (int)$id, $status);
  163. \crmeb\services\CacheService::clear();
  164. return app('json')->success('修改成功');
  165. } else {
  166. return app('json')->fail('修改失败');
  167. }
  168. }
  169. }