lbs.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import axios from 'axios'
  2. export function supervisorChangeList(keyword) {
  3. return new Promise(function (r, j) {
  4. try {
  5. new AMap.Autocomplete({
  6. city: '全国'
  7. }).search(keyword, function (status, result) {
  8. if (status === 'complete') {
  9. r(result)
  10. } else {
  11. console.error('根据地址查询位置失败')
  12. j(result)
  13. }
  14. })
  15. } catch (error) {
  16. j(error)
  17. }
  18. })
  19. }
  20. /**
  21. * 根据地址查坐标
  22. * @param {*} address 例如:广东省广州市天河区邮通小区
  23. * @returns Object
  24. */
  25. export function getLocation(address) {
  26. return new Promise(function (r, j) {
  27. try {
  28. new AMap.Geocoder().getLocation(address, function (status, result) {
  29. if (status === 'complete' && result.geocodes?.length) {
  30. r(result.geocodes)
  31. } else {
  32. console.error('根据地址查询位置失败')
  33. j(result)
  34. }
  35. })
  36. } catch (error) {
  37. j(error)
  38. }
  39. })
  40. }
  41. /**
  42. * 根据坐标查地址
  43. * @param {*} location 例如:[113.36242, 23.1368425]
  44. * @returns Object
  45. */
  46. export function getAddress(location) {
  47. return new Promise(function (r, j) {
  48. try {
  49. new AMap.Geocoder().getAddress(location, function (status, result) {
  50. if (status === 'complete' && result.regeocode) {
  51. r(result.regeocode)
  52. } else {
  53. console.error('根据地址查询位置失败')
  54. j(result)
  55. }
  56. })
  57. } catch (error) {
  58. j(error)
  59. }
  60. })
  61. }
  62. export function axiosMapZb(params) {
  63. return axios({
  64. url: `https://restapi.amap.com/v3/place/around`,
  65. method: 'get',
  66. params: {
  67. key: '428a7111e02ea8367a3b34804eaa025b',
  68. ...params
  69. }
  70. })
  71. }
  72. // 获取ip定位地址
  73. export function getIPAdd() {
  74. return new Promise(function (r, j) {
  75. try {
  76. getIPs(ip => {
  77. axios
  78. .get('https://restapi.amap.com/v3/ip?output=json&key=428a7111e02ea8367a3b34804eaa025b&ip=' + ip)
  79. .then(r)
  80. .catch(j)
  81. })
  82. } catch (error) {
  83. j(error)
  84. }
  85. })
  86. }
  87. // export function getIPAdd() {
  88. // return new Promise(function (r, j) {
  89. // try {
  90. // axios
  91. // .get('https://qifu-api.baidubce.com/ip/local/geo/v1/district')
  92. // .then((res) => {
  93. // r(res);
  94. // })
  95. // .catch(j);
  96. // } catch (error) {
  97. // j(error);
  98. // }
  99. // });
  100. // }
  101. // 获取当前ip
  102. function getIPs(callback) {
  103. var ip_dups = {}
  104. var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection
  105. var useWebKit = !!window.webkitRTCPeerConnection
  106. var mediaConstraints = {
  107. optional: [{ RtpDataChannels: true }]
  108. }
  109. var servers = {
  110. iceServers: [{ urls: 'stun:stun.services.mozilla.com' }, { urls: 'stun:stun.l.google.com:19302' }]
  111. }
  112. var pc = new RTCPeerConnection(servers, mediaConstraints)
  113. function handleCandidate(candidate) {
  114. var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
  115. var hasIp = ip_regex.exec(candidate)
  116. if (hasIp) {
  117. var ip_addr = ip_regex.exec(candidate)[1]
  118. if (ip_dups[ip_addr] === undefined) callback(ip_addr)
  119. ip_dups[ip_addr] = true
  120. }
  121. }
  122. pc.onicecandidate = function (ice) {
  123. if (ice.candidate) {
  124. handleCandidate(ice.candidate.candidate)
  125. }
  126. }
  127. pc.createDataChannel('')
  128. pc.createOffer(
  129. function (result) {
  130. pc.setLocalDescription(
  131. result,
  132. function () {},
  133. function () {}
  134. )
  135. },
  136. function () {}
  137. )
  138. setTimeout(function () {
  139. var lines = pc.localDescription.sdp.split('\n')
  140. lines.forEach(function (line) {
  141. if (line.indexOf('a=candidate:') === 0) handleCandidate(line)
  142. })
  143. }, 500)
  144. }