sunshibo 2 сар өмнө
parent
commit
8dac386807
2 өөрчлөгдсөн 42 нэмэгдсэн , 5 устгасан
  1. 18 4
      utils/file.js
  2. 24 1
      utils/request.js

+ 18 - 4
utils/file.js

@@ -36,11 +36,25 @@ export function getFileUrl(url, planB = false) {
 	let prefix = uni.getStorageSync(CDN_CACHE_KEY) || DEFAULT_CDN_URL;
 	if (planB) prefix = uni.getStorageSync('CDN_UPLOAD') || UPLOAD;
 
-	// ios老版本不支持webp
-	const systemInfo = uni.getSystemInfoSync();
-	const platform = systemInfo.platform;
+	// ios老版本不支持webp(使用 getDeviceInfo 替代已废弃的 getSystemInfoSync)
+	let platform = '';
+	let systemVersion = '';
+	try {
+		if (typeof uni.getDeviceInfo === 'function') {
+			const deviceInfo = uni.getDeviceInfo();
+			platform = deviceInfo.platform || '';
+			systemVersion = deviceInfo.system || '';
+		} else {
+			const systemInfo = uni.getSystemInfoSync();
+			platform = systemInfo.platform || '';
+			systemVersion = systemInfo.osVersion || systemInfo.system || '';
+		}
+	} catch (e) {}
 	if (url.split('.')[1] == 'webp' && platform == 'ios') {
-		if (systemInfo.osVersion.split('.')[0] < '18' || (systemInfo.osVersion.split('.')[0] == '18' && systemInfo.osVersion.split('.')[1] < '2')) {
+		const parts = systemVersion.split('.');
+		const major = parts[0] || '0';
+		const minor = parts[1] || '0';
+		if (major < '18' || (major == '18' && minor < '2')) {
 			url = url.split('.')[0] + '.jpg';
 		}
 	}

+ 24 - 1
utils/request.js

@@ -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();