import { createRouter, createWebHistory } from 'vue-router';
import store from '../store';
import { Page } from './Page';
import { Dialog } from 'vant';
import http from '../plugins/http';

console.log(http);
function jsapiSign() {
    if (/micromessenger/i.test(navigator.userAgent)) {
        http.http
            .get('/wx/jsapiSign', { url: location.origin + location.pathname })
            .then(res => {
                res.debug = false;
                res.jsApiList = [
                    'chooseWXPay',
                    'updateAppMessageShareData',
                    'updateTimelineShareData',
                    'hideAllNonBaseMenuItem',
                    'scanQRCode'
                ];
                wx.config(res);
            })
            .catch(e => {});
    }
}
jsapiSign();
const routes = [
    {
        path: '/',
        redirect: 'home'
    },
    {
        path: '/',
        component: () => import('../views/Index.vue'),
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('../views/Home.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: '第九空间'
                }
            },
            {
                path: '/discover',
                name: 'discover',
                component: () => import('../views/Discover.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: '第九空间'
                }
            },
            {
                path: '/creator',
                name: 'creator',
                component: () => import('../views/Creator.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: '第九空间'
                }
            },
            {
                path: '/store',
                name: 'store',
                component: () => import('../views/Store.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: '第九空间'
                }
            },
            {
                path: '/mine',
                name: 'mine',
                component: () => import('../views/Mine.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    title: '第九空间'
                }
            }
        ]
    },
    {
        path: '/login',
        name: 'userLogin',
        component: () => import('../views/account/Login.vue'),
        meta: {
            pageType: Page.Login,
            menuPage: true,
            title: '登录'
        }
    },
    {
        path: '/register',
        name: 'userRegister',
        component: () => import('../views/account/Register.vue'),
        meta: {
            pageType: Page.Login,
            menuPage: true,
            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: '/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: '/tradingPassword',
        name: 'tradingPassword',
        component: () => import('../views/account/TradingPassword.vue')
    },
    {
        path: '/productDetail',
        name: 'productDetail',
        component: () => import('../views/product/Detail.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/productList',
        name: 'productList',
        component: () => import('../views/product/List.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/productSearch',
        name: 'productSearch',
        component: () => import('../views/product/Search.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/creatorDetail',
        name: 'creatorDetail',
        component: () => import('../views/creator/Detail.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/creatorList',
        name: 'creatorList',
        component: () => import('../views/creator/List.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/creatorSearch',
        name: 'creatorSearch',
        component: () => import('../views/creator/Search.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/assetDetail',
        name: 'assetDetail',
        component: () => import('../views/asset/Detail.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/assetSearch',
        name: 'assetSearch',
        component: () => import('../views/asset/Search.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/submit',
        name: 'submit',
        component: () => import('../views/Submit.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/creatorDetail',
        name: 'creatorDetail',
        component: () => import('../views/creator/Detail.vue'),
        meta: {
            pageType: Page.Every,
            title: '第九空间'
        }
    },
    {
        path: '/orders',
        name: 'orders',
        component: () => import('../views/order/Orders.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/orderDetail',
        name: 'orderDetail',
        component: () => import('../views/order/Detail.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineFollows',
        name: 'mineFollows',
        component: () => import('../views/user/Follows.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineFollowers',
        name: 'mineFollowers',
        component: () => import('../views/user/Followers.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineLikes',
        name: 'mineLikes',
        component: () => import('../views/user/Likes.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineAddress',
        name: 'mineAddress',
        component: () => import('../views/user/Address.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineAddressEdit',
        name: 'mineAddressEdit',
        component: () => import('../views/user/AddressEdit.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/mineWallet',
        name: 'mineWallet',
        component: () => import('../views/user/Wallet.vue'),
        meta: {
            title: '第九空间'
        }
    },
    {
        path: '/minePoint',
        name: 'minePoint',
        component: () => import('../views/user/Point.vue'),
        meta: {
            title: '第九空间'
        }
    }
];

const router = createRouter({
    history: 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;
    }
    if (!store.state.userInfo && to.meta.pageType !== Page.Login) {
        store
            .dispatch('getUserInfo')
            .then(() => {
                next();
            })
            .catch(() => {
                if (to.meta.pageType != Page.Every) {
                    Dialog.confirm({
                        title: '提示',
                        message: '用户未登录,是否立即登录'
                    })
                        .then(() => {
                            next('/login');
                        })
                        .catch(() => {
                            next(false);
                        });
                } else {
                    next();
                }
            });
    } else {
        next();
    }
});
router.afterEach((to, from) => {
    jsapiSign();
});
export default router;
