addressComponent.js 5.7 KB

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