SupplierAccess.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\admin\model;
  3. use app\common\model\supplier\Access as AccessModel;
  4. /**
  5. * Class Access
  6. * 商家用户权限模型
  7. * @package app\admin\model
  8. */
  9. class SupplierAccess extends AccessModel
  10. {
  11. /**
  12. * 获取权限列表
  13. */
  14. public function getList()
  15. {
  16. $all = static::getAll(-1);
  17. $res = $this->recursiveMenuArray($all, 0);
  18. return array_values($this->foo($res));
  19. }
  20. /**
  21. * 新增记录
  22. */
  23. public function add($data)
  24. {
  25. // 校验路径
  26. if(!$this->validate($data)){
  27. return false;
  28. }
  29. $data['access_id'] = time();
  30. $data['app_id'] = self::$app_id;
  31. return $this->save($data);
  32. }
  33. /**
  34. * 更新记录
  35. */
  36. public function edit($data)
  37. {
  38. if ($data['access_id'] == $data['parent_id']) {
  39. $this->error = '上级菜单不允许设置为当前菜单';
  40. return false;
  41. }
  42. // 判断上级角色是否为当前子级
  43. if ($data['parent_id'] > 0) {
  44. // 获取所有上级id集
  45. $parentIds = $this->getTopAccessIds($data['parent_id']);
  46. if (in_array($data['access_id'], $parentIds)) {
  47. $this->error = '上级菜单不允许设置为当前子菜单';
  48. return false;
  49. }
  50. }
  51. // 校验路径,不限制大小写
  52. if(strtolower($data['path']) !== strtolower($this['path'])){
  53. if(!$this->validate($data)){
  54. return false;
  55. }
  56. }
  57. $data['redirect_name'] = ($data['is_route'] == 1) ? $data['redirect_name'] : '';
  58. $data['icon'] = ($data['parent_id'] == 0) ? $data['icon'] : '';
  59. return $this->save($data);
  60. }
  61. /**
  62. * 验证
  63. */
  64. private function validate($data){
  65. $count = $this->where(['path' => $data['path']])->count();
  66. if($count > 0){
  67. $this->error = '路径已存在,请重新更改';
  68. return false;
  69. }
  70. return true;
  71. }
  72. public function getChildCount($where)
  73. {
  74. return $this->where($where)->count();
  75. }
  76. /**
  77. * 删除权限
  78. */
  79. public function remove($access_id)
  80. {
  81. return $this->where('access_id', '=', $access_id)->delete();
  82. }
  83. /**
  84. * 删除插件
  85. */
  86. public function removePlus()
  87. {
  88. return $this->save([
  89. 'plus_category_id' => 0
  90. ]);
  91. }
  92. /**
  93. * 获取所有上级id集
  94. */
  95. public function getTopAccessIds($access_id, &$all = null)
  96. {
  97. static $ids = [];
  98. is_null($all) && $all = $this->getAll();
  99. foreach ($all as $item) {
  100. if ($item['access_id'] == $access_id && $item['parent_id'] > 0) {
  101. $ids[] = $item['parent_id'];
  102. $this->getTopAccessIds($item['parent_id'], $all);
  103. }
  104. }
  105. return $ids;
  106. }
  107. /**
  108. * 递归获取获取分类
  109. */
  110. public function recursiveMenuArray($data, $pid)
  111. {
  112. $re_data = [];
  113. foreach ($data as $key => $value) {
  114. if ($value['parent_id'] == $pid) {
  115. $re_data[$value['access_id']] = $value;
  116. $re_data[$value['access_id']]['children'] = $this->recursiveMenuArray($data, $value['access_id']);
  117. } else {
  118. continue;
  119. }
  120. }
  121. return $re_data;
  122. }
  123. /**
  124. * 格式化递归数组下标
  125. */
  126. public function foo(&$ar)
  127. {
  128. if (!is_array($ar)) return;
  129. foreach ($ar as $k => &$v) {
  130. if (is_array($v)) $this->foo($v);
  131. if ($k == 'children') $v = array_values($v);
  132. }
  133. return $ar;
  134. }
  135. /**
  136. * 更改显示状态
  137. */
  138. public function status($status){
  139. return $this->save([
  140. 'is_show' => $status
  141. ]);
  142. }
  143. /**
  144. * 获取所有插件
  145. */
  146. public static function getAllPlus(){
  147. $model = new static();
  148. $plus = $model->where('path', '=', '/plus/plus/index')->find();
  149. return $model->where('parent_id', '=', $plus['access_id'])
  150. ->where('plus_category_id', '=', 0)
  151. ->select();
  152. }
  153. /**
  154. * 保存插件分类
  155. * @param $data
  156. */
  157. public function addPlus($data){
  158. $model = new self();
  159. return $model->where('access_id', '=', $data['access_id'])->save([
  160. 'plus_category_id' => $data['plus_category_id']
  161. ]);
  162. }
  163. }