/*
 * @Description: file content
 * @Version: 2.0
 * @Author: ljl
 * @Date: 2022-06-15 17:53:10
 * @LastEditors: ljl
 * @LastEditTime: 2022-12-02 13:10:29
 * @FilePath: \zyypt-ssb-vue_alipay\src\router\index.js
 */
import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
// import Layout from '@/layout'

export const constantRoutes = [

    // {
    //     path: '/',
    //     name: 'Home',
    //     meta: {
    //         title: '中药安心达'
    //     },
    //     component: () =>
    //         import('@/views/index'),
    // },
    {
        path: '/',
        name: 'ConsultationRecord',
        meta: {
            title: '中药安心达'
        },
        component: () =>
            import('@/views/consultationRecord'),
    },
    {
        path: '/relativeInfo',
        name: 'RelativeInfo',
        meta: {
            title: '亲属信息'
        },
        component: () =>
            import('@/views/relativeInfo'),
    },
    {
        path: '/applyNotice',
        name: 'ApplyNotice',
        meta: {
            title: '添加须知'
        },
        component: () =>
            import('@/views/applyNotice'),
    },
    {
        path: '/addRelative',
        name: 'AddRelative',
        meta: {
            title: '亲属信息添加',
            keepAlive: true //配置路由元信息
        },
        component: () =>
            import('@/views/addRelative'),
    },
    {
        path: '/addSuccess',
        name: 'AddSuccess',
        meta: {
            title: '添加成功'
        },
        component: () =>
            import('@/views/addSuccess'),
    },
    {
        path: '/addError',
        name: 'AddError',
        meta: {
            title: '添加失败'
        },
        component: () =>
            import('@/views/addError'),
    },
    {
        path: '/prescriptionDetails',
        name: 'PrescriptionDetails',
        meta: {
            title: '处方明细'
        },
        component: () =>
            import('@/views/prescriptionDetails'),
    },
    {
        path: '/drugTrace',
        name: 'DrugTrace',
        meta:{
            title:'药品溯源'
        },
        component: () =>
            import('@/views/drugTrace'),
    },
    // {
    //     path: '/404',
    //     name: '404',
    //     component: () =>
    //         import('@/views/404'),
    // },

]


const createRouter = () => new Router({
    // mode: 'history', // require service support
    // base: '/dist',
    scrollBehavior: () => ({
        y: 0
    }),
    routes: constantRoutes
})

// const originalPush = Router.prototype.push
// Router.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err => err)
// }


const router = createRouter()
router.afterEach(route => {
    // 从路由的元信息中获取 title 属性
    if (route.meta.title) {
        document.title = route.meta.title;
        // 如果是 iOS 设备，则使用如下 hack 的写法实现页面标题的更新
        if (navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
            const hackIframe = document.createElement('iframe');
            hackIframe.style.display = 'none';
            hackIframe.src = '/static/html/fixIosTitle.html?r=' + Math.random();
            document.body.appendChild(hackIframe);
            setTimeout(_ => {
                document.body.removeChild(hackIframe)
            }, 300)
        }
    }
});


router.onError((error) => {
    const pattern = /Loading chunk (\d)+ failed/g;
    const isChunkLoadFailed = error.message.match(pattern);
    if (isChunkLoadFailed) {
        window.location.reload();
        // router.replace(router.history.pending.fullPath);
    } else {
        console.log("错误信息")
        // console.log(error)
    }
});

// 需要左方向动画的路由用this.$router.to('****')
Router.prototype.togo = function (path) {
    // this.isleft = true
    // this.isright = false
    this.push(path)
}
// 需要右方向动画的路由用this.$router.goRight('****')
Router.prototype.goRight = function (path) {
    // this.isright = true
    // this.isleft = false
    this.push(path)
}
// 需要返回按钮动画的路由用this.$router.goBack()，返回上一个路由
Router.prototype.goBack = function () {
    // this.isright = true
    // this.isleft = false
    this.go(-1)
}
// 点击浏览器返回按钮执行，此时不需要路由回退
Router.prototype.togoback = function () {
    // this.isright = true
    // this.isleft = false
}

export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router