|
@@ -7,6 +7,7 @@ import {
|
|
|
import {LOGIN_ID_STORAGE_KEY } from '@/xiaolanbenlib/constant'
|
|
import {LOGIN_ID_STORAGE_KEY } from '@/xiaolanbenlib/constant'
|
|
|
|
|
|
|
|
import Taro, { FileSystemManager } from "@tarojs/taro";
|
|
import Taro, { FileSystemManager } from "@tarojs/taro";
|
|
|
|
|
+import { error } from "console";
|
|
|
import { useRef, useCallback, useEffect } from "react";
|
|
import { useRef, useCallback, useEffect } from "react";
|
|
|
|
|
|
|
|
const appTokenKey = `${APP_NAME}+${APP_VERSION}token`;
|
|
const appTokenKey = `${APP_NAME}+${APP_VERSION}token`;
|
|
@@ -282,8 +283,8 @@ export const getCanvasTempPath = (
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-export const savePicture = async (tmpPath: string): Promise<boolean> => {
|
|
|
|
|
- return new Promise((resove, reject) => {
|
|
|
|
|
|
|
+export const saveMediaFile = async (tmpPath: string, isVideo: boolean = false): Promise<boolean> => {
|
|
|
|
|
+ return new Promise((resove) => {
|
|
|
const params = {
|
|
const params = {
|
|
|
filePath: tmpPath,
|
|
filePath: tmpPath,
|
|
|
success() {
|
|
success() {
|
|
@@ -291,11 +292,11 @@ export const savePicture = async (tmpPath: string): Promise<boolean> => {
|
|
|
resove(true);
|
|
resove(true);
|
|
|
},
|
|
},
|
|
|
fail(error: any) {
|
|
fail(error: any) {
|
|
|
- console.log(error);
|
|
|
|
|
- reject(false);
|
|
|
|
|
|
|
+ console.log("save fail", error);
|
|
|
|
|
+ resove(false);
|
|
|
},
|
|
},
|
|
|
};
|
|
};
|
|
|
- Taro.saveImageToPhotosAlbum(params);
|
|
|
|
|
|
|
+ isVideo ? Taro.saveVideoToPhotosAlbum(params) : Taro.saveImageToPhotosAlbum(params);
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -368,14 +369,14 @@ export function generateUUID(): string {
|
|
|
|
|
|
|
|
export const pickNonEmpty = <T extends object>(obj: T): Partial<T> => {
|
|
export const pickNonEmpty = <T extends object>(obj: T): Partial<T> => {
|
|
|
return Object.fromEntries(
|
|
return Object.fromEntries(
|
|
|
- Object.entries(obj).filter(([_, value]) =>
|
|
|
|
|
|
|
+ Object.entries(obj).filter(([_, value]) =>
|
|
|
value !== "" && value != null
|
|
value !== "" && value != null
|
|
|
)
|
|
)
|
|
|
) as Partial<T>;
|
|
) as Partial<T>;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
export const getLoginId = () => {
|
|
export const getLoginId = () => {
|
|
|
- return Taro.getStorageSync(LOGIN_ID_STORAGE_KEY) // 打开小程序时创建 login_uuid
|
|
|
|
|
|
|
+ return Taro.getStorageSync(LOGIN_ID_STORAGE_KEY) // 打开小程序时创建 login_uuid
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export const isSuccess = (status: number)=> {
|
|
export const isSuccess = (status: number)=> {
|
|
@@ -408,33 +409,33 @@ export interface FileSizeOptions {
|
|
|
* @returns 转换后的文件大小字符串或数字
|
|
* @returns 转换后的文件大小字符串或数字
|
|
|
*/
|
|
*/
|
|
|
export function convertFileSize(size: number, options: FileSizeOptions = {}): string | number {
|
|
export function convertFileSize(size: number, options: FileSizeOptions = {}): string | number {
|
|
|
- const {
|
|
|
|
|
- precision = 2,
|
|
|
|
|
- binary = true,
|
|
|
|
|
- unit,
|
|
|
|
|
- withUnit = true
|
|
|
|
|
|
|
+ const {
|
|
|
|
|
+ precision = 2,
|
|
|
|
|
+ binary = true,
|
|
|
|
|
+ unit,
|
|
|
|
|
+ withUnit = true
|
|
|
} = options;
|
|
} = options;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 检查输入是否为有效数字
|
|
// 检查输入是否为有效数字
|
|
|
if (isNaN(size) || !isFinite(size)) {
|
|
if (isNaN(size) || !isFinite(size)) {
|
|
|
return withUnit ? 'Invalid size' : NaN;
|
|
return withUnit ? 'Invalid size' : NaN;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 处理零值
|
|
// 处理零值
|
|
|
if (size === 0) {
|
|
if (size === 0) {
|
|
|
return withUnit ? `0${unit || 'B'}` : 0;
|
|
return withUnit ? `0${unit || 'B'}` : 0;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 计算转换基数
|
|
// 计算转换基数
|
|
|
const base = binary ? 1024 : 1000;
|
|
const base = binary ? 1024 : 1000;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 定义单位列表
|
|
// 定义单位列表
|
|
|
const units: FileSizeUnit[] = ['bit', 'B', 'KB', 'MB', 'GB', 'TB'];
|
|
const units: FileSizeUnit[] = ['bit', 'B', 'KB', 'MB', 'GB', 'TB'];
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 如果指定了单位,直接转换
|
|
// 如果指定了单位,直接转换
|
|
|
if (unit) {
|
|
if (unit) {
|
|
|
const targetIndex = units.indexOf(unit);
|
|
const targetIndex = units.indexOf(unit);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 特殊处理bit到B的转换(1字节=8比特)
|
|
// 特殊处理bit到B的转换(1字节=8比特)
|
|
|
if (unit === 'B') {
|
|
if (unit === 'B') {
|
|
|
size = size / 8;
|
|
size = size / 8;
|
|
@@ -442,39 +443,39 @@ export function convertFileSize(size: number, options: FileSizeOptions = {}): st
|
|
|
// 如果目标是bit,直接返回原始值
|
|
// 如果目标是bit,直接返回原始值
|
|
|
return withUnit ? `${size} bit` : size;
|
|
return withUnit ? `${size} bit` : size;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 计算从B到目标单位的转换
|
|
// 计算从B到目标单位的转换
|
|
|
if (targetIndex > 1) {
|
|
if (targetIndex > 1) {
|
|
|
size = size / 8 / Math.pow(base, targetIndex - 1);
|
|
size = size / 8 / Math.pow(base, targetIndex - 1);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const result = Number(size.toFixed(precision));
|
|
const result = Number(size.toFixed(precision));
|
|
|
return withUnit ? `${result} ${unit}` : result;
|
|
return withUnit ? `${result} ${unit}` : result;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 特殊处理bit和B
|
|
// 特殊处理bit和B
|
|
|
if (size < 8) {
|
|
if (size < 8) {
|
|
|
return withUnit ? `${size} bit` : size;
|
|
return withUnit ? `${size} bit` : size;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (size < base * 8) {
|
|
if (size < base * 8) {
|
|
|
const result = Number((size / 8).toFixed(precision));
|
|
const result = Number((size / 8).toFixed(precision));
|
|
|
return withUnit ? `${result} B` : result;
|
|
return withUnit ? `${result} B` : result;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 自动选择合适的单位
|
|
// 自动选择合适的单位
|
|
|
let unitIndex = 2; // 从KB开始
|
|
let unitIndex = 2; // 从KB开始
|
|
|
let convertedSize = size / 8; // 先转换为字节
|
|
let convertedSize = size / 8; // 先转换为字节
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
while (convertedSize >= base && unitIndex < units.length - 1) {
|
|
while (convertedSize >= base && unitIndex < units.length - 1) {
|
|
|
convertedSize /= base;
|
|
convertedSize /= base;
|
|
|
unitIndex++;
|
|
unitIndex++;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
const result = Number(convertedSize.toFixed(precision));
|
|
const result = Number(convertedSize.toFixed(precision));
|
|
|
return withUnit ? `${result} ${units[unitIndex]}` : result;
|
|
return withUnit ? `${result} ${units[unitIndex]}` : result;
|
|
|
}
|
|
}
|
|
|
-//
|
|
|
|
|
|
|
+//
|
|
|
export const pxToRpx = (px: number) => {
|
|
export const pxToRpx = (px: number) => {
|
|
|
const systemInfo = Taro.getSystemInfoSync()
|
|
const systemInfo = Taro.getSystemInfoSync()
|
|
|
return (750 / systemInfo.windowWidth) * px
|
|
return (750 / systemInfo.windowWidth) * px
|
|
@@ -502,10 +503,10 @@ export function addOssProcessLowQualityParam(url: string): string {
|
|
|
|
|
|
|
|
// 分离URL和查询参数
|
|
// 分离URL和查询参数
|
|
|
const [baseUrl, existingQuery] = url.split('?');
|
|
const [baseUrl, existingQuery] = url.split('?');
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 构建新的查询参数
|
|
// 构建新的查询参数
|
|
|
const ossParam = 'x-oss-process=image/quality,Q_60/format,jpg';
|
|
const ossParam = 'x-oss-process=image/quality,Q_60/format,jpg';
|
|
|
- const newQuery = existingQuery
|
|
|
|
|
|
|
+ const newQuery = existingQuery
|
|
|
? `${existingQuery}&${ossParam}` // 已有查询参数,追加OSS参数
|
|
? `${existingQuery}&${ossParam}` // 已有查询参数,追加OSS参数
|
|
|
: ossParam; // 没有查询参数,直接使用OSS参数
|
|
: ossParam; // 没有查询参数,直接使用OSS参数
|
|
|
|
|
|
|
@@ -518,13 +519,13 @@ export function addOssProcessLowQualityParam(url: string): string {
|
|
|
// const exampleUrl2 = "https://example.com/image.png?width=200&height=200";
|
|
// 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";
|
|
// const exampleUrl3 = "https://example.com/image.webp?x-oss-process=image/resize,w_300";
|
|
|
|
|
|
|
|
-// console.log(addOssProcessParam(exampleUrl1));
|
|
|
|
|
|
|
+// console.log(addOssProcessParam(exampleUrl1));
|
|
|
// // 输出: https://example.com/image.jpg?x-oss-process=image/quality,Q_60/format,jpg
|
|
// // 输出: https://example.com/image.jpg?x-oss-process=image/quality,Q_60/format,jpg
|
|
|
|
|
|
|
|
-// console.log(addOssProcessParam(exampleUrl2));
|
|
|
|
|
|
|
+// console.log(addOssProcessParam(exampleUrl2));
|
|
|
// // 输出: https://example.com/image.png?width=200&height=200&x-oss-process=image/quality,Q_60/format,jpg
|
|
// // 输出: https://example.com/image.png?width=200&height=200&x-oss-process=image/quality,Q_60/format,jpg
|
|
|
|
|
|
|
|
-// console.log(addOssProcessParam(exampleUrl3));
|
|
|
|
|
|
|
+// console.log(addOssProcessParam(exampleUrl3));
|
|
|
// // 输出: https://example.com/image.webp?x-oss-process=image/resize,w_300 (保持不变,因为已包含OSS参数)
|
|
// // 输出: https://example.com/image.webp?x-oss-process=image/resize,w_300 (保持不变,因为已包含OSS参数)
|
|
|
|
|
|
|
|
|
|
|
|
@@ -537,7 +538,7 @@ export function restrictedPage() {
|
|
|
withShareTicket: false,
|
|
withShareTicket: false,
|
|
|
menus: [] // 不提供任何分享菜单
|
|
menus: [] // 不提供任何分享菜单
|
|
|
})
|
|
})
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 禁止分享到朋友圈
|
|
// 禁止分享到朋友圈
|
|
|
Taro.hideShareMenu({
|
|
Taro.hideShareMenu({
|
|
|
menus: ['shareAppMessage', 'shareTimeline']
|
|
menus: ['shareAppMessage', 'shareTimeline']
|