cookie
This commit is contained in:
parent
58931fcf2e
commit
691af9e35c
@ -119,6 +119,7 @@ export class BasicConstant {
|
||||
static readonly addWorkPlace= BasicConstant.urlExpertApp+"addWorkPlace";//执业地点-增加
|
||||
static readonly editWorkPlace= BasicConstant.urlExpertApp+"editWorkPlace";//执业地点-修改
|
||||
static readonly addInterrogationAnswer= BasicConstant.urlExpertAPI+"addInterrogationAnswer";// 一问多答 回答
|
||||
static readonly checkUser = BasicConstant.urlExpertAPI+'checkUser'
|
||||
static readonly province=['全国','北京市','天津市','河北省','山西省'
|
||||
,'内蒙古自治区','辽宁省','吉林省','黑龙江省','上海市','江苏省','浙江省'
|
||||
,'安徽省','福建省','江西省','山东省','河南省','湖北省','湖南省','广东省',
|
||||
@ -146,5 +147,6 @@ export class BasicConstant {
|
||||
static readonly consult = "consult";
|
||||
|
||||
static readonly ExpertAesKey = 'deoep09_klodLdAo'
|
||||
static readonly EXPERT_HCP_TOKEN = 'hcp_token'
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import webview from '@ohos.web.webview';
|
||||
import { HdNav } from '@itcast/basic';
|
||||
import { BasicConstant, HdNav, preferenceStore } from '@itcast/basic';
|
||||
import router from '@ohos.router';
|
||||
import { image } from '@kit.ImageKit';
|
||||
import { photoAccessHelper } from '@kit.MediaLibraryKit';
|
||||
@ -16,6 +16,124 @@ struct WebPage {
|
||||
|
||||
// 修改为移动端User-Agent,解决链接中有视频的问题
|
||||
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;
|
||||
@ -23,6 +141,7 @@ struct WebPage {
|
||||
@State title: string = this.params.title;
|
||||
// customUserAgent: string = 'gdxz-expert';
|
||||
|
||||
|
||||
onBackPress(): boolean | void {
|
||||
if (this.controller.accessStep(-1)) {
|
||||
this.controller.backward();
|
||||
@ -51,8 +170,31 @@ 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();
|
||||
})
|
||||
.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) => {
|
||||
@ -86,10 +228,9 @@ struct WebPage {
|
||||
|
||||
async saveWebViewImage() {
|
||||
try {
|
||||
// 1. 获取整个网页的长截图
|
||||
this.controller.webPageSnapshot({
|
||||
id: "webView",
|
||||
size: { width: this.contentWidth, height:this.contentHeight } // 确保捕获全尺寸
|
||||
size: { width: this.contentWidth, height:this.controller.getPageHeight() }
|
||||
}, async (error, result) => {
|
||||
if (error) {
|
||||
promptAction.showToast({ message: `截图失败: ${error.message}` });
|
||||
@ -102,7 +243,6 @@ struct WebPage {
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 转为JPEG并写入沙箱
|
||||
const ctx = getContext(this);
|
||||
const imagePacker = image.createImagePacker();
|
||||
const arrayBuffer = await imagePacker.packToData(
|
||||
@ -115,7 +255,6 @@ struct WebPage {
|
||||
fileIo.writeSync(file.fd, arrayBuffer);
|
||||
fileIo.closeSync(file.fd);
|
||||
|
||||
// 3. 保存到相册
|
||||
const helper = photoAccessHelper.getPhotoAccessHelper(ctx);
|
||||
const sandboxUri = fileUri.getUriFromPath(sandboxPath);
|
||||
const request = photoAccessHelper.MediaAssetChangeRequest.createImageAssetRequest(ctx, sandboxUri);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user