addressComponent.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict';
  2. import BaseComponent from './baseComponent'
  3. /*
  4. 腾讯地图和百度地图API统一调配组件
  5. */
  6. class AddressComponent extends BaseComponent {
  7. constructor(){
  8. super();
  9. this.tencentkey = 'RLHBZ-WMPRP-Q3JDS-V2IQA-JNRFH-EJBHL';
  10. this.tencentkey2 = 'RRXBZ-WC6KF-ZQSJT-N2QU7-T5QIT-6KF5X';
  11. this.baidukey = 'fjke3YUipM9N64GdOIh1DNeK2APO2WcT';
  12. this.baidukey2 = 'fjke3YUipM9N64GdOIh1DNeK2APO2WcT';
  13. }
  14. //获取定位地址
  15. async guessPosition(req){
  16. return new Promise(async (resolve, reject) => {
  17. let ip = req.headers['x-forwarded-for'] ||
  18. req.connection.remoteAddress ||
  19. req.socket.remoteAddress ||
  20. req.connection.socket.remoteAddress;
  21. const ipArr = ip.split(':');
  22. ip = ipArr[ipArr.length -1];
  23. if (process.env.NODE_ENV == 'development') {
  24. ip = '180.158.102.141';
  25. }
  26. try{
  27. let result = await this.fetch('http://apis.map.qq.com/ws/location/v1/ip', {
  28. ip,
  29. key: this.tencentkey2,
  30. })
  31. if (result.status != 0) {
  32. result = await this.fetch('http://apis.map.qq.com/ws/location/v1/ip', {
  33. ip,
  34. key: this.tencentkey2,
  35. })
  36. }
  37. if (result.status == 0) {
  38. const cityInfo = {
  39. lat: result.result.location.lat,
  40. lng: result.result.location.lng,
  41. city: result.result.ad_info.city,
  42. }
  43. cityInfo.city = cityInfo.city.replace(/市$/, '');
  44. resolve(cityInfo)
  45. }else{
  46. console.log('定位失败', result)
  47. reject('定位失败');
  48. }
  49. }catch(err){
  50. reject(err);
  51. }
  52. })
  53. }
  54. //搜索地址
  55. async searchPlace(keyword, cityName, type = 'search'){
  56. try{
  57. const resObj = await this.fetch('http://apis.map.qq.com/ws/place/v1/search', {
  58. key: this.tencentkey,
  59. keyword: encodeURIComponent(keyword),
  60. boundary: 'region(' + encodeURIComponent(cityName) + ',0)',
  61. page_size: 10,
  62. });
  63. if (resObj.status == 0) {
  64. return resObj
  65. }else{
  66. throw new Error('搜索位置信息失败');
  67. }
  68. }catch(err){
  69. throw new Error(err);
  70. }
  71. }
  72. //测量距离
  73. async getDistance(from, to, type){
  74. try{
  75. let res
  76. res = await this.fetch('http://api.map.baidu.com/routematrix/v2/driving', {
  77. ak: this.baidukey,
  78. output: 'json',
  79. origins: from,
  80. destinations: to,
  81. })
  82. if(res.status !== 0){
  83. res = await this.fetch('http://api.map.baidu.com/routematrix/v2/driving', {
  84. ak: this.baidukey2,
  85. output: 'json',
  86. origins: from,
  87. destinations: to,
  88. })
  89. }
  90. if(res.status == 0){
  91. const positionArr = [];
  92. let timevalue;
  93. res.result.forEach(item => {
  94. timevalue = parseInt(item.duration.value) + 1200;
  95. let durationtime = Math.ceil(timevalue%3600/60) + '分钟';
  96. if(Math.floor(timevalue/3600)){
  97. durationtime = Math.floor(timevalue/3600) + '小时' + durationtime;
  98. }
  99. positionArr.push({
  100. distance: item.distance.text,
  101. order_lead_time: durationtime,
  102. })
  103. })
  104. if (type == 'tiemvalue') {
  105. return timevalue
  106. }else{
  107. return positionArr
  108. }
  109. }else{
  110. throw new Error('调用百度地图测距失败');
  111. }
  112. }catch(err){
  113. console.log('获取位置距离失败')
  114. throw new Error(err);
  115. }
  116. }
  117. //通过ip地址获取精确位置
  118. async geocoder(req){
  119. try{
  120. const address = await this.guessPosition(req);
  121. const res = await this.fetch('http://apis.map.qq.com/ws/geocoder/v1/', {
  122. key: this.tencentkey,
  123. location: address.lat + ',' + address.lng
  124. })
  125. if (res.status == 0) {
  126. return res
  127. }else{
  128. throw new Error('获取具体位置信息失败');
  129. }
  130. }catch(err){
  131. console.log('geocoder获取定位失败')
  132. throw new Error(err);
  133. }
  134. }
  135. //通过geohash获取精确位置
  136. async getpois(lat, lng){
  137. try{
  138. const res = await this.fetch('http://apis.map.qq.com/ws/geocoder/v1/', {
  139. key: this.tencentkey,
  140. location: lat + ',' + lng
  141. })
  142. if (res.status == 0) {
  143. return res
  144. }else{
  145. throw new Error('通过获geohash取具体位置失败');
  146. }
  147. }catch(err){
  148. console.log('getpois获取定位失败')
  149. throw new Error(err);
  150. }
  151. }
  152. }
  153. export default AddressComponent