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.

176 lines
5.3 KiB
JavaScript

11 months ago
/**
* 通用uni-app网络请求
* 基于 Promise 对象实现更简单的 request 使用方式支持请求和响应拦截
*/
import apiUrl from "./api";
export default {
config: {
baseUrl: "http://114.218.158.24:9020",
//"https://appointtest2.szjixun.cn",
// "https://appointteam.szjixun.cn",
header: {
"Content-Type": "application/json",
// '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 = {};
}
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("pay-token") || "46d71a72d8d845ad7ed23eba9bdde260e635407190c2ce1bf7fd22088e41682ea07773ec65cae8946d2003f264d55961f96e0fc5da10eb96d3a348c1664e9644e756eda7154e1af9e70d1c9d2f100823a26885ea6df3249fe619995cb79dc5dbd5ead32d43b955d6b3ce83129097bb21bb8169898f48692de4f966db140c71b85a2065acfc948561c465279fc05194a79a1115f3b00170944b6c4bd6c52ada909a075c55d18d76c2ed2175602421b34b27362a05c350733ed73382471df0a08950f7f1e812a610c17bdac82d82d54be38969f6b41201af79b8d36ef177c5b94b180c1e16c357f434e29e022c62394499059c937382faf6e496d68b48d5f0c8770bff6906979526141dacb14bb2edae6f0325160574152e3f7a0944600210214ba274ed1e49813121ce451c0016e420ea0634a8e6936726c5d09024d22202c8d0",
tokenC: uni.getStorageSync("pay-token") || "undefined",
};
options.header = Object.assign({}, options.header, _token);
/*
_sign = {'sign': sign(JSON.stringify(options.data))}
options.header = Object.assign({}, options.header, _token,_sign)
*/
return new Promise((resolve, reject) => {
let _config = null;
options.complete = (response) => {
let statusCode = response.statusCode;
response.config = _config;
if (process.env.NODE_ENV === "development") {
if (statusCode === 200) {
// console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data))
}
}
if (this.interceptor.response) {
let newResponse = this.interceptor.response(response);
if (newResponse) {
response = newResponse;
}
}
if (document.cookie?.split("=")[1] && response.data.status === 401) {
uni.navigateTo({
url:
"/pages/verify/login?id=" +
uni.getStorageSync("verifyDataId") +
"&type=" +
uni.getStorageSync("type"),
});
return;
}
// 统一的响应日志记录
_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);
}
// 统一的请求日志记录
_reqlog(_config);
uni.request(_config);
});
},
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 _reqlog(req) {
if (process.env.NODE_ENV === "development") {
// console.log("【" + req.requestId + "】 地址:" + req.url)
if (req.data) {
// console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data))
}
}
//TODO 调接口异步写入日志数据库
}
/**
* 响应接口日志记录
*/
function _reslog(res) {
let _statusCode = res.statusCode;
if (process.env.NODE_ENV === "development") {
// console.log("【" + res.config.requestId + "】 地址:" + res.config.url)
if (res.config.data) {
// console.log("【" + res.config.requestId + "】 请求参数:" + JSON.stringify(res.config.data))
}
// console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res))
}
//TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
switch (_statusCode) {
case 200:
break;
case 401:
break;
case 404:
break;
default:
break;
}
}