search.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. import AddressComponent from '../../prototype/addressComponent';
  3. import Cities from '../../models/v1/cities';
  4. import CityHandle from './cities'
  5. class SearchPlace extends AddressComponent{
  6. constructor(){
  7. super()
  8. this.search = this.search.bind(this)
  9. }
  10. async search(req, res, next){
  11. let {type = 'search', city_id, keyword} = req.query;
  12. if (!keyword) {
  13. res.send({
  14. name: 'ERROR_QUERY_TYPE',
  15. message: '参数错误',
  16. })
  17. return
  18. }else if(isNaN(city_id)){
  19. try{
  20. const cityname = await CityHandle.getCityName(req);
  21. const cityInfo = await Cities.cityGuess(cityname);
  22. city_id = cityInfo.id;
  23. }catch(err){
  24. console.log('搜索地址时,获取定位城失败')
  25. res.send({
  26. name: 'ERROR_GET_POSITION',
  27. message: '获取数据失败',
  28. })
  29. }
  30. }
  31. try{
  32. const cityInfo = await Cities.getCityById(city_id);
  33. const resObj = await this.searchPlace(keyword, cityInfo.name, type);
  34. const cityList = [];
  35. resObj.data.forEach((item, index) => {
  36. cityList.push({
  37. name: item.title,
  38. address: item.address,
  39. latitude: item.location.lat,
  40. longitude: item.location.lng,
  41. geohash: item.location.lat + ',' + item.location.lng,
  42. })
  43. });
  44. res.send(cityList);
  45. }catch(err){
  46. res.send({
  47. name: 'GET_ADDRESS_ERROR',
  48. message: '获取地址信息失败',
  49. });
  50. }
  51. }
  52. }
  53. export default new SearchPlace();