addressComponent.js 4.0 KB

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