addressComponent.js 4.2 KB

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