diff --git a/app/controller/merchant/store/shipping/City.php b/app/controller/merchant/store/shipping/City.php index c93f6f38..800ff4c7 100644 --- a/app/controller/merchant/store/shipping/City.php +++ b/app/controller/merchant/store/shipping/City.php @@ -59,115 +59,121 @@ class City extends BaseController $pinyin = $this->request->param('pinyin') ?? 0; $list = app()->make(CityAreaRepository::class)->getGeoChildren(['province_code' => $provinceCode, 'city_code' => $cityCode, 'area_code' => $areaCode, 'street_code' => $streetCode]); $geoList = []; + $pyList = []; foreach ($list as $v) { + $temp = []; if (!empty($v['village_id'])) { - $temp = [ - 'type' => 'village', - 'id' => $v['village_id'], - 'level' => 5, - 'name' => $v['village_name'] ?? '', - 'code' => $v['village_code'] ?? '' - ]; if ($pinyin == 1) { - $temp = [ - 'pinyin' => strtoupper((new Pinyin)->abbr($v['village_name'])[0] ?? '-'), - 'data' => [ - 'type' => 'village', - 'id' => $v['village_id'], - 'level' => 5, - 'name' => $v['village_name'] ?? '', - 'code' => $v['village_code'] ?? '' - ] + $py = strtoupper((new Pinyin)->abbr($v['village_name'])[0] ?? '-'); + $pyList[$py][] = [ + 'type' => 'village', + 'id' => $v['village_id'], + 'level' => 5, + 'name' => $v['village_name'] ?? '', + 'code' => $v['village_code'] ?? '' ]; + } else { + $temp = [ + 'type' => 'village', + 'id' => $v['village_id'], + 'level' => 5, + 'name' => $v['village_name'] ?? '', + 'code' => $v['village_code'] ?? '' + ]; + $geoList[] = $temp; } - $geoList[] = $temp; } if (!empty($v['street_id'])) { - $temp = [ - 'type' => 'street', - 'id' => $v['street_id'], - 'level' => 4, - 'name' => $v['street_name'] ?? '', - 'code' => $v['street_code'] ?? '' - ]; if ($pinyin == 1) { - $temp = [ - 'pinyin' => strtoupper((new Pinyin)->abbr($v['street_name'])[0] ?? '-'), - 'data' => [ - 'type' => 'street', - 'id' => $v['street_id'], - 'level' => 4, - 'name' => $v['street_name'] ?? '', - 'code' => $v['street_code'] ?? '' - ] + $py = strtoupper((new Pinyin)->abbr($v['street_name'])[0] ?? '-'); + $pyList[$py][] = [ + 'type' => 'street', + 'id' => $v['street_id'], + 'level' => 4, + 'name' => $v['street_name'] ?? '', + 'code' => $v['street_code'] ?? '' ]; + } else { + $temp = [ + 'type' => 'street', + 'id' => $v['street_id'], + 'level' => 4, + 'name' => $v['street_name'] ?? '', + 'code' => $v['street_code'] ?? '' + ]; + $geoList[] = $temp; } - $geoList[] = $temp; } if (!empty($v['area_id'])) { - $temp = [ - 'type' => 'area', - 'id' => $v['area_id'], - 'level' => 3, - 'name' => $v['area_name'] ?? '', - 'code' => $v['area_code'] ?? '' - ]; if ($pinyin == 1) { - $temp = [ - 'pinyin' => strtoupper((new Pinyin)->abbr($v['area_name'])[0] ?? '-'), - 'data' => [ - 'type' => 'area', - 'id' => $v['area_id'], - 'level' => 3, - 'name' => $v['area_name'] ?? '', - 'code' => $v['area_code'] ?? '' - ] + $py = strtoupper((new Pinyin)->abbr($v['area_name'])[0] ?? '-'); + $pyList[$py][] = [ + 'type' => 'area', + 'id' => $v['area_id'], + 'level' => 3, + 'name' => $v['area_name'] ?? '', + 'code' => $v['area_code'] ?? '' ]; + } else { + $temp = [ + 'type' => 'area', + 'id' => $v['area_id'], + 'level' => 3, + 'name' => $v['area_name'] ?? '', + 'code' => $v['area_code'] ?? '' + ]; + $geoList[] = $temp; } - $geoList[] = $temp; } if (!empty($v['city_id'])) { - $temp = [ - 'type' => 'city', - 'id' => $v['city_id'], - 'level' => 2, - 'name' => $v['city_name'] ?? '', - 'code' => $v['city_code'] ?? '', - ]; if ($pinyin == 1) { - $temp = [ - 'pinyin' => strtoupper((new Pinyin)->abbr($v['city_name'])[0] ?? '-'), - 'data' => [ - 'type' => 'city', - 'id' => $v['city_id'], - 'level' => 2, - 'name' => $v['city_name'] ?? '', - 'code' => $v['city_code'] ?? '' - ] + $py = strtoupper((new Pinyin)->abbr($v['city_name'])[0] ?? '-'); + $pyList[$py][] = [ + 'type' => 'city', + 'id' => $v['city_id'], + 'level' => 2, + 'name' => $v['city_name'] ?? '', + 'code' => $v['city_code'] ?? '' ]; + } else { + $temp = [ + 'type' => 'city', + 'id' => $v['city_id'], + 'level' => 2, + 'name' => $v['city_name'] ?? '', + 'code' => $v['city_code'] ?? '', + ]; + $geoList[] = $temp; } - $geoList[] = $temp; } if (!empty($v['province_id'])) { - $temp = [ - 'type' => 'province', - 'id' => $v['province_id'], - 'level' => 1, - 'name' => $v['province_name'] ?? '', - 'code' => $v['province_code'] ?? '' - ]; if ($pinyin == 1) { - $temp = [ - 'pinyin' => strtoupper((new Pinyin)->abbr($v['province_name'])[0] ?? '-'), - 'data' => [ - 'type' => 'province', - 'id' => $v['province_id'], - 'level' => 1, - 'name' => $v['province_name'] ?? '', - 'code' => $v['province_code'] ?? '' - ] + $py = strtoupper((new Pinyin)->abbr($v['province_name'])[0] ?? '-'); + $pyList[$py][] = [ + 'type' => 'province', + 'id' => $v['province_id'], + 'level' => 1, + 'name' => $v['province_name'] ?? '', + 'code' => $v['province_code'] ?? '' ]; + } else { + $temp = [ + 'type' => 'province', + 'id' => $v['province_id'], + 'level' => 1, + 'name' => $v['province_name'] ?? '', + 'code' => $v['province_code'] ?? '' + ]; + $geoList[] = $temp; } + } + } + if ($pinyin == 1) { + $geoList = []; + foreach($pyList as $k=>$v) { + $temp = []; + $temp['pinyin'] = $k; + $temp['data'] = $pyList[$k]; $geoList[] = $temp; } }