From 48ae01d903537bfa667c77076bc4de3d30e8f7ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8A=E5=A4=A9?= <442384644@qq.com> Date: Fri, 16 Oct 2026 15:15:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=94=A8=E6=88=B7=E5=88=97=E8=A1=A8):=20?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E5=88=97=E4=BC=98=E5=85=88=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=94=B6=E8=B4=A7=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 批量取用户默认收货地址按 uid 索引,无默认地址时回退微信省市,避免逐行点查 --- backend/app/dao/user/UserAddressDao.php | 13 +++++++++++++ backend/app/services/user/UserAddressServices.php | 10 ++++++++++ backend/app/services/user/UserServices.php | 7 ++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/backend/app/dao/user/UserAddressDao.php b/backend/app/dao/user/UserAddressDao.php index 3c35d3bbb..49e8887be 100644 --- a/backend/app/dao/user/UserAddressDao.php +++ b/backend/app/dao/user/UserAddressDao.php @@ -46,4 +46,17 @@ class UserAddressDao extends BaseDao { return $this->search($where)->field($field)->page($page, $limit)->order('is_default DESC,id DESC')->select()->toArray(); } + + /** + * 批量获取多个用户的默认地址,按 uid 索引 + * @param array $uids + * @return array + */ + public function getDefaultAddressMapByUids(array $uids): array + { + if (!$uids) { + return []; + } + return $this->getColumn([['uid', 'in', array_unique($uids)], ['is_default', '=', 1], ['is_del', '=', 0]], 'province,city,district,detail', 'uid'); + } } diff --git a/backend/app/services/user/UserAddressServices.php b/backend/app/services/user/UserAddressServices.php index 46af6ab14..bc264724e 100644 --- a/backend/app/services/user/UserAddressServices.php +++ b/backend/app/services/user/UserAddressServices.php @@ -95,6 +95,16 @@ class UserAddressServices extends BaseService return $this->dao->getOne(['uid' => $uid, 'is_default' => 1, 'is_del' => 0], $field); } + /** + * 批量获取多个用户的默认地址,按 uid 索引 + * @param array $uids + * @return array + */ + public function getDefaultAddressMapByUids(array $uids): array + { + return $this->dao->getDefaultAddressMapByUids($uids); + } + /** * 获取条数 * @param array $where diff --git a/backend/app/services/user/UserServices.php b/backend/app/services/user/UserServices.php index 66237e389..e0c50c3db 100644 --- a/backend/app/services/user/UserServices.php +++ b/backend/app/services/user/UserServices.php @@ -571,9 +571,14 @@ class UserServices extends BaseService $levelName = app()->make(SystemUserLevelServices::class)->getUsersLevel(array_unique(array_column($list, 'level'))); $userLevel = app()->make(UserLevelServices::class)->getUsersLevelInfo($uids); $spread_names = $this->dao->getColumn([['uid', 'in', array_unique(array_column($list, 'spread_uid'))]], 'nickname', 'uid'); + $defaultAddressMap = app()->make(UserAddressServices::class)->getDefaultAddressMapByUids($uids); foreach ($list as &$item) { if (empty($item['addres'])) { - if (!empty($item['country']) || !empty($item['province']) || !empty($item['city'])) { + // 优先展示用户默认收货地址,无默认地址时回退微信用户省市 + $defaultAddress = $defaultAddressMap[$item['uid']] ?? null; + if ($defaultAddress) { + $item['addres'] = $defaultAddress['province'] . $defaultAddress['city'] . $defaultAddress['district'] . $defaultAddress['detail']; + } elseif (!empty($item['country']) || !empty($item['province']) || !empty($item['city'])) { $item['addres'] = $item['country'] . $item['province'] . $item['city']; } }