|
@@ -479,3 +479,50 @@ export const pxToRpx = (px: number) => {
|
|
|
const systemInfo = Taro.getSystemInfoSync()
|
|
const systemInfo = Taro.getSystemInfoSync()
|
|
|
return (750 / systemInfo.windowWidth) * px
|
|
return (750 / systemInfo.windowWidth) * px
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 为图片URL添加阿里云OSS处理参数
|
|
|
|
|
+ * 如果URL中不包含?x-oss-process=,则添加?x-oss-process=image/quality,Q_60/format,jpg
|
|
|
|
|
+ * 如果URL中已包含其他查询参数,则追加&x-oss-process=image/quality,Q_60/format,jpg
|
|
|
|
|
+ * @param url 原始图片URL
|
|
|
|
|
+ * @returns 处理后的图片URL
|
|
|
|
|
+ */
|
|
|
|
|
+export function addOssProcessLowQualityParam(url: string): string {
|
|
|
|
|
+ // 检查URL是否有效
|
|
|
|
|
+ if (!url || typeof url !== 'string') {
|
|
|
|
|
+ return url;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 检查是否已经包含OSS处理参数
|
|
|
|
|
+ if (url.includes('x-oss-process=')) {
|
|
|
|
|
+ return url;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 分离URL和查询参数
|
|
|
|
|
+ const [baseUrl, existingQuery] = url.split('?');
|
|
|
|
|
+
|
|
|
|
|
+ // 构建新的查询参数
|
|
|
|
|
+ const ossParam = 'x-oss-process=image/quality,Q_60/format,jpg';
|
|
|
|
|
+ const newQuery = existingQuery
|
|
|
|
|
+ ? `${existingQuery}&${ossParam}` // 已有查询参数,追加OSS参数
|
|
|
|
|
+ : ossParam; // 没有查询参数,直接使用OSS参数
|
|
|
|
|
+
|
|
|
|
|
+ // 返回处理后的URL
|
|
|
|
|
+ return `${baseUrl}?${newQuery}`;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// 使用示例
|
|
|
|
|
+// const exampleUrl1 = "https://example.com/image.jpg";
|
|
|
|
|
+// const exampleUrl2 = "https://example.com/image.png?width=200&height=200";
|
|
|
|
|
+// const exampleUrl3 = "https://example.com/image.webp?x-oss-process=image/resize,w_300";
|
|
|
|
|
+
|
|
|
|
|
+// console.log(addOssProcessParam(exampleUrl1));
|
|
|
|
|
+// // 输出: https://example.com/image.jpg?x-oss-process=image/quality,Q_60/format,jpg
|
|
|
|
|
+
|
|
|
|
|
+// console.log(addOssProcessParam(exampleUrl2));
|
|
|
|
|
+// // 输出: https://example.com/image.png?width=200&height=200&x-oss-process=image/quality,Q_60/format,jpg
|
|
|
|
|
+
|
|
|
|
|
+// console.log(addOssProcessParam(exampleUrl3));
|
|
|
|
|
+// // 输出: https://example.com/image.webp?x-oss-process=image/resize,w_300 (保持不变,因为已包含OSS参数)
|