location.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /** 用戶位置信息 */
  2. import {
  3. getUserRegion
  4. } from '@/api/system'
  5. // import store from '@/store'
  6. /** 用戶手机定位 */
  7. let mLocation = {
  8. lat: null,
  9. lng: null
  10. }
  11. /** 用户地址回调 */
  12. let callFunction = null
  13. export async function mGetPosition() {
  14. let getCurPages = getCurrentPages()
  15. let getRoute = getCurPages.length > 0 ? getCurPages[0].route : ''
  16. const client_hash = uni.getStorageSync('client_hash')
  17. if (mLocation.lat && mLocation.lng) {
  18. return mLocation
  19. }
  20. if (!client_hash && getRoute == 'pages/page/login/index') {
  21. return null
  22. }
  23. let getObj = await new Promise(resolve => {
  24. uni.getLocation({
  25. type: 'gcj02',
  26. success: res => resolve(res),
  27. fail: e => {
  28. console.log('获取位置失败!', e)
  29. }
  30. })
  31. })
  32. mLocation = getObj
  33. mLocation.lat = getObj.latitude
  34. mLocation.lng = getObj.longitude
  35. return mLocation
  36. }
  37. /** 腾讯地图定位 */
  38. // 初始化或更新用户位置信息缓存
  39. let positionType = null
  40. let retryNum = 1
  41. export function getPosition(callFn) {
  42. if (callFn) {
  43. callFunction = callFn
  44. }
  45. mGetPosition().then(res => {
  46. showPosition()
  47. })
  48. }
  49. function showPosition() {
  50. if (retryNum > 6) return false
  51. let userRegion = uni.getStorageSync('userRegion')
  52. let intervalTime = 30 * 60 * 1000 // 默认半小时过期
  53. // 判断是否已存在位置信息缓存
  54. if (userRegion) {
  55. userRegion = JSON.parse(userRegion)
  56. // 获取后台返回的有效时间间隔
  57. let timesTamp = new Date().getTime()
  58. if (userRegion.expire * 1000 > timesTamp) {
  59. intervalTime = userRegion.expire * 1000 - timesTamp
  60. retryNum = 1
  61. } else {
  62. intervalTime = 10000
  63. retryNum++
  64. }
  65. }
  66. if (mLocation.lat && mLocation.lng) {
  67. positionType = 'mobile'
  68. if (userRegion) {
  69. // 当前时间大于等于过期时间(超过有效时间间隔后)重新请求接口更新缓存
  70. if (callFunction) {
  71. callFunction(userRegion)
  72. }
  73. callFunction = null
  74. setTimeout(() => {
  75. fetchPosition(mLocation)
  76. }, intervalTime)
  77. } else {
  78. // 页面首次渲染请求接口设置缓存
  79. fetchPosition(mLocation)
  80. }
  81. }
  82. }
  83. // 根据用户经纬度请求用户位置信息
  84. function fetchPosition(position) {
  85. if (!position.lat && !position.lng) {
  86. console.error('获取经纬度失败!')
  87. return false
  88. }
  89. uni.setStorageSync('miscPosition', JSON.stringify(position))
  90. let params = {
  91. lng_lat_type: positionType,
  92. lat: position.lat,
  93. lng: position.lng
  94. }
  95. getUserRegion(params).then(res => {
  96. if (res.status === 'success') {
  97. // 设置或更新用户位置信息缓存
  98. if (callFunction) {
  99. callFunction(res.data)
  100. }
  101. callFunction = null
  102. uni.setStorageSync('userRegion', JSON.stringify(res.data))
  103. // 提交用户位置信息状态
  104. // store.commit('FETCH_USER_REGION', res.data)
  105. showPosition()
  106. }
  107. })
  108. }
  109. export default {
  110. mLocation,
  111. mGetPosition,
  112. getPosition
  113. }