|
|
@ -103,3 +103,34 @@ export function formatDate(timestamp) { |
|
|
|
|
|
|
|
return `${year}-${month}-${day}`; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 预览 Base64 图片 |
|
|
|
* @param {string} base64 - base64 图片字符串(可包含 data:image/png;base64, 前缀) |
|
|
|
*/ |
|
|
|
export function previewBase64Image(base64) { |
|
|
|
const fs = tt.getFileSystemManager(); |
|
|
|
// 去掉前缀,如果有的话
|
|
|
|
const pureBase64 = base64.replace(/^data:image\/\w+;base64,/, ''); |
|
|
|
// 获取用户数据路径(替代 tt.env.USER_DATA_PATH)
|
|
|
|
const userDataPath = tt.getEnvInfoSync().common.USER_DATA_PATH; |
|
|
|
// 生成临时文件路径
|
|
|
|
const filePath = `${userDataPath}/preview_${Date.now()}.png`; |
|
|
|
// 写入临时文件
|
|
|
|
fs.writeFile({ |
|
|
|
filePath, |
|
|
|
data: pureBase64, |
|
|
|
encoding: 'base64', |
|
|
|
success: () => { |
|
|
|
// 调用预览
|
|
|
|
tt.previewImage({ |
|
|
|
current: filePath, |
|
|
|
urls: [filePath], |
|
|
|
success: () => console.log('预览成功'), |
|
|
|
fail: (err) => console.error('预览失败', err) |
|
|
|
}); |
|
|
|
}, |
|
|
|
fail: (err) => console.error('写入文件失败', err) |
|
|
|
}); |
|
|
|
} |