lbs.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // 获取当前ip
  88. function getIPs(callback) {
  89. var ip_dups = {}
  90. var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection
  91. var useWebKit = !!window.webkitRTCPeerConnection
  92. var mediaConstraints = {
  93. optional: [{ RtpDataChannels: true }]
  94. }
  95. var servers = {
  96. iceServers: [{ urls: 'stun:stun.services.mozilla.com' }, { urls: 'stun:stun.l.google.com:19302' }]
  97. }
  98. var pc = new RTCPeerConnection(servers, mediaConstraints)
  99. function handleCandidate(candidate) {
  100. var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
  101. var hasIp = ip_regex.exec(candidate)
  102. if (hasIp) {
  103. var ip_addr = ip_regex.exec(candidate)[1]
  104. if (ip_dups[ip_addr] === undefined) callback(ip_addr)
  105. ip_dups[ip_addr] = true
  106. }
  107. }
  108. pc.onicecandidate = function (ice) {
  109. if (ice.candidate) {
  110. handleCandidate(ice.candidate.candidate)
  111. }
  112. }
  113. pc.createDataChannel('')
  114. pc.createOffer(
  115. function (result) {
  116. pc.setLocalDescription(
  117. result,
  118. function () {},
  119. function () {}
  120. )
  121. },
  122. function () {}
  123. )
  124. setTimeout(function () {
  125. var lines = pc.localDescription.sdp.split('\n')
  126. lines.forEach(function (line) {
  127. if (line.indexOf('a=candidate:') === 0) handleCandidate(line)
  128. })
  129. }, 500)
  130. }