@@ -34,53 +34,39 @@ const NodeInfoLogger = (function() {
3434 return `${ baseUrl } ?filename=${ filename } &subfolder=&type=${ type } &rand=${ Math . random ( ) } ` ;
3535 } ;
3636
37- // 获取图片并转换为base64
37+ // 获取图片并转换为base64(根据 server_mode 判断逻辑)
3838 const getImageAsBase64 = async ( filename , type ) => {
3939 try {
4040 // 检查服务器模式
4141 const serverModeResponse = await fetch ( "/bizyair/server_mode" ) ;
4242 const serverModeData = await serverModeResponse . json ( ) ;
4343
44- let token = null ;
45- if ( serverModeData . data . server_mode ) {
46- // 服务器模式,需要token
47- token = await new Promise ( ( resolve ) => {
48- const checkToken = ( ) => {
49- const token = getCookie ( "bizy_token" ) ;
50- if ( token ) {
51- clearInterval ( timer ) ;
52- resolve ( token ) ;
53- }
54- } ;
55-
56- const timer = setInterval ( checkToken , 300 ) ;
57- checkToken ( ) ; // 立即执行一次检查
58- } ) ;
44+ // 非服务器模式下,如果 filename 已经是 base64 数据,直接返回
45+ if ( ! serverModeData . data . server_mode && filename . startsWith ( 'data:' ) ) {
46+ console . log ( '本地模式:filename 已经是 base64 数据,直接返回' ) ;
47+ return filename ;
5948 }
60-
61- const imageUrl = buildImageUrl ( filename , type ) ;
62- const headers = { } ;
63- if ( token ) {
64- headers [ "Authorization" ] = `Bearer ${ token } ` ;
49+
50+ let imageUrl ;
51+ let headers = { } ;
52+
53+ if ( serverModeData . data . server_mode ) {
54+ // 服务器模式,改为请求后端转发接口
55+ imageUrl = `/bizyair/proxy_view?filename=${ encodeURIComponent ( filename ) } ` ;
56+ } else {
57+ // 本地模式,使用原有 buildImageUrl
58+ imageUrl = buildImageUrl ( filename , type ) ;
6559 }
6660
67- const response = await fetch ( imageUrl , {
68- headers : headers
69- } ) ;
61+ const response = await fetch ( imageUrl , { headers } ) ;
7062 if ( ! response . ok ) {
7163 throw new Error ( `获取图片失败: ${ response . status } ${ response . statusText } ` ) ;
7264 }
7365 const blob = await response . blob ( ) ;
74-
75- // 将blob转为File对象
66+ // 转 base64
7667 const file = new File ( [ blob ] , filename , { type : blob . type } ) ;
77-
78- // 使用formatToWebp进行压缩
7968 const { base64 : compressedBase64 } = await formatToWebp ( file ) ;
80-
8169 return compressedBase64 ;
82-
83-
8470 } catch ( error ) {
8571 console . error ( '获取图片并转换为base64失败:' , error ) ;
8672 return null ;
@@ -121,17 +107,32 @@ const NodeInfoLogger = (function() {
121107 const blob = new Blob ( [ byteArray ] , { type : mimeType || base64 . split ( ':' ) [ 1 ] . split ( ';' ) [ 0 ] } )
122108 return new File ( [ blob ] , filename , { type : blob . type } )
123109 }
124- // 构建图片信息对象
125- const buildImageInfo = ( imageData ) => {
110+ // 构建图片信息对象(根据 server_mode 判断逻辑)
111+ const buildImageInfo = async ( imageData ) => {
126112 if ( ! imageData ?. filename ) return null ;
127-
128- // 根据节点类型确定图片类型
129- const type = imageData . type || 'temp' ;
113+ // 检查服务器模式
114+ const serverModeResponse = await fetch ( "/bizyair/server_mode" ) ;
115+ const serverModeData = await serverModeResponse . json ( ) ;
116+ const type = imageData . type || 'temp' ;
130117 const path = `/${ type } /${ imageData . filename } ` ;
131-
118+ let url ;
119+
120+ if ( serverModeData . data . server_mode ) {
121+ // 服务器模式,使用 view 接口
122+ url = `/view?filename=${ encodeURIComponent ( imageData . filename ) } ` ;
123+ } else {
124+ // 本地模式,如果 filename 是 base64 数据,直接使用
125+ if ( imageData . filename . startsWith ( 'data:' ) ) {
126+ url = imageData . filename ;
127+ } else {
128+ // 否则使用 buildImageUrl 构建 URL
129+ url = buildImageUrl ( imageData . filename , type ) ;
130+ }
131+ }
132+
132133 return {
133134 filename : imageData . filename ,
134- url : buildImageUrl ( imageData . filename , type ) ,
135+ url : url ,
135136 path : path ,
136137 type : type
137138 } ;
@@ -252,7 +253,7 @@ const NodeInfoLogger = (function() {
252253 // 使用异步IIFE处理图片,不阻塞主流程
253254 ( async ( ) => {
254255 try {
255- const imageInfo = buildImageInfo ( this . images [ 0 ] ) ;
256+ const imageInfo = await buildImageInfo ( this . images [ 0 ] ) ;
256257
257258 // 检查是否存在全局bizyAirLib对象及logNodeInfo函数
258259 if ( typeof bizyAirLib !== 'undefined' && typeof bizyAirLib . logNodeInfo === 'function' ) {
0 commit comments