Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

书源管理,新增书源页面继续完善,将搜索,发现,详情,目录,正文的你字段录入 #171

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions entry/src/main/ets/common/utils/FileHandler.ets
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BusinessError } from '@ohos.base';
import fs, { Options } from '@ohos.file.fs';
import fs, { Options, WriteOptions } from '@ohos.file.fs';
import { common, Context } from '@kit.AbilityKit';
import { chaptersItem } from 'ets/componets/dataList/ReaderChaptersItem';
import { BookList } from '../../componets/dataList/bookList';
Expand All @@ -19,7 +19,9 @@ export class FileHandler {
static CopyFile(srcUrl: string) {
let file = fs.openSync(srcUrl, fs.OpenMode.READ_WRITE);
let dstPath = filePath + '/' + file.name; // file.name:《幽影之冰》.txt
addShelfBook(0,new BookList('',file.name,'会说话的肘子·未读过1','更新至·第340章 镇压',$r('app.media.cover_list'),'测试作者','未分组',dstPath))
addShelfBook(0,
new BookList('', file.name, '会说话的肘子·未读过1', '更新至·第340章 镇压', $r('app.media.cover_list'), '测试作者',
'未分组', dstPath))
showMessage('添加到书架成功')
fs.copyFile(file.fd, dstPath, 0).then(() => {
showMessage('导入沙箱成功')
Expand All @@ -35,7 +37,8 @@ export class FileHandler {
const chapters: chaptersItem[] = [];
console.info('readFileUrl:' + readFileUrl)
//const regex = /===第(.*?)章 (.*?)===/g;
const regex = /^[=|<]{0,4}((?:序章|楔子|番外|第\s{0,4})([\d〇零一二两三四五六七八九十百千万壹贰叁肆伍陆柒捌玖拾佰仟]+?\s{0,4})(?:章|回(?!合)|话(?!说)|节(?!课)|卷|篇(?!张)))(.{0,30})/g;
const regex =
/^[=|<]{0,4}((?:序章|楔子|番外|第\s{0,4})([\d〇零一二两三四五六七八九十百千万壹贰叁肆伍陆柒捌玖拾佰仟]+?\s{0,4})(?:章|回(?!合)|话(?!说)|节(?!课)|卷|篇(?!张)))(.{0,30})/g;
await fs.readLines(readFileUrl, options).then((readerIterator: fs.ReaderIterator) => {
for (let it = readerIterator.next();!it.done; it = readerIterator.next()) {
const match = regex.exec(it.value);
Expand All @@ -56,7 +59,7 @@ export class FileHandler {
return chapters
}

static async pickerFile (context: Context) {
static async pickerFile(context: Context) {
try {
const documentSelectOptions = new picker.DocumentSelectOptions();
// 选择文档的最大数目(可选)
Expand All @@ -65,7 +68,7 @@ export class FileHandler {
documentSelectOptions.fileSuffixFilters = ['.json'];
let documentViewPicker = new picker.DocumentViewPicker(context);
const documentSelectResult: Array<string> = await documentViewPicker.select(documentSelectOptions)
const pathUrl = documentSelectResult[0];
const pathUrl = documentSelectResult[0];
console.info('文件目录:' + pathUrl);
let file = fs.openSync(pathUrl, fs.OpenMode.READ_WRITE);
let buf = new ArrayBuffer(40960000);
Expand All @@ -83,4 +86,52 @@ export class FileHandler {
return Promise.reject(err)
}
}

static async readJsonFile(pathUrl: string) {
try {
console.info('文件目录:' + pathUrl);
let file = fs.openSync(pathUrl, fs.OpenMode.READ_WRITE);
let buf = new ArrayBuffer(40960000);
fs.readSync(file.fd, buf);
fs.closeSync(file);
let uint8View = new Uint8Array(buf);
// 设置编码格式为“utf-8”
let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM: true });
// 解码输后获取对应文本
let readString = textDecoder.decodeWithStream(uint8View, { stream: false });
console.log('读取的数据内容:' + readString);
return JSON.parse(readString)
} catch (err) {
console.error(`读取失败:${err}`);
return Promise.reject(err)
}
}

// TODO 目前写入存在问题,等官方回复
static async writeJsonFile(pathUrl: string, context: ArrayBuffer | string) {
console.info('文件目录:' + pathUrl);
try {
let file = fs.openSync(filePath, fs.OpenMode.TRUNC);
try {
let writeLen = fs.writeSync(file.fd, context);
fs.closeSync(file);
console.info('文件写入成功,文件长度:' + writeLen);
} catch (e) {
return Promise.reject('写入失败')
}
return Promise.resolve('写入成功')
} catch (e) {
console.info("文件不存在,开始创建文件");
try {
let file = fs.openSync(filePath, fs.OpenMode.CREATE);
let writeLen = fs.writeSync(file.fd, context);
fs.closeSync(file);
console.info('文件写入成功,文件长度:' + writeLen);
return Promise.resolve('写入成功')
} catch (e) {
console.error('写入失败'+JSON.stringify(e))
return Promise.reject('写入失败'+JSON.stringify(e))
}
}
}
}
8 changes: 8 additions & 0 deletions entry/src/main/ets/componets/Form/FormItem.ets
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export enum FormItemType {

@Component
export default struct FormItem {
@Builder rightBuilder() {}
// 自定义右侧区域
@BuilderParam customRightBuilder: () => void = this.rightBuilder
@Prop value: string
type: FormItemType = FormItemType.Input
label: string = ''
Expand All @@ -27,7 +30,12 @@ export default struct FormItem {
}
}
.alignItems(VerticalAlign.Center)

if (this.customRightBuilder !== this.rightBuilder) {
this.customRightBuilder()
}
}
.width('100%')
.justifyContent(FlexAlign.SpaceBetween)

Stack() {
Expand Down
132 changes: 94 additions & 38 deletions entry/src/main/ets/componets/dataList/BookSource.ets
Original file line number Diff line number Diff line change
@@ -1,60 +1,116 @@
export interface RuleBookInfo {
init: string;
export interface BookInfoRule {
init?: string;
name?: string;
author?: string;
intro?: string;
kind?: string;
lastChapter?: string;
updateTime?: string;
coverUrl?: string;
tocUrl?: string;
wordCount?: string;
canReName?: string;
downloadUrls?: string
}

export interface RuleContent {
content: string;
export interface ContentRule {
content?: string;
title?: string; //有些网站只能在正文中获取标题
nextContentUrl?: string;
webJs?: string;
sourceRegex?: string;
replaceRegex?: string; //替换规则
imageStyle?: string; //默认大小居中,FULL最大宽度
imageDecode?: string; //图片bytes二次解密js, 返回解密后的bytes
payAction?: string; //购买操作,js或者包含{{js}}的url
}

export interface RuleExplore {
bookList: string;
export interface ExploreRule {
bookList?: string;
name?: string;
author?: string;
intro?: string;
kind?: string;
lastChapter?: string;
updateTime?: string;
bookUrl?: string;
coverUrl?: string;
wordCount?: string
}

export interface GroupList {
title: string
list: BookSource[]
export interface SearchRule {
checkKeyWord?: string;
bookList?: string;
name?: string;
author?: string;
kind?: string;
wordCount?: string;
lastChapter?: string;
intro?: string;
coverUrl?: string;
bookUrl?: string;
}

export interface RuleSearch {
author: string;
bookList: string;
bookUrl: string;
coverUrl: string;
intro: string;
kind: string;
lastChapter: string;
name: string;
wordCount: string;
}

export interface RuleToc {
chapterList: string;
chapterName: string;
chapterUrl: string;
isVip: string;
updateTime: string;
export interface TocRule {
preUpdateJs?: string;
chapterList?: string;
chapterName?: string;
chapterUrl?: string;
formatJs?: string;
isVolume?: string;
isVip?: string;
isPay?: string;
updateTime?: string;
nextTocUrl?: string
}

export interface BookSource {
bookSourceComment: string;
bookSourceGroup: string;
bookSourceName: string;
bookSourceType: number;
bookSourceUrl: string;
bookSourceType: number;
bookSourceGroup: string;
grade: number,
score: string,
bookSourceComment: string;
loginUrl: string;
loginUi: string;
loginCheckJs: string;
bookUrlPattern: string;
customOrder: number;
coverDecodeJs: string;
header: string;
variableComment: string;
concurrentRate: string;
jsLib: string;
enabled: boolean;
enabledCookieJar: boolean;
enabledExplore: boolean;
exploreUrl: string;
lastUpdateTime: number;
loginUrl: string;
respondTime: number;
ruleBookInfo: RuleBookInfo;
ruleContent: RuleContent;
ruleExplore: RuleExplore;
ruleSearch: RuleSearch;
ruleToc: RuleToc;
bookInfoRule?: BookInfoRule;
contentRule?: ContentRule;
exploreRule?: ExploreRule;
searchRule?: SearchRule;
tocRule?: TocRule;
searchUrl: string;

customOrder: number;
exploreUrl: string;
weight: number;
}

export interface GroupList {
title: string
list: BookSource[]
}

export const GRADE_TYPE: Record<number, string> = {
0: '常规',
1: '优质',
2: '精品'
}

export const BOOK_SOURCE_TYPE: Record<number, string> = {
0 : '小说',
1 : '漫画',
2 : '有声'
}
3 changes: 3 additions & 0 deletions entry/src/main/ets/entryability/EntryAbility.ets
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export default class EntryAbility extends UIAbility {
}

onWindowStageCreate(windowStage: window.WindowStage) {
// 获取本地文件存储路径
const pathDir = this.context.filesDir;
AppStorage.setOrCreate('pathDir', pathDir);
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
// 全屏
Expand Down
Loading
Loading