import { post } from "@/request/api.js";
import { setCache, getCache } from "@/utils/storage.js"
// 此执行为获取实名认证的开关状态
post("v1/isAuthentication").then(res => {
    if (res.code === 0) {
        uni.setStorageSync("isAuthentication", res.data.data);
    }
})

// 获取userinfo方法挂载uni公共对象上
uni.userfun = () => {
    return new Promise((resolve, reject) => {
        post("v1/user/userinfo").then((res) => {
            if (res.code === 0) {
                uni.setStorageSync("userinfo", res.data.data);
                resolve(res.data.data)
            } else {
                resolve({})
            }
        });
    })
}
uni.Luserfun = () => {
    return new Promise((resolve, reject) => {
        post("local/userinfo").then((res) => {
            if (res.code === 0) {
                uni.setStorageSync("localInfo", res.data);
                resolve(res.data)
            } else {
                resolve({})
            }
        });
    })
}

// 获取位置信息
uni.Location = () => {
    return new Promise((resolve, reject) => {
        let adres = getCache("location");
        if (!adres) {
            uni.getLocation({
                type: 'wgs84',
                success(res) {
                    // latitude: 22.53270706658251 longitude: 113.94161660883711
                    // {lat: 22.532707, lng: 113.941617}
                    let location = { lat: res.latitude, lng: res.longitude };
                    setCache("location", location, 60 * 2);
                    resolve(location);
                },
                fail(e) {
                    if (e.errMsg === 'getLocation:fail auth deny') {
                        let errMsgTit = '你已拒绝授权，是否跳转至设置页面开启权限';
                        uni.showModal({
                            title: '提示',
                            content: `${errMsgTit}`,
                            success(res) {
                                if (res.confirm) {
                                    // 获取设置页面权限信息
                                    uni.getSetting({
                                        success(res) {
                                            // 判断是否开启获取位置权限
                                            if (!res.authSetting['scope.userLocation']) {
                                                // 如果没有开启，点击确认后打开设置页面
                                                uni.openSetting({});
                                            }
                                        }
                                    });
                                    console.log('用户点击确定');
                                } else if (res.cancel) {
                                    console.log('用户点击取消');
                                }
                            }
                        });
                    }
                }
            });
        } else resolve(adres);
    })
}

Array.prototype.indexOf = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};
Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

// 逆地址解析reverseGeocoder
// import QQMapWX from '@/utils/qqmap-wx-jssdk.min.js'
// const showmap = new QQMapWX({ key: 'K7LBZ-RE23Q-JEN5D-4ZNEI-MS54Q-BHF6D' });
// showmap.reverseGeocoder({
//     success(result) {
//         console.log('逆地址解析：', result);
//     }
// });