231 lines
6.9 KiB
JavaScript
231 lines
6.9 KiB
JavaScript
import Request from "./request/index.js";
|
||
import useToast from "@/hooks/toast/useToast.js";
|
||
import { OAorMc } from "../index.js";
|
||
const { showMessage } = useToast();
|
||
|
||
console.log("webview", window?.plus?.webview.getWebviewById("wv").tokenScan);
|
||
const request = new Request({
|
||
baseURL:
|
||
OAorMc() === "MC"
|
||
? import.meta.env.VITE_API_URL
|
||
: import.meta.env.VITE_API_OA_URL,
|
||
timeout: 1000 * 60 * 5,
|
||
interceptors: {
|
||
//实例的请求拦截器
|
||
requestInterceptors: (config) => {
|
||
config.headers["Content-Type"] =
|
||
config.method === "get"
|
||
? "application/x-www-form-urlencoded"
|
||
: "application/json";
|
||
|
||
const token =
|
||
uni.getStorageSync("token") ||
|
||
window?.plus?.storage.getItemAsync("token") ||
|
||
window?.plus?.webview.getWebviewById("wv").tokenScan ||
|
||
uni.getStorageSync("store-token") ||
|
||
"";
|
||
console.log(
|
||
"tokenServer",
|
||
|
||
uni.getStorageSync("token"),
|
||
"realtoken",
|
||
token
|
||
);
|
||
if (config.isFormData) {
|
||
config.headers["Content-Type"] = "multipart/form-data";
|
||
config.headers["Authorization"] = token;
|
||
} else {
|
||
config.headers["Authorization"] = token;
|
||
}
|
||
return config;
|
||
},
|
||
//实例的响应拦截器
|
||
responseInterceptors: async (response) => {
|
||
if (response.data.status === 1) {
|
||
showMessage({ type: "error", message: response.data.msg });
|
||
}
|
||
// if(response.data.status === 0){
|
||
// uni.setStorageSync("token", response.data.data.Token);
|
||
// uni.setStorageSync("userInfo", response.data.data.AccountInfo);
|
||
// response.config.headers["Authorization"] = response.data.data.Token;
|
||
// }
|
||
if (response.data.code === 401) {
|
||
uni.clearStorageSync(); //清缓存
|
||
const params = new URLSearchParams(window.location.search); //属性包含当前 URL 的查询字符串部分
|
||
const id = params.get("id");
|
||
const pid = params.get("pid");
|
||
uni.navigateTo({
|
||
url: "/pages/login/index?id=" + id + "&pid=" + pid,
|
||
});
|
||
return false;
|
||
}
|
||
if ([200, 201, 204].includes(response.status)) {
|
||
return response.config.responseType === "blob" ? response : response;
|
||
} else {
|
||
// showMessage({ type: "error", message: response.data.msg });
|
||
return Promise.reject(
|
||
new Error(response.data.msg || "An error occurred.")
|
||
);
|
||
}
|
||
},
|
||
},
|
||
});
|
||
|
||
const fontRequest = (config) => {
|
||
console.log(import.meta);
|
||
|
||
if (["get", "GET"].includes(config.method)) {
|
||
config.params = config.data;
|
||
}
|
||
return request.request(config);
|
||
};
|
||
|
||
/**
|
||
* 通用uni-app网络请求
|
||
* 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截
|
||
*/
|
||
// import useToast from "@/hooks/toast/useToast.js";
|
||
// import { OAorMc } from "../index.js";
|
||
// const { showMessage } = useToast();
|
||
// export default {
|
||
// config: {
|
||
// baseUrl:
|
||
// OAorMc() === "MC"
|
||
// ? import.meta.env.VITE_API_URL
|
||
// : import.meta.env.VITE_API_OA_URL,
|
||
// header: {
|
||
// "Content-Type": "application/json;charset=UTF-8",
|
||
// // 'Content-Type':'application/x-www-form-urlencoded'
|
||
// },
|
||
// data: {},
|
||
// method: "GET",
|
||
// dataType: "json" /* 如设为json,会对返回的数据做一次 JSON.parse */,
|
||
// responseType: "text",
|
||
// success() {},
|
||
// fail() {},
|
||
// complete() {},
|
||
// },
|
||
// interceptor: {
|
||
// request: null,
|
||
// response: null,
|
||
// },
|
||
// request(options) {
|
||
// if (!options) {
|
||
// options = {};
|
||
// }
|
||
// console.log(123, options.baseUrl);
|
||
// options.baseUrl = options.baseUrl || this.config.baseUrl;
|
||
// options.dataType = options.dataType || this.config.dataType;
|
||
// options.url = options.baseUrl + options.url;
|
||
// options.data = options.data || {};
|
||
// options.method = options.method || this.config.method;
|
||
// //TODO 加密数据
|
||
// options.header = options.header || this.config.header;
|
||
// //TODO 数据签名
|
||
// let _token = {
|
||
// Authorization: uni.getStorageSync("oa-token") || "undefined",
|
||
// };
|
||
// options.header = Object.assign({}, options.header, _token);
|
||
|
||
// return new Promise((resolve, reject) => {
|
||
// let _config = null;
|
||
|
||
// options.complete = async (response) => {
|
||
// let statusCode = response.statusCode;
|
||
// response.config = _config;
|
||
// if (this.interceptor.response) {
|
||
// let newResponse = this.interceptor.response(response);
|
||
// if (newResponse) {
|
||
// response = newResponse;
|
||
// }
|
||
// }
|
||
// if (response.data.status === 401) {
|
||
// uni.clearStorageSync(); //清缓存
|
||
// const params = new URLSearchParams(window.location.search); //属性包含当前 URL 的查询字符串部分
|
||
// const id = params.get("id");
|
||
// const pid = params.get("pid");
|
||
// uni.navigateTo({
|
||
// url: "/pages/login/index?id=" + id + "&pid=" + pid,
|
||
// });
|
||
// return false;
|
||
// } else {
|
||
// // 统一的响应日志记录
|
||
// _reslog(response);
|
||
// if (statusCode === 200) {
|
||
// //成功
|
||
// resolve(response.data);
|
||
// } else {
|
||
// reject(response);
|
||
// }
|
||
// }
|
||
// };
|
||
|
||
// _config = Object.assign({}, this.config, options);
|
||
// _config.requestId = new Date().getTime();
|
||
|
||
// if (this.interceptor.request) {
|
||
// this.interceptor.request(_config);
|
||
// }
|
||
|
||
// uni.request(_config);
|
||
// });
|
||
// },
|
||
// isRefreshing: false,
|
||
// refreshSubscribers: [],
|
||
// get(url, data, options) {
|
||
// if (!options) {
|
||
// options = {};
|
||
// }
|
||
// options.url = url;
|
||
// options.data = data;
|
||
// options.method = "GET";
|
||
// return this.request(options);
|
||
// },
|
||
// post(url, data, options, header) {
|
||
// if (!options) {
|
||
// options = {};
|
||
// }
|
||
// options.url = url;
|
||
// options.data = data;
|
||
// options.header = header;
|
||
// options.method = "POST";
|
||
// return this.request(options);
|
||
// },
|
||
// put(url, data, options) {
|
||
// if (!options) {
|
||
// options = {};
|
||
// }
|
||
// options.url = url;
|
||
// options.data = data;
|
||
// options.method = "PUT";
|
||
// return this.request(options);
|
||
// },
|
||
// delete(url, data, options) {
|
||
// if (!options) {
|
||
// options = {};
|
||
// }
|
||
// options.url = url;
|
||
// options.data = data;
|
||
// options.method = "DELETE";
|
||
// return this.request(options);
|
||
// },
|
||
// };
|
||
|
||
// /**
|
||
// * 响应接口日志记录
|
||
// */
|
||
// function _reslog(res) {
|
||
// let _statusCode = res.data.status;
|
||
// //TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
|
||
// switch (_statusCode) {
|
||
// case 200:
|
||
// break;
|
||
// case 401:
|
||
// break;
|
||
// case 404:
|
||
// break;
|
||
// default:
|
||
// break;
|
||
// }
|
||
// }
|