import Vue from 'vue'
import valid from './valid'
import message from './message'
import common from './common'
import cache from './cache'
import router from './router'
import service from '../service'
import event from './event'
import throttle from './throttle'
import wxLog from './wxLog'

const logger = {
    log(message) {
        if (process.env.NODE_ENV != 'production') {
            console.log(message)
        }
    }
}

export const init = () => {
    // #ifdef MP-WEIXIN
    Vue.mixin({
        methods: {
            setTabBarIndex(index) {
                if (this.$mp.page && typeof this.$mp.page.getTabBar === 'function' && this.$mp.page.getTabBar()) {
                    this.$mp.page.getTabBar().setData({
                        selected: index
                    })
                }
            }
        }
    })
    // #endif
}

export const initUser = async () => {
    if (common.isLogin()) return
    uni.login({
        scopes: 'auth_base',
        success: async result => {
            if (result.code) {
                const res = await service.user.login(result.code)
                if (res && !res.needLogin) {
                    service.user.info()
                    event.emit(event.key.LOGIN)
                }
                res && event.emit(event.key.LANUCH_COUPON)
            }
        }
    })
}


export { logger, valid, message, common, cache, router, event, throttle, wxLog }
