Răsfoiți Sursa

修改共享门店位置

付晓文。 1 lună în urmă
părinte
comite
3fefa6d0ee
3 a modificat fișierele cu 130 adăugiri și 18 ștergeri
  1. 65 4
      api/memberPrepaid.js
  2. 32 7
      pages/topUp/shareStore.vue
  3. 33 7
      pages/topUp/shareStoreAdd.vue

+ 65 - 4
api/memberPrepaid.js

@@ -9,6 +9,51 @@ export function getMerchantId() {
 	return info.merchantId || ''
 }
 
+/** 获取本商户经纬度(共享门店列表按门店坐标算距离) */
+export function getMerchantLocation(merchantId = getMerchantId()) {
+	if (!merchantId) {
+		return Promise.reject(new Error('未获取到门店信息'))
+	}
+	return http
+		.get('/merchant/localMerchantInfo/queryById', {
+			params: { id: merchantId }
+		})
+		.then((res) => {
+			if (res.data.code !== 200 || !res.data.result) {
+				return Promise.reject(new Error(res.data.message || '获取门店信息失败'))
+			}
+			const { longitude, latitude } = res.data.result
+			if (
+				longitude === undefined ||
+				longitude === null ||
+				longitude === '' ||
+				latitude === undefined ||
+				latitude === null ||
+				latitude === ''
+			) {
+				return Promise.reject(new Error('门店未配置经纬度'))
+			}
+			return {
+				longitude: Number(longitude),
+				latitude: Number(latitude)
+			}
+		})
+}
+
+/** 格式化门店距离展示,如 746.00m / 3.24km */
+export function formatStoreDistance(item = {}) {
+	if (item.distance == null || item.distance === '') {
+		return item.distanceText || ''
+	}
+	const num = Number(item.distance)
+	if (Number.isNaN(num)) {
+		return String(item.distanceText || item.distance)
+	}
+	const text = Number.isInteger(num) ? String(num) : num.toFixed(2)
+	const unit = item.distanceUnit || ''
+	return unit ? `${text}${unit}` : text
+}
+
 /**
  * auditStatus: 0未申请 / 1待审核 / 2已通过 / 3已驳回
  * operationStatus: 0关闭 / 1开启
@@ -55,17 +100,33 @@ const api = {
 		return http.get(`${BASE}/store/current`, { params: { merchantId } })
 	},
 
-	/** 可添加共享门店分页 */
+	/**
+	 * 可添加共享门店分页
+	 * 必传 longitude / latitude,按距离由近到远
+	 */
 	getShareableStores(params = {}) {
 		return http.get(`${BASE}/store/shareable/page`, {
-			params: { merchantId: getMerchantId(), pageNo: 1, pageSize: 10, ...params }
+			params: {
+				merchantId: getMerchantId(),
+				pageNo: 1,
+				pageSize: 10,
+				...params
+			}
 		})
 	},
 
-	/** 已共享门店分页 */
+	/**
+	 * 已共享门店分页
+	 * 必传 longitude / latitude,按距离由近到远
+	 */
 	getSharedStores(params = {}) {
 		return http.get(`${BASE}/store/shared/page`, {
-			params: { merchantId: getMerchantId(), pageNo: 1, pageSize: 10, ...params }
+			params: {
+				merchantId: getMerchantId(),
+				pageNo: 1,
+				pageSize: 10,
+				...params
+			}
 		})
 	},
 

+ 32 - 7
pages/topUp/shareStore.vue

@@ -57,7 +57,7 @@
 </template>
 
 <script>
-	import prepaidApi, { getMerchantId } from '@/api/memberPrepaid.js'
+	import prepaidApi, { getMerchantId, getMerchantLocation, formatStoreDistance } from '@/api/memberPrepaid.js'
 
 	const DEFAULT_LOGO = '/static/index/shop.png'
 
@@ -70,7 +70,9 @@
 				storeList: [],
 				confirmVisible: false,
 				pendingDelete: null,
