diff --git a/src/api/article.js b/src/api/article.js
index 1519cb9c6c1c5ddb66ac1e90e3902736e2e1ada1..fad7e132375c005f7c81531200a0ae9443f55fab 100644
--- a/src/api/article.js
+++ b/src/api/article.js
@@ -1,4 +1,4 @@
-import { fetch } from 'utils/fetch';
+import fetch from 'utils/fetch';
export function getList() {
return fetch({
diff --git a/src/api/article_table.js b/src/api/article_table.js
index e56c58e33ce5d04a037ce3bf95ac471a8b3fda8d..2e3edf5207d975fd8ea7aa695016d0a26dad8015 100644
--- a/src/api/article_table.js
+++ b/src/api/article_table.js
@@ -1,4 +1,4 @@
-import { fetch } from 'utils/fetch';
+import fetch from 'utils/fetch';
export function fetchList(query) {
return fetch({
diff --git a/src/api/login.js b/src/api/login.js
index a0d8d8709406d45042975eb00c23412c2607bbd1..cc8599b5bf123d704b7af4cc6245962462894706 100644
--- a/src/api/login.js
+++ b/src/api/login.js
@@ -1,4 +1,4 @@
-import { fetch } from 'utils/fetch';
+import fetch from 'utils/fetch';
export function loginByEmail(email, password) {
const data = {
@@ -19,7 +19,6 @@ export function logout() {
});
}
-
export function getInfo(token) {
return fetch({
url: '/user/info',
diff --git a/src/api/qiniu.js b/src/api/qiniu.js
index d63319a3326c03f856b342d84ae6a22104be931f..ce998a594fa3eb8156db2217a9ed3dc691a08abd 100644
--- a/src/api/qiniu.js
+++ b/src/api/qiniu.js
@@ -1,28 +1,28 @@
-import fetch, { tpFetch } from 'utils/fetch';
+// import fetch, { tpFetch } from 'utils/fetch';
-export function getToken() {
- return fetch({
- url: '/qiniu/upload/token',
- method: 'get'
- });
-}
-export function upload(data) {
- return tpFetch({
- url: 'https://upload.qbox.me',
- method: 'post',
- data
- });
-}
+// export function getToken() {
+// return fetch({
+// url: '/qiniu/upload/token',
+// method: 'get'
+// });
+// }
+// export function upload(data) {
+// return tpFetch({
+// url: 'https://upload.qbox.me',
+// method: 'post',
+// data
+// });
+// }
-/* 外部uri转七牛uri*/
-export function netUpload(token, net_url) {
- const imgData = {
- net_url
- };
- return fetch({
- url: '/qiniu/upload/net/async',
- method: 'post',
- data: imgData
- });
-}
+// /* 外部uri转七牛uri*/
+// export function netUpload(token, net_url) {
+// const imgData = {
+// net_url
+// };
+// return fetch({
+// url: '/qiniu/upload/net/async',
+// method: 'post',
+// data: imgData
+// });
+// }
diff --git a/src/api/remoteSearch.js b/src/api/remoteSearch.js
index 0e3c341ddbfe73e0386da1f02a866d0781702b5c..ed45bd74ac1d27b33fb5e483bca289738860e56f 100644
--- a/src/api/remoteSearch.js
+++ b/src/api/remoteSearch.js
@@ -1,4 +1,4 @@
-import { fetch } from 'utils/fetch';
+import fetch from 'utils/fetch';
export function userSearch(name) {
return fetch({
diff --git a/src/mock/article.js b/src/mock/article.js
index 029b1555ebd4095d4fb45f27ad214cd2edd0eed1..539338f128908b2223498b01923360304565acad 100644
--- a/src/mock/article.js
+++ b/src/mock/article.js
@@ -17,33 +17,21 @@ for (let i = 0; i < count; i++) {
}
export default {
- getList: () => new Promise(resolve => {
- setTimeout(() => {
- resolve([200, {
- data: List
- }]);
- }, 100);
- }),
- getArticle: () => new Promise(resolve => {
- setTimeout(() => {
- resolve([200, {
- data: {
- id: 120000000001,
- author: { key: 'mockPan' },
- source_name: '原创作者',
- category_item: [{ key: 'global', name: '全球' }],
- comment_disabled: false,
- content: '
我是测试数据我是测试数据
![](https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943)
"',
- content_short: '我是测试数据',
- display_time: +new Date(),
- image_uri: 'https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3',
- platforms: ['a-platform'],
- source_uri: 'https://github.com/PanJiaChen/vue-element-admin',
- status: 'published',
- tags: [],
- title: ''
- }
- }]);
- }, 100);
+ getList: () => List,
+ getArticle: () => ({
+ id: 120000000001,
+ author: { key: 'mockPan' },
+ source_name: '原创作者',
+ category_item: [{ key: 'global', name: '全球' }],
+ comment_disabled: false,
+ content: '我是测试数据我是测试数据
![](https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943)
"',
+ content_short: '我是测试数据',
+ display_time: +new Date(),
+ image_uri: 'https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3',
+ platforms: ['a-platform'],
+ source_uri: 'https://github.com/PanJiaChen/vue-element-admin',
+ status: 'published',
+ tags: [],
+ title: ''
})
};
diff --git a/src/mock/article_table.js b/src/mock/article_table.js
index 8320fe0cdbd393921f6c9f7660cc01b7dbadf008..7d9ecd1ed5a72911a48058dd5af7b36975f732ab 100644
--- a/src/mock/article_table.js
+++ b/src/mock/article_table.js
@@ -1,5 +1,5 @@
import Mock from 'mockjs';
-
+import { param2Obj } from 'utils';
const List = [];
const count = 100;
@@ -21,9 +21,9 @@ for (let i = 0; i < count; i++) {
export default {
getList: config => {
- const { importance, type, title, page, limit, sort } = config.params;
+ const { importance, type, title, page, limit, sort } = param2Obj(config.url);
let mockList = List.filter(item => {
- if (importance && item.importance !== importance) return false;
+ if (importance && item.importance !== +importance) return false;
if (type && item.type !== type) return false;
if (title && item.title.indexOf(title) < 0) return false;
return true;
@@ -33,21 +33,12 @@ export default {
}
const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1));
-
- return new Promise(resolve => {
- setTimeout(() => {
- resolve([200, {
- total: mockList.length,
- items: pageList
- }]);
- }, 100);
- })
+ return {
+ total: mockList.length,
+ items: pageList
+ }
},
- getPv: () => new Promise(resolve => {
- setTimeout(() => {
- resolve([200, {
- pvData: [{ key: 'PC网站', pv: 1024 }, { key: 'mobile网站', pv: 1024 }, { key: 'ios', pv: 1024 }, { key: 'android', pv: 1024 }]
- }]);
- }, 100);
+ getPv: () => ({
+ pvData: [{ key: 'PC网站', pv: 1024 }, { key: 'mobile网站', pv: 1024 }, { key: 'ios', pv: 1024 }, { key: 'android', pv: 1024 }]
})
};
diff --git a/src/mock/index.js b/src/mock/index.js
index 0e699c8ed9aa24ee609a3901a6636abd2885de6c..dea59284c9ee2cad714f6e64d5c28e43fad206c7 100644
--- a/src/mock/index.js
+++ b/src/mock/index.js
@@ -1,27 +1,25 @@
-import axios from 'axios';
-import MockAdapter from 'axios-mock-adapter';
+import Mock from 'mockjs';
import loginAPI from './login';
import articleAPI from './article';
import article_tableAPI from './article_table';
import remoteSearchAPI from './remoteSearch';
-const mock = new MockAdapter(axios);
+
// 登录相关
-mock.onPost('/login/loginbyemail').reply(loginAPI.loginByEmail);
-mock.onPost('/login/logout').reply(loginAPI.logout);
-mock.onGet('/user/info').reply(loginAPI.getInfo);
+Mock.mock(/\/login\/loginbyemail/, 'post', loginAPI.loginByEmail);
+Mock.mock(/\/login\/logout/, 'post', loginAPI.logout);
+Mock.mock(/\/user\/info\.*/, 'get', loginAPI.getInfo)
-// 文章相关
-mock.onGet('/article/list').reply(articleAPI.getList);
-mock.onGet('/article/detail').reply(articleAPI.getArticle);
+// // 文章相关
+Mock.mock(/\/article\/list/, 'get', articleAPI.getList);
+Mock.mock(/\/article\/detail/, 'get', articleAPI.getArticle);
-// table example相关
-mock.onGet('/article_table/list').reply(article_tableAPI.getList);
-mock.onGet('/article_table/pv').reply(article_tableAPI.getPv);
+// // table example相关
+Mock.mock(/\/article_table\/list/, 'get', article_tableAPI.getList);
+Mock.mock(/\/article_table\/p/, 'get', article_tableAPI.getPv);
-// 搜索相关
-mock.onGet('/search/user').reply(remoteSearchAPI.searchUser);
+// // 搜索相关
+Mock.mock(/\/search\/user/, 'get', remoteSearchAPI.searchUser);
-mock.onAny().passThrough();
-export default mock;
+export default Mock;
diff --git a/src/mock/login.js b/src/mock/login.js
index 5ab27568a58b6a0e17195438c0b60b915583a5a7..b3609ef9b72023166ab7d00a752b03ad9b355a8f 100644
--- a/src/mock/login.js
+++ b/src/mock/login.js
@@ -1,3 +1,5 @@
+import { param2Obj } from 'utils';
+
const userMap = {
admin: {
role: ['admin'],
@@ -28,36 +30,17 @@ const userMap = {
export default {
loginByEmail: config => {
- const { email } = JSON.parse(config.data);
- return new Promise((resolve, reject) => {
- if (userMap[email.split('@')[0]]) {
- setTimeout(() => {
- resolve([200, {
- data: userMap[email.split('@')[0]]
- }]);
- }, 500);
- } else {
- reject('账号不正确')
- }
- })
+ console.log(config)
+ const { email } = JSON.parse(config.body);
+ return userMap[email.split('@')[0]];
},
getInfo: config => {
- const { token } = config.params;
- return new Promise((resolve, reject) => {
- if (userMap[token]) {
- setTimeout(() => {
- resolve([200, {
- data: userMap[token]
- }]);
- }, 100);
- } else {
- reject('获取失败')
- }
- })
+ const { token } = param2Obj(config.url);
+ if (userMap[token]) {
+ return userMap[token];
+ } else {
+ return Promise.reject('a');
+ }
},
- logout: () => new Promise(resolve => {
- setTimeout(() => {
- resolve([200, { data: 'success' }]);
- }, 100);
- })
+ logout: () => 'success'
};
diff --git a/src/mock/remoteSearch.js b/src/mock/remoteSearch.js
index b5429562b7495c5e6e8d1f08ff7a283c6f3f456d..3d06a4f1efc7f4311dc7f1c5d8d837cf6565f168 100644
--- a/src/mock/remoteSearch.js
+++ b/src/mock/remoteSearch.js
@@ -1,4 +1,5 @@
import Mock from 'mockjs';
+import { param2Obj } from 'utils';
const NameList = [];
const count = 100;
@@ -12,18 +13,12 @@ NameList.push({ name: 'mockPan' })
export default {
searchUser: config => {
- const { name } = config.params;
+ const { name } = param2Obj(config.url);
const mockNameList = NameList.filter(item => {
const lowerCaseName = item.name.toLowerCase()
if (name && lowerCaseName.indexOf(name.toLowerCase()) < 0) return false;
return true;
});
- return new Promise(resolve => {
- setTimeout(() => {
- resolve([200, {
- items: mockNameList
- }]);
- }, 100);
- })
+ return { items: mockNameList }
}
};
diff --git a/src/utils/fetch.js b/src/utils/fetch.js
index 1ef50a0a29f2aaeb5b5bc93b733174d0f0165d41..45761c8ad5515ff1fa2e64676a3bc99b4a0cf813 100644
--- a/src/utils/fetch.js
+++ b/src/utils/fetch.js
@@ -3,69 +3,44 @@ import { Message } from 'element-ui';
import store from '../store';
import router from '../router';
-export default function _fetch(options) {
- return new Promise((resolve, reject) => {
- const instance = axios.create({
- baseURL: process.env.BASE_API,
- // timeout: 2000,
- headers: { 'X-Ivanka-Token': store.getters.token }
- });
- instance(options)
- .then(response => {
- const res = response.data;
- if (res.code !== 20000) {
- console.log(options); // for debug
- Message({
- message: res.message,
- type: 'error',
- duration: 5 * 1000
- });
- // 50014:Token 过期了 50012:其他客户端登录了 50008:非法的token
- if (res.code === 50008 || res.code === 50014 || res.code === 50012) {
- Message({
- message: res.message,
- type: 'error',
- duration: 5 * 1000
- });
- // 登出
- store.dispatch('FedLogOut').then(() => {
- router.push({ path: '/login' })
- });
- }
- reject(res);
- }
- resolve(res);
- })
- .catch(error => {
- Message({
- message: '发生异常错误,请刷新页面重试,或联系程序员',
- type: 'error',
- duration: 5 * 1000
- });
- console.log(error); // for debug
- reject(error);
- });
- });
-}
-export function fetch(options) {
- return new Promise((resolve, reject) => {
- const instance = axios.create({
- timeout: 2000 // 超时
- });
- instance(options)
- .then(response => {
- const res = response.data;
- resolve(res);
- })
- .catch(error => {
- Message({
- message: error,
- type: 'error',
- duration: 5 * 1000
- });
- console.log(error); // for debug
- reject(error);
- });
- });
-}
+const service = axios.create({
+ baseURL: process.env.BASE_API
+});
+
+service.interceptors.request.use(config => {
+ // Do something before request is sent
+ if (store.state.token) {
+ config.headers.Token = store.state.token;
+ }
+ return config;
+}, error => {
+ // Do something with request error
+ console.log(error); // for debug
+ Promise.reject(error);
+})
+
+service.interceptors.response.use(
+ response => {
+ console.log(response)
+ return response;
+ },
+ error => {
+ console.log('err' + error);// for debug
+ const code = error.response.data;
+ if (code === 50008 || code === 50014 || code === 50012) {
+ Message({
+ message: res.message,
+ type: 'error',
+ duration: 5 * 1000
+ });
+ // 登出
+ store.dispatch('FedLogOut').then(() => {
+ router.push({ path: '/login' })
+ });
+ }
+ return Promise.reject(error);
+ }
+)
+
+export default service;
diff --git a/src/utils/index.js b/src/utils/index.js
index c95b4ab4cac2ace47114f609db6a41a0afda9b22..3f4c88bb1fe48709744f16df2b821169de3331b3 100644
--- a/src/utils/index.js
+++ b/src/utils/index.js
@@ -110,6 +110,11 @@
})).join('&');
}
+ export function param2Obj(url) {
+ const search = url.split('?')[1];
+ return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
+ }
+
export function html2Text(val) {
const div = document.createElement('div');
div.innerHTML = val;
diff --git a/src/views/example/dragTable.vue b/src/views/example/dragTable.vue
index 9ad715c101af0698d7358babb7c10cf3417e518c..512c8a250e77cdfcbecabbd074ab92fdc21b8326 100644
--- a/src/views/example/dragTable.vue
+++ b/src/views/example/dragTable.vue
@@ -96,8 +96,8 @@
getList() {
this.listLoading = true;
fetchList(this.listQuery).then(response => {
- this.list = response.items;
- this.total = response.total;
+ this.list = response.data.items;
+ this.total = response.data.total;
this.listLoading = false;
this.olderList = this.list.map(v => v.id);
this.newList = this.olderList.slice();
@@ -124,6 +124,7 @@
.drag-handler{
width: 30px;
height: 30px;
+ cursor: pointer;
}
.show-d{
margin-top: 15px;
diff --git a/src/views/example/form1.vue b/src/views/example/form1.vue
index d41cf9f22b3a60cd4d1c0b5b7a30cb0b2bdc5d75..ff982ec9fc4e6dcdd0516109924bf2d030f8147b 100644
--- a/src/views/example/form1.vue
+++ b/src/views/example/form1.vue
@@ -237,9 +237,9 @@
},
getRemoteUserList(query) {
userSearch(query).then(response => {
- if (!response.items) return;
+ if (!response.data.items) return;
console.log(response)
- this.userLIstOptions = response.items.map(v => ({
+ this.userLIstOptions = response.data.items.map(v => ({
key: v.name
}));
})
diff --git a/src/views/example/inlineEditTable.vue b/src/views/example/inlineEditTable.vue
index 50ebca008713dcfce0dceed0f3bb9af53e6a227f..d9bd0dd73a523ef25658dd7a4a7e21c7983b5da3 100644
--- a/src/views/example/inlineEditTable.vue
+++ b/src/views/example/inlineEditTable.vue
@@ -59,7 +59,6 @@
data() {
return {
list: null,
- total: null,
listLoading: true,
listQuery: {
page: 1,
@@ -84,11 +83,10 @@
getList() {
this.listLoading = true;
fetchList(this.listQuery).then(response => {
- this.list = response.items.map(v => {
+ this.list = response.data.items.map(v => {
v.edit = false;
return v
});
- this.total = response.total;
this.listLoading = false;
})
}
diff --git a/src/views/example/table.vue b/src/views/example/table.vue
index 69e531797156f63e9e37aff8d0609142c39dc2a0..5d597e0801c7b8662f340dd258ee1e66246b14cd 100644
--- a/src/views/example/table.vue
+++ b/src/views/example/table.vue
@@ -227,8 +227,8 @@
getList() {
this.listLoading = true;
fetchList(this.listQuery).then(response => {
- this.list = response.items;
- this.total = response.total;
+ this.list = response.data.items;
+ this.total = response.data.total;
this.listLoading = false;
})
},
diff --git a/src/views/permission/index.vue b/src/views/permission/index.vue
index a33540bb79ae12dd421a3484b6fac70d790e0548..b0b9540377eef1db1491f7fb0decc38e572efdc7 100644
--- a/src/views/permission/index.vue
+++ b/src/views/permission/index.vue
@@ -24,7 +24,7 @@
watch: {
role(val) {
this.$store.commit('SET_ROLES', [val]);
- window.location.reload()
+ this.$router.push({ path: '/permission/index?' + +new Date() });
}
}
}