Skip to content

Commit

Permalink
refactor(find): 重构发现页面组件并优化数据结构 (#279)
Browse files Browse the repository at this point in the history
- 将 bookSourceUrl 和 exploreRule 替换为单个 BookSource 对象
- 优化了数据传递和组件间的交互
- 统一了请求参数的处理逻辑
- 移除了不必要的状态和属性
- 当exploreRule规则没有时,使用searchRule
  • Loading branch information
yi-boide authored Nov 15, 2024
1 parent c5846ac commit 6be419e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 46 deletions.
49 changes: 32 additions & 17 deletions entry/src/main/ets/pages/view/Find/CategoryList/Index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,35 @@ import axios, { AxiosResponse } from '@ohos/axios';
import noFind from '../../../../componets/common/noFind';
import { CardItem } from '../components/CardItem';
import CommonConstants from '../../../../common/constants/CommonConstants';
import { BookSource } from '../../../../database/entities/BookSource';

interface RouteParams {
bookSourceUrl: string,
bookSource: BookSource,
exploreItem: ExploreItem,
exploreRule: ExploreRule,
currentIndex: number
}

@Entry
@Component
struct CateGoryListPage {
@State bookSourceUrl: string = ''
@State exploreItem: ExploreItem = {
title: '',
url: ''
}
@State exploreRule: ExploreRule = {}
@State bookSource?: BookSource = {
bookSourceUrl: '',
bookSourceName: '',
bookSourceType: 0,
customOrder: 0,
enabled: false,
enabledExplore: false,
lastUpdateTime: 0,
respondTime: 0,
weight: 0,
isTop: false,
showRecentIcon: false,
showExplore: true
}
@State pageNum: number = 1
@State pageSize: number = 10
@State bookList: ExploreRule[] = []
Expand All @@ -38,30 +50,33 @@ struct CateGoryListPage {
const params = router.getParams() as RouteParams;
this.currentIndex = params.currentIndex
console.log('TagInfo', JSON.stringify(params))
this.bookSourceUrl = params.bookSourceUrl
this.bookSource = params.bookSource
this.exploreItem = params.exploreItem
this.exploreRule = params.exploreRule
await this.getList()
this.startLoading = false
}

async getList() {
if (!this.bookSource) return
const bookSourceUrl = this.bookSource.bookSourceUrl
const exploreRule = this.bookSource.ruleExplore
const searchRule = this.bookSource.ruleSearch
let url = this.exploreItem.url.replace('{{page}}', this.pageNum.toString())
if (!isNetworkUrl(url)) {
url = this.bookSourceUrl + url
url = bookSourceUrl + url
}
const exploreQuery: ExploreQuery = {
url,
bookList: this.exploreRule.bookList,
pageSize: this.pageSize,
name: this.exploreRule.name,
author: this.exploreRule.author,
intro: this.exploreRule.intro,
kind: this.exploreRule.kind,
lastChapter: this.exploreRule.lastChapter,
bookUrl: this.exploreRule.bookUrl,
coverUrl: this.exploreRule.coverUrl,
wordCount: this.exploreRule.wordCount
bookList: exploreRule?.bookList || searchRule?.bookList,
name: exploreRule?.name || searchRule?.name,
author: exploreRule?.author || searchRule?.author,
intro: exploreRule?.intro || searchRule?.intro,
kind: exploreRule?.kind || searchRule?.kind,
lastChapter: exploreRule?.lastChapter || searchRule?.lastChapter,
bookUrl: exploreRule?.bookUrl || searchRule?.bookUrl,
coverUrl: exploreRule?.coverUrl || searchRule?.coverUrl,
wordCount: exploreRule?.wordCount || searchRule?.wordCount
}
if (this.pageNum * this.pageSize - this.bookList.length >= this.pageSize * 2 && !this.startLoading) {
return
Expand All @@ -72,7 +87,7 @@ struct CateGoryListPage {
console.info('TagInfo, 解析内容:', JSON.stringify(res.data))
const bookList = (res.data as ExploreRule[]).map(item => {
if (item.coverUrl && !isNetworkUrl(item.coverUrl)) {
item.coverUrl = this.bookSourceUrl + item.coverUrl
item.coverUrl = bookSourceUrl + item.coverUrl
}
return item
})
Expand Down
12 changes: 5 additions & 7 deletions entry/src/main/ets/pages/view/Find/components/AllCategory.ets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ExploreItem } from '../../../../database/types/BookSourceType'
import { router } from '@kit.ArkUI'
import { ExploreRule } from '../../../../database/entities/rule'
import { BookSource } from '../../../../database/entities/BookSource'

export interface GroupExploreList {
title: string,
Expand All @@ -14,8 +14,7 @@ export default struct AllCategory {
@StorageLink('bottomRectHeight') bottomRectHeight: number = 0
@Prop list: GroupExploreList[] = []
@Link showSheet: boolean
@Prop bookSourceUrl: string = ''
@Prop exploreRule: ExploreRule = {}
@Prop bookSource?: BookSource

build() {
Column() {
Expand Down Expand Up @@ -106,15 +105,14 @@ export default struct AllCategory {
.backgroundColor('#0F000000')
.borderRadius(4)
.onClick(() => {
if (!item.url) {
if (!item.url || !this.bookSource) {
return
}
router.pushUrl({
url: 'pages/view/Find/CategoryList/Index',
params: {
bookSourceUrl: this.bookSourceUrl,
exploreItem: item,
exploreRule: this.exploreRule
bookSource: this.bookSource,
exploreItem: item
}
})
})
Expand Down
35 changes: 20 additions & 15 deletions entry/src/main/ets/pages/view/Find/components/BookFindContent.ets
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,54 @@ import mayAlsoLike from '../mayAlsoLike'
import { CardItem } from './CardItem'
import { router } from '@kit.ArkUI'
import CommonConstants from '../../../../common/constants/CommonConstants';
import { BookSource } from '../../../../database/entities/BookSource'

@Component
export default struct BookFindContent {
@Prop currentIndex: number
@Prop bookSourceUrl: string
@Prop exploreItem: ExploreItem
@Prop @Watch('getList') counter: number = 0
@State loading: boolean = true
@State errorExplore: boolean = false
@State bookList: ExploreRule[] = []
@Prop exploreRule: ExploreRule = {}
@Prop bookSource?: BookSource
handleClick: () => void = () => {
}

aboutToAppear() {
this.getList()
if (!this.bookSource) return
}

getList() {
if (!this.bookSource) return
const bookSourceUrl = this.bookSource.bookSourceUrl
const exploreRule = this.bookSource.ruleExplore
const searchRule = this.bookSource.ruleSearch
this.loading = true
let url = this.exploreItem.url.replace('{{page}}', "1")
if (!isNetworkUrl(url)) {
url = this.bookSourceUrl + url
url = bookSourceUrl + url
}
const exploreQuery: ExploreQuery = {
url,
bookList: this.exploreRule.bookList,
pageSize: 3,
name: this.exploreRule.name,
author: this.exploreRule.author,
intro: this.exploreRule.intro,
kind: this.exploreRule.kind,
lastChapter: this.exploreRule.lastChapter,
bookUrl: this.exploreRule.bookUrl,
coverUrl: this.exploreRule.coverUrl,
wordCount: this.exploreRule.wordCount
bookList: exploreRule?.bookList || searchRule?.bookList,
name: exploreRule?.name || searchRule?.name,
author: exploreRule?.author || searchRule?.author,
intro: exploreRule?.intro || searchRule?.intro,
kind: exploreRule?.kind || searchRule?.kind,
lastChapter: exploreRule?.lastChapter || searchRule?.lastChapter,
bookUrl: exploreRule?.bookUrl || searchRule?.bookUrl,
coverUrl: exploreRule?.coverUrl || searchRule?.coverUrl,
wordCount: exploreRule?.wordCount || searchRule?.wordCount
}
console.info('解析参数:', JSON.stringify(exploreQuery))
axios.post(CommonConstants.BASE_URL + '/common/analysisRules', exploreQuery).then((res: AxiosResponse) => {
console.info('TagInfo, 解析内容:', JSON.stringify(res.data))
this.bookList = (res.data as ExploreRule[]).map(item => {
if (item.coverUrl && !isNetworkUrl(item.coverUrl)) {
item.coverUrl = this.bookSourceUrl + item.coverUrl
item.coverUrl = bookSourceUrl + item.coverUrl
}
return item
})
Expand Down Expand Up @@ -115,9 +121,8 @@ export default struct BookFindContent {
router.pushUrl({
url: 'pages/view/Find/CategoryList/Index',
params: {
bookSourceUrl: this.bookSourceUrl,
bookSource: this.bookSource,
exploreItem: this.exploreItem,
exploreRule: this.exploreRule,
currentIndex: this.currentIndex
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import RefreshComponent from '../../../../componets/common/RefreshComponent'
import { BookSource } from '../../../../database/entities/BookSource'
import BookFindContent from './BookFindContent'
import { JSON } from '@kit.ArkTS'
import { ExploreRule } from '../../../../database/entities/rule'
import { ExploreItem } from '../../../../database/types/BookSourceType'
import AllCategory, { GroupExploreList } from './AllCategory'

Expand Down Expand Up @@ -36,7 +35,6 @@ export default struct ContentRefresh {
@State groupExploreList: GroupExploreList[] = []
@State exploreList: ExploreItem[] = []
@State showExploreList: ExploreItem[] = []
@State exploreRule?: ExploreRule = {}
@State showSheet: boolean = false

aboutToAppear() {
Expand All @@ -49,7 +47,6 @@ export default struct ContentRefresh {

getList () {
try {
this.exploreRule = this.bookSource.ruleExplore
console.info('TagInfo: 书源地址:', this.bookSource.bookSourceUrl)
console.info('TagInfo: 发现地址:', JSON.stringify(JSON.parse(this.bookSource.exploreUrl ?? '[]')))
const exploreList = JSON.parse(this.bookSource.exploreUrl ?? '[]') as ExploreItem[]
Expand Down Expand Up @@ -122,9 +119,8 @@ export default struct ContentRefresh {
ForEach(this.showExploreList, (item: ExploreItem) => {
BookFindContent({
currentIndex: this.currentIndex,
bookSourceUrl: this.bookSource.bookSourceUrl,
exploreItem: item,
exploreRule: this.exploreRule,
bookSource: this.bookSource,
counter: this.counter,
handleClick: () => {
this.showSheet = true
Expand Down Expand Up @@ -180,8 +176,7 @@ export default struct ContentRefresh {
Column() {
AllCategory({
showSheet: this.showSheet,
bookSourceUrl: this.bookSource.bookSourceUrl,
exploreRule: this.exploreRule,
bookSource: this.bookSource,
list: this.groupExploreList
})
}
Expand Down

0 comments on commit 6be419e

Please sign in to comment.