/*
 * @Author: micky
 * @Date: 2024-08-20 11:36:44
 * @LastEditors: micky
 * @LastEditTime: 2024-09-06 11:46:36
 * @FilePath: \chargingpile-uniapp\src\hooks\useLocation.ts
 */

import { useAppStore } from '@/stores/app'
import { ref } from 'vue'

const appStore = useAppStore()

export function useLocation() {
    const checkAuthLocation = ref(true)
    // 判断是否需要开启定时器
    const initLocation = async () => {
        // 只有登录进来之后第一次进入页面需要授权定位，若用户没有授权定位，再次进来页面也不应该弹出授权窗口，而是引导用户点击顶部的开启按钮
        const flag = appStore.firstLogin
        if (flag) await appStore.getLocationFunc()
        checkAuthLocation.value = (await appStore._checkAuthByLocation()) as boolean
        if (flag && checkAuthLocation.value) appStore.updateLocation()
        appStore.setFirstLogin(false)
    }

    // 授权定位
    const openLocation = async () => {
        const flag = (await appStore.getLocationFunc()) as boolean
        checkAuthLocation.value = flag
        flag && appStore.updateLocation()
    }
    return {
        initLocation,
        openLocation,
        checkAuthLocation
    }
}
