소스 검색

fix(user): 修复用户地址区域查询逻辑

- 查询城市时增加省份ID作为父ID条件,避免跨省匹配错误
- 查询区县时增加城市ID作为父ID条件,确保层级准确性
- 查询街道时增加区县ID作为父ID条件,防止数据错乱
- 为街道字段添加空值保护,避免未定义变量错误
shichen 2 달 전
부모
커밋
63cc2dd398
1개의 변경된 파일10개의 추가작업 그리고 7개의 파일을 삭제
  1. 10 7
      app/common/repositories/user/UserAddressRepository.php

+ 10 - 7
app/common/repositories/user/UserAddressRepository.php

@@ -241,35 +241,38 @@ class UserAddressRepository extends BaseRepository
241 241
         }
242 242
         $data['province_id'] = $provinceId;
243 243
 
244
-        // 查询城市 id (level = 2),未查到返回 0
245
-        if (!empty($data['city'])) {
244
+        // 查询城市 id (level = 2),带上 province_id 作为 pid,未查到返回 0
245
+        if (!empty($data['city']) && $provinceId > 0) {
246 246
             $city = \think\facade\Db::name('regions')
247 247
                 ->whereLike('name', '%' . $data['city'] . '%')
248 248
                 ->where('level', 2)
249
+                ->where('pid', $provinceId)
249 250
                 ->find();
250 251
             $cityId = $city ? (int)$city['id'] : 0;
251 252
         }
252 253
         $data['city_id'] = $cityId;
253 254
 
254
-        // 查询区/县 id (level = 3),未查到返回 0
255
-        if (!empty($data['district'])) {
255
+        // 查询区/县 id (level = 3),带上 city_id 作为 pid,未查到返回 0
256
+        if (!empty($data['district']) && $cityId > 0) {
256 257
             $district = \think\facade\Db::name('regions')
257 258
                 ->whereLike('name', '%' . $data['district'] . '%')
258 259
                 ->where('level', 3)
260
+                ->where('pid', $cityId)
259 261
                 ->find();
260 262
             $districtId = $district ? (int)$district['id'] : 0;
261 263
         }
262 264
         $data['district_id'] = $districtId;
263 265
 
264
-        // 查询街道/乡镇 id (level = 4),未查到返回 0
265
-        if (!empty($data['street'])) {
266
+        // 查询街道/乡镇 id (level = 4),带上 district_id 作为 pid,未查到返回 0
267
+        if (!empty($data['street']) && $districtId > 0) {
266 268
             $street = \think\facade\Db::name('regions')
267 269
                 ->whereLike('name', '%' . $data['street'] . '%')
268 270
                 ->where('level', 4)
271
+                ->where('pid', $districtId)
269 272
                 ->find();
270 273
             $streetId = $street ? (int)$street['id'] : 0;
271 274
         }
272
-        $data['village'] = $data['street'];
275
+        $data['village'] = $data['street'] ?? '';
273 276
         $data['village_id'] = $streetId;
274 277
 
275 278
         // 拼接 code_list,格式:province_id,city_id,district_id,street_id