addressComponent.js 5.6 KB

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