/**
 * 定位相关的业务服务
 */
// import { qqMapKey } from '@/common/config.js'
// const QQMapWX = require('@/static/base/js/qqmap-wx-jssdk.min.js')
// const qqmapsdk = new QQMapWX({
//   coord_type: 5,
//   key: qqMapKey
// })

export default class LocateService {
    constructor(AjaxService) {
        Object.assign(this, { AjaxService })
        this.name = 'LocateService'
    }
    /* 获取当前的地理位置 */
    getGeoLocation() {
        return new Promise(function(reslove, reject) {
            wx.getLocation({
                type: 'gcj02',
                success: function(res) {
                    reslove(res)
                },
                fail: function(err) {
                    reject(err)
                },
                complete: function(res) {}
            })
        })
    }

    /* 获取城市名 */
    getCityName(lat, lon) {
        return new Promise(function(reslove, reject) {
            qqmapsdk.reverseGeocoder({
                location: {
                    latitude: lat,
                    longitude: lon
                },
                success: function(res) {
                    const {
                        ad_info: addressInfo,
                        location,
                        formatted_addresses: formattedAddress = {},
                        address
                    } = res.result || {}

                    let locationInfo = {
                        cityName: addressInfo.city,
                        location: location,
                        address: formattedAddress.recommend || address
                    }
                    console.log('locationInfo: ' + locationInfo)
                    reslove(locationInfo)
                },
                fail: function(err) {
                    reject(err)
                }
            })
        })
    }

    /* 获取当前定位 */
    async getLocationByLocate() {
        // 获取地理位置
        let locationInfo = await this.getGeoLocation()
        const { latitude, longitude } = locationInfo || {}
        // 获取城市名
        let cityInfo = await this.getCityName(latitude, longitude)
        const { location = {}, cityName, address } = cityInfo
    }
}
