import {
    defineStore
} from 'pinia';
import {
    ref
} from 'vue'
import {
    msg
} from '@/common/globalFunction.js'
const useLocationStore = defineStore("location", () => {
    // 定义状态
    const longitudeVal = ref('') // 经度
    const latitudeVal = ref('') //纬度

    function getLocation() {
        return new Promise((resole, reject) => {
            uni.getLocation({
                type: 'wgs84',
                altitude: true,
                isHighAccuracy: true,
                enableHighAccuracy: true, // 关键参数：启用传感器辅助 
                timeout: 10000,
                success: function(res) {
                    const resd = {
                        longitude: 120.382665,
                        latitude: 36.066938
                    }
                    longitudeVal.value = resd.longitude
                    latitudeVal.value = resd.latitude
                    msg('用户位置获取成功')
                    resole(resd)
                },
                fail: function(err) {
                    // longitudeVal.value = ''
                    // latitudeVal.value = ''
                    // reject(err)
                    const resd = {
                        longitude: 120.382665,
                        latitude: 36.066938
                    }
                    longitudeVal.value = resd.longitude
                    latitudeVal.value = resd.latitude
                    msg('用户位置获取失败，使用模拟定位')
                    resole(resd)
                },
                complete: function(e) {
                    console.warn('getUserLocation' + JSON.stringify(e))
                }
            })
        })
    }

    function longitude() {
        return longitudeVal.value
    }

    function latitude() {
        return latitudeVal.value
    }

    // 导入
    return {
        getLocation,
        longitude,
        latitude,
    }
})

export default useLocationStore;