xugenyuan

ref |> 必修--文章详情页H5外链鸿蒙版跳转页面后展示效果与安卓/IOS不一致

http://192.168.1.3:8080/zentao/bug-view-19286.html
... ... @@ -66,4 +66,10 @@ export class SpConstants{
static GETUI_PUSH_DEVICE_TOKEN = "deviceToken"
//link
static HAS_LINK="has_link"
// 是否开启H5白名单
static H5_WHITE_LIST_ENABLE ="h5_whilte_list_enable"
static H5_WHITE_LIST_LAST_DATA_MD5 ="h5_whilte_list_last_data_md5"
}
\ No newline at end of file
... ...
import fs from '@ohos.file.fs';
import { BusinessError } from '@kit.BasicServicesKit';
import { util } from '@kit.ArkTS';
export class FileUtils {
... ... @@ -31,4 +32,26 @@ export class FileUtils {
})
})
}
static readFile(path: string): ArrayBuffer {
try {
let stat = fs.statSync(path)
let fd = fs.openSync(path, fs.OpenMode.READ_ONLY).fd;
let length = stat.size
let buf = new ArrayBuffer(length);
fs.readSync(fd, buf)
fs.closeSync(fd)
return buf
} catch (e) {
console.log("FileUtils - readFilePicSync " + e)
return new ArrayBuffer(0)
}
}
static readFileAsString(path: string): string {
const data = FileUtils.readFile(path)
let decoder = util.TextDecoder.create('utf-8');
let str = decoder.decodeToString(new Uint8Array(data));
return str
}
}
\ No newline at end of file
... ...
import { SpConstants } from 'wdConstant/Index';
import { DeviceUtil, SPHelper, StringUtils } from 'wdKit/Index';
import { DeviceUtil, Logger, SPHelper, StringUtils } from 'wdKit/Index';
import { HttpRequest } from '../http/HttpRequest';
import { http } from '@kit.NetworkKit';
import fs from '@ohos.file.fs';
import { BusinessError } from '@kit.BasicServicesKit';
import { JSON } from '@kit.ArkTS';
const TAG: string = '[HttpUtils]'
... ... @@ -81,4 +85,37 @@ export class HttpUtils {
}
return true
}
static downloadUrlToPathWithout(url: string, toFilePath: string): Promise<boolean> {
Logger.debug(TAG, "will donwload url:" + url + " ======> " + toFilePath);
return new Promise((reslove, fail) => {
let httpRequest = http.createHttp()
httpRequest.request(url, (err, data) => {
if (!err && data.responseCode == http.ResponseCode.OK) {
let imgFile = fs.openSync(toFilePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
let wroteLength = 0
fs.write(imgFile.fd, data.result as ArrayBuffer).then((writeLen: number) => {
Logger.debug(TAG, "write data to file succeed and size is:" + writeLen);
wroteLength = writeLen
}).catch((err: BusinessError) => {
Logger.error(TAG, "write data to file failed with error message: " + err.message + ", error code: " + err.code);
}).finally(() => {
fs.closeSync(imgFile);
httpRequest.destroy()
reslove(wroteLength > 0)
});
return
}
Logger.error(TAG, "download failed " + JSON.stringify(err));
httpRequest.destroy()
fail("failed")
});
})
}
}
\ No newline at end of file
... ...
... ... @@ -114,6 +114,9 @@ struct LaunchPage {
if (!dataModelStr) {
//直接跳转首页
WDRouterRule.jumpWithReplacePage(WDRouterPage.mainPage)
//同意隐私协议后每次启动app请求启动页相关数据,并更新数据
this.requestLaunchPageData();
return
}
... ...
... ... @@ -2,22 +2,26 @@
import LaunchDataModel from '../viewModel/LaunchDataModel'
import { HttpRequest } from 'wdNetwork/src/main/ets/http/HttpRequest';
import { HttpUrlUtils, ResponseDTO } from 'wdNetwork/Index';
import { DisplayUtils, Logger, SPHelper } from 'wdKit/Index';
import { HttpUrlUtils, HttpUtils, ResponseDTO } from 'wdNetwork/Index';
import { AppUtils, DisplayUtils, Logger, SPHelper } from 'wdKit/Index';
import { SpConstants } from 'wdConstant/Index';
import { FileUtils } from '@ohos/imageknife';
export class LaunchPageModel {
getLaunchPageData(): Promise<LaunchDataModel> {
return new Promise<LaunchDataModel>((success, fail) => {
HttpRequest.get<ResponseDTO<LaunchDataModel>>(HttpUrlUtils.getLaunchPageDataUrl()+ `?height=${DisplayUtils.getDeviceHeight()}&width=${DisplayUtils.getDeviceWidth()}`).then((data: ResponseDTO<LaunchDataModel>) => {
HttpRequest.get<ResponseDTO<LaunchDataModel>>(HttpUrlUtils.getLaunchPageDataUrl()+ `?height=${DisplayUtils.getDeviceHeight()}&width=${DisplayUtils.getDeviceWidth()}`)
.then(async (data: ResponseDTO<LaunchDataModel>) => {
if (!data || !data.data) {
fail("数据为空")
LaunchPageModel.dealWithLaunchDataModel()
return
}
if (data.code != 0) {
fail(data.message)
LaunchPageModel.dealWithLaunchDataModel()
return
}
// Logger.debug("LaunchPageModel获取启动相关数据获取成功:success ", JSON.stringify(data))
... ... @@ -27,6 +31,8 @@ export class LaunchPageModel {
// console.log(obj)
SPHelper.default.saveSync(SpConstants.APP_LAUNCH_PAGE_DATA_MODEL,obj)
LaunchPageModel.dealWithLaunchDataModel(data.data)
}, (error: Error) => {
// Logger.debug("LaunchPageModel获取启动相关数据获取失败:error ", error.toString())
fail(error.message)
... ... @@ -34,6 +40,47 @@ export class LaunchPageModel {
})
}
static async dealWithLaunchDataModel(model?: LaunchDataModel) {
if (!model) {
let dataModelStr = SPHelper.default.getSync(SpConstants.APP_LAUNCH_PAGE_DATA_MODEL, '') as string
model = JSON.parse(dataModelStr ?? "")
if (!model) { return }
}
LaunchPageModel.dealWithWhiteListWith(model)
// TODO: others
}
static async dealWithWhiteListWith(model: LaunchDataModel) {
SPHelper.default.saveSync(SpConstants.H5_WHITE_LIST_ENABLE, model.operationUrl.enabled)
const lastMD5 = SPHelper.default.getSync(SpConstants.H5_WHITE_LIST_LAST_DATA_MD5, "") as string
if (lastMD5 && lastMD5 == model.operationUrl.md5) {
// MD5未变更,保持已下载的白名单
if (AppUtils.gotApplicationContextFunc) {
let filePath = AppUtils.gotApplicationContextFunc().getApplicationContext().filesDir + "/h5_whitelist.txt"
if (FileUtils.getInstance().exist(filePath)) {
return
}
}
}
if (!AppUtils.gotApplicationContextFunc) {
return
}
let filePath = AppUtils.gotApplicationContextFunc().getApplicationContext().filesDir + "/h5_whitelist.txt"
const result = await HttpUtils.downloadUrlToPathWithout(model.operationUrl.linkUrl, filePath)
if (result) {
SPHelper.default.saveSync(SpConstants.H5_WHITE_LIST_LAST_DATA_MD5, model.operationUrl.md5 ?? "")
}
}
getMournsInfoData(): Promise<LaunchDataModel> {
return new Promise<LaunchDataModel>((success, fail) => {
HttpRequest.get<ResponseDTO<LaunchDataModel>>(HttpUrlUtils.getLaunchPageDataUrl()).then((data: ResponseDTO<LaunchDataModel>) => {
... ...
... ... @@ -62,10 +62,8 @@ struct H5TipsPage {
// 获取系统剪贴板对象
let systemPasteboard = pasteboard.getSystemPasteboard();
// 创建一条纯文本类型的剪贴板内容对象createPlainTextData
let pasteData = pasteboard.createPlainTextData(this.webUrl);
pasteData.addTextRecord(this.webUrl);
// 将数据写入系统剪贴板
let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, this.webUrl)
// // 将数据写入系统剪贴板
systemPasteboard.setData(pasteData).then(()=>{
// 存入成功,处理正常场景
ToastUtils.shortToast('复制成功')
... ...
import { FileUtils as imageFileUtils } from '@ohos/imageknife'
import { Action } from 'wdBean'
import { AppUtils, FileUtils, SPHelper } from 'wdKit'
import { JumpInterceptorAction, RouterJumpInterceptor, WDRouterPage } from 'wdRouter'
import { JSON, uri } from '@kit.ArkTS'
import { SpConstants } from 'wdConstant'
import { router } from '@kit.ArkUI'
interface WhiteListModel {
whiteurls: string[]
}
class DefaultWebJumpHandler implements JumpInterceptorAction {
/// 说明是调用了跳转 WDRouterPage.loginPage 页面的行为
on(params?: object | undefined, singleMode?: boolean | undefined): boolean {
let action = params as Action
if (action.params?.url) {
const whiteListEnable = SPHelper.default.getSync(SpConstants.H5_WHITE_LIST_ENABLE, false) as boolean
if (!whiteListEnable) {
return false
}
const jumpHost = new uri.URI(action.params?.url).host
const list = this.configWhiteLists()
list.push(...this.localWhiteLists())
const inWhilteList = list.filter((host) => {
return host == jumpHost
}).length > 0
if (false == inWhilteList) {
router.pushUrl({url: WDRouterPage.h5TipsPage.url(), params: action.params})
return true
}
}
return false
}
configWhiteLists() : string[] {
let list : string[] = []
if (!AppUtils.gotApplicationContextFunc) {
return list
}
let filePath = AppUtils.gotApplicationContextFunc().getApplicationContext().filesDir + "/h5_whitelist.txt"
const jsonStr = FileUtils.readFileAsString(filePath)
const listObj = JSON.parse(jsonStr) as WhiteListModel
if (listObj && listObj.whiteurls && listObj.whiteurls.length) {
const urls = listObj.whiteurls.map((obj) => {
if (obj.startsWith("http")) {
return new uri.URI(obj).host
}
return new uri.URI("https://" + obj).host
})
list.push(...urls)
}
return list
}
localWhiteLists() : string[] {
const list = [
"https://cdnpeoplefrontdev.aikan.pdnews.cn",
"https://cdnpeoplefrontsit.aikan.pdnews.cn",
"https://cdnpeoplefrontuat.aikan.pdnews.cn",
"https://cdnpeoplefront.aikan.pdnews.cn",
"https://pd-people-uat.pdnews.cn",
"https://pd-people-sit.pdnews.cn",
"https://pd-people-dev.pdnews.cn",
"https://www.peopleapp.com",
"https://h5.peopleapp.com"
]
return list.map((url) => {
return new uri.URI(url).host
})
}
}
export function registerDefaultWebJumpInterceptor() {
RouterJumpInterceptor.register(WDRouterPage.defaultWebPage, new DefaultWebJumpHandler())
RouterJumpInterceptor.register(WDRouterPage.h5NewsWebPage, new DefaultWebJumpHandler())
}
... ...
... ... @@ -27,6 +27,7 @@ import { initGlobalPlayerSettings } from 'wdPlayer/src/main/ets/utils/GlobalSett
import { BackgroundAudioController } from 'wdPlayer/Index'
import { SpConstants } from 'wdConstant'
import { WDShareBase } from 'wdShareBase/Index';
import { registerDefaultWebJumpInterceptor } from '../pages/web/WebPageJumpInterceptor'
const TAG = "[StartupManager]"
... ... @@ -59,6 +60,7 @@ export class StartupManager {
KVStoreHelper.init(context)
// 路由注册
registerRouter();
registerDefaultWebJumpInterceptor()
// 设置全局context
AppUtils.gotApplicationContextFunc = () => {
... ...