search.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. import BaseComponent from '../../prototype/baseComponent';
  3. import Cities from '../../models/v1/cities';
  4. class SearchPlace extends BaseComponent{
  5. constructor(){
  6. super()
  7. this.search = this.search.bind(this)
  8. }
  9. async search(req, res, next){
  10. const {type, city_id, keyword} = req.query;
  11. if (!type || isNaN(city_id) || !keyword) {
  12. res.send({
  13. name: 'ERROR_QUERY_TYPE',
  14. message: '参数错误',
  15. })
  16. return
  17. }
  18. try{
  19. const cityInfo = await Cities.getCityById(city_id);
  20. /*
  21. 调用腾讯地图api
  22. */
  23. const resObj = await this.fetch('http://apis.map.qq.com/ws/place/v1/search', {
  24. key: 'RLHBZ-WMPRP-Q3JDS-V2IQA-JNRFH-EJBHL',
  25. keyword: encodeURIComponent(keyword),
  26. boundary: 'region(' + encodeURIComponent(cityInfo.name) + ',0)',
  27. page_size: 10,
  28. });
  29. const resArr = [];
  30. resObj.data.forEach((item, index) => {
  31. resArr.push({
  32. name: item.title,
  33. address: item.address,
  34. latitude: item.location.lat,
  35. longitude: item.location.lng,
  36. geohash: item.location.lat + ',' + item.location.lng,
  37. })
  38. });
  39. res.send(resArr);
  40. }catch(err){
  41. res.send({
  42. name: 'GET_ADDRESS_ERROR',
  43. message: '获取地址信息失败',
  44. });
  45. }
  46. }
  47. }
  48. export default new SearchPlace();