<template>
    <view class=""><tui-skeleton v-if="true" :loadingType="3" :isLoading="true"></tui-skeleton></view>
</template>

<script>
import { mapActions, mapMutations } from 'vuex'
import shareEntry from './mixins/share-entry.js'

// 页面初始化
export default {
    mixins: [shareEntry],
    data() {
        return {
            scene: '',
            state_str: ''
        }
    },
    onLoad(options) {
        this.scene = options.scene
        this.state_str = options.state_str
        this.initUserInfo()
    },
    methods: {
        ...mapMutations('user', ['SET_USER_INFO']),
        ...mapActions('user', ['wxAutoLogin', 'getAccessToken']),
        ...mapActions('coupon', ['initCouponCount']),
        ...mapActions('cart', ['fetchCartKindCount']),

        // 初始化页面数据
        initPageData() {
            this.getAccessToken()
            this.initCouponCount()
            this.fetchCartKindCount()
        },

        // 初始化用户信息
        async initUserInfo() {
            try {
                const user = this.$getStorage('USER_INFO')
                if (user) {
                    // 用户已登录
                    this.SET_USER_INFO(user)
                } else {
                    // 微信登录
                    await this.wxAutoLogin()
                }
            } catch (e) {
                console.log('初始化用户信息时出现了错误')
                console.log(e)
            } finally {
                this.initPageData()
                this.redirectToEntry()
            }
        },

        // 跳转入口页面
        redirectToEntry() {
            if (this.scene) {
                // 处理分享信息
                this.shareHandle(this.scene, 'scene')
            } else if (this.state_str) {
                this.shareHandle(this.state_str, 'state_str')
            } else {
                // 默认调整首页
                this.$router.switchTab('home')
            }
        }
    }
}
</script>
