Skip to content

Commit

Permalink
v3.0
Browse files Browse the repository at this point in the history
home页面
  • Loading branch information
PGzxc committed Nov 12, 2023
1 parent d8029f2 commit 8476f8a
Show file tree
Hide file tree
Showing 10 changed files with 1,025 additions and 25 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@
* viewmodel中Promise+async解析结果数据为Bean
* Page页面aboutToAppear(页面即将显示)方法中调用viewmodel中的接口

### 3.3 v3.0

* 使用UI容器(Scroll、Row、Column、List)和常用组件Text、Image搭建UI界面
* 自定义组件用于List-ListItem使用
* Swiper实现轮播图
* @State实现数据变化监听
* 样式文件实现复杂布局
* Resource:color-颜色命名文字不能大写,float-fp后缀对应文字,vp后缀对应宽度长度
1 change: 1 addition & 0 deletions entry/src/main/ets/bean/ArticleBean.ets
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class ArticleList {


export class Article {
isTop:boolean = false; //是否为置顶
adminAdd: boolean = false; //是否为管理员添加
apkLink: string = ''; //apk链接
audit: number = 0;
Expand Down
50 changes: 37 additions & 13 deletions entry/src/main/ets/pages/HomePage.ets
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// 导入页面路由模块
import router from '@ohos.router';
import HttpConfig from '../api/HttpConfig';
import { Article, ArticleList, ArticleTop } from '../bean/ArticleBean';
import { Banner } from '../bean/BannerBean';
import Constants from '../common/constants/Constants';
import Logger from '../utils/Logger';
import ArticleItem from '../view/ArticleItem';
import HomeViewModel from '../viewmodel/HomeViewModel';

/**
Expand All @@ -12,23 +15,33 @@ import HomeViewModel from '../viewmodel/HomeViewModel';
@Entry
@Component
export default struct HomePage {
private swiperController: SwiperController = new SwiperController();
@State bannerData: Banner[] = [];
@State articleTopData: Article[] = [];
@State articleListData: ArticleList = new ArticleList()
@State articleData: Article[] = [];


//页面即将显示时
aboutToAppear() {

//1-首页全部接口数据
HomeViewModel.getHomeData().then(result => {

//1-result[0]-Banner
let bannerData = result[0]
//2-result[1]-置顶文章
let articleTopData = result[1]
//3-result[3] - 文章列表
let articleListData = result[2]
this.bannerData = result[0] as Banner[]
this.articleTopData = (result[1] as Article[])
this.articleTopData.forEach(item => {
item.isTop = true
})
this.articleListData = result[2]

this.articleData = [...this.articleTopData, ...this.articleListData.datas]
console.log("result")

})
//2-首页-文章接口数据
HomeViewModel.getArticle(0).then(result => {
var aa = this.bannerData;
result.datas.forEach((article, index) => {
console.log('article>>>', article)
})
Expand All @@ -37,14 +50,25 @@ export default struct HomePage {
}

build() {
Row() {
Scroll() {
Column() {
Text("首页")
.fontSize(50)
.fontWeight(FontWeight.Bold)
if (this.bannerData.length > 0) {
Swiper(this.swiperController) {
ForEach(this.bannerData, (banner: Banner) => {
Image(banner.imagePath)
}, (img: Resource) => JSON.stringify(img.id))
}.height(200).width('100%')
.autoPlay(true)
}
//Article
List() {
ForEach(this.articleData, (article: Article) => {
ListItem() {
ArticleItem({ article: article })
}
})
}
}
.width('100%')
}
.height('100%')
}.height(Constants.FULL_PERCENT)
}
}
4 changes: 2 additions & 2 deletions entry/src/main/ets/pages/Splash.ets
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ struct Splash {
.width(Constants.FULL_PERCENT)
.height(Constants.FULL_PERCENT)
Image($r('app.media.ic_logo'))
.width($r('app.float.192'))
.height($r('app.float.192'))
.width($r('app.float.192vp'))
.height($r('app.float.192vp'))
.offset({
y: `-${Constants.PERCENTAGE_15}`
})
Expand Down
25 changes: 25 additions & 0 deletions entry/src/main/ets/utils/Utils.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

/**
* @desc:工具类
*/

//1-判断是否为空字符串
const isEmpty = (data) => {
return data == undefined || data == "" || data.length <= 0

};
//2-格式化字符串
const formatHtml = (html) => {
let data = ''
if (!isEmpty(html)) {
let regSymbol = RegExp("&.*;")
let regHtml = RegExp(/<[^>]*>/g);
data = html.replace(regSymbol, '').replace(regHtml, '')
}
return data
}

export {
isEmpty,
formatHtml
};
169 changes: 169 additions & 0 deletions entry/src/main/ets/view/ArticleItem.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/**
* @desc:文章Item
*/
import * as Utils from "../utils/Utils";
import { Article } from '../bean/ArticleBean'
import Constants from '../common/constants/Constants';
import { when } from '@ohos/hypium';

@Component
export default struct ArticleItem {
article: Article = new Article()
@State isTop: boolean = false;
@State hasTag: boolean = false;
@State tag: string = '';
@State author: string | number = '';
@State title: string = '';
@State desc: string = '';
@State hasPic: boolean = false;
@State hasSuperChapterName: boolean = false;
@State superChapterName: string = '';
@State hasChapterName: boolean = false;
@State chapterName: string = '';

aboutToAppear() {
let item = this.article

this.isTop = item.isTop;
this.hasTag = !Utils.isEmpty(item.tags);
this.tag = !this.hasTag ? '暂无' : item.tags[0].name;
this.author = Utils.isEmpty(item.author) ? Utils.isEmpty(item.shareUser) ? '' : item.shareUser : item.author;


//this.author = Utils.isEmpty(item.author) ? Utils.isEmpty(item.shareUser) ? '' : item.shareUser : item.author;
this.title = Utils.formatHtml(item.title);
this.desc = Utils.formatHtml(item.desc)
this.hasPic = !Utils.isEmpty(item.envelopePic)
this.hasSuperChapterName = !Utils.isEmpty(item.superChapterName)
this.superChapterName = Utils.isEmpty(item.superChapterName) ? '' : Utils.formatHtml(item.superChapterName)
this.hasChapterName = !Utils.isEmpty(item.chapterName)
this.chapterName = Utils.isEmpty(item.chapterName) ? '' : Utils.formatHtml(item.chapterName)

}

build() {
Column() {
//第一行
Row() {

//1-tag
if (this.hasTag) {
Text(this.tag)
.height($r('app.float.23vp'))
.fontSize($r('app.float.12fp'))
.textAlign(TextAlign.Center)
.borderRadius($r('app.float.3vp'))
.fontColor($r('app.color.white'))
.backgroundColor('red')
.padding({
left: $r('app.float.10vp'),
right: $r('app.float.10vp'),
top: $r('app.float.5vp'),
bottom: $r('app.float.5vp')
})
}
Row() {
Image($r('app.media.ic_me_normal')).width($r('app.float.18vp')).height($r('app.float.18vp'))
Text(this.author.toString()).margin({ left: $r('app.float.5vp') }) //2-author
}.margin({ left: $r('app.float.5vp'), right: $r('app.float.5vp') })

Row() {
Image($r('app.media.ic_clock')).width($r('app.float.18vp')).height($r('app.float.18vp'))
Text(this.article.niceDate) //3-time
}
}.width(Constants.FULL_PERCENT).justifyContent(FlexAlign.SpaceBetween)
//第2行
Flex({ direction: FlexDirection.Row }) {
Column() {
Text(this.title)
//.width(Constants.FULL_PERCENT)
.maxLines(2)
.textOverflow({ overflow: TextOverflow.Ellipsis })
.fontSize($r('app.float.18fp'))
.fontColor(Color.Black)
.align(Alignment.TopStart)
Text(this.desc)
//.width(Constants.FULL_PERCENT)
.maxLines(2)
.textOverflow({ overflow: TextOverflow.Ellipsis })
.fontColor(Color.Gray)
.align(Alignment.TopStart)
}.flexGrow(1)
//.width('70%')
.alignItems(HorizontalAlign.Start)

if (this.hasPic) {
//Image('https://img1.baidu.com/it/u=2653588009,1450076812&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1699722000&t=b4f916a93f7e4735cf7c4c69416ab2fb')
Image(this.article.envelopePic)
.width($r('app.float.60vp'))
.height('100%')
}
}.height($r('app.float.80vp')).margin({ top: $r('app.float.5vp') })
//第3行
Row() {
//置顶
if (this.isTop) {
Text('置顶')
.height($r('app.float.23vp'))
.fontSize($r('app.float.12fp'))
.borderRadius($r('app.float.3vp'))
.textAlign(TextAlign.Center)
.fontColor($r('app.color.white'))
.backgroundColor($r('app.color.dark_orange'))
.padding({
left: $r('app.float.10vp'),
right: $r('app.float.10vp'),
top: $r('app.float.5vp'),
bottom: $r('app.float.5vp')
})
.margin({ right: $r('app.float.10vp') })
}

if (this.hasSuperChapterName) {
Text(this.superChapterName)
.height($r('app.float.23vp'))
.fontSize($r('app.float.12fp'))
.borderRadius($r('app.float.3vp'))
.textAlign(TextAlign.Center)
.fontColor($r('app.color.white'))
.backgroundColor($r('app.color.olive'))
.padding({
left: $r('app.float.10vp'),
right: $r('app.float.10vp'),
top: $r('app.float.5vp'),
bottom: $r('app.float.5vp')
})
.margin({ right: $r('app.float.10vp') })
}
if (this.hasChapterName) {
Text(this.chapterName)
.height($r('app.float.23vp'))
.fontSize($r('app.float.12fp'))
.borderRadius($r('app.float.3vp'))
.textAlign(TextAlign.Center)
.fontColor($r('app.color.white'))
.backgroundColor($r('app.color.teal'))
.padding({
left: $r('app.float.10vp'),
right: $r('app.float.10vp'),
top: $r('app.float.5vp'),
bottom: $r('app.float.5vp')
})
.margin({ right: $r('app.float.10vp') })
}
}.width(Constants.FULL_PERCENT).justifyContent(FlexAlign.Start).margin({ top: $r('app.float.5vp') })

}
//.height(160)
.width(Constants.FULL_PERCENT)
.padding({
top: $r('app.float.5vp'),
bottom: $r('app.float.5vp'),
left: $r('app.float.10vp'),
right: $r('app.float.10vp')
})
.borderWidth(0.5)
.borderColor(Color.Gray)
.backgroundColor('#FFF1F3F5')
}
}
19 changes: 16 additions & 3 deletions entry/src/main/ets/viewmodel/HomeViewModel.ets
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,32 @@ import List from '@ohos.util.List';
import HttpConfig from '../api/HttpConfig';
import { articleListReq, articleTopReq, bannerReq } from '../api/HttpRequest';
import ResponseResult from '../api/ResponseResult';
import { ArticleList, ArticleTop } from '../bean/ArticleBean';
import { Article, ArticleList, ArticleTop } from '../bean/ArticleBean';
import { Banner } from '../bean/BannerBean';
import Logger from '../utils/Logger';
/**
* @desc:HomePage对应数据处理类
*/
class HomeViewModel {
// bannerData: List<Banner>
// articleTopData: ArticleTop
// articleListData: ArticleList


//首页-banner轮播图、置顶文章、文章三个接口
getHomeData() {
return Promise.all([this.getBanner(), this.getArticleTop(), this.getArticle(0)])
// .then(result => {
// //1-result[0]-Banner
// this.bannerData = result[0]
// //2-result[1]-置顶文章
// this.articleTopData = result[1]
// //3-result[3] - 文章列表
// this.articleListData = result[2]
// })
}
//1-首页-Banner
getBanner(): Promise<List<Banner>> {
getBanner(): Promise<Banner[]> {
return new Promise(async (resolve: Function, reject: Function) => {
bannerReq().then((data: ResponseResult) => {
if (data.errorCode === HttpConfig.SERVER_CODE_SUCCESS) {
Expand All @@ -30,7 +43,7 @@ class HomeViewModel {
})
}
//2-首页-置顶
getArticleTop(): Promise<ArticleTop> {
getArticleTop(): Promise<Article[]> {
return new Promise(async (resolve: Function, reject: Function) => {
articleTopReq().then((data: ResponseResult) => {
if (data.errorCode === HttpConfig.SERVER_CODE_SUCCESS) {
Expand Down
Loading

0 comments on commit 8476f8a

Please sign in to comment.