Files
erp_sb/electron-vue-template/src/renderer/utils/imageProxy.ts
zhangzijienbplus 132299c4b7 feat(electron-vue-template):重构认证与设备管理模块
- 统一token存取逻辑,封装getToken/setToken/removeToken方法
-优化设备ID获取逻辑,调整API路径
- 完善设备管理接口类型定义,增强类型安全
- 调整SSE连接逻辑,使用统一配置管理- 重构HTTP客户端,集中管理后端服务配置
- 更新认证相关API接口,完善请求/响应类型
- 优化设备列表展示逻辑,移除冗余字段
- 调整图片代理路径,统一API前缀
- 完善用户反馈列表展示功能,增强交互体验
- 移除冗余的错误处理逻辑,简化代码结构
2025-10-16 10:37:00 +08:00

78 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 通过后端代理获取图片并转换为Base64
* @param imageUrl 原始图片URL
* @param maxSize 最大尺寸默认80px
* @returns Promise<string | null> Base64字符串或null
*/
export async function convertImageToBase64ViaProxy(imageUrl: string, maxSize: number = 80): Promise<string | null> {
if (!imageUrl) return null
try {
const proxyUrl = `http://127.0.0.1:8081/api/system/proxy/image?url=${encodeURIComponent(imageUrl)}`
const response = await fetch(proxyUrl)
if (!response.ok) return null
const contentType = response.headers.get('Content-Type')
const arrayBuffer = await response.arrayBuffer()
if (!arrayBuffer || arrayBuffer.byteLength === 0) return null
if (arrayBuffer.byteLength < 1000) return null
const mimeType = contentType && contentType.startsWith('image/') ? contentType : 'image/jpeg'
const imageBlob = new Blob([arrayBuffer], { type: mimeType })
const objectUrl = URL.createObjectURL(imageBlob)
return new Promise((resolve) => {
const img = new Image()
img.onload = () => {
try {
const canvas = document.createElement('canvas')
const ratio = Math.min(maxSize / img.width, maxSize / img.height)
canvas.width = img.width * ratio
canvas.height = img.height * ratio
const ctx = canvas.getContext('2d')
if (!ctx) {
URL.revokeObjectURL(objectUrl)
resolve(null)
return
}
ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
const base64 = canvas.toDataURL('image/jpeg', 0.8)
URL.revokeObjectURL(objectUrl)
resolve(base64)
} catch (error) {
URL.revokeObjectURL(objectUrl)
resolve(null)
}
}
img.onerror = () => {
URL.revokeObjectURL(objectUrl)
resolve(null)
}
img.src = objectUrl
})
} catch (error) {
return null
}
}
/**
* 批量处理图片转换
* @param imageUrls 图片URL数组
* @param maxSize 最大尺寸
* @returns Promise<(string | null)[]> Base64数组
*/
export async function batchConvertImages(imageUrls: string[], maxSize: number = 80): Promise<(string | null)[]> {
const promises = imageUrls.map(async (url) => {
if (!url) return null
return await convertImageToBase64ViaProxy(url, maxSize)
})
return await Promise.all(promises)
}