import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '@/store/index';
const jwt = require('jsonwebtoken');
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
Vue.use(VueRouter);
const market = [
  {
    path: '/market/patentInfo/index',
    meta: { title: '专利超市-专利检索' },
    component: () => import('../views/market/patentInfo/index.vue'),
  },
  {
    path: '/market/transfer/index',
    meta: { title: '专利超市-专利转让' },
    component: () => import('../views/market/transfer/index.vue'),
  },
  {
    path: '/market/demand/index',
    meta: { title: '专利超市-专利需求' },
    component: () => import('../views/market/demand/index.vue'),
  },
  {
    path: '/market/demand/detail',
    meta: { title: '专利需求-详细信息' },
    component: () => import('../views/market/demand/detail.vue'),
  },
  {
    path: '/market/navigation/index',
    meta: { title: '专利超市-专利导航' },
    component: () => import('../views/market/navigation/index.vue'),
  },
  {
    path: '/market/navigation/detail',
    meta: { title: '专利导航-详细信息' },
    component: () => import('../views/market/navigation/detail.vue'),
  },
];
const service = [
  {
    path: '/service/notice/index',
    meta: { title: '资讯服务-政策资讯' },
    component: () => import('../views/service/notice/index.vue'),
  },
  {
    path: '/service/notice/detail',
    meta: { title: '政策资讯-详细信息' },
    component: () => import('../views/service/notice/detail.vue'),
  },
  {
    path: '/service/law/index',
    meta: { title: '资讯服务-法律法规' },
    component: () => import('../views/service/law/index.vue'),
  },
  {
    path: '/service/law/detail',
    meta: { title: '法律法规-详细信息' },
    component: () => import('../views/service/law/detail.vue'),
  },
  {
    path: '/service/dimension/index',
    meta: { title: '资讯服务-研究报告' },
    component: () => import('../views/service/dimension/index.vue'),
  },
  {
    path: '/service/dimension/detail',
    meta: { title: '研究报告-详细信息' },
    component: () => import('../views/service/dimension/detail.vue'),
  },
  {
    path: '/service/interflow/index',
    meta: { title: '资讯服务-使用指南' },
    component: () => import('../views/service/interflow/index.vue'),
  },
  {
    path: '/service/interflow/detail',
    meta: { title: '使用指南-详细信息' },
    component: () => import('../views/service/interflow/detail.vue'),
  },
];
const account = [
  {
    path: '/account/index',
    meta: { title: '管理中心' },
    component: () => import('../views/account/index.vue'),
  },
  {
    path: '/account/mechanism/index',
    meta: { title: '机构用户' },
    component: () => import('../views/account/mechanism/index.vue'),
  },
  {
    path: '/account/mechanism/detail',
    meta: { title: '机构用户-信息管理' },
    component: () => import('../views/account/mechanism/detail.vue'),
  },
  {
    path: '/account/users/index',
    meta: { title: '平台用户' },
    component: () => import('../views/account/users/index.vue'),
  },
  {
    path: '/account/users/detail',
    meta: { title: '平台用户-信息管理' },
    component: () => import('../views/account/users/detail.vue'),
  },
  {
    path: '/account/patent/index',
    meta: { title: '平台用户-专利信息' },
    component: () => import('../views/account/patent/index.vue'),
  },
  {
    path: '/account/users/import_user',
    meta: { title: '平台用户-批量注册用户' },
    component: () => import('../views/account/users/import_user.vue'),
  },
  {
    path: '/account/agentmech/index',
    meta: { title: '代理机构' },
    component: () => import('../views/account/agentmech/index.vue'),
  },
  {
    path: '/account/agentmech/detail',
    meta: { title: '代理机构-信息管理' },
    component: () => import('../views/account/agentmech/detail.vue'),
  },
  {
    path: '/account/answertea/index',
    meta: { title: '咨询师' },
    component: () => import('../views/account/answertea/index.vue'),
  },
  {
    path: '/account/answertea/detail',
    meta: { title: '咨询师-信息管理' },
    component: () => import('../views/account/answertea/detail.vue'),
  },
  {
    path: '/account/basic/index',
    meta: { title: '基本信息' },
    component: () => import('../views/account/basic/index.vue'),
  },
  {
    path: '/account/password/index',
    meta: { title: '修改密码' },
    component: () => import('../views/account/password/index.vue'),
  },
];
const patent = [
  {
    path: '/patent/index',
    meta: { title: '专利管理中心' },
    component: () => import('../views/patent/index.vue'),
  },
  // 专利超市
  {
    path: '/patent/market/index',
    meta: { title: '专利超市' },
    component: () => import('../views/patent/market/index.vue'),
  },
  {
    path: '/patent/market/detail',
    meta: { title: '专利超市-详细信息' },
    component: () => import('../views/patent/market/detail.vue'),
  },
  {
    path: '/patent/market/transDetail',
    meta: { title: '专利超市-详细信息' },
    component: () => import('../views/patent/market/transDetail.vue'),
  },
  // 资讯服务
  {
    path: '/patent/user/chat/index',
    meta: { title: '咨询服务' },
    component: () => import('../views/patent/user/chat/index.vue'),
  },
  {
    path: '/patent/user/chat/detail',
    meta: { title: '咨询服务-问题编辑' },
    component: () => import('../views/patent/user/chat/detail.vue'),
  },
  // 专利运营-个人用户
  // 我的消息
  {
    path: '/patent/user/message/unRead',
    meta: { title: '未读信息查看' },
    component: () => import('../views/patent/user/message/unRead.vue'),
  },
  {
    path: '/patent/user/message/read',
    meta: { title: 'CPC消息' },
    component: () => import('../views/patent/user/message/read.vue'),
  },
  {
    path: '/patent/user/message/notice',
    meta: { title: '通知查看' },
    component: () => import('../views/patent/user/message/notice.vue'),
  },
  // 我的申请
  // 专利申请
  {
    path: '/patent/user/apply/apply',
    meta: { title: '专利申请' },
    component: () => import('../views/patent/user/apply/apply/index.vue'),
  },
  {
    path: '/patent/user/apply/apply/gzjDetail',
    meta: { title: '专利申请-国知局信息' },
    component: () => import('../views/patent/user/apply/apply/gzjDetail.vue'),
  },
  // 查新检索
  {
    path: '/patent/user/apply/analysis',
    meta: { title: '查新检索' },
    component: () => import('../views/patent/user/apply/analysis/index.vue'),
  },
  //价值评估
  {
    path: '/patent/user/apply/assessment',
    meta: { title: '价值评估' },
    component: () => import('../views/patent/user/apply/assessment/index.vue'),
  },
  // 我的专利
  // 专利信息
  {
    path: '/patent/user/patent/information',
    meta: { title: '专利管理' },
    component: () => import('../views/patent/user/patent/information/index.vue'),
  },
  {
    path: '/patent/user/patent/information/trans_create',
    meta: { title: '专利管理-发起交易' },
    component: () => import('../views/patent/user/patent/information/trans_create.vue'),
  },
  // 专利维权
  {
    path: '/patent/user/patent/safe',
    meta: { title: '专利维权' },
    component: () => import('../views/patent/user/patent/safe/index.vue'),
  },
  // 专利预警
  {
    path: '/patent/user/patent/early',
    meta: { title: '专利预警' },
    component: () => import('../views/patent/user/patent/early/index.vue'),
  },
  // 国知局反馈信息
  {
    path: '/patent/user/patent/gzj',
    meta: { title: '国知局反馈信息' },
    component: () => import('../views/patent/user/patent/gzj/index.vue'),
  },
  // 我的交易
  {
    path: '/patent/user/transaction/index',
    meta: { title: '专利交易' },
    component: () => import('../views/patent/user/transaction/index.vue'),
  },
  {
    path: '/patent/user/transaction/contract',
    meta: { title: '专利交易-填写合同' },
    component: () => import('../views/patent/user/transaction/contract.vue'),
  },
  // 机构用户
  // 我的消息
  {
    path: '/patent/mech/message/examine',
    meta: { title: '审核通知' },
    component: () => import('../views/patent/mech/message/examine.vue'),
  },
  {
    path: '/patent/mech/message/notice',
    meta: { title: '通知信息' },
    component: () => import('../views/patent/mech/message/notice.vue'),
  },
  {
    path: '/patent/mech/message/notice_create',
    meta: { title: '通知信息-编辑通知' },
    component: () => import('../views/patent/mech/message/notice_create.vue'),
  },
  // 我的审核
  {
    path: '/patent/mech/examine/patent',
    meta: { title: '专利审核' },
    component: () => import('../views/patent/mech/examine/patent.vue'),
  },
  {
    path: '/patent/mech/examine/trans_examine',
    meta: { title: '交易审核' },
    component: () => import('../views/patent/mech/examine/trans_examine.vue'),
  },
  {
    path: '/patent/mech/examine/contract',
    meta: { title: '合同审核' },
    component: () => import('../views/patent/mech/examine/contract.vue'),
  },
  {
    path: '/patent/mech/examine/trans',
    meta: { title: '交易归档' },
    component: () => import('../views/patent/mech/examine/trans.vue'),
  },
  // 我的专利
  {
    path: '/patent/mech/patent/information',
    meta: { title: '专利信息' },
    component: () => import('../views/patent/mech/patent/information/index.vue'),
  },
  {
    path: '/patent/mech/patent/navigation',
    meta: { title: '专利检索' },
    component: () => import('../views/patent/mech/patent/navigation/index.vue'),
  },
  {
    path: '/patent/mech/patent/navigation/info',
    meta: { title: '专利检索-详细信息' },
    component: () => import('../views/patent/mech/patent/navigation/info.vue'),
  },
  {
    path: '/patent/mech/patent/early',
    meta: { title: '专利预警' },
    component: () => import('../views/patent/mech/patent/early/index.vue'),
  },
  // 我的交易
  {
    path: '/patent/mech/transaction/index',
    meta: { title: '专利交易' },
    component: () => import('../views/patent/mech/transaction/index.vue'),
  },
  // 管理用户
  // 我的消息
  {
    path: '/patent/admin/message/question',
    meta: { title: '问题管理' },
    component: () => import('../views/patent/admin/message/question.vue'),
  },
  {
    path: '/patent/admin/message/service',
    meta: { title: '咨询服务' },
    component: () => import('../views/patent/admin/message/service.vue'),
  },
  {
    path: '/patent/admin/message/service_detail',
    meta: { title: '咨询服务-聊天详情' },
    component: () => import('../views/patent/admin/message/service_detail.vue'),
  },
  {
    path: '/patent/admin/message/notice',
    meta: { title: '通知信息' },
    component: () => import('../views/patent/admin/message/notice.vue'),
  },
  {
    path: '/patent/admin/message/notice_create',
    meta: { title: '通知信息-编辑通知' },
    component: () => import('../views/patent/admin/message/notice_create.vue'),
  },
  {
    path: '/patent/admin/message/examine',
    meta: { title: '审核通知' },
    component: () => import('../views/patent/admin/message/examine.vue'),
  },
  {
    path: '/patent/admin/message/warning',
    meta: { title: '专利预警' },
    component: () => import('../views/patent/admin/message/warning.vue'),
  },
  // 专利审核
  {
    path: '/patent/admin/examine/patent',
    meta: { title: '专利申请' },
    component: () => import('../views/patent/admin/examine/patent.vue'),
  },
  {
    path: '/patent/admin/examine/hairmess',
    meta: { title: '分发消息' },
    component: () => import('../views/patent/admin/examine/hairmess.vue'),
  },
  {
    path: '/patent/admin/examine/hairmesserror',
    meta: { title: '分发消息(错误)' },
    component: () => import('../views/patent/admin/examine/hairmesserror.vue'),
  },
  {
    path: '/patent/admin/examine/hairmess_create',
    meta: { title: '分发消息-添加消息' },
    component: () => import('../views/patent/admin/examine/hairmess_create.vue'),
  },
  {
    path: '/patent/admin/examine/hairmess_autoCreate',
    meta: { title: '分发消息-自动分发消息' },
    component: () => import('../views/patent/admin/examine/hairmess_autoCreate.vue'),
  },
  {
    path: '/patent/admin/examine/hairmess_manualCreate',
    meta: { title: '分发消息-手动分发消息' },
    component: () => import('../views/patent/admin/examine/hairmess_manualCreate.vue'),
  },
  {
    path: '/patent/admin/examine/analysis',
    meta: { title: '查新检索' },
    component: () => import('../views/patent/admin/examine/analysis.vue'),
  },
  // 专利管理
  {
    path: '/patent/admin/patent/info',
    meta: { title: '专利信息' },
    component: () => import('../views/patent/admin/patent/index.vue'),
  },
  {
    path: '/patent/admin/patent/detail',
    meta: { title: '专利信息-信息管理' },
    component: () => import('../views/patent/admin/patent/detail.vue'),
  },
  {
    path: '/patent/admin/patent/import',
    meta: { title: '专利信息-专利导入' },
    component: () => import('../views/patent/admin/patent/import.vue'),
  },
  {
    path: '/patent/admin/patent/distribute',
    meta: { title: '专利信息批量分配' },
    component: () => import('../views/patent/admin/patent/distribute.vue'),
  },
  {
    path: '/patent/admin/patent/assessment',
    meta: { title: '价值评估' },
    component: () => import('../views/patent/admin/patent/assessment.vue'),
  },
  {
    path: '/patent/admin/patent/trans',
    meta: { title: '专利交易' },
    component: () => import('../views/patent/admin/patent/trans.vue'),
  },
  {
    path: '/patent/admin/patent/trans_import',
    meta: { title: '专利交易-信息导入' },
    component: () => import('../views/patent/admin/patent/trans_import.vue'),
  },
  {
    path: '/patent/admin/patent/trans_create',
    meta: { title: '专利交易-信息维护' },
    component: () => import('../views/patent/admin/patent/trans_create.vue'),
  },
  {
    path: '/patent/admin/patent/safeg',
    meta: { title: '专利维权' },
    component: () => import('../views/patent/admin/safeg/index.vue'),
  },
  {
    path: '/patent/admin/patent/techol',
    meta: { title: '专利需求' },
    component: () => import('../views/patent/admin/techol/index.vue'),
  },
  // 咨询师
  {
    path: '/patent/answertea/message/question',
    meta: { title: '问题管理' },
    component: () => import('../views/patent/answertea/message/question.vue'),
  },
  // 代理机构
  {
    path: '/patent/agentmech/message/examine',
    meta: { title: '审核通知' },
    component: () => import('../views/patent/agentmech/message/examine.vue'),
  },
  {
    path: '/patent/agentmech/examine/patent',
    meta: { title: '专利审核' },
    component: () => import('../views/patent/agentmech/examine/patent.vue'),
  },
  {
    path: '/patent/agentmech/apply/apply',
    meta: { title: '专利申请' },
    component: () => import('../views/patent/agentmech/apply/apply/index.vue'),
  },
  {
    path: '/patent/agentmech/apply/apply/gzjDetail',
    meta: { title: '专利申请-国知局信息' },
    component: () => import('../views/patent/agentmech/apply/apply/gzjDetail.vue'),
  },
];
const patentCommon = [
  {
    path: '/commonData/transaction/index',
    meta: { title: '专利转让-添加信息' },
    component: () => import('../views/commonData/transaction/index.vue'),
  },
];
const web = [
  {
    path: '/',
    name: 'index',
    meta: { title: '网站首页' },
    component: () => import('../views/index.vue'),
  },
  ...market,
  ...service,
  ...account,
  ...patent,
  ...patentCommon,
  {
    path: '/login',
    name: 'login',
    meta: { title: '登录', key: 'login' },
    component: () => import('../views/login.vue'),
  },
  {
    path: '/register',
    name: 'register',
    meta: { title: '账户注册', key: 'register' },
    component: () => import('../views/register.vue'),
  },
  // 常用方法页面
  {
    path: '/test',
    name: 'test',
    meta: { title: '登录', key: 'test' },
    component: () => import('../views/test.vue'),
  },
  {
    path: '/patent',
    meta: { title: '吉林专利市场', key: 'patent' },
    component: () => import('../views/patent.vue'),
  },
];
const routes = [...web];
const router = new VueRouter({
  mode: 'history',
  base: process.env.VUE_APP_ROUTER,
  routes,
});
router.beforeEach((to, from, next) => {
  let openid = to.query.openid;
  sessionStorage.setItem('openid', openid);
  document.title = `${to.meta.title} `;
  const token = localStorage.getItem('token');
  if (to.path == '/account/index' || to.path == '/patent/index') {
    if (!token) {
      next(`/login?path=${to.path}`);
    } else {
      let user = jwt.decode(token);
      store.commit('setUser', user, { root: true });
      next();
    }
  } else {
    let user = jwt.decode(token);
    store.commit('setUser', user, { root: true });
    next();
  }
});

export default router;
