router.js 4.9 KB
Newer Older
sin's avatar
sin committed
1 2 3
import Vue from 'vue';
import Router from 'vue-router';

YunaiV's avatar
YunaiV committed
4 5
import { getAccessToken } from '../utils/cache';

sin's avatar
sin committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Vue.use(Router);

const routes = [
  {
    path: '*',
    redirect: '/home'
  },
  {
    name: 'home',
    component: () => import('../page/index'),
    meta: {
      title: '首页'
    }
  },
  {
		path: '/login',
    component: () => import('../page/account/phonelogin'),
    meta: {
24
      title: '登录'
sin's avatar
sin committed
25 26
    }
  },
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  // {
	// 	path: '/login/password',
  //   component: () => import('../page/account/password'),
  //   meta: {
  //     title: '登录'
  //   }
  // },
  // {
	// 	path: '/login/phone',
  //   component: () => import('../page/account/phonelogin'),
  //   meta: {
  //     title: '手机号登录'
  //   }
  // },
  // {
	// 	path: '/login/register',
  //   component: () => import('../page/account/register'),
  //   meta: {
  //     title: '注册'
  //   }
  // },
sin's avatar
sin committed
48 49 50 51 52 53 54 55 56 57 58 59 60
  {
		path: '/user/index',
    component: () => import('../page/user/index'),
    name: 'user',
    meta: {
      title: '会员中心'
    }
  },
  {
		path: '/user/info',
    component: () => import('../page/user/info/detail'),
    name: 'user',
    meta: {
61 62
      title: '个人信息',
      requireAuth: true,
sin's avatar
sin committed
63 64 65 66 67 68
    }
  },
  {
    path: '/user/address',
    component: () => import('../page/user/address/list'),
    meta: {
YunaiV's avatar
YunaiV committed
69 70
      title: '我的地址',
      requireAuth: true,
sin's avatar
sin committed
71 72 73 74 75 76
    }
  },
  {
    path: '/user/address/edit',
    component: () => import('../page/user/address/edit'),
    meta: {
YunaiV's avatar
YunaiV committed
77 78
      title: '修改地址',
      requireAuth: true,
sin's avatar
sin committed
79 80 81 82 83 84
    }
  },
  {
    path: '/user/favorite',
    component: () => import('../page/user/favorite/list'),
    meta: {
YunaiV's avatar
YunaiV committed
85 86
      title: '我的收藏',
      requireAuth: true,
sin's avatar
sin committed
87 88 89 90
    }
  },
  {
    path: '/user/coupon',
91
    component: () => import('../page/coupon/list'),
sin's avatar
sin committed
92
    meta: {
YunaiV's avatar
YunaiV committed
93 94
      title: '我的优惠券',
      requireAuth: true,
sin's avatar
sin committed
95 96 97 98 99 100 101
    }
  },
  {
    path: '/user/order',
    component: () => import('../page/user/order/list'),
    meta: {
      title: '我的订单'
102
    },
sin's avatar
sin committed
103 104 105 106 107
  },
  {
    path: '/user/order/:id',
    component: () => import('../page/user/order/list'),
    meta: {
YunaiV's avatar
YunaiV committed
108 109
      title: '我的订单',
      requireAuth: true,
sin's avatar
sin committed
110 111 112 113 114 115
    }
  },
  {
    path: '/user/order/info/:id',
    component: () => import('../page/user/order/info'),
    meta: {
YunaiV's avatar
YunaiV committed
116 117
      title: '我的订单',
      requireAuth: true,
sin's avatar
sin committed
118 119 120 121 122 123
    }
  },
  {
    path: '/user/order/logistics/:id',
    component: () => import('../page/user/order/logistics'),
    meta: {
YunaiV's avatar
YunaiV committed
124 125
      title: '订单追踪',
      requireAuth: true,
sin's avatar
sin committed
126 127 128 129 130 131 132 133 134 135
    }
  },
  {
    path: '/user/aftersale',
    component: () => import('../page/user/aftersale/list'),
    meta: {
      title: '售后'
    }
  },
  {
sin's avatar
sin committed
136
    path: '/user/aftersale/apply/:orderId',
sin's avatar
sin committed
137 138 139 140 141 142
    component: () => import('../page/user/aftersale/apply'),
    meta: {
      title: '申请售后'
    }
  },
  {
sin's avatar
sin committed
143
    path: '/user/aftersale/detail/:orderId',
sin's avatar
sin committed
144 145 146 147 148 149
    component: () => import('../page/user/aftersale/detail'),
    meta: {
      title: '服务单详情'
    }
  },
  {
sin's avatar
sin committed
150
    path: '/user/aftersale/track/:id/:serviceNumber',
sin's avatar
sin committed
151 152 153 154 155 156
    component: () => import('../page/user/aftersale/track'),
    meta: {
      title: '进度详情'
    }
  },
  {
157
    name: '/product/search',
158 159
    path: '/product/search',
    component: () => import('../page/product/search'),
sin's avatar
sin committed
160
    meta: {
161
      title: '商品搜索'
sin's avatar
sin committed
162 163 164
    }
  },
  {
165 166
    path: '/product/:id',
    component: () => import('../page/product/detail'),
167
    meta: {
168
      title: '商品详情'
169 170 171 172
    }
  },
  {
    path: '/products/list',
sin's avatar
sin committed
173 174 175 176 177 178 179 180 181
    component: () => import('../page/product/list'),
    meta: {
      title: '商品列表'
    }
  },
  {
    name: 'cart',
    component: () => import('../page/cart/index'),
    meta: {
YunaiV's avatar
YunaiV committed
182 183
      title: '购物车',
      requireAuth: true,
sin's avatar
sin committed
184 185 186 187 188 189
    }
  },
  {
    path: '/order',
    component: () => import('../page/shipping/order'),
    meta: {
190 191
      title: '确认订单',
      requireAuth: true,
sin's avatar
sin committed
192 193 194 195 196 197 198 199 200
    }
  },
  {
    name: 'category',
    component: () => import('../page/category/index'),
    meta: {
      title: '分类'
    }
  },
201 202 203 204 205 206
  {
    path: '/coupon/fetch',
    component: () => import('../page/coupon/fetch'),
    meta: {
      title: '优惠劵领取'
    }
207 208 209 210 211
  },
  {
    path: '/pay',
    component: () => import('../page/pay/index'),
    meta: {
YunaiV's avatar
YunaiV committed
212 213
      title: '收银台',
      requireAuth: true,
214
    }
215
  }
sin's avatar
sin committed
216 217 218 219 220 221 222 223 224 225
];

// add route path
routes.forEach(route => {
  route.path = route.path || '/' + (route.name || '');
});

const router = new Router({ routes });

router.beforeEach((to, from, next) => {
YunaiV's avatar
YunaiV committed
226 227 228 229 230 231 232 233 234 235 236 237
  // 判断是否需要认证
  const requireAuth = to.meta && to.meta.requireAuth;
  if (requireAuth) {
    if (!getAccessToken()) { // 未登陆
      next({
        path: '/login',
        query: {redirect: to.fullPath}  // 将跳转的路由path作为参数,登录成功后跳转到该路由
      });
      return;
    }
  }
  // 处理标题
sin's avatar
sin committed
238 239 240 241
  const title = to.meta && to.meta.title;
  if (title) {
    document.title = title;
  }
YunaiV's avatar
YunaiV committed
242
  // 继续路由
sin's avatar
sin committed
243 244 245 246 247 248
  next();
});

export {
  router
};