diff --git a/products/expert/src/main/ets/pages/WebView/WebPage.ets b/products/expert/src/main/ets/pages/WebView/WebPage.ets index 4b2e1f5..b457cae 100644 --- a/products/expert/src/main/ets/pages/WebView/WebPage.ets +++ b/products/expert/src/main/ets/pages/WebView/WebPage.ets @@ -4,9 +4,8 @@ import router from '@ohos.router'; import { image } from '@kit.ImageKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { promptAction } from '@kit.ArkUI'; -import { componentSnapshot } from '@kit.ArkUI'; import { fileIo, fileUri } from '@kit.CoreFileKit'; -const TAG = 'WebViewSaveImage'; +import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component @@ -18,129 +17,21 @@ struct WebPage { private customUserAgent: string = 'Mozilla/5.0 (Linux; Android 10; HarmonyOS) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 gdxz-expert'; private hcp_token:string = preferenceStore.getItemString(BasicConstant.EXPERT_HCP_TOKEN) - // 需要Cookie的特定域名列表 - private cookieDomains: string[] = [ - 'dev-wx.igandan.com', - 'oss.igandan.com' - ]; - - // 检查URL是否需要Cookie注入 - private needsCookieInjection(url: string): boolean { - return this.cookieDomains.some(domain => url.includes(domain)) && - this.hcp_token.length > 0; - } - - // 方法1: 使用JavaScript注入设置cookie - private injectCookieJS() { - if (!this.needsCookieInjection(this.url)) { - console.info('123', 'URL不需要cookie注入或token为空'); - return; - } - - const cookieScript = `document.cookie = 'hcp_token=${this.hcp_token}; domain=.igandan.com; path=/'; console.log('Cookie已设置:', document.cookie);`; - - this.controller.runJavaScript(cookieScript, (error, result) => { - if (error) { - console.error('123', 'JS注入失败:', error); - this.showDebugAlert('JS注入失败', error.message); - } else { - console.info('123', 'JS注入成功'); - // 延迟验证cookie - setTimeout(() => { - this.verifyCookie(); - }, 1000); - } - }); - } - - // 方法2: 使用更完整的cookie设置脚本 - private injectCookieWithOptions() { - if (!this.needsCookieInjection(this.url)) { - return; - } - - const cookieScript = ` - try { - // 设置cookie,包含更多选项 - document.cookie = 'hcp_token=${this.hcp_token}; domain=.igandan.com; path=/; secure; samesite=none'; - console.log('完整cookie设置完成:', document.cookie); - return 'success'; - } catch(e) { - console.error('设置cookie失败:', e); - return 'error: ' + e.message; - } - `; - - this.controller.runJavaScript(cookieScript, (error, result) => { - if (error) { - console.error('123', '完整cookie设置失败:', error); - } else { - console.info('123', '完整cookie设置结果:', result); - } - }); - } - - // 方法3: 使用localStorage作为备选方案 - private injectTokenToLocalStorage() { - if (!this.needsCookieInjection(this.url)) { - return; - } - - const localStorageScript = ` - try { - localStorage.setItem('hcp_token', '${this.hcp_token}'); - sessionStorage.setItem('hcp_token', '${this.hcp_token}'); - console.log('Token已存储到localStorage和sessionStorage'); - return 'success'; - } catch(e) { - console.error('存储token失败:', e); - return 'error: ' + e.message; - } - `; - - this.controller.runJavaScript(localStorageScript, (error, result) => { - if (error) { - console.error('123', 'localStorage设置失败:', error); - } else { - console.info('123', 'localStorage设置结果:', result); - } - }); - } - - // 调试方法:显示弹窗 - private showDebugAlert(title: string, message: string) { - promptAction.showToast({ - message: `${title}: ${message}`, - duration: 3000 - }); - } - - private verifyCookie() { - this.controller.runJavaScript(` - const hasCookie = document.cookie.includes('hcp_token=${this.hcp_token}'); - console.log('Cookie验证结果:', hasCookie); - console.log('当前所有cookie:', document.cookie); - return hasCookie; - `, (error, result) => { - if (error) { - console.error('123', 'Cookie验证失败:', error); - this.showDebugAlert('Cookie验证失败', error.message); - } else if (!result) { - console.warn('123', 'Cookie未生效'); - this.showDebugAlert('Cookie状态', '未检测到有效Cookie'); - } else { - console.info('123', 'Cookie验证通过'); - this.showDebugAlert('Cookie状态', '注入成功'); - } - }); - } - @State contentWidth: number = 0; @State contentHeight: number = 0; @State url: string = this.params.url; @State title: string = this.params.title; // customUserAgent: string = 'gdxz-expert'; + private cookieDomains: string[] = [ + 'https://dev-wx.igandan.com/modernHepatology/index.htm#/liver-tools', + 'https://oss.igandan.com/modernHepatology/index.htm#/liver-tools' + ]; + + private needsCookieInjection(url: string): boolean { + return this.cookieDomains.some(domain => url.includes(domain)) && + this.hcp_token.length > 0; + } onBackPress(): boolean | void { if (this.controller.accessStep(-1)) { @@ -170,31 +61,17 @@ struct WebPage { .onControllerAttached(() => { let userAgent = this.controller.getUserAgent() + this.customUserAgent; this.controller.setCustomUserAgent(userAgent); - - // 在WebView控制器附加时注入cookie - setTimeout(() => { - this.injectCookieJS(); - this.injectCookieWithOptions(); - this.injectTokenToLocalStorage(); - }, 500); }) - .onPageBegin(() => { - // 在页面开始加载时注入cookie - console.info('123', '页面开始加载,注入cookie'); - this.injectCookieJS(); - this.injectCookieWithOptions(); - this.injectTokenToLocalStorage(); + .onPageBegin(()=>{ + if (this.needsCookieInjection(this.url)) { + try { + webview.WebCookieManager.configCookieSync('https://dev-wx.igandan.com', 'hcp_from=expert_app;path=/;domain=.igandan.com;hcp_token='+this.hcp_token+';'); + } catch (error) { + console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); + } + } }) .onPageEnd(() => { - // 在页面加载完成后再次注入cookie - console.info('123', '页面加载完成,再次注入cookie'); - setTimeout(() => { - this.injectCookieJS(); - this.injectCookieWithOptions(); - this.injectTokenToLocalStorage(); - this.verifyCookie(); - }, 1000); - // 注入JS获取body高度 this.controller.runJavaScript( 'document.documentElement.scrollWidth', (error, result) => { @@ -204,6 +81,7 @@ struct WebPage { this.controller.runJavaScript('document.body.scrollHeight',(error,result)=>{ if (!error) this.contentHeight = Number(result); }) + }) .width('100%') .layoutWeight(1)