cities.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. import Cities from '../../models/v1/cities'
  3. import pinyin from "pinyin"
  4. import AddressComponent from '../../prototype/addressComponent'
  5. class CityHandle extends AddressComponent{
  6. constructor(){
  7. super()
  8. this.getCity = this.getCity.bind(this);
  9. this.getExactAddress = this.getExactAddress.bind(this);
  10. this.pois = this.pois.bind(this);
  11. }
  12. async getCity(req, res, next){
  13. const type = req.query.type;
  14. let cityInfo;
  15. try{
  16. switch (type){
  17. case 'guess':
  18. const city = await this.getCityName(req);
  19. cityInfo = await Cities.cityGuess(city);
  20. break;
  21. case 'hot':
  22. cityInfo = await Cities.cityHot();
  23. break;
  24. case 'group':
  25. cityInfo = await Cities.cityGroup();
  26. break;
  27. default:
  28. res.json({
  29. name: 'ERROR_QUERY_TYPE',
  30. message: '参数错误',
  31. })
  32. return
  33. }
  34. res.send(cityInfo);
  35. }catch(err){
  36. res.send({
  37. name: 'ERROR_DATA',
  38. message: '获取数据失败',
  39. });
  40. }
  41. }
  42. async getCityById(req, res, next){
  43. const cityid = req.params.id;
  44. if (isNaN(cityid)) {
  45. res.json({
  46. name: 'ERROR_PARAM_TYPE',
  47. message: '参数错误',
  48. })
  49. return
  50. }
  51. try{
  52. const cityInfo = await Cities.getCityById(cityid);
  53. res.send(cityInfo);
  54. }catch(err){
  55. res.send({
  56. name: 'ERROR_DATA',
  57. message: '获取数据失败',
  58. });
  59. }
  60. }
  61. async getCityName(req){
  62. try{
  63. const cityInfo = await this.guessPosition(req);
  64. /*
  65. 汉字转换成拼音
  66. */
  67. const pinyinArr = pinyin(cityInfo.city, {
  68. style: pinyin.STYLE_NORMAL,
  69. });
  70. let cityName = '';
  71. pinyinArr.forEach(item => {
  72. cityName += item[0];
  73. })
  74. return cityName;
  75. }catch(err){
  76. return '北京';
  77. }
  78. }
  79. async getExactAddress(req, res, next){
  80. try{
  81. const position = await this.geocoder(req)
  82. res.send(position);
  83. }catch(err){
  84. console.log('获取精确位置信息失败');
  85. res.send({
  86. name: 'ERROR_DATA',
  87. message: '获取精确位置信息失败',
  88. });
  89. }
  90. }
  91. async pois(req, res, next){
  92. try{
  93. const geohash = req.params.geohash || '';
  94. if (geohash.indexOf(',') == -1) {
  95. res.send({
  96. status: 0,
  97. type: 'ERROR_PARAMS',
  98. message: '参数错误',
  99. })
  100. return;
  101. }
  102. const poisArr = geohash.split(',');
  103. const result = await this.getpois(poisArr[0], poisArr[1]);
  104. const address = {
  105. address: result.result.address,
  106. city: result.result.address_component.province,
  107. geohash,
  108. latitude: poisArr[0],
  109. longitude: poisArr[1],
  110. name: result.result.formatted_addresses.recommend,
  111. }
  112. res.send(address);
  113. }catch(err){
  114. console.log('getpois返回信息失败', err);
  115. res.send({
  116. status: 0,
  117. type: 'ERROR_DATA',
  118. message: '获取数据失败',
  119. })
  120. }
  121. }
  122. }
  123. export default new CityHandle()