maguohua 8 жил өмнө
parent
commit
47c36f388b

+ 18 - 1
controller/shopping/shop.js

@@ -188,7 +188,24 @@ class Shop extends AddressComponent{
 			return
 		}
 		const restaurants = await ShopModel.find().limit(Number(limit)).skip(Number(offset));
-		res.send(restaurants)
+		const from = latitude + ',' + longitude;
+		let to = '';
+		restaurants.forEach((item, index) => {
+			const spStr = (index == restaurants.length -1) ? '':';';
+			to += item.latitude + ',' + item.longitude + spStr;
+		})
+		try{
+			const distance = await this.getDistance(from, to)
+			res.send(distance)
+
+		}catch(err){
+			console.log('设置商铺距离信息失败');
+			res.send({
+				status: 0,
+				type: 'ERROR_DATA',
+				message: '获取信息失败'
+			})
+		}
 	}
 }
 

+ 17 - 0
prototype/addressComponent.js

@@ -35,6 +35,7 @@ class AddressComponent extends BaseComponent {
 	 			cityInfo.city = cityInfo.city.replace(/市$/, '');
 	 			return cityInfo
 	 		}else{
+	 			console.log('定位失败');
 	 			throw new Error('定位失败');
 	 		}
  			
@@ -54,12 +55,28 @@ class AddressComponent extends BaseComponent {
 			if (resObj.status == 0) {
 				return resObj
 			}else{
+				console.log('搜索位置信息失败')
 				throw new Error('搜索位置信息失败');
 			}
 		}catch(err){
 			throw new Error(err);
 		}
 	}
+	//测量距离
+	async getDistance(from, to){
+		try{
+			const res = await this.fetch('http://apis.map.qq.com/ws/distance/v1/', {
+				key: this.key,
+				from,
+				to,
+			})
+			return res
+		}catch(err){
+			console.log('获取位置距离失败')
+			throw new Error('获取位置距离失败');
+		}
+	}
+
 }
 
 export default AddressComponent