SystemRole.php 6.7 KB

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