Skip to content

Commit 25fd455

Browse files
author
zhongzhenlong
committed
add mock,未完待续
1 parent 585958e commit 25fd455

File tree

9 files changed

+133
-148
lines changed

9 files changed

+133
-148
lines changed

.babelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"presets": ["es2015", "stage-2"],
2+
"presets": ["stage-2"],
33
"plugins": ["transform-runtime","transform-vue-jsx",["component", [
44
{
55
"libraryName": "element-ui",
@@ -9,7 +9,7 @@
99
"comments": false,
1010
"env": {
1111
"test": {
12-
"presets": ["es2015", "stage-2"],
12+
"presets": ["stage-2"],
1313
"plugins": [ "istanbul" ]
1414
}
1515
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"dependencies": {
1515
"animate.css": "3.5.2",
1616
"axios": "^0.16.2",
17-
"babel-polyfill": "^6.23.0",
17+
"babel-polyfill": "^6.26.0",
1818
"echarts": "^3.5.1",
1919
"element-ui": "^1.4.3",
2020
"eventsource-polyfill": "^0.9.6",
@@ -39,7 +39,6 @@
3939
"babel-plugin-syntax-jsx": "^6.18.0",
4040
"babel-plugin-transform-runtime": "^6.22.0",
4141
"babel-plugin-transform-vue-jsx": "^3.5.0",
42-
"babel-polyfill": "^6.26.0",
4342
"babel-preset-es2015": "^6.24.1",
4443
"babel-preset-latest": "^6.22.0",
4544
"babel-preset-stage-2": "^6.24.1",

src/axios.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
1-
/**
2-
* Created by lanux on 2017/3/9.
3-
*/
41
import axios from "axios";
5-
6-
// 适配vue-resource
2+
import auth from "./auth";
3+
import {getBaseUrl} from "./common/utils"
74

85
const instance = axios.create();
9-
instance.interceptors.request.use(config => {
10-
//Serialize.decode(config);
11-
return config;
12-
});
13-
instance.interceptors.response.use(response => {
14-
return response.data;
15-
}, err => {
16-
if (err.response) {
17-
axios.post('/v1/error', err.response);
18-
return Promise.reject(err.response.data);
19-
}
20-
return Promise.reject({code: 1024, message: err.message});
21-
});
226

7+
instance.interceptors.response.use(
8+
response => {
9+
if (response.data && response.data.code) {
10+
if (response.data.code === '2001') {
11+
auth.logout()
12+
}
13+
}
14+
return response;
15+
},
16+
error => {
17+
if (error.response) {
18+
//全局ajax错误信息提示
19+
Element.MessageBox({type:"error",message:error.response.data,title:"温馨提示"});
20+
}
21+
return Promise.reject(error);
22+
});
2323

2424
function plugin(Vue)
2525
{
2626
if (plugin.installed) {
2727
return;
2828
}
29+
// instance.defaults.baseURL = 'https://www.baidu.com';
30+
instance.defaults.baseURL = getBaseUrl(window.location.href);
31+
instance.defaults.headers.common['authUid'] = auth.getUid();
32+
instance.defaults.headers.common['authSid'] = auth.getSid();
33+
34+
Vue.prototype.$http = instance
35+
Vue.axios = instance
2936
Vue.http = instance;
3037
}
3138

src/common/utils.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
import pathToRegexp from "path-to-regexp";
22

3+
export const getSessionKey = (key, defaultValue) => {
4+
const item = window.sessionStorage.getItem(key);
5+
if (item == undefined && defaultValue != undefined) {
6+
return defaultValue
7+
}
8+
return item;
9+
}
10+
11+
export const getBaseUrl = (url) => {
12+
var reg = /^((\w+):\/\/([^\/:]*)(?::(\d+))?)(.*)/;
13+
reg.exec(url);
14+
return RegExp.$1;
15+
}
16+
317
export const keyMirror = (obj) => {
418
let key
519
let mirrored = {}
620
if (obj && typeof obj === 'object') {
721
for (key in obj) {
822
if ({}.hasOwnProperty.call(obj, key)) {
9-
mirrored[key] = key
23+
mirrored[ key ] = key
1024
}
1125
}
1226
}
1327
return mirrored
1428
}
1529

16-
1730
/**
1831
* 数组格式转树状结构
1932
* @param {array} array
@@ -27,36 +40,33 @@ export const arrayToTree = (array, id = 'id', pid = 'pid', children = 'children'
2740
let result = []
2841
let hash = {}
2942
data.forEach((item, index) => {
30-
hash[data[index][id]] = data[index]
43+
hash[ data[ index ][ id ] ] = data[ index ]
3144
})
3245

3346
data.forEach((item) => {
34-
let hashVP = hash[item[pid]]
47+
let hashVP = hash[ item[ pid ] ]
3548
if (hashVP) {
36-
!hashVP[children] && (hashVP[children] = [])
37-
hashVP[children].push(item)
49+
!hashVP[ children ] && (hashVP[ children ] = [])
50+
hashVP[ children ].push(item)
3851
} else {
3952
result.push(item)
4053
}
4154
})
4255
return result
4356
}
4457

45-
46-
47-
48-
export function getCurrentMenu(location,arrayMenu){
49-
if(!!arrayMenu) {
58+
export function getCurrentMenu (location, arrayMenu) {
59+
if (!!arrayMenu) {
5060
let current = []
51-
for(let i=0; i<arrayMenu.length; i++){
52-
const e = arrayMenu[i];
53-
const child = getCurrentMenu(location,e.children);
54-
if (!!child && child.length>0) {
55-
child.push({...e,children:null});
61+
for (let i = 0; i < arrayMenu.length; i++) {
62+
const e = arrayMenu[ i ];
63+
const child = getCurrentMenu(location, e.children);
64+
if (!!child && child.length > 0) {
65+
child.push({ ...e, children: null });
5666
return child;
5767
}
58-
if (e.href && pathToRegexp(e.href).exec(location)){
59-
current.push({...e,children:null});
68+
if (e.href && pathToRegexp(e.href).exec(location)) {
69+
current.push({ ...e, children: null });
6070
return current;
6171
}
6272
}

src/components/sideMenu.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
</el-scrollbar>
1515
<div class="sidebar" v-else>
1616
<el-menu :default-active="onRoutes"
17-
:default-openeds="onRouteKeys"
1817
class="el-menu-vertical-demo"
1918
theme="light" router :collapse="sidebar.collapsed&&!device.isMobile" @select="handleSelect">
2019
<template v-for="item in menuList">
@@ -27,7 +26,7 @@
2726
<script>
2827
import subMenu from "./subMenu.vue"
2928
import {mapGetters, mapActions, mapMutations} from 'vuex'
30-
import * as types from "../store/mutation-types"
29+
import types from "../store/mutation-types"
3130
3231
3332
export default {

src/main.js

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,21 @@
1-
// The Vue build version to load with the `import` command
2-
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
31
import 'babel-polyfill'
42
import 'eventsource-polyfill'
53
import Vue from "vue";
64
import frame from "./frame.vue";
7-
import VueRouter from "vue-router";
8-
import routeConfig from "./router";
9-
import {sync} from "vuex-router-sync";
5+
import router from "./router";
106
import store from "./store";
11-
import axios from "axios";
7+
import axios from "./axios";
128
import filters from "./filters";
139
import VueProgressBar from "vue-progressbar";
14-
import types from "./store/mutation-types";
15-
import auth from "./auth";
1610
import Element from "element-ui";
1711
// import 'element-ui/lib/theme-default/index.css';
1812
import "./css/theme/index.css";
1913
import ImpPanel from "./components/panel.vue";
2014

15+
Vue.use(axios);
2116

2217
Vue.use(Element);
2318

24-
function getBaseUrl(url) {
25-
var reg = /^((\w+):\/\/([^\/:]*)(?::(\d+))?)(.*)/;
26-
reg.exec(url);
27-
return RegExp.$1;
28-
}
29-
30-
// axios.defaults.baseURL = 'https://www.baidu.com';
31-
axios.defaults.baseURL = getBaseUrl(window.location.href);
32-
axios.defaults.headers.common['authUid'] = auth.getUid();
33-
axios.defaults.headers.common['authSid'] = auth.getSid();
34-
35-
Vue.prototype.$http = axios
36-
Vue.axios = axios
37-
38-
39-
//加载路由中间件
40-
Vue.use(VueRouter)
41-
4219
Vue.component(ImpPanel.name, ImpPanel);
4320

4421
Vue.use(VueProgressBar, {
@@ -54,47 +31,6 @@ Vue.use(VueProgressBar, {
5431
inverse: false
5532
})
5633

57-
//定义路由
58-
const router = new VueRouter({
59-
routes: routeConfig,
60-
//mode: 'history'
61-
})
62-
63-
sync(store, router)
64-
65-
const {state} = store
66-
67-
router.beforeEach((route, redirect, next) => {
68-
if (state.device.isMobile && state.sidebar.opened) {
69-
store.commit(types.TOGGLE_SIDEBAR, false)
70-
}
71-
if (!auth.loggedIn() && route.path !== '/login') {
72-
next({
73-
path: '/login',
74-
query: {redirect: route.fullPath}
75-
})
76-
} else {
77-
next()
78-
}
79-
})
80-
81-
axios.interceptors.response.use(
82-
response => {
83-
if (response.data && response.data.code) {
84-
if (response.data.code === '2001') {
85-
auth.logout()
86-
}
87-
}
88-
return response;
89-
},
90-
error => {
91-
if (error.response) {
92-
//全局ajax错误信息提示
93-
Element.MessageBox({type:"error",message:error.response.data,title:"温馨提示"});
94-
}
95-
return Promise.reject(error);
96-
});
97-
9834

9935
Object.keys(filters).forEach(key => {
10036
Vue.filter(key, filters[key])

src/router/index.js

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
1-
import dashboard from "../pages/dashboard.vue";
2-
import NotFoundView from "../components/404.vue";
3-
import menuList from "../pages/sys/menu.vue";
4-
import role from "../pages/sys/role.vue";
5-
import resource from "../pages/sys/resource.vue";
6-
import login from "../pages/login.vue";
7-
import app from "../App.vue";
8-
import sysUser from "../pages/sys/user.vue";
9-
import userAdd from "../pages/sys/userAdd.vue";
10-
import resetPwd from "../pages/resetPwd.vue";
11-
// Routes
12-
const routes = [
13-
{path: '/login', component: login},
14-
{
15-
path: '/test', component: app, children: [
16-
{path: '*', component: NotFoundView}
17-
]
18-
},
19-
{
20-
path: '', component: app, children: [
21-
{path: '/resetPwd', component: resetPwd},
22-
{path: '/index', component: dashboard},
23-
{path: '/sys/menuList', component: menuList},
24-
{path: '/sys/roleList', component: role},
25-
{path: '/sys/userList', component: sysUser},
26-
{path: '/sys/userAdd', component: userAdd},
27-
{path: '/sys/resource', component: resource}
28-
]
29-
},
30-
{path: '*', component: NotFoundView}
31-
]
1+
import Vue from "vue";
2+
import VueRouter from "vue-router";
3+
import routeConfig from "./routes";
4+
import {sync} from "vuex-router-sync";
5+
import store from "../store";
6+
import types from "../store/mutation-types";
7+
import auth from "../auth";
328

9+
//加载路由中间件
10+
Vue.use(VueRouter)
3311

34-
export default routes
12+
//定义路由
13+
const router = new VueRouter({
14+
routes: routeConfig,
15+
//mode: 'history'
16+
})
3517

18+
sync(store, router)
19+
20+
const {state} = store
21+
22+
router.beforeEach((route, redirect, next) => {
23+
if (state.device.isMobile && state.sidebar.opened) {
24+
store.commit(types.TOGGLE_SIDEBAR, false)
25+
}
26+
if (!auth.loggedIn() && route.path !== '/login') {
27+
next({
28+
path: '/login',
29+
query: {redirect: route.fullPath}
30+
})
31+
} else {
32+
next()
33+
}
34+
})
35+
36+
export default router

src/router/routes.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import dashboard from "../pages/dashboard.vue";
2+
import NotFoundView from "../components/404.vue";
3+
import menuList from "../pages/sys/menu.vue";
4+
import role from "../pages/sys/role.vue";
5+
import resource from "../pages/sys/resource.vue";
6+
import login from "../pages/login.vue";
7+
import app from "../App.vue";
8+
import sysUser from "../pages/sys/user.vue";
9+
import userAdd from "../pages/sys/userAdd.vue";
10+
import resetPwd from "../pages/resetPwd.vue";
11+
// Routes
12+
const routes = [
13+
{path: '/login', component: login},
14+
{
15+
path: '/test', component: app, children: [
16+
{path: '*', component: NotFoundView}
17+
]
18+
},
19+
{
20+
path: '', component: app, children: [
21+
{path: '/resetPwd', component: resetPwd},
22+
{path: '/index', component: dashboard},
23+
{path: '/sys/menuList', component: menuList},
24+
{path: '/sys/roleList', component: role},
25+
{path: '/sys/userList', component: sysUser},
26+
{path: '/sys/userAdd', component: userAdd},
27+
{path: '/sys/resource', component: resource}
28+
]
29+
},
30+
{path: '*', component: NotFoundView}
31+
]
32+
33+
34+
export default routes
35+

0 commit comments

Comments
 (0)