【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.

186 lines
4.5 KiB

4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
4 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. res.latitude = position.latitude
  60. res.longitude = position.longitude
  61. resolve(res)
  62. })
  63. },
  64. fail: function() { //使用ip获取定位
  65. getUserAddressByIp(key).then(res => {
  66. resolve(res)
  67. })
  68. }
  69. })
  70. })
  71. }
  72. function getUserAddress(latitude, longitude, key) { //通过经纬度获取用户详细地址
  73. return new Promise((resolve, reject) => {
  74. let url = `/ws/geocoder/v1/?location=${latitude},${longitude}&key=${key}`
  75. // #ifndef H5
  76. url = `https://apis.map.qq.com` + url
  77. // #endif
  78. uni.request({
  79. url,
  80. success: (res) => {
  81. let {
  82. lat,
  83. lng
  84. } = res.data.result.ad_info.location;
  85. let data = {
  86. position: {
  87. latitude: lat,
  88. longitude: lng
  89. },
  90. addressDetail: res.data.result.ad_info,
  91. address : res.data.result.address
  92. }
  93. resolve(data)
  94. },
  95. fail(err) {
  96. reject(err)
  97. }
  98. })
  99. })
  100. }
  101. function getUserAddressByIp(key) { //根据IP获取当前用户位置
  102. return new Promise((resolve, reject) => {
  103. uni.request({
  104. url: 'https://api.ipify.org?format=json',
  105. success: (ipInfo) => {
  106. let url = `/ws/location/v1/ip?ip=${ipInfo.data.ip}&key=${key}`
  107. // #ifndef H5
  108. url = `https://apis.map.qq.com` + url
  109. // #endif
  110. uni.request({
  111. url,
  112. success: (res) => {
  113. let {
  114. lat,
  115. lng
  116. } = res.data.result.location;
  117. let data = {
  118. addressDetail: res.data.result.ad_info,
  119. ip: res.data.result.ip,
  120. position: {
  121. latitude: lat,
  122. longitude: lng
  123. }
  124. }
  125. resolve(data)
  126. },
  127. fail(err) {
  128. reject(err)
  129. }
  130. })
  131. }
  132. })
  133. })
  134. }
  135. //打开地图让用户选择位置
  136. function selectAddress(longitude, latitude, successCallback) {
  137. uni.chooseLocation({
  138. longitude, //经度
  139. latitude, //纬度
  140. success: function(res) {
  141. successCallback && successCallback(res)
  142. }
  143. });
  144. }
  145. //sdk自带获取位置方法(只支持微信环境),这里只当提示在用了(具体获取地址逻辑上面几个方法已完成)
  146. function wxGetLocation(successCallback, failCallback) {
  147. // #ifdef MP-WEIXIN
  148. // #endif
  149. console.log('wx.getLocation');
  150. wx.getLocation({
  151. type: 'gcj02',
  152. isHighAccuracy: true,
  153. highAccuracyExpireTime: 2000,
  154. success(res) {},
  155. fail(err) {
  156. if(err.errMsg == 'getLocation:gps closed'){
  157. uni.showToast({
  158. title: '请打开GPS定位,否则定位不准确',
  159. icon: 'none'
  160. })
  161. }
  162. }
  163. })
  164. }
  165. export default {
  166. calculateDistance, //计算两点距离
  167. getLocationDetail, //获取用户详细地址
  168. getLocation, //获取用户经纬度
  169. selectAddress, //打开地图让用户选择位置
  170. wxGetLocation,
  171. }