【PT.SCC实名制管理系统】24.10.01 -30天,考勤打卡小程序
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.

184 lines
4.4 KiB

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