FengR 1 день назад
Родитель
Сommit
1b83782bfb
3 измененных файлов с 47 добавлено и 0 удалено
  1. 20 0
      app/JsonRpc/WebService.php
  2. 5 0
      app/JsonRpc/WebServiceInterface.php
  3. 22 0
      app/Tools/Result.php

+ 20 - 0
app/JsonRpc/WebService.php

@@ -3800,4 +3800,24 @@ class WebService implements WebServiceInterface
     ];
     return Result::success($result);
   }
+  /**
+   * @param array $data
+   * @return array
+   */
+  /**
+   * 获取省份城市列表
+   * @param array $data
+   * @return array
+   */
+  public function getWebsiteProvinceCity(array $data): array
+  {
+    $province_city = District::whereIn('level', [1,2])
+      ->select('id', 'name', 'pid', 'abbreviation')
+      ->get()->all();
+    if (empty($province_city)) {
+      return Result::error("暂无相关省份城市信息", 0);
+    }
+    $result = Result::buildCityTree($province_city);
+    return Result::success($result);
+  }
 }

+ 5 - 0
app/JsonRpc/WebServiceInterface.php

@@ -211,4 +211,9 @@ interface WebServiceInterface
      * @return array
      */
     public function getWebsiteMessage(array $data): array;
+    /**
+     * @param array $data
+     * @return array
+     */
+    public function getWebsiteProvinceCity(array $data): array;
 }

+ 22 - 0
app/Tools/Result.php

@@ -45,4 +45,26 @@ class Result
         }
         return $tree;
     }
+    /**
+     * 递归查询
+     * @param $menuItems
+     * @param $parentId
+     * @return array
+     */
+    public static  function buildCityTree($menuItems, $parentId = 0) {
+        $tree = [];
+        foreach ($menuItems as $item) {
+            if ($item['pid'] == $parentId) {
+                // 找到子菜单
+                $children = self::buildCityTree($menuItems, $item['id']);
+                // 如果子菜单存在,则添加到当前菜单的children中
+                if ($children) {
+                    $item['children'] = $children;
+                }
+                // 将当前菜单添加到树中
+                $tree[] = $item;
+            }
+        }
+        return $tree;
+    }
 }