export function onDialingPhoneNumber(phone) {
    return new Promise((resolve, reject) => {
        uni.makePhoneCall({
            phoneNumber: phone, // 电话号码
            success: (res) => {
                resolve(res)
            },
            fail: (err) => {
                reject(err)
            } // 失败
        });
    })
}
/**
 * 个位数，加0前缀
 * @param {*} number
 * @returns
 */
export function addZeroPrefix(number) {
    return number < 10 ? `0${number}` : number
}

export function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
    const R = 6371; // 地球平均半径，单位为公里
    const dLat = deg2rad(lat2 - lat1);
    const dLon = deg2rad(lon2 - lon1);
    const a =
        Math.sin(dLat / 2) * Math.sin(dLat / 2) +
        Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
        Math.sin(dLon / 2) * Math.sin(dLon / 2);
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    const d = R * c;
    return {
        km: d,
        m: d * 1000
    };
}

// 将角度转换为弧度
function deg2rad(deg) {
    return deg * (Math.PI / 180);
}

export function debounce(fun, delay) {
    return function(args) {
        let that = this;
        let _args = args;
        clearTimeout(fun.id);
        fun.id = setTimeout(() => {
            fun.call(that, _args)
        }, delay)
    }
}


export default {
    onDialingPhoneNumber,
    addZeroPrefix,
    getDistanceFromLatLonInKm,
    debounce
}