加油站付款小程序,打印小票
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.1 KiB

6 months ago
  1. import config from '../config.js'
  2. function calculateDistance(lat1, lon1, lat2, lon2) { //计算两点距离
  3. let distance = 0
  4. if (!lat2 || !lon2) return distance
  5. //先强制转换一下(后端给的字符串)
  6. lat1 = parseFloat(lat1)
  7. lon1 = parseFloat(lon1)
  8. lat2 = parseFloat(lat2)
  9. lon2 = parseFloat(lon2)
  10. // 将角度转换为弧度
  11. const R = 6371; // 地球半径,单位公里
  12. const dLat = (lat2 - lat1) * Math.PI / 180;
  13. const dLon = (lon2 - lon1) * Math.PI / 180;
  14. lat1 = lat1 * Math.PI / 180;
  15. lat2 = lat2 * Math.PI / 180;
  16. // Haversine公式
  17. const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
  18. Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
  19. const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  20. // 计算距离
  21. distance = R * c;
  22. return distance.toFixed(0)
  23. }
  24. function getLocation(fn) { //获取用户经纬度
  25. wxGetLocation() //此方法只用于提示用户打开gps
  26. uni.getLocation({
  27. type: 'gcj02',
  28. isHighAccuracy: true,
  29. highAccuracyExpireTime: 1000,
  30. success: function(position) {
  31. fn(position)
  32. },
  33. fail: function() { //使用ip获取定位
  34. let key = config.mapKey; //腾讯地图key
  35. getUserAddressByIp(key).then(res => {
  36. fn(res.position) //返回经纬度
  37. })
  38. }
  39. })
  40. }
  41. function getLocationDetail() { //获取用户详细地址
  42. wxGetLocation()
  43. return new Promise((resolve, reject) => {
  44. let key = config.mapKey; //腾讯地图key
  45. uni.getLocation({
  46. type: 'gcj02',
  47. isHighAccuracy: true,
  48. highAccuracyExpireTime: 1000,
  49. success: function(position) {
  50. getUserAddress(position.latitude, position.longitude, key).then(res => {
  51. resolve(res)
  52. })
  53. },
  54. fail: function() { //使用ip获取定位
  55. getUserAddressByIp(key).then(res => {
  56. resolve(res)
  57. })
  58. }
  59. })
  60. })
  61. }
  62. function getUserAddress(latitude, longitude, key) { //通过经纬度获取用户详细地址
  63. return new Promise((resolve, reject) => {
  64. let url = `/ws/geocoder/v1/?location=${latitude},${longitude}&key=${key}`
  65. // #ifndef H5
  66. url = `https://apis.map.qq.com` + url
  67. // #endif
  68. uni.request({
  69. url,
  70. success: (res) => {
  71. let {
  72. lat,
  73. lng
  74. } = res.data.result.ad_info.location;
  75. let data = {
  76. position: {
  77. latitude: lat,
  78. longitude: lng
  79. },
  80. addressDetail: res.data.result.ad_info
  81. }
  82. resolve(data)
  83. },
  84. fail(err) {
  85. reject(err)
  86. }
  87. })
  88. })
  89. }
  90. function getUserAddressByIp(key) { //根据IP获取当前用户位置
  91. return new Promise((resolve, reject) => {
  92. uni.request({
  93. url: 'https://api.ipify.org?format=json',
  94. success: (ipInfo) => {
  95. let url = `/ws/location/v1/ip?ip=${ipInfo.data.ip}&key=${key}`
  96. // #ifndef H5
  97. url = `https://apis.map.qq.com` + url
  98. // #endif
  99. uni.request({
  100. url,
  101. success: (res) => {
  102. let {
  103. lat,
  104. lng
  105. } = res.data.result.location;
  106. let data = {
  107. addressDetail: res.data.result.ad_info,
  108. ip: res.data.result.ip,
  109. position: {
  110. latitude: lat,
  111. longitude: lng
  112. }
  113. }
  114. resolve(data)
  115. },
  116. fail(err) {
  117. reject(err)
  118. }
  119. })
  120. }
  121. })
  122. })
  123. }
  124. //打开地图让用户选择位置
  125. function selectAddress(longitude, latitude, successCallback) {
  126. uni.chooseLocation({
  127. longitude, //经度
  128. latitude, //纬度
  129. success: function(res) {
  130. successCallback && successCallback(res)
  131. }
  132. });
  133. }
  134. //sdk自带获取位置方法(只支持微信环境),这里只当提示在用了(具体获取地址逻辑上面几个方法已完成)
  135. function wxGetLocation(successCallback, failCallback) {
  136. // #ifdef MP-WEIXIN
  137. // #endif
  138. console.log('wx.getLocation');
  139. wx.getLocation({
  140. type: 'gcj02',
  141. isHighAccuracy: true,
  142. highAccuracyExpireTime: 2000,
  143. success(res) {},
  144. fail(err) {
  145. if(err.errMsg == 'getLocation:gps closed'){
  146. uni.showToast({
  147. title: '请打开GPS定位,否则定位不准确',
  148. icon: 'none'
  149. })
  150. }
  151. }
  152. })
  153. }
  154. export default {
  155. calculateDistance, //计算两点距离
  156. getLocationDetail, //获取用户详细地址
  157. getLocation, //获取用户经纬度
  158. selectAddress, //打开地图让用户选择位置
  159. wxGetLocation,
  160. }