addressComponent.js 4.0 KB

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