|
|
@@ -19,6 +19,15 @@ const hideLoading = () => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+// 将对象转为 x-www-form-urlencoded 字符串(兼容小程序环境无 URLSearchParams)
|
|
|
+function toFormUrlEncoded(obj) {
|
|
|
+ if (obj == null || typeof obj !== 'object') return '';
|
|
|
+ return Object.keys(obj)
|
|
|
+ .filter((k) => obj[k] != null && obj[k] !== '')
|
|
|
+ .map((k) => encodeURIComponent(k) + '=' + encodeURIComponent(obj[k]))
|
|
|
+ .join('&');
|
|
|
+}
|
|
|
+
|
|
|
// 获取当前页面路径以及参数
|
|
|
export function getCurrentPageUrlWithArgs() {
|
|
|
const pages = getCurrentPages();
|
|
|
@@ -49,6 +58,7 @@ export class Request {
|
|
|
message = true,
|
|
|
hideErrorModal = false, // 报误弹窗
|
|
|
params, // 请求参数
|
|
|
+ formUrlEncoded = false, // 是否以 application/x-www-form-urlencoded 发送
|
|
|
customizeUrl = '' //自定义url
|
|
|
} = config;
|
|
|
|
|
|
@@ -62,6 +72,12 @@ export class Request {
|
|
|
params = Object.assign(params || {}, { _t: now });
|
|
|
}
|
|
|
|
|
|
+ let data = params;
|
|
|
+ if (formUrlEncoded && params && typeof params === 'object') {
|
|
|
+ data = toFormUrlEncoded(params);
|
|
|
+ header['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
|
+ }
|
|
|
+
|
|
|
loading && showLoading();
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
@@ -70,7 +86,7 @@ export class Request {
|
|
|
header,
|
|
|
method,
|
|
|
timeout,
|
|
|
- data: params,
|
|
|
+ data,
|
|
|
dataType: 'json',
|
|
|
success: async (res) => {
|
|
|
const { data } = res;
|
|
|
@@ -143,6 +159,13 @@ export class Request {
|
|
|
method: 'POST'
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ put(options) {
|
|
|
+ return this.#request({
|
|
|
+ ...options,
|
|
|
+ method: 'PUT'
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export const api = new Request();
|