Access.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\admin\model\store;
  3. use app\common\model\store\Access as AccessModel;
  4. /**
  5. * 商家用户权限模型
  6. * Class Access
  7. * @package app\admin\model\store
  8. */
  9. class Access extends AccessModel
  10. {
  11. /**
  12. * 获取权限列表
  13. * @throws \think\Exception
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\ModelNotFoundException
  16. * @throws \think\exception\DbException
  17. */
  18. public function getList()
  19. {
  20. $all = static::getAll();
  21. return $this->formatTreeData($all);
  22. }
  23. /**
  24. * 新增记录
  25. * @param $data
  26. * @return false|int
  27. */
  28. public function add($data)
  29. {
  30. $data['wxapp_id'] = self::$wxapp_id;
  31. return $this->allowField(true)->save($data);
  32. }
  33. /**
  34. * 更新记录
  35. * @param $data
  36. * @return bool
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. * @throws \think\exception\DbException
  40. */
  41. public function edit($data)
  42. {
  43. // 判断上级角色是否为当前子级
  44. if ($data['parent_id'] > 0) {
  45. // 获取所有上级id集
  46. $parentIds = $this->getTopAccessIds($data['parent_id']);
  47. if (in_array($this['access_id'], $parentIds)) {
  48. $this->error = '上级权限不允许设置为当前子权限';
  49. return false;
  50. }
  51. }
  52. return $this->allowField(true)->save($data) !== false;
  53. }
  54. /**
  55. * 删除权限
  56. * @return bool|int
  57. * @throws \think\exception\DbException
  58. */
  59. public function remove()
  60. {
  61. // 判断是否存在下级权限
  62. if (self::detail(['parent_id' => $this['access_id']])) {
  63. $this->error = '当前权限下存在子权限,请先删除';
  64. return false;
  65. }
  66. return $this->delete();
  67. }
  68. /**
  69. * 获取所有上级id集
  70. * @param $access_id
  71. * @param null $all
  72. * @return array
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. * @throws \think\exception\DbException
  76. */
  77. private function getTopAccessIds($access_id, &$all = null)
  78. {
  79. static $ids = [];
  80. is_null($all) && $all = $this->getAll();
  81. foreach ($all as $item) {
  82. if ($item['access_id'] == $access_id && $item['parent_id'] > 0) {
  83. $ids[] = $item['parent_id'];
  84. $this->getTopAccessIds($item['parent_id'], $all);
  85. }
  86. }
  87. return $ids;
  88. }
  89. /**
  90. * 获取权限列表
  91. * @param $all
  92. * @param int $parent_id
  93. * @param int $deep
  94. * @return array
  95. */
  96. private function formatTreeData(&$all, $parent_id = 0, $deep = 1)
  97. {
  98. static $tempTreeArr = [];
  99. foreach ($all as $key => $val) {
  100. if ($val['parent_id'] == $parent_id) {
  101. // 记录深度
  102. $val['deep'] = $deep;
  103. // 根据角色深度处理名称前缀
  104. $val['name_h1'] = $this->htmlPrefix($deep) . $val['name'];
  105. $tempTreeArr[] = $val;
  106. $this->formatTreeData($all, $val['access_id'], $deep + 1);
  107. }
  108. }
  109. return $tempTreeArr;
  110. }
  111. private function htmlPrefix($deep)
  112. {
  113. // 根据角色深度处理名称前缀
  114. $prefix = '';
  115. if ($deep > 1) {
  116. for ($i = 1; $i <= $deep - 1; $i++) {
  117. $prefix .= '&nbsp;&nbsp;&nbsp;├ ';
  118. }
  119. $prefix .= '&nbsp;';
  120. }
  121. return $prefix;
  122. }
  123. }