<script setup>
import {onLaunch, onShow, onHide} from '@dcloudio/uni-app'
import {reactive, provide, inject} from 'vue'
import api from "./api/api";

//定义全局数据
const globalData = reactive({
  appName: '超邦手工单系统',
  appIcon: '/static/img/logo.png',
  statusBarH: '',
  customBarH: ''
})
provide('globalData', globalData)

onLaunch(() => {
  initSysInfo()

  setInterval(() => {
    updateUserLocation()
  }, 60 * 1000)
})
onShow(() => {
  updateUserLocation()
})
onHide(() => {
  updateUserLocation()
})

/**
 * 更新用户位置
 */
const updateUserLocation = () => {
  let token = uni.getStorageSync('token')
  //未登录不更新位置
  if (!token) {
    return
  }

  uni.getLocation({
    type: 'gcj02',
    success(res) {
      let data = {longitude: res.longitude, latitude: res.latitude}
      api.updateUserLocation(data).then(() => {
      }).catch(() => {})
    },
    fail() {
    }
  });
}

const initSysInfo = () => {
  // 获取手机状态栏高度
  let statusBar = uni.getWindowInfo().statusBarHeight
  let customBar

  // 获取胶囊按钮的布局位置信息
  let menu = wx.getMenuButtonBoundingClientRect()
  // 导航栏高度 = 胶囊下距离 + 胶囊上距离 - 状态栏高度
  customBar = menu.bottom + menu.top - statusBar

  const globalData = inject('globalData')
  globalData.statusBarH = statusBar
  globalData.customBarH = customBar
}
</script>

<style lang="scss">
@import "/static/css/common.scss";
@import "/static/css/form.scss";
@import '/theme/theme.scss';

page {
  background-color: var(--pageBgColor);
}

</style>
