
var secret = 'CBSBZ-QTZH3-KLQ3F-YWBAX-VXTB2-5KF3R';
function getLocation(that) {
    return new Promise((succesPromise) => {
        uni.getLocation({
            success: function (res) {
                var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
                var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
                var speed = res.speed; // 速度，以米/每秒计
                var accuracy = res.accuracy; // 位置精度
                setLocation(res).then(setLocation_Succss => {
                    succesPromise(setLocation_Succss);
                });
            },
            cancel: function (res) {
                // alert(ju_jue_tip);
            },
        })
    })
}

function setLocation(res) {
    return new Promise(setLocation_Succss => {
        getApp().globalData.setCache(
            "mypos",
            {
                lat: res.latitude,
                lng: res.longitude,
                speed: res.speed,
                accuracy: res.accuracy,
            },
            7200
        );

        getApp().globalData.request({
            s: 'Tools_Location_Index.url',
            url: 'https://apis.map.qq.com/ws/geocoder/v1/?' + 'location=' + res.latitude + ',' + res.longitude + '&key=' + secret,
        }).then(result => {
            setLocation_Succss(result.data.data.result);
        });
    });
}

module.exports = {
    getLocation: getLocation,
    getAddress2Gps: function (addressValue) {
        return new Promise(function (success, fail) {
            uni.request({
                url: 'https://apis.map.qq.com/ws/geocoder/v1/?address=' + encodeURI(addressValue) + '&key=CBSBZ-QTZH3-KLQ3F-YWBAX-VXTB2-5KF3R',
                complete: function (res) {
                    if (typeof res.data.result.location.lng != 'undefined'
                        && res.data.result.location.lng) {
                        success(res.data.result)
                    } else {
                        fail(res.data)
                    }
                }
            })
        })
    }
}