فهرست منبع

增加搜索地址API

maguohua 8 سال پیش
والد
کامیت
8f48737f4f
5فایلهای تغییر یافته به همراه35 افزوده شده و 20 حذف شده
  1. 1 1
      README.md
  2. 1 1
      controller/v1/cities.js
  3. 26 13
      controller/v1/search.js
  4. 4 4
      models/v1/cities.js
  5. 3 1
      prototype/baseComponent.js

+ 1 - 1
README.md

@@ -52,7 +52,7 @@ npm run dev
 
 - [x] 定位功能
 - [x] 城市列表
-- [ ] 搜索地址
+- [x] 搜索地址
 - [ ] 展示所选地址附近商家列表
 - [ ] 搜索美食,餐馆
 - [ ] 根据距离、销量、评分、特色菜、配送方式等进行排序和筛选

+ 1 - 1
controller/v1/cities.js

@@ -61,7 +61,7 @@ class CityHandle extends BaseComponent{
 	 		req.connection.socket.remoteAddress;
 	 		const ipArr = ip.split(':');
 	 		ip = ipArr[ipArr.length -1];
-	 		ip = '116.231.55.195';
+	 		//ip = '116.231.55.195';
 	 		/*
 	 		调用新浪接口,获取ip地址信息
 	 		 */

+ 26 - 13
controller/v1/search.js

@@ -3,9 +3,11 @@
 import BaseComponent from '../../prototype/baseComponent';
 import Cities from '../../models/v1/cities';
 
+
 class SearchPlace extends BaseComponent{
 	constructor(){
 		super()
+		this.search = this.search.bind(this)
 	}
 	async search(req, res, next){
 		const {type, city_id, keyword} = req.query;
@@ -18,20 +20,31 @@ class SearchPlace extends BaseComponent{
 		}
 		try{
 			const cityInfo = await Cities.getCityById(city_id);
-			const aaa = await this.fetch('http://restapi.amap.com/v3/place/text?key=e1467cff48d3359df43012aa8c3a252b&keywords=北京大学&types=141201&city=北京&children=1&offset=20&page=1&extensions=all')
-			// const resList = await this.fetch(http://restapi.amap.com/v3/place/text', {
-			// 	key: 'e1467cff48d3359df43012aa8c3a252b',
-			// 	keywords: keyword,
-			// 	types: 141201,
-			// 	city: cityInfo.name,
-			// 	children: 1,
-			// 	offset: 10,
-			// 	page: 1,
-			// 	extensions: 'all',
-			// })
-			res.send(aaa);
+			/*
+			调用腾讯地图api
+			 */
+			const resObj = await this.fetch('http://apis.map.qq.com/ws/place/v1/search', {
+				key: 'RLHBZ-WMPRP-Q3JDS-V2IQA-JNRFH-EJBHL',
+				keyword: encodeURIComponent(keyword),
+				boundary: 'region(' + encodeURIComponent(cityInfo.name) + ',0)',
+				page_size: 10,
+			});
+			const resArr = [];
+			resObj.data.forEach((item, index) => {
+				resArr.push({
+					name: item.title,
+					address: item.address,
+					latitude: item.location.lat,
+					longitude: item.location.lng,
+					geohash: item.location.lat + ',' + item.location.lng,
+				})
+			});
+			res.send(resArr);
 		}catch(err){
-			res.send(err);
+			res.send({
+				name: 'GET_ADDRESS_ERROR',
+				message: '获取地址信息失败',
+			});
 		}
 	}
 }

+ 4 - 4
models/v1/cities.js

@@ -33,7 +33,7 @@ citySchema.statics.cityGuess = function(name){
 				name: 'ERROR_DATA',
 				message: '查找数据失败',
 			});
-			console.log(err);
+			console.error(err);
 		}
 	})
 }
@@ -48,7 +48,7 @@ citySchema.statics.cityHot = function (){
 				name: 'ERROR_DATA',
 				message: '查找数据失败',
 			});
-			console.log(err);
+			console.error(err);
 		}
 	})
 }
@@ -66,7 +66,7 @@ citySchema.statics.cityGroup = function (){
 				name: 'ERROR_DATA',
 				message: '查找数据失败',
 			});
-			console.log(err);
+			console.error(err);
 		}
 	})
 }
@@ -89,7 +89,7 @@ citySchema.statics.getCityById = function(id){
 				name: 'ERROR_DATA',
 				message: '查找数据失败',
 			});
-			console.log(err);
+			console.error(err);
 		}
 	})
 }

+ 3 - 1
prototype/baseComponent.js

@@ -33,14 +33,16 @@ export default class BaseComponent {
 			})
 		}
 		let responseJson;
+		console.log(url)
 		try {
-			let response = await fetch(url, requestConfig);
+			const response = await fetch(url, requestConfig);
 			if (resType === 'TEXT') {
 				responseJson = await response.text();
 			}else{
 				responseJson = await response.json();
 			}
 		} catch (error) {
+			console.error(error)
 			throw new Error(error)
 		}
 		return responseJson