You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
7.3 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { back, showModal } from "./traoAPI";
import { WCUserLogin } from "./Interface";
// import { getGlobalData, setGlobalData } from "./global";
import Taro from "@tarojs/taro";
import formdata from "./wxFormdata/formData";
import store from "../store";
const global = store.getState().global;
const global_requestUrlList = []; // 全局正在请求的url地址防止多次点击
export const Ajax = (params) => {
const app = Taro.getApp();
const domain = global.domain;
// console.log("Ajaxdomain", app, domain);
// Taro.showLoading({
// title: '请求中...',
// mask: true
// });
// 防止多次点击
const requestUrlList = global_requestUrlList;
const whiteList = []; // 白名单
if (
requestUrlList.indexOf(params.url) > -1 &&
whiteList.indexOf(params.url) < 0
) {
return;
} else {
requestUrlList.push(params.url);
}
return new Promise((reslove, reject) => {
Taro.request({
url: domain + params.url,
method: params.method || "GET",
header: {
Authorization: "Bearer " + Taro.getStorageSync("token"),
...params.header,
},
data: params.data,
success(res) {
if (res.statusCode !== 200) {
if (res.statusCode == 403) {
Taro.showModal({
title: "提示",
content: "登录已过期,请重新登录",
showCancel: false,
}).then((res) => {
Taro.setStorageSync("token", null);
Taro.clearStorage(); // 清空所有缓存
Taro.reLaunch({
// url: "/pages/login/login",
url: "/pages/login/login",
});
});
return;
}
Taro.showModal({
title: "提示",
content: String("错误码:" + res.statusCode),
showCancel: false,
});
return false;
}
if (params.url == "/Api/ReadFileInstrument") {
reslove(res);
return false;
}
if (res.data.code !== 200) {
let msg =
typeof res.data.msg == "string" ? res.data.msg : "没内容提示";
if (msg == "请不要操作太快哦") {
reject(res);
return false;
}
const noTipsCodes = [5001, 5002, 5003];
if (noTipsCodes.indexOf(res.data.code) == -1) {
Taro.showModal({
title: "提示",
content: msg,
showCancel: false,
}).then(() => {
reject(res);
if (res.data.code == 403 || res.data.msg === "登录状态已过期") {
Taro.setStorageSync("token", null);
Taro.reLaunch({
// url: "/pages/login/login",
url: "/pages/login/login",
});
}
});
} else {
reject(res);
}
return false;
}
reslove(res);
},
fail(err) {
console.log(err);
Taro.showModal({
title: "提示",
content: "请检查网络是否连接",
showCancel: false,
});
},
complete() {
Taro.hideLoading();
const requestUrlList = global_requestUrlList;
const newRequestUrlList = requestUrlList.splice(
requestUrlList.indexOf(params.url),
1
);
},
});
});
};
export const AjaxuploadFile = (params) => {
const app = Taro.getApp();
const domain = global.domain;
// Taro.showLoading({
// title: "请求中...",
// mask: true,
// });
return new Promise((reslove, reject) => {
Taro.uploadFile({
filePath: params.filePath,
name: "file",
url: domain + params.url,
formData: {
type: "api",
},
header: {
token: getGlobalData("token") || Taro.getStorageSync("token"),
},
success(res) {
let o = JSON.parse(res.data);
Taro.hideLoading();
if (res.statusCode !== 200) {
Taro.showModal({
title: "提示",
content: "上传出错",
showCancel: false,
});
return false;
}
if (o.code !== 200) {
let msg = typeof o.msg == "string" ? o.msg : "没内容提示";
Taro.showModal({
title: "提示",
content: msg,
showCancel: false,
});
return false;
}
reslove(o);
},
fail() {
Taro.hideLoading();
Taro.showModal({
title: "提示",
content: "请检查网络是否连接",
showCancel: false,
});
},
});
});
};
export const AjaxFormData = (params) => {
const app = Taro.getApp();
const domain = global.domain;
// 防止多次点击
const requestUrlList = global_requestUrlList;
const whiteList = ["/Api/MessageList"];
if (
requestUrlList.indexOf(params.url) > -1 &&
whiteList.indexOf(params.url) < 0
) {
return;
} else {
requestUrlList.push(params.url);
}
return new Promise((reslove, reject) => {
Taro.request({
url: domain + params.url,
method: params.method || "GET",
header: {
Authorization: "Bearer " + Taro.getStorageSync("token"),
...params.header,
"content-type": params.data.contentType,
},
data: params.data.buffer,
success(res) {
if (res.statusCode !== 200) {
if (res.statusCode == 403) {
Taro.showModal({
title: "提示",
content: "登录已过期,请重新登录",
showCancel: false,
}).then((res) => {
Taro.setStorageSync("token", null);
Taro.clearStorage(); // 清空所有缓存
Taro.reLaunch({
url: "/pages/login/login",
});
});
return;
}
Taro.showModal({
t2: String("错误码:" + res.statusCode),
showCancel: false,
});
return false;
}
if (res.data.code !== 200) {
let msg =
typeof res.data.msg == "string" ? res.data.msg : "没内容提示";
if (msg == "请不要操作太快哦") {
reject(res);
return false;
}
const noTipsCodes = [5001, 5002, 5003];
if (noTipsCodes.indexOf(res.data.code) == -1) {
Taro.showModal({
title: "提示",
content: msg,
showCancel: false,
}).then(() => {
reject(res);
if (res.data.code == 403 || res.data.msg === "登录状态已过期") {
Taro.setStorageSync("token", null);
Taro.reLaunch({
url: "/pages/login/login",
});
}
});
} else {
reject(res);
}
return false;
}
reslove(res);
},
fail(err) {
console.log(err);
Taro.showModal({
title: "提示",
content: "请检查网络是否连接",
showCancel: false,
});
},
complete() {
Taro.hideLoading();
const requestUrlList = global_requestUrlList;
const newRequestUrlList = requestUrlList.splice(
requestUrlList.indexOf(params.url),
1
);
},
});
});
};