import store from '@/store'

export default {
    parseUrl(page, param) {
        let params = ''
        if (param) {
            params = '?'
            for (let x in param) {
                params += (params === '?' ? '' : '&') + x + '=' + param[x]
            }
        }
        return `/pages/${page}/index${params}`
    },
    push(page, param) {
        let url = this.parseUrl(page, param)
        uni.navigateTo({
            url
        })
    },
    replace(page, param) {
        let url = this.parseUrl(page, param)
        uni.redirectTo({
            url
        })
    },
    switchTab(page, param) {
        let url = this.parseUrl(page, param)
        uni.switchTab({
            url
        })
    },
    pushCheck(page, param) {
        if (!store.state.token) {
            this.push('login')
            return
        }
        this.push(page, param)
    },
    replaceCheck(page, param) {
        if (!store.state.token) {
            this.push('login')
            return
        }
        this.replace(page, param)
    },
    back() {
        let curPages = getCurrentPages()
        if (curPages.length > 1) {
            uni.navigateBack({
                delta: 1
            })
        } else {
            uni.reLaunch({
                url: '/pages/award/index'
            })
        }
    },
    index() {
        uni.reLaunch({
            url: '/pages/award/index'
        })
    },
    web(url, title) {
		uni.navigateTo({
			url:`/pages/web/index?title=${title}&url=${encodeURIComponent(url)}`
		})
        // this.push('web', {
        //     title,
        //     url: encodeURIComponent(url)
        // })
    },
    login() {
        const pages = getCurrentPages()
        const page = pages[pages.length - 1]
        if (page.route === 'pages/login/index') {
            return
        }
        this.push('login')
    }
}
