|
@@ -1,69 +1,75 @@
|
|
-import config from "../config/index.js";
|
|
|
|
-import { Toast } from "vant";
|
|
|
|
-import "vant/lib/toast/style/index";
|
|
|
|
|
|
+import config from '../config/index.js';
|
|
|
|
+import { Toast } from 'vant';
|
|
|
|
+import 'vant/lib/toast/style/index';
|
|
|
|
|
|
function getdata(data) {
|
|
function getdata(data) {
|
|
- let text = "";
|
|
|
|
|
|
+ let text = '';
|
|
for (const key in data) {
|
|
for (const key in data) {
|
|
- text += key + "=" + data[key] + "&";
|
|
|
|
|
|
+ text += key + '=' + data[key] + '&';
|
|
}
|
|
}
|
|
- text ? (text = "?" + text) : "";
|
|
|
|
- text = text.replace(/&$/, "");
|
|
|
|
|
|
+ text ? (text = '?' + text) : '';
|
|
|
|
+ text = text.replace(/&$/, '');
|
|
return text;
|
|
return text;
|
|
}
|
|
}
|
|
|
|
|
|
export default function (ori) {
|
|
export default function (ori) {
|
|
- let baseurl = config.base[ori.urlType || "defaultURl"];
|
|
|
|
|
|
+ let baseurl = config.base[ori.urlType || 'defaultURl'];
|
|
let url = baseurl + ori.url;
|
|
let url = baseurl + ori.url;
|
|
- Toast.loading({
|
|
|
|
- message: '加载中...',
|
|
|
|
- forbidClick: true,
|
|
|
|
- duration: 0
|
|
|
|
- });
|
|
|
|
|
|
+ let isLoad = false;
|
|
|
|
+ let time = setTimeout(() => {
|
|
|
|
+ clearTimeout(time);
|
|
|
|
+ if(isLoad) return
|
|
|
|
+ Toast.loading({
|
|
|
|
+ message: '加载中...',
|
|
|
|
+ forbidClick: true,
|
|
|
|
+ duration: 0,
|
|
|
|
+ });
|
|
|
|
+ }, 200);
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
var xhttp;
|
|
var xhttp;
|
|
if (window.XMLHttpRequest) xhttp = new XMLHttpRequest();
|
|
if (window.XMLHttpRequest) xhttp = new XMLHttpRequest();
|
|
else if (window.ActiveXObject)
|
|
else if (window.ActiveXObject)
|
|
- xhttp = new window.ActiveXObject("Microsoft.XMLHTTP");
|
|
|
|
|
|
+ xhttp = new window.ActiveXObject('Microsoft.XMLHTTP');
|
|
let method = ori.method.toUpperCase();
|
|
let method = ori.method.toUpperCase();
|
|
- if (method === "GET") url += getdata(ori.data || {});
|
|
|
|
|
|
+ if (method === 'GET') url += getdata(ori.data || {});
|
|
xhttp.open(method, url, true);
|
|
xhttp.open(method, url, true);
|
|
xhttp.setRequestHeader(
|
|
xhttp.setRequestHeader(
|
|
- "Content-type",
|
|
|
|
|
|
+ 'Content-type',
|
|
ori.header
|
|
ori.header
|
|
- ? ori.header.contentType || "application/json"
|
|
|
|
- : "application/json"
|
|
|
|
|
|
+ ? ori.header.contentType || 'application/json'
|
|
|
|
+ : 'application/json'
|
|
);
|
|
);
|
|
- method === "GET" ? xhttp.send() : xhttp.send(JSON.stringify(ori.data));
|
|
|
|
|
|
+ method === 'GET' ? xhttp.send() : xhttp.send(JSON.stringify(ori.data));
|
|
xhttp.onreadystatechange = function () {
|
|
xhttp.onreadystatechange = function () {
|
|
if (this.readyState != 4) return;
|
|
if (this.readyState != 4) return;
|
|
- Toast.clear()
|
|
|
|
|
|
+ isLoad = true;
|
|
|
|
+ Toast.clear();
|
|
if (this.status === 404) {
|
|
if (this.status === 404) {
|
|
- Toast("请求失败 " + this.status);
|
|
|
|
|
|
+ Toast('请求失败 ' + this.status);
|
|
reject(this.status);
|
|
reject(this.status);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
if (this.status != 200) {
|
|
if (this.status != 200) {
|
|
- let t = JSON.parse(this.responseText || "{}");
|
|
|
|
- Toast(t.message || "请求失败 " + this.status);
|
|
|
|
|
|
+ let t = JSON.parse(this.responseText || '{}');
|
|
|
|
+ Toast(t.message || '请求失败 ' + this.status);
|
|
reject(t.message);
|
|
reject(t.message);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- // console.log(this.responseText)
|
|
|
|
|
|
+ // console.log(this.responseText)
|
|
let data = {};
|
|
let data = {};
|
|
try {
|
|
try {
|
|
data =
|
|
data =
|
|
- this.responseText !== "null"
|
|
|
|
- ? JSON.parse(this.responseText || "{}")
|
|
|
|
|
|
+ this.responseText !== 'null'
|
|
|
|
+ ? JSON.parse(this.responseText || '{}')
|
|
: {};
|
|
: {};
|
|
} catch (err) {
|
|
} catch (err) {
|
|
- Toast("请求失败");
|
|
|
|
|
|
+ Toast('请求失败');
|
|
console.error(err);
|
|
console.error(err);
|
|
reject(err);
|
|
reject(err);
|
|
}
|
|
}
|
|
if (data.code === 0) resolve(data.data);
|
|
if (data.code === 0) resolve(data.data);
|
|
else {
|
|
else {
|
|
- Toast(data.message || "请求失败");
|
|
|
|
|
|
+ Toast(data.message || '请求失败');
|
|
reject(data);
|
|
reject(data);
|
|
}
|
|
}
|
|
};
|
|
};
|