cities.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. let cityInfo;
  63. try{
  64. cityInfo = await this.guessPosition(req);
  65. }catch(err){
  66. console.error('获取IP位置信息失败');
  67. res.send({
  68. name: 'ERROR_DATA',
  69. message: '获取数据失败',
  70. });
  71. return
  72. }
  73. /*
  74. 汉字转换成拼音
  75. */
  76. const pinyinArr = pinyin(cityInfo.city, {
  77. style: pinyin.STYLE_NORMAL,
  78. });
  79. let cityName = '';
  80. pinyinArr.forEach(item => {
  81. cityName += item[0];
  82. })
  83. return cityName
  84. }
  85. async getExactAddress(req, res, next){
  86. try{
  87. const position = await this.geocoder(req)
  88. res.send(position);
  89. }catch(err){
  90. console.log('获取精确位置信息失败');
  91. res.send({
  92. name: 'ERROR_DATA',
  93. message: '获取精确位置信息失败',
  94. });
  95. }
  96. }
  97. async pois(req, res, next){
  98. const geohash = req.params.geohash;
  99. try{
  100. if (geohash.indexOf(',') == -1) {
  101. throw new Error('参数错误')
  102. }
  103. }catch(err){
  104. console.log('参数错误');
  105. res.send({
  106. status: 0,
  107. type: 'ERROR_PARAMS',
  108. message: '参数错误',
  109. })
  110. return
  111. }
  112. const poisArr = geohash.split(',');
  113. try{
  114. const result = await this.getpois(poisArr[0], poisArr[1]);
  115. const address = {
  116. address: result.result.address,
  117. city: result.result.address_component.province,
  118. geohash,
  119. latitude: poisArr[0],
  120. longitude: poisArr[1],
  121. name: result.result.formatted_addresses.recommend,
  122. }
  123. res.send(address);
  124. }catch(err){
  125. console.log('getpois返回信息失败');
  126. res.send({
  127. status: 0,
  128. type: 'ERROR_DATA',
  129. message: '获取数据失败',
  130. })
  131. }
  132. }
  133. }
  134. export default new CityHandle()