// 分享
import { mapMutations } from 'vuex'

const shareEntry = {
    data() {
        return {
            // 分享链接携带的数据
            shareData: {
                type: '',
                inviteUserId: '',
                activityId: '',
                productId: '',
                userId: '',
                keyWord: '',
                jumpState: ''
            }
        }
    },
    methods: {
        ...mapMutations('user', ['setInviteUserId']),
        // 处理分享参数
        shareHandle(option) {
            let state_str = option.state_str
            if (!state_str) return
            state_str = decodeURIComponent(state_str)
            try {
                const decrypt = this.$crypto.decrypt(state_str)
                // 解密参数
                this.shareData = {
                    ...this.shareData,
                    ...JSON.parse(decrypt)
                }
                // 保存分享者的用户id
                this.setInviteUserId(this.shareData.inviteUserId)
                this.jumpTopSharePage()
            } catch (e) {
                console.log(e)
                return
            }
        },
        // 跳转到对应的分享页面
        jumpTopSharePage() {
            if (!this.shareData.type || this.shareData.type === 0) return
            const path = {
                0: '/pages/tabBar/index/index', //首页
                1: `/pages/goods/product-detail?productId=${this.shareData.productId}&jumpState=${this.shareData.jumpState}`, //商品详情
                2: `/pages/goods/search?type=share&keyWord=${this.shareData.keyWord}`, // 搜索页面
                3: `/pages/user/activity/activity?type=share&activityId=${this.shareData.activityId}&userId=${this.shareData.userId}` //活动商品列表
            }
            // 1000 ms后跳转
            setTimeout(() => {
                this.$api.navigateTo(path[this.shareData.type])
            }, 1000)
        }
    }
}

export default shareEntry