-				deleting: false
+				deleting: false,
+				longitude: null,
+				latitude: null
 			};
 		},
 		onShow() {
@@ -101,7 +103,7 @@
 					address: item.address || '',
 					rating: Number(item.grade || item.rating || 0),
 					reviewCount: item.commentNum ?? item.reviewCount ?? '',
-					distance: item.distance || item.distanceText || ''
+					distance: formatStoreDistance(item)
 				};
 			},
 			parseRecords(result) {
@@ -123,10 +125,30 @@
 					this.canAdd = Number(data.auditStatus) === 2 && Number(data.operationStatus) === 1;
 				});
 			},
+			ensureLocation() {
+				if (this.longitude != null && this.latitude != null) {
+					return Promise.resolve({
+						longitude: this.longitude,
+						latitude: this.latitude
+					});
+				}
+				return getMerchantLocation().then((loc) => {
+					this.longitude = loc.longitude;
+					this.latitude = loc.latitude;
+					return loc;
+				});
+			},
 			loadList() {
 				this.loading = true;
-				prepaidApi
-					.getSharedStores({ pageNo: 1, pageSize: 100 })
+				this.ensureLocation()
+					.then((loc) =>
+						prepaidApi.getSharedStores({
+							pageNo: 1,
+							pageSize: 100,
+							longitude: loc.longitude,
+							latitude: loc.latitude
+						})
+					)
 					.then((res) => {
 						if (res.data.code !== 200) {
 							this.storeList = [];
@@ -136,9 +158,12 @@
 						const records = this.parseRecords(res.data.result);
 						this.storeList = records.map((item) => this.mapStore(item));
 					})
-					.catch(() => {
+					.catch((err) => {
 						this.storeList = [];
-						uni.showToast({ title: '网络异常,请稍后重试', icon: 'none' });
+						uni.showToast({
+							title: (err && err.message) || '加载失败,请稍后重试',
+							icon: 'none'
+						});
 					})
 					.then(() => {
 						this.loading = false;

+ 33 - 7
pages/topUp/shareStoreAdd.vue

@@ -67,7 +67,7 @@
 </template>
 
 <script>
-	import prepaidApi from '@/api/memberPrepaid.js'
+	import prepaidApi, { getMerchantLocation, formatStoreDistance } from '@/api/memberPrepaid.js'
 
 	const DEFAULT_LOGO = '/static/index/shop.png'
 
@@ -86,7 +86,9 @@
 				candidateList: [],
 				selectedIds: [],
 				loading: false,
-				submitting: false
+				submitting: false,
+				longitude: null,
+				latitude: null
 			};
 		},
 		computed: {
@@ -119,13 +121,33 @@
 					address: item.address || '',
 					rating: Number(item.grade || item.rating || 0),
 					reviewCount: item.commentNum ?? item.reviewCount ?? '',
-					distance: item.distance || item.distanceText || ''
+					distance: formatStoreDistance(item)
 				};
 			},
+			ensureLocation() {
+				if (this.longitude != null && this.latitude != null) {
+					return Promise.resolve({
+						longitude: this.longitude,
+						latitude: this.latitude
+					});
+				}
+				return getMerchantLocation().then((loc) => {
+					this.longitude = loc.longitude;
+					this.latitude = loc.latitude;
+					return loc;
+				});
+			},
 			loadCandidates() {
 				this.loading = true;
-				prepaidApi
-					.getShareableStores({ pageNo: 1, pageSize: 100 })
+				this.ensureLocation()
+					.then((loc) =>
+						prepaidApi.getShareableStores({
+							pageNo: 1,
+							pageSize: 100,
+							longitude: loc.longitude,
+							latitude: loc.latitude
+						})
+					)
 					.then((res) => {
 						if (res.data.code !== 200) {
 							uni.showToast({ title: res.data.message || '加载失败', icon: 'none' });
@@ -136,8 +158,12 @@
 						this.candidateList = records.map((item) => this.mapStore(item));
 						this.selectedIds = [];
 					})
-					.catch(() => {
-						uni.showToast({ title: '网络异常,请稍后重试', icon: 'none' });
+					.catch((err) => {
+						this.candidateList = [];
+						uni.showToast({
+							title: (err && err.message) || '加载失败,请稍后重试',
+							icon: 'none'
+						});
 					})
 					.finally(() => {
 						this.loading = false;