|
@@ -2,6 +2,7 @@ import Vue from 'vue';
|
|
|
import axios from 'axios';
|
|
|
import VueAxios from 'vue-axios';
|
|
|
import config from './config'
|
|
|
+import Qs from 'qs';
|
|
|
|
|
|
const { serverAddress } = config
|
|
|
|
|
@@ -14,25 +15,53 @@ Vue.use(VueAxios, axios)
|
|
|
/* 常用ajax封装 (固定URL) */
|
|
|
export const ajax = function (type = "post", url, params) {
|
|
|
let token = localStorage.getItem("token")
|
|
|
- return new Promise((resole, reject) => {
|
|
|
- axios({
|
|
|
- method: type,
|
|
|
- url: serverAddress + url,
|
|
|
- params,
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
- 'x-auth-token': token
|
|
|
- }
|
|
|
- }).then((res) => {
|
|
|
- switch(res.data.errCode) {
|
|
|
- case 'request_not_authorize': // 登录过期
|
|
|
- let url = window.location.protocol + "//" + window.location.host + "/#/login"
|
|
|
- window.location.replace(url)
|
|
|
- break;
|
|
|
- }
|
|
|
- resole(res.data);
|
|
|
- }).catch((err) => {
|
|
|
- reject(err);
|
|
|
+ if(type.toLowerCase() == 'get'){
|
|
|
+ return new Promise((resole, reject) => {
|
|
|
+ axios({
|
|
|
+ method: type,
|
|
|
+ url: serverAddress + url,
|
|
|
+ params,
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
+ 'x-auth-token': token
|
|
|
+ }
|
|
|
+ }).then((res) => {
|
|
|
+ switch (res.data.errCode) {
|
|
|
+ case 'request_not_authorize': // 登录过期
|
|
|
+ let url = window.location.protocol + "//" + window.location.host + "/#/login"
|
|
|
+ window.location.replace(url)
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ resole(res.data);
|
|
|
+ }).catch((err) => {
|
|
|
+ reject(err);
|
|
|
+ });
|
|
|
});
|
|
|
- });
|
|
|
+ } else {
|
|
|
+ return new Promise((resole, reject) => {
|
|
|
+ axios({
|
|
|
+ method: type,
|
|
|
+ url: serverAddress + url,
|
|
|
+ params,
|
|
|
+ paramsSerializer: function(params) {
|
|
|
+ return Qs.stringify(params, {arrayFormat: 'brackets'})
|
|
|
+ },
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
+ 'x-auth-token': token
|
|
|
+ }
|
|
|
+ }).then((res) => {
|
|
|
+ switch (res.data.errCode) {
|
|
|
+ case 'request_not_authorize': // 登录过期
|
|
|
+ let url = window.location.protocol + "//" + window.location.host + "/#/login"
|
|
|
+ window.location.replace(url)
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ resole(res.data);
|
|
|
+ }).catch((err) => {
|
|
|
+ reject(err);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
}
|