search.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. import AddressComponent from '../../prototype/addressComponent';
  3. import Cities from '../../models/v1/cities';
  4. class SearchPlace extends AddressComponent{
  5. constructor(){
  6. super()
  7. this.search = this.search.bind(this)
  8. }
  9. async search(req, res, next){
  10. const {type = 'search', city_id, keyword} = req.query;
  11. if (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. const resObj = await this.searchPlace(keyword, cityInfo.name);
  21. const cityList = [];
  22. resObj.data.forEach((item, index) => {
  23. cityList.push({
  24. name: item.title,
  25. address: item.address,
  26. latitude: item.location.lat,
  27. longitude: item.location.lng,
  28. geohash: item.location.lat + ',' + item.location.lng,
  29. })
  30. });
  31. res.send(cityList);
  32. }catch(err){
  33. res.send({
  34. name: 'GET_ADDRESS_ERROR',
  35. message: '获取地址信息失败',
  36. });
  37. }
  38. }
  39. }
  40. export default new SearchPlace();