Role.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\model\store;
  13. use app\common\library\helper;
  14. use app\common\model\store\Role as RoleModel;
  15. use app\store\model\store\RoleMenu as RoleMenuModel;
  16. /**
  17. * 商家用户角色模型
  18. * Class Role
  19. * @package app\store\model\store
  20. */
  21. class Role extends RoleModel
  22. {
  23. /**
  24. * 获取角色列表
  25. * @return array
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\DbException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. */
  30. public function getList()
  31. {
  32. $list = $this->getAll();
  33. return $this->getTreeData($list);
  34. }
  35. /**
  36. * 获取所有角色
  37. * @return \think\Collection
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. private function getAll()
  43. {
  44. // 获取列表数据
  45. $list = $this->addHidden(['roleMenu'])
  46. ->with(['roleMenu'])
  47. ->order(['sort' => 'asc', 'create_time' => 'asc'])
  48. ->select();
  49. // 整理角色的菜单ID集
  50. return $this->getRoleMenuIds($list);
  51. }
  52. /**
  53. * 整理菜单的api ID集
  54. * @param $list
  55. * @return mixed
  56. */
  57. private function getRoleMenuIds($list)
  58. {
  59. foreach ($list as &$item) {
  60. if (!empty($item['roleMenu'])) {
  61. $item['menuIds'] = helper::getArrayColumn($item['roleMenu'], 'menu_id');
  62. }
  63. }
  64. return $list;
  65. }
  66. /**
  67. * 获取树状列表
  68. * @param $list
  69. * @param int $parentId
  70. * @return array
  71. */
  72. private function getTreeData(&$list, int $parentId = 0)
  73. {
  74. $data = [];
  75. foreach ($list as $key => $item) {
  76. if ($item['parent_id'] == $parentId) {
  77. $children = $this->getTreeData($list, $item['role_id']);
  78. !empty($children) && $item['children'] = $children;
  79. $data[] = $item;
  80. unset($list[$key]);
  81. }
  82. }
  83. return $data;
  84. }
  85. /**
  86. * 新增记录
  87. * @param array $data
  88. * @return bool
  89. */
  90. public function add(array $data)
  91. {
  92. if (empty($data['menus'])) {
  93. $this->error = '菜单权限不能为空,请重新选择';
  94. return false;
  95. }
  96. $this->transaction(function () use ($data) {
  97. // 新增角色记录
  98. $data['store_id'] = self::$storeId;
  99. $this->save($data);
  100. // 新增角色菜单关系记录
  101. RoleMenuModel::increased((int)$this['role_id'], $data['menus']);
  102. });
  103. return true;
  104. }
  105. /**
  106. * 更新记录
  107. * @param array $data
  108. * @return bool
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\DbException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. */
  113. public function edit(array $data)
  114. {
  115. if (empty($data['menus'])) {
  116. $this->error = '菜单权限不能为空,请重新选择';
  117. return false;
  118. }
  119. // 判断上级角色是否为当前子级
  120. if ($data['parent_id'] > 0) {
  121. // 获取所有上级id集
  122. $parentIds = $this->getTopRoleIds($data['parent_id']);
  123. if (in_array($this['role_id'], $parentIds)) {
  124. $this->error = '上级角色不允许设置为当前子角色';
  125. return false;
  126. }
  127. }
  128. $this->transaction(function () use ($data) {
  129. // 更新角色记录
  130. $this->allowField(['role_name', 'parent_id', 'sort'])->save($data);
  131. // 更新角色菜单关系记录
  132. RoleMenuModel::updates((int)$this['role_id'], $data['menus']);
  133. });
  134. return true;
  135. }
  136. /**
  137. * 获取所有上级id集
  138. * @param int $roleId
  139. * @param null|array $list
  140. * @return array
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\DbException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. */
  145. private function getTopRoleIds(int $roleId, $list = null)
  146. {
  147. static $parentIds = [];
  148. is_null($list) && $list = $this->getAll();
  149. foreach ($list as $item) {
  150. if ($item['role_id'] == $roleId && $item['parent_id'] > 0) {
  151. $parentIds[] = $item['parent_id'];
  152. $this->getTopRoleIds($item['parent_id'], $list);
  153. }
  154. }
  155. return $parentIds;
  156. }
  157. /**
  158. * 删除记录
  159. * @return bool
  160. * @throws \Exception
  161. */
  162. public function remove()
  163. {
  164. // 判断是否存在下级角色
  165. if (static::detail(['parent_id' => $this['role_id']])) {
  166. $this->error = '当前角色下存在子角色,不允许删除';
  167. return false;
  168. }
  169. // 判断当前角色下存在用户
  170. if (UserRole::isExistsUserByRoleId($this['role_id'])) {
  171. $this->error = '当前角色下存在用户,不允许删除';
  172. return false;
  173. }
  174. // 删除对应的菜单关系
  175. RoleMenuModel::deleteAll(['role_id' => $this['role_id']]);
  176. return $this->delete();
  177. }
  178. }