import { enCodeQueryStr } from '@/common/share.helper.js'
import { queryParse } from '@/common/utils.js'

const shareEntry = {
    data() {
        return {
            // 分享链接携带的数据
            shareData: {
                type: '',
                inviteUserId: '', // 邀请者用户ID
                activityId: '', // 分享者活动ID
                dealerUserId: '', // 协销用户ID
                keyWord: '', // 搜索关键词
                productId: '', // 产品ID
                jumpState: '', // 跳转类型 普通用户 协销
                collageId: ''
            }
        }
    },
    methods: {
        // 处理分享参数
        shareHandle(query, type) {
            if (!query) return
            console.log(query)
            // 小程序分享
            if (type === 'state_str') {
                this.shareData = {
                    ...this.shareData,
                    ...JSON.parse(this.$crypto.decrypt(decodeURIComponent(query)))
                }
            } else if (type === 'scene') {
                this.shareData = {
                    ...this.shareData,
                    ...queryParse(enCodeQueryStr(decodeURIComponent(query)))
                }
            } else {
                return this.$router.switchTab('home')
            }

            // 保存分享者的用户id
            if (this.shareData.inviteUserId) {
                this.$store.commit('user/SET_INVITE_USER_ID', this.shareData.inviteUserId)
            }

            console.log(this.shareData)

            this.shareData.type = parseInt(this.shareData.type)
            this.jumpTopSharePage()
        },

        // 跳转到对应的分享页面
        jumpTopSharePage() {
            const { type = 0, collageId, jumpState, productId, activityId, dealerUserId, keyWord } = this.shareData
            const path = {
                0: 'home', //首页
                1: `goods/goods-detail?type=share&jumpState=${jumpState}&productId=${productId}`, //商品详情
                // 2: `goods/goods-search?keyWord=${keyWord}`, // 搜索页面
                3: `activity/activity-detail?type=share&activityId=${activityId}&dealerUserId=${dealerUserId}`, //活动商品列表
                4: `share-buy/share-buy-entry?type=share&collageId=${collageId}` // 拼团分享
            }

            if (type === 0) {
                this.$router.switchTab(path[type])
            } else {
                this.$router.redirectTo(path[type])
            }
        }
    }
}

export default shareEntry
