/* eslint-disable no-unreachable */
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';
import store from '../store';
import { Page } from './Page';
import { Dialog } from 'vant';
import http from '../plugins/http';

jsapiSign();
function jsapiSign() {
    return;
    setTimeout(() => {
        if (/micromessenger/i.test(navigator.userAgent)) {
            if (/localhost|(192\.168)/i.test(location.host)) {
                return;
            }
            let isIOS = /iphone|ipad/i.test(navigator.userAgent);
            http.http
                .get('/wx/jsapiSign', { url: isIOS ? store.state.firstUrl : location.origin + location.href })
                .then(res => {
                    wx.config({
                        debug: false,
                        appId: res.appId,
                        timestamp: res.timestamp,
                        nonceStr: res.nonceStr,
                        signature: res.signature,
                        jsApiList: [
                            'chooseWXPay',
                            'updateAppMessageShareData',
                            'updateTimelineShareData',
                            'hideAllNonBaseMenuItem',
                            'scanQRCode'
                        ],
                        openTagList: ['wx-open-launch-weapp']
                    });
                    wx.error(function (res) {
                        console.log(res);
                    });
                    wx.ready(function () {
                        wx.updateAppMessageShareData({
                            title: 'Cosmos Art',
                            desc: '“Cosmos Art”为数字藏品展示、收藏、集换为一体的潮玩数字藏品平台',
                            link: location.origin + '/cosmos',
                            imgUrl: 'https://9space-2021.oss-cn-shenzhen.aliyuncs.com/thumb_image/6761642385045_.pic.jpg',
                            success: function () {
                                // 设置成功
                                console.log('7273');
                            }
                        });
                        wx.updateTimelineShareData({
                            title: 'Cosmos Art',
                            desc: '“Cosmos Art”为数字藏品展示、收藏、集换为一体的潮玩数字藏品平台',
                            link: location.origin + '/cosmos',
                            imgUrl: 'https://9space-2021.oss-cn-shenzhen.aliyuncs.com/thumb_image/6761642385045_.pic.jpg'
                        });
                    });
                })
                .catch(e => {});
        }
    }, 200);
}
jsapiSign();
const routes = [
    {
        path: '/',
        redirect: 'home'
    },
    {
        path: '/',
        name: 'index',
        component: () => import('../views/Index.vue'),
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('../views/Home.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: 'Cosmos Art'
                }
            },
            {
                path: '/discover',
                name: 'discover',
                component: () => import('../views/Discover.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: 'Cosmos Art'
                }
            },
            {
                path: '/creator',
                name: 'creator',
                component: () => import('../views/Creator.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: 'Cosmos Art'
                }
            },
            {
                path: '/store',
                name: 'store',
                component: () => import('../views/Store.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: 'Cosmos Art'
                }
            },
            {
                path: '/mine',
                name: 'mine',
                component: () => import('../views/Mine.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: 'Cosmos Art'
                }
            }
        ]
    },
    {
        path: '/login',
        name: 'userLogin',
        component: () => import('../views/account/Login.vue'),
        meta: {
            pageType: Page.Login,
            title: '登录'
        }
    },
    {
        path: '/agreement',
        name: 'agreement',
        component: () => import('../views/account/Agreement.vue'),
        meta: {
            pageType: Page.Login,
            title: '协议'
        }
    },
    {
        path: '/register',
        name: 'userRegister',
        component: () => import('../views/account/Register.vue'),
        meta: {
            pageType: Page.Login,
            title: '注册'
        }
    },
    {
        path: '/forget',
        name: 'userForget',
        component: () => import('../views/account/Forget.vue'),
        meta: {
            pageType: Page.Login,
            // menuPage: true,
            title: '忘记密码'
        }
    },
    {
        path: '/changePhone',
        name: 'changePhone',
        component: () => import('../views/account/ChangePhone.vue'),
        meta: {
            title: '修改密码'
        }
    },
    {
        path: '/verified',
        name: 'userVerified',
        component: () => import('../views/account/Verified.vue')
    },
    {
        path: '/verifiedSuc',
        name: 'verifiedSuc',
        component: () => import('../views/account/VerifiedSuc.vue')
    },
    {
        path: '/verifiedSucs',
        name: 'verifiedSucs',
        component: () => import('../views/account/VerifiedSucs.vue')
    },
    {
        path: '/Authentication',
        name: 'Authentication',
        component: () => import('../views/account/Authentication.vue')
    },
    {
        path: '/setting',
        name: 'userSetting',
        component: () => import('../views/account/Setting.vue')
    },
    {
        path: '/changeText',
        name: 'userChangeText',
        component: () => import('../views/account/ChangeText.vue')
    },
    {
        path: '/waiting',
        name: 'userWaiting',
        component: () => import('../views/account/Waiting.vue')
    },
    {
        path: '/security',
        name: 'security',
        component: () => import('../views/account/Security.vue')
    },
    {
        path: '/about',
        name: 'about',
        component: () => import('../views/account/About.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/introduction',
        name: 'introduction',
        component: () => import('../views/account/Introduction.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/question',
        name: 'question',
        component: () => import('../views/account/Question.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/connect',
        name: 'connect',
        component: () => import('../views/account/Connect.vue')
    },
    {
        path: '/tradingPassword',
        name: 'tradingPassword',
        component: () => import('../views/account/TradingPassword.vue')
    },
    {
        path: '/productDetail',
        name: 'productDetail',
        component: () => import('../views/product/Detail.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
            // tabColor: '#181818'
        }
    },
    {
        path: '/productDetail2',
        name: 'productDetail2',
        component: () => import('../views/product/DetailMode.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/productList',
        name: 'productList',
        component: () => import('../views/product/List.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/productSearch',
        name: 'productSearch',
        component: () => import('../views/product/Search.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/creatorDetail',
        name: 'creatorDetail',
        component: () => import('../views/creator/Detail.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/creatorList',
        name: 'creatorList',
        component: () => import('../views/creator/List.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/creatorSearch',
        name: 'creatorSearch',
        component: () => import('../views/creator/Search.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/assetDetail',
        name: 'assetDetail',
        component: () => import('../views/asset/Detail.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
            // tabColor: '#181818'
        }
    },
    { 
        path: '/assetPost',
        name: 'assetPost',
        component: () => import('../views/asset/Post.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
            // tabColor: '#181818'
        }
    },
    {
        path: '/Consignment',
        name: 'Consignment',
        component: () => import('../views/asset/Consignment.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/assetSearch',
        name: 'assetSearch',
        component: () => import('../views/asset/Search.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/giveSearch',
        name: 'giveSearch',
        component: () => import('../views/asset/GiveSearch.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/submit',
        name: 'submit',
        component: () => import('../views/Submit.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/couponList',
        name: 'couponList',
        component: () => import('../views/order/CouponList.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/givesubmit',
        name: 'givesubmit',
        component: () => import('../views/Givesubmit.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/creatorDetail',
        name: 'creatorDetail',
        component: () => import('../views/creator/Detail.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/ranking',
        name: 'ranking',
        component: () => import('../views/creator/Ranking.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/orders',
        name: 'orders',
        component: () => import('../views/order/Orders.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/orderDetail',
        name: 'orderDetail',
        component: () => import('../views/order/Detail.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/activityList',
        name: 'activityList',
        component: () => import('../views/activity/List.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/activityDetail',
        name: 'activityDetail',
        component: () => import('../views/activity/Detail.vue'),
        meta: {
            pageType: Page.Every,
            title: 'Cosmos Art'
        }
    },
    {
        path: '/mineFollows',
        name: 'mineFollows',
        component: () => import('../views/user/Follows.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/mineFollowers',
        name: 'mineFollowers',
        component: () => import('../views/user/Followers.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/mineLikes',
        name: 'mineLikes',
        component: () => import('../views/user/Likes.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/mineBanks',
        name: 'mineBanks',
        component: () => import('../views/user/Banks.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/mineBanksAdd',
        name: 'mineBanksAdd',
        component: () => import('../views/user/BanksAdd.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/mineAddress',
        name: 'mineAddress',
        component: () => import('../views/user/Address.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/mineAddressEdit',
        name: 'mineAddressEdit',
        component: () => import('../views/user/AddressEdit.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/mineWallet',
        name: 'mineWallet',
        component: () => import('../views/user/Wallet.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/minePoint',
        name: 'minePoint',
        component: () => import('../views/user/Point.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    },
    {
        path: '/mineExchange',
        name: 'mineExchange',
        component: () => import('../views/user/Exchange.vue'),
        meta: {
            title: 'Cosmos Art'
        }
    }
];

const router = createRouter({
    history: process.env.VUE_APP_CORDOVA === 'true' ? createWebHashHistory() : createWebHistory(process.env.BASE_URL),
    routes,
    scrollBehavior(to, from, savedPosition) {
        return { x: 0, y: 0 };
    }
});

router.beforeEach((to, from, next) => {
    if (/^\/http/.test(to.path)) {
        let url = to.path.replace('/', '');
        let params = [];
        if (to.query) {
            for (let key in to.query) {
                // eslint-disable-next-line no-prototype-builtins
                if (to.query.hasOwnProperty(key)) {
                    params.push(`${key}=${to.query[key]}`);
                }
            }
        }
        if (params.length > 0) {
            url += `?${params.join('&')}`;
        }
        window.open(url);
        return;
    }
    console.log(to);

    if (to.query.invitor) {
        sessionStorage.setItem('invitor', to.query.invitor);
    }

    if (!store.state.userInfo && to.meta.pageType !== Page.Login) {
        store
            .dispatch('getUserInfo')
            .then(() => {
                next(backNext(to));
            })
            .catch(() => {
                if (to.meta.pageType != Page.Every && to.path !== '/collectionDetail' && to.path !== '/castingDetail') {
                    Dialog.confirm({
                        title: '提示',
                        message: '用户未登录,是否立即登录'
                    })
                        .then(() => {
                            next('/login');
                        })
                        .catch(() => {
                            next(false);
                        });
                } else {
                    next(backNext(to));
                }
            });
    } else {
        next(backNext(to));
    }
});
router.afterEach((to, from) => {
    jsapiSign();
});

function backNext(to) {
    if (to.path === '/collectionDetail') {
        to.path = '/productDetail';
        return to;
    } else if (to.path === '/castingDetail') {
        to.path = '/creatorDetail';
        return to;
    } else {
        return;
    }
}
export default router;
