Commit c40d344e2b8675477869a8da3a7eafcdcc94c913
1 parent
60fa0be9db
Exists in
master
auto commit the code by alias command
Showing
6 changed files
with
87 additions
and
44 deletions
Show diff stats
mock/user.js
1 | const tokens = { | 1 | const tokens = { |
2 | admin: { | 2 | admin: { |
3 | token: 'admin-token' | 3 | token: 'admin-token' |
4 | }, | 4 | }, |
5 | assistant: { | 5 | assistant: { |
6 | token: 'assistant-token' | 6 | token: 'assistant-token' |
7 | }, | 7 | }, |
8 | runner: { | 8 | runner: { |
9 | token: 'runner-token' | 9 | token: 'runner-token' |
10 | }, | 10 | }, |
11 | shoper: { | 11 | shoper: { |
12 | token: 'shoper-token' | 12 | token: 'shoper-token' |
13 | } | 13 | } |
14 | } | 14 | } |
15 | 15 | ||
16 | const users = { | 16 | const users = { |
17 | 'admin-token': { //管理员 | 17 | 'admin-token': { //管理员 |
18 | roles: ['admin'], | 18 | roles: ['admin'], |
19 | introduction: 'I am a super administrator', | 19 | introduction: 'I am a super administrator', |
20 | avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', | 20 | avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', |
21 | name: 'Super Admin' | 21 | name: 'Super Admin' |
22 | }, | 22 | }, |
23 | 'assistant-token': { //管理员助理 | 23 | 'assistant-token': { //管理员助理 |
24 | roles: ['assistant'], | 24 | roles: ['assistant'], |
25 | introduction: 'I am a assistant of administrator', | 25 | introduction: 'I am a assistant of administrator', |
26 | avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', | 26 | avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', |
27 | name: 'assistant Admin' | 27 | name: 'assistant Admin' |
28 | }, | 28 | }, |
29 | 'runner-token': { //运营人员 | 29 | 'runner-token': { //运营人员 |
30 | roles: ['runner'], | 30 | roles: ['runner'], |
31 | introduction: 'I am an runner', | 31 | introduction: 'I am an runner', |
32 | avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', | 32 | avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', |
33 | name: 'Normal runner' | 33 | name: 'Normal runner' |
34 | }, | 34 | }, |
35 | 'shoper-token': { //供应商 | 35 | 'shoper-token': { //供应商 |
36 | roles: ['shoper'], | 36 | roles: ['shoper'], |
37 | introduction: 'I am an shoper', | 37 | introduction: 'I am an shoper', |
38 | avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', | 38 | avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', |
39 | name: 'Normal shoper' | 39 | name: 'Normal shoper' |
40 | } | 40 | } |
41 | } | 41 | } |
42 | 42 | ||
43 | export default [{ // user login | 43 | export default [{ // user login |
44 | url: '/yp/user/login', | 44 | url: '/yp/user/login', |
45 | type: 'post', | 45 | type: 'post', |
46 | response: config => { | 46 | response: config => { |
47 | console.log('config-------->', config.body); | 47 | console.log('config-----111--->', config.body); |
48 | const { | 48 | const { |
49 | username, | 49 | username, |
50 | password | 50 | password |
51 | } = config.body; | 51 | } = config.body; |
52 | if (username == 'admin' && password == '111111') { | 52 | if (username == 'admin' && password == '111111') { |
53 | const token = tokens[username]; | 53 | const token = tokens[username]; |
54 | if (!token) { | 54 | if (!token) { |
55 | return { | 55 | return { |
56 | code: 60204, | 56 | code: 60204, |
57 | message: 'Account and password are incorrect.' | 57 | message: 'Account and password are incorrect.' |
58 | } | 58 | } |
59 | } else { | 59 | } else { |
60 | return { | 60 | return { |
61 | code: 20000, | 61 | code: 20000, |
62 | data: token | 62 | data: token |
63 | } | 63 | } |
64 | } | 64 | } |
65 | } else { | 65 | } else { |
66 | console.log('passwordpasswordpassword', username, password); | ||
66 | return { | 67 | return { |
67 | code: 60204, | 68 | code: 60204, |
68 | message: 'Account and password are incorrect.' | 69 | message: 'Account and password are incorrect///.' |
69 | } | 70 | } |
70 | } | 71 | } |
71 | } | 72 | } |
72 | }, | 73 | }, |
73 | 74 | ||
74 | // get user info | 75 | // get user info |
75 | { | 76 | { |
76 | url: '/yp/user/info\.*', | 77 | url: '/yp/user/info\.*', |
77 | type: 'get', | 78 | type: 'get', |
78 | response: config => { | 79 | response: config => { |
79 | const { | 80 | const { |
80 | token | 81 | token |
81 | } = config.query | 82 | } = config.query |
82 | const info = users[token] | 83 | const info = users[token] |
83 | 84 | ||
84 | // mock error | 85 | // mock error |
85 | if (!info) { | 86 | if (!info) { |
86 | return { | 87 | return { |
87 | code: 50008, | 88 | code: 50008, |
88 | message: 'Login failed, unable to get user details.' | 89 | message: 'Login failed, unable to get user details.' |
89 | } | 90 | } |
90 | } | 91 | } |
91 | 92 | ||
92 | return { | 93 | return { |
93 | code: 20000, | 94 | code: 20000, |
94 | data: info | 95 | data: info |
95 | } | 96 | } |
96 | } | 97 | } |
97 | }, | 98 | }, |
98 | 99 | ||
99 | // user logout | 100 | // user logout |
100 | { | 101 | { |
101 | url: '/yp/user/logout', | 102 | url: '/yp/user/logout', |
102 | type: 'post', | 103 | type: 'post', |
103 | response: _ => { | 104 | response: _ => { |
104 | return { | 105 | return { |
105 | code: 20000, | 106 | code: 20000, |
106 | data: 'success' | 107 | data: 'success' |
107 | } | 108 | } |
108 | } | 109 | } |
109 | } | 110 | } |
110 | ] | 111 | ] |
111 | 112 |
src/api/user.js
1 | import request from '@/utils/request' | 1 | import request from '@/utils/request' |
2 | // var qs = require('Qs'); | 2 | // var qs = require('Qs'); |
3 | 3 | ||
4 | export function login(data) { | 4 | export function login(data) { |
5 | console.log('login.1111...', data) | 5 | console.log('login.1111...', data) |
6 | return request({ | 6 | return request({ |
7 | // url: '/yp/user/login', | 7 | url: '/yp/user/login', |
8 | url: '/yp.user.login.php', | 8 | // url: '/yp.user.login.php', |
9 | method: 'post', | 9 | method: 'post', |
10 | headers: { | 10 | // headers: { |
11 | 'Content-type': 'application/x-www-form-urlencoded' | 11 | // 'Content-type': 'application/x-www-form-urlencoded' |
12 | }, | 12 | // }, |
13 | data, | 13 | data, |
14 | // responseType: 'text/plain', | 14 | // responseType: 'text/plain', |
15 | // onDownloadProgress: false, | 15 | // onDownloadProgress: false, |
16 | // onUploadProgress: true, | 16 | // onUploadProgress: true, |
17 | // proxy: {} | 17 | // proxy: {} |
18 | }); | 18 | }); |
19 | } | 19 | } |
20 | 20 | ||
21 | export function getInfo(token) { | 21 | export function getInfo(token) { |
22 | console.log('getInfo....', token) | 22 | console.log('getInfo....', token) |
23 | return request({ | 23 | return request({ |
24 | url: '/yp/user/info', | 24 | url: '/yp/user/info', |
25 | // url: '/yp.user.info.php', | ||
25 | method: 'get', | 26 | method: 'get', |
26 | params: { | 27 | params: { |
27 | token | 28 | token |
28 | } | 29 | } |
30 | // , | ||
31 | // headers: { | ||
32 | // 'Content-type': 'application/x-www-form-urlencoded' | ||
33 | // }, | ||
29 | }) | 34 | }) |
30 | } | 35 | } |
31 | 36 | ||
32 | export function list(token) { | 37 | export function list(token) { |
33 | console.log('listUser....', token) | 38 | console.log('listUser....', token) |
34 | return request({ | 39 | return request({ |
35 | url: '/yp/user/list', | 40 | url: '/yp/user/list', |
36 | method: 'get', | 41 | method: 'get', |
37 | params: { | 42 | params: { |
38 | token | 43 | token |
39 | } | 44 | } |
40 | }) | 45 | }) |
41 | } | 46 | } |
42 | 47 | ||
43 | export function add(token) { | 48 | export function add(token) { |
44 | console.log('addUser....', token) | 49 | console.log('addUser....', token) |
45 | return request({ | 50 | return request({ |
46 | url: '/yp/user/add', | 51 | url: '/yp/user/add', |
47 | method: 'get', | 52 | method: 'post', |
48 | params: { | 53 | params: { |
49 | token | 54 | token |
50 | } | 55 | } |
51 | }) | 56 | }) |
52 | } | 57 | } |
53 | 58 | ||
54 | export function modi(token) { | 59 | export function modi(token) { |
55 | console.log('modiUser....', token) | 60 | console.log('modiUser....', token) |
56 | return request({ | 61 | return request({ |
57 | url: '/yp/user/modi', | 62 | url: '/yp/user/modi', |
58 | method: 'get', | 63 | method: 'post', |
59 | params: { | 64 | params: { |
60 | token | 65 | token |
61 | } | 66 | } |
62 | }) | 67 | }) |
63 | } | 68 | } |
64 | 69 | ||
65 | export function logout() { | 70 | export function logout(token) { |
66 | console.log('logout....') | 71 | console.log('logout....', token) |
67 | return request({ | 72 | return request({ |
68 | url: '/yp/user/logout', | 73 | url: '/yp/user/logout', |
69 | method: 'post' | 74 | // url: '/yp.user.logout.php', |
75 | method: 'post', | ||
70 | }) | 76 | }) |
71 | } | 77 | } |
72 | 78 |
src/store/modules/app.js
1 | import Cookies from 'js-cookie' | 1 | import Cookies from 'js-cookie' |
2 | 2 | ||
3 | //state | ||
3 | const state = { | 4 | const state = { |
4 | sidebar: { | 5 | sidebar: { |
5 | opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true, | 6 | opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true, |
6 | withoutAnimation: false | 7 | withoutAnimation: false |
7 | }, | 8 | }, |
8 | device: 'desktop' | 9 | device: 'desktop' |
9 | } | 10 | } |
10 | 11 | ||
12 | //mutations | ||
11 | const mutations = { | 13 | const mutations = { |
12 | TOGGLE_SIDEBAR: state => { | 14 | TOGGLE_SIDEBAR: state => { |
13 | state.sidebar.opened = !state.sidebar.opened | 15 | state.sidebar.opened = !state.sidebar.opened |
14 | state.sidebar.withoutAnimation = false | 16 | state.sidebar.withoutAnimation = false |
15 | if (state.sidebar.opened) { | 17 | if (state.sidebar.opened) { |
16 | Cookies.set('sidebarStatus', 1) | 18 | Cookies.set('sidebarStatus', 1) |
17 | } else { | 19 | } else { |
18 | Cookies.set('sidebarStatus', 0) | 20 | Cookies.set('sidebarStatus', 0) |
19 | } | 21 | } |
20 | }, | 22 | }, |
21 | CLOSE_SIDEBAR: (state, withoutAnimation) => { | 23 | CLOSE_SIDEBAR: (state, withoutAnimation) => { |
22 | Cookies.set('sidebarStatus', 0) | 24 | Cookies.set('sidebarStatus', 0) |
23 | state.sidebar.opened = false | 25 | state.sidebar.opened = false |
24 | state.sidebar.withoutAnimation = withoutAnimation | 26 | state.sidebar.withoutAnimation = withoutAnimation |
25 | }, | 27 | }, |
26 | TOGGLE_DEVICE: (state, device) => { | 28 | TOGGLE_DEVICE: (state, device) => { |
27 | state.device = device | 29 | state.device = device |
28 | } | 30 | } |
29 | } | 31 | } |
30 | 32 | ||
33 | //actions | ||
31 | const actions = { | 34 | const actions = { |
32 | toggleSideBar({ commit }) { | 35 | toggleSideBar({ commit }) { |
33 | commit('TOGGLE_SIDEBAR') | 36 | commit('TOGGLE_SIDEBAR') |
34 | }, | 37 | }, |
35 | closeSideBar({ commit }, { withoutAnimation }) { | 38 | closeSideBar({ commit }, { withoutAnimation }) { |
36 | commit('CLOSE_SIDEBAR', withoutAnimation) | 39 | commit('CLOSE_SIDEBAR', withoutAnimation) |
37 | }, | 40 | }, |
38 | toggleDevice({ commit }, device) { | 41 | toggleDevice({ commit }, device) { |
39 | commit('TOGGLE_DEVICE', device) | 42 | commit('TOGGLE_DEVICE', device) |
40 | } | 43 | } |
41 | } | 44 | } |
42 | 45 | ||
43 | export default { | 46 | export default { |
44 | namespaced: true, | 47 | namespaced: true, |
45 | state, | 48 | state, |
46 | mutations, | 49 | mutations, |
47 | actions | 50 | actions |
48 | } | 51 | } |
49 | 52 |
src/store/modules/user.js
1 | import { login, logout, getInfo } from '@/api/user' | 1 | import { |
2 | import { getToken, setToken, removeToken } from '@/utils/auth' | 2 | login, |
3 | import { resetRouter } from '@/router' | 3 | logout, |
4 | getInfo | ||
5 | } from '@/api/user' | ||
6 | import { | ||
7 | getToken, | ||
8 | setToken, | ||
9 | removeToken | ||
10 | } from '@/utils/auth' | ||
11 | import { | ||
12 | resetRouter | ||
13 | } from '@/router' | ||
4 | 14 | ||
5 | const getDefaultState = () => { | 15 | const getDefaultState = () => { |
6 | return { | 16 | return { |
7 | token: getToken(), | 17 | token: getToken(), |
8 | name: '', | 18 | name: '', |
9 | avatar: '', | 19 | avatar: '', |
10 | roles: [] | 20 | roles: [] |
11 | } | 21 | } |
12 | } | 22 | } |
13 | 23 | ||
14 | const state = getDefaultState() | 24 | const state = getDefaultState() |
15 | 25 | ||
16 | const mutations = { | 26 | const mutations = { |
17 | RESET_STATE: (state) => { | 27 | RESET_STATE: (state) => { |
18 | Object.assign(state, getDefaultState()) | 28 | Object.assign(state, getDefaultState()) |
19 | }, | 29 | }, |
20 | SET_TOKEN: (state, token) => { | 30 | SET_TOKEN: (state, token) => { |
21 | state.token = token | 31 | state.token = token |
22 | }, | 32 | }, |
23 | SET_NAME: (state, name) => { | 33 | SET_NAME: (state, name) => { |
24 | state.name = name | 34 | state.name = name |
25 | }, | 35 | }, |
26 | SET_AVATAR: (state, avatar) => { | 36 | SET_AVATAR: (state, avatar) => { |
27 | state.avatar = avatar | 37 | state.avatar = avatar |
28 | }, | 38 | }, |
29 | SET_ROLES: (state, roles) => { | 39 | SET_ROLES: (state, roles) => { |
30 | state.roles = roles | 40 | state.roles = roles |
31 | } | 41 | } |
32 | } | 42 | } |
33 | 43 | ||
34 | const actions = { | 44 | const actions = { |
35 | // user login | 45 | // user login |
36 | login({ commit }, userInfo) { | 46 | login({ |
37 | const { username, password } = userInfo | 47 | commit |
48 | }, userInfo) { | ||
49 | const { | ||
50 | username, | ||
51 | password | ||
52 | } = userInfo | ||
38 | return new Promise((resolve, reject) => { | 53 | return new Promise((resolve, reject) => { |
39 | login({ username: username.trim(), password: password }).then(response => { | 54 | login({ |
40 | const { data } = response | 55 | username: username.trim(), |
41 | console.log('action------->login------->处理', data); | 56 | password: password |
42 | commit('SET_TOKEN', data.token) | 57 | }).then(response => { |
43 | setToken(data.token) | 58 | console.log('返回的数据-- step 3------>', response); |
59 | // const { token, name, id, password } = response | ||
60 | const {token,name} = response | ||
61 | console.log('返回的数据-- step 4------>', response); | ||
62 | commit('SET_TOKEN', token) | ||
63 | setToken(token) | ||
44 | resolve() | 64 | resolve() |
45 | }).catch(error => { | 65 | }).catch(error => { |
66 | console.log('返回的数据-- step 4.1------>', error); | ||
46 | reject(error) | 67 | reject(error) |
47 | }) | 68 | }) |
48 | }) | 69 | }) |
49 | }, | 70 | }, |
50 | 71 | ||
51 | // get user info | 72 | // get user info |
52 | getInfo({ commit, state }) { | 73 | getInfo({ |
74 | commit, | ||
75 | state | ||
76 | }) { | ||
53 | return new Promise((resolve, reject) => { | 77 | return new Promise((resolve, reject) => { |
54 | getInfo(state.token).then(response => { | 78 | getInfo(state.token).then(response => { |
55 | const { data } = response | 79 | console.log('返回的数据-- step 5------>', state); |
56 | if (!data) { | 80 | console.log('返回的数据-- step 5.1------>', response); |
81 | const {avatar,id,name,password,roles,token} = response | ||
82 | if (!roles) { | ||
57 | reject('Verification failed, please Login again.') | 83 | reject('Verification failed, please Login again.') |
58 | } | 84 | } |
59 | const { roles, name, avatar } = data | 85 | console.error('返回的数据-- step 5.2------>', response); |
60 | // roles must be a non-empty array | 86 | // roles must be a non-empty array |
61 | if (!roles || roles.length <= 0) { | 87 | if (!roles || roles.length <= 0) { |
62 | reject('getInfo: roles must be a non-null array!') | 88 | reject('getInfo: roles must be a non-null array!') |
63 | } | 89 | } |
64 | commit('SET_ROLES', roles) | 90 | commit('SET_ROLES', roles) |
65 | commit('SET_NAME', name) | 91 | commit('SET_NAME', name) |
66 | commit('SET_AVATAR', avatar) | 92 | commit('SET_AVATAR', avatar) |
67 | resolve(data) | 93 | resolve(response) |
68 | }).catch(error => { | 94 | }).catch(error => { |
95 | console.log('返回的数据-- step 5.3------>', error); | ||
69 | reject(error) | 96 | reject(error) |
70 | }) | 97 | }) |
71 | }) | 98 | }) |
72 | }, | 99 | }, |
73 | 100 | ||
74 | // user logout | 101 | // user logout |
75 | logout({ commit, state }) { | 102 | logout({ |
103 | commit, | ||
104 | state | ||
105 | }) { | ||
76 | return new Promise((resolve, reject) => { | 106 | return new Promise((resolve, reject) => { |
77 | logout(state.token).then(() => { | 107 | logout(state.token).then(() => { |
78 | removeToken() // must remove token first | 108 | removeToken() // must remove token first |
79 | resetRouter() | 109 | resetRouter() |
80 | commit('RESET_STATE') | 110 | commit('RESET_STATE') |
81 | resolve() | 111 | resolve() |
82 | }).catch(error => { | 112 | }).catch(error => { |
83 | reject(error) | 113 | reject(error) |
84 | }) | 114 | }) |
85 | }) | 115 | }) |
86 | }, | 116 | }, |
87 | 117 | ||
88 | // remove token | 118 | // remove token |
89 | resetToken({ commit }) { | 119 | resetToken({ |
120 | commit | ||
121 | }) { | ||
90 | return new Promise(resolve => { | 122 | return new Promise(resolve => { |
91 | removeToken() // must remove token first | 123 | removeToken() // must remove token first |
92 | commit('RESET_STATE') | 124 | commit('RESET_STATE') |
93 | resolve() | 125 | resolve() |
94 | }) | 126 | }) |
95 | } | 127 | } |
96 | } | 128 | } |
97 | 129 | ||
98 | export default { | 130 | export default { |
99 | namespaced: true, | 131 | namespaced: true, |
100 | state, | 132 | state, |
101 | mutations, | 133 | mutations, |
102 | actions | 134 | actions |
103 | } | 135 | } |
104 |
src/utils/request.js
1 | import axios from 'axios' | 1 | import axios from 'axios' |
2 | import { | 2 | import { |
3 | MessageBox, | 3 | MessageBox, |
4 | Message | 4 | Message |
5 | } from 'element-ui' | 5 | } from 'element-ui' |
6 | import store from '@/store' | 6 | import store from '@/store' |
7 | import { | 7 | import { |
8 | getToken | 8 | getToken |
9 | } from '@/utils/auth' | 9 | } from '@/utils/auth' |
10 | 10 | ||
11 | // create an axios instance | 11 | // create an axios instance |
12 | // 创建axios实例 | 12 | // 创建axios实例 |
13 | const service = axios.create({ | 13 | const service = axios.create({ |
14 | // baseURL: '', // url = base url + request url | 14 | // baseURL: '', // url = base url + request url |
15 | baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url | 15 | baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url |
16 | // withCredentials: true, // send cookies when cross-domain requests | 16 | // withCredentials: true, // send cookies when cross-domain requests |
17 | timeout: 5000, // request timeout | 17 | timeout: 5000, // request timeout |
18 | headers: { | 18 | // headers: { |
19 | 'Content-Type': 'application/x-www-form-urlencoded' | 19 | // 'Content-Type': 'application/x-www-form-urlencoded' |
20 | } | 20 | // } |
21 | }) | 21 | }) |
22 | 22 | ||
23 | // request interceptor | 23 | // request interceptor |
24 | // request拦截器 | 24 | // request拦截器 |
25 | service.interceptors.request.use( | 25 | service.interceptors.request.use( |
26 | config => { | 26 | config => { |
27 | // do something before request is sent | 27 | // do something before request is sent |
28 | // console.log('do something before request is sent') | 28 | // console.log('do something before request is sent') |
29 | if (store.getters.token) { | 29 | if (store.getters.token) { |
30 | // console.log('[X-Token] is a custom headers key') | 30 | // console.log('[X-Token] is a custom headers key') |
31 | // let each request carry token | 31 | // let each request carry token |
32 | // ['X-Token'] is a custom headers key | 32 | // ['X-Token'] is a custom headers key |
33 | // please modify it according to the actual situation | 33 | // please modify it according to the actual situation |
34 | config.headers['X-Token'] = getToken(); | 34 | config.headers['X-Token'] = getToken(); |
35 | config.headers['Content-Type'] = 'application/x-www-form-urlencoded'; | 35 | // config.headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
36 | // var token = getToken() | 36 | // var token = getToken() |
37 | // Object.assign(config.headers, { 'token': token }) | 37 | // Object.assign(config.headers, { 'token': token }) |
38 | } | 38 | } |
39 | return config | 39 | return config |
40 | }, | 40 | }, |
41 | error => { | 41 | error => { |
42 | // console.log('do something with request error') | 42 | // console.log('do something with request error') |
43 | // do something with request error | 43 | // do something with request error |
44 | console.error(error) // for debug | 44 | console.error('出现错误。在use里。======>', error) // for debug |
45 | return Promise.reject(error) | 45 | return Promise.reject(error) |
46 | } | 46 | } |
47 | ) | 47 | ) |
48 | 48 | ||
49 | // response interceptor | 49 | // response interceptor |
50 | // respone拦截器 | 50 | // respone拦截器 |
51 | service.interceptors.response.use( | 51 | service.interceptors.response.use( |
52 | /** | 52 | /** |
53 | * If you want to get http information such as headers or status | 53 | * If you want to get http information such as headers or status |
54 | * Please return response => response | 54 | * Please return response => response |
55 | */ | 55 | */ |
56 | 56 | ||
57 | /** | 57 | /** |
58 | * Determine the request status by custom code | 58 | * Determine the request status by custom code |
59 | * Here is just an example | 59 | * Here is just an example |
60 | * You can also judge the status by HTTP Status Code | 60 | * You can also judge the status by HTTP Status Code |
61 | */ | 61 | */ |
62 | response => { | 62 | response => { |
63 | const res = response.data | 63 | const res = response.data |
64 | console.log('返回的数据-------->', res); | 64 | console.log('返回的数据-- step 1------>', res); |
65 | /** | 65 | /** |
66 | * 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页 | 66 | * 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页 |
67 | * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中 | 67 | * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中 |
68 | */ | 68 | */ |
69 | // console.log('if the custom code is not 20000, it is judged as an error.') | 69 | // console.log('if the custom code is not 20000, it is judged as an error.') |
70 | if (res.code !== 20000) { | 70 | if (res.code !== 20000) { |
71 | Message({ | 71 | Message({ |
72 | message: res.message || 'Error', | 72 | message: res.message || 'Error', |
73 | type: 'error', | 73 | type: 'error', |
74 | duration: 5 * 1000 | 74 | duration: 5 * 1000 |
75 | }) | 75 | }) |
76 | 76 | ||
77 | // 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了; | 77 | // 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了; |
78 | if (res.code === 50008 || res.code === 50012 || res.code === 50014) { | 78 | if (res.code === 50008 || res.code === 50012 || res.code === 50014) { |
79 | // to re-login | 79 | // to re-login |
80 | // MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', { | 80 | // MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', { |
81 | MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', { | 81 | MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', { |
82 | // confirmButtonText: 'Re-Login', | 82 | // confirmButtonText: 'Re-Login', |
83 | confirmButtonText: '重新登录', | 83 | confirmButtonText: '重新登录', |
84 | // cancelButtonText: 'Cancel', | 84 | // cancelButtonText: 'Cancel', |
85 | cancelButtonText: '取消', | 85 | cancelButtonText: '取消', |
86 | type: 'warning' | 86 | type: 'warning' |
87 | }).then(() => { | 87 | }).then(() => { |
88 | store.dispatch('user/resetToken').then(() => { | 88 | store.dispatch('user/resetToken').then(() => { |
89 | location.reload() // 为了重新实例化vue-router对象 避免bug | 89 | location.reload() // 为了重新实例化vue-router对象 避免bug |
90 | }) | 90 | }) |
91 | }) | 91 | }) |
92 | } | 92 | } |
93 | return Promise.reject(new Error(res.message || 'Error')) | 93 | return Promise.reject(new Error(res.message || 'Error')) |
94 | } else { | 94 | } else { |
95 | // const token = res.data.token; | 95 | // const token = res.data.token; |
96 | console.log('进入20000号判断-------->', res); | 96 | console.log('进入20000号判断- step 2------->', res.data); |
97 | return res; | 97 | const data = res.data; |
98 | return data; | ||
98 | } | 99 | } |
99 | }, | 100 | }, |
100 | error => { | 101 | error => { |
101 | console.error('===============发生错误!!!!!===============' + error) // for debug | 102 | console.error('===============发生错误!!!!!===============' + error.message); // for debug |
102 | Message({ | 103 | // Message({ |
103 | message: error.message, | 104 | // message: error.message, |
104 | type: 'error', | 105 | // type: 'error', |
105 | duration: 5 * 1000 | 106 | // duration: 5 * 1000 |
106 | }) | 107 | // }) |
107 | return Promise.reject(error) | 108 | return Promise.reject(error) |
108 | } | 109 | } |
109 | ) | 110 | ) |
110 | 111 | ||
111 | export default service | 112 | export default service |
112 | 113 |
vue.config.js
1 | 'use strict' | 1 | 'use strict' |
2 | const path = require('path') | 2 | const path = require('path') |
3 | const defaultSettings = require('./src/settings.js') | 3 | const defaultSettings = require('./src/settings.js') |
4 | 4 | ||
5 | function resolve(dir) { | 5 | function resolve(dir) { |
6 | return path.join(__dirname, dir) | 6 | return path.join(__dirname, dir) |
7 | } | 7 | } |
8 | 8 | ||
9 | const name = defaultSettings.title || 'vue Admin Template' // page title | 9 | const name = defaultSettings.title || 'vue Admin Template' // page title |
10 | 10 | ||
11 | // If your port is set to 80, | 11 | // If your port is set to 80, |
12 | // use administrator privileges to execute the command line. | 12 | // use administrator privileges to execute the command line. |
13 | // For example, Mac: sudo npm run | 13 | // For example, Mac: sudo npm run |
14 | // You can change the port by the following methods: | 14 | // You can change the port by the following methods: |
15 | // port = 9528 npm run dev OR npm run dev --port = 9528 | 15 | // port = 9528 npm run dev OR npm run dev --port = 9528 |
16 | const port = process.env.port || process.env.npm_config_port || 9528 // dev port | 16 | const port = process.env.port || process.env.npm_config_port || 9528 // dev port |
17 | 17 | ||
18 | // All configuration item explanations can be find in https://cli.vuejs.org/config/ | 18 | // All configuration item explanations can be find in https://cli.vuejs.org/config/ |
19 | module.exports = { | 19 | module.exports = { |
20 | /** | 20 | /** |
21 | * You will need to set publicPath if you plan to deploy your site under a sub path, | 21 | * You will need to set publicPath if you plan to deploy your site under a sub path, |
22 | * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/, | 22 | * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/, |
23 | * then publicPath should be set to "/bar/". | 23 | * then publicPath should be set to "/bar/". |
24 | * In most cases please use '/' !!! | 24 | * In most cases please use '/' !!! |
25 | * Detail: https://cli.vuejs.org/config/#publicpath | 25 | * Detail: https://cli.vuejs.org/config/#publicpath |
26 | */ | 26 | */ |
27 | publicPath: '/', | 27 | publicPath: '/', |
28 | outputDir: 'dist', | 28 | outputDir: 'dist', |
29 | assetsDir: 'static', | 29 | assetsDir: 'static', |
30 | lintOnSave: process.env.NODE_ENV === 'development', | 30 | lintOnSave: process.env.NODE_ENV === 'development', |
31 | productionSourceMap: false, | 31 | productionSourceMap: false, |
32 | devServer: { | 32 | devServer: { |
33 | port: port, | 33 | port: port, |
34 | open: true, | 34 | open: true, |
35 | overlay: { | 35 | overlay: { |
36 | warnings: false, | 36 | warnings: false, |
37 | errors: true | 37 | errors: true |
38 | }, | 38 | }, |
39 | //线下API测试时,可以去掉这个Mock数据。 | ||
39 | before: require('./mock/mock-server.js') | 40 | before: require('./mock/mock-server.js') |
40 | }, | 41 | }, |
41 | configureWebpack: { | 42 | configureWebpack: { |
42 | // provide the app's title in webpack's name field, so that | 43 | // provide the app's title in webpack's name field, so that |
43 | // it can be accessed in index.html to inject the correct title. | 44 | // it can be accessed in index.html to inject the correct title. |
44 | name: name, | 45 | name: name, |
45 | resolve: { | 46 | resolve: { |
46 | alias: { | 47 | alias: { |
47 | '@': resolve('src') | 48 | '@': resolve('src') |
48 | } | 49 | } |
49 | } | 50 | } |
50 | }, | 51 | }, |
51 | chainWebpack(config) { | 52 | chainWebpack(config) { |
52 | config.plugins.delete('preload') // TODO: need test | 53 | config.plugins.delete('preload') // TODO: need test |
53 | config.plugins.delete('prefetch') // TODO: need test | 54 | config.plugins.delete('prefetch') // TODO: need test |
54 | 55 | ||
55 | // set svg-sprite-loader | 56 | // set svg-sprite-loader |
56 | config.module | 57 | config.module |
57 | .rule('svg') | 58 | .rule('svg') |
58 | .exclude.add(resolve('src/icons')) | 59 | .exclude.add(resolve('src/icons')) |
59 | .end() | 60 | .end() |
60 | config.module | 61 | config.module |
61 | .rule('icons') | 62 | .rule('icons') |
62 | .test(/\.svg$/) | 63 | .test(/\.svg$/) |
63 | .include.add(resolve('src/icons')) | 64 | .include.add(resolve('src/icons')) |
64 | .end() | 65 | .end() |
65 | .use('svg-sprite-loader') | 66 | .use('svg-sprite-loader') |
66 | .loader('svg-sprite-loader') | 67 | .loader('svg-sprite-loader') |
67 | .options({ | 68 | .options({ |
68 | symbolId: 'icon-[name]' | 69 | symbolId: 'icon-[name]' |
69 | }) | 70 | }) |
70 | .end() | 71 | .end() |
71 | 72 | ||
72 | // set preserveWhitespace | 73 | // set preserveWhitespace |
73 | config.module | 74 | config.module |
74 | .rule('vue') | 75 | .rule('vue') |
75 | .use('vue-loader') | 76 | .use('vue-loader') |
76 | .loader('vue-loader') | 77 | .loader('vue-loader') |
77 | .tap(options => { | 78 | .tap(options => { |
78 | options.compilerOptions.preserveWhitespace = true | 79 | options.compilerOptions.preserveWhitespace = true |
79 | return options | 80 | return options |
80 | }) | 81 | }) |
81 | .end() | 82 | .end() |
82 | 83 | ||
83 | config | 84 | config |
84 | // https://webpack.js.org/configuration/devtool/#development | 85 | // https://webpack.js.org/configuration/devtool/#development |
85 | .when(process.env.NODE_ENV === 'development', | 86 | .when(process.env.NODE_ENV === 'development', |
86 | config => config.devtool('cheap-source-map') | 87 | config => config.devtool('cheap-source-map') |
87 | ) | 88 | ) |
88 | 89 | ||
89 | config | 90 | config |
90 | .when(process.env.NODE_ENV !== 'development', | 91 | .when(process.env.NODE_ENV !== 'development', |
91 | config => { | 92 | config => { |
92 | config | 93 | config |
93 | .plugin('ScriptExtHtmlWebpackPlugin') | 94 | .plugin('ScriptExtHtmlWebpackPlugin') |
94 | .after('html') | 95 | .after('html') |
95 | .use('script-ext-html-webpack-plugin', [{ | 96 | .use('script-ext-html-webpack-plugin', [{ |
96 | // `runtime` must same as runtimeChunk name. default is `runtime` | 97 | // `runtime` must same as runtimeChunk name. default is `runtime` |
97 | inline: /runtime\..*\.js$/ | 98 | inline: /runtime\..*\.js$/ |
98 | }]) | 99 | }]) |
99 | .end() | 100 | .end() |
100 | config | 101 | config |
101 | .optimization.splitChunks({ | 102 | .optimization.splitChunks({ |
102 | chunks: 'all', | 103 | chunks: 'all', |
103 | cacheGroups: { | 104 | cacheGroups: { |
104 | libs: { | 105 | libs: { |
105 | name: 'chunk-libs', | 106 | name: 'chunk-libs', |
106 | test: /[\\/]node_modules[\\/]/, | 107 | test: /[\\/]node_modules[\\/]/, |
107 | priority: 10, | 108 | priority: 10, |
108 | chunks: 'initial' // only package third parties that are initially dependent | 109 | chunks: 'initial' // only package third parties that are initially dependent |
109 | }, | 110 | }, |
110 | elementUI: { | 111 | elementUI: { |
111 | name: 'chunk-elementUI', // split elementUI into a single package | 112 | name: 'chunk-elementUI', // split elementUI into a single package |
112 | priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app | 113 | priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app |
113 | test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm | 114 | test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm |
114 | }, | 115 | }, |
115 | commons: { | 116 | commons: { |
116 | name: 'chunk-commons', | 117 | name: 'chunk-commons', |
117 | test: resolve('src/components'), // can customize your rules | 118 | test: resolve('src/components'), // can customize your rules |
118 | minChunks: 3, // minimum common number | 119 | minChunks: 3, // minimum common number |
119 | priority: 5, | 120 | priority: 5, |
120 | reuseExistingChunk: true | 121 | reuseExistingChunk: true |
121 | } | 122 | } |
122 | } | 123 | } |
123 | }) | 124 | }) |
124 | config.optimization.runtimeChunk('single') | 125 | config.optimization.runtimeChunk('single') |
125 | } | 126 | } |
126 | ) | 127 | ) |
127 | } | 128 | } |
128 | } | 129 | } |
129 | 130 |