本文档梳理本项目主要功能、入口与导航、每个功能模块对应的实现文件、可配置项与资源位置,便于快速理解与二次开发。
- 应用标识与名称:
- 应用配置:
AppScope/app.json5:1(bundleName、icon、label) - 应用名称资源:
AppScope/resources/base/element/string.json:1(app_name) - 应用图标:
AppScope/resources/base/media/app_icon.png
- 应用配置:
- 主模块(入口):
- 模块配置:
products/default/src/main/module.json5:1 - 入口Ability:
products/default/src/main/ets/entryability/EntryAbility.ets:1 - 首页页面(四大Tab容器):
products/default/src/main/ets/pages/Index.ets:1
- 模块配置:
- 功能模块(feature):
- 新闻:
features/news/src/main/ets/pages/NewsPage.ets - 视频:
features/video/src/main/ets/pages/VideoPage.ets(核心UI在features/video/src/main/ets/view/Video.ets) - 关注:
features/follows/src/main/ets/view/Follow.ets+NewsFollowService.ets - 我的:
features/mine/src/main/ets/pages/MinePage.ets(核心UI在features/mine/src/main/ets/view/Mine.ets) - 登录注册:
features/login/src/main/ets/pages/LoginPage.ets、RegisterPage.ets+ 模型与服务
- 新闻:
- 公共能力(commons):
- UI 组件:
commons/uicomponents/src/main/ets/components/... - 工具/网络/配置/解析:
commons/utils/src/...
- UI 组件:
products/default/src/main/ets/entryability/EntryAbility.etsonWindowStageCreate中加载主页:windowStage.loadContent('pages/Index', ...)requestFullScreen以全屏布局展示首页。
products/default/src/main/ets/pages/Index.ets- 定义四个 Tab 容器(新闻/视频/关注/我的),负责 Tab 之间的导航与状态管理。
TabBuilder(title, index)组装 TabBar 项,控制选中态颜色、字体大小与点击切换逻辑。- 关键状态:
currentTabIndex:当前选中Tab索引isLoggedIn、currentUser:登录态与用户信息(从登录模块读取)followRefreshTrigger:触发关注页刷新(切换到关注Tab时自增)
- 修改记录:已将 Tab 字号与高度放大(
fontSize 14、项高68)。
- 页面:
- 登录页:
features/login/src/main/ets/pages/LoginPage.ets- 负责输入手机号/密码并调用
AuthService.login。 - 登录成功后通过回调
onLoginSuccess通知首页切换登录态。 - 顶部首图引用:
Image($r('app.media.logo'))(资源见下文)。
- 负责输入手机号/密码并调用
- 注册页:
features/login/src/main/ets/pages/RegisterPage.ets- 负责输入昵称/手机号/密码并调用
AuthService.register(若实现)。 - 顶部首图同样引用
app.media.logo。
- 负责输入昵称/手机号/密码并调用
- 登录页:
- 核心服务与模型:
features/login/src/main/ets/model/UserService.ets(别名 AuthService)login(phone, password): 调用后端POST /api/users/login(当前硬编码域名http://hmos.w1.luyouxia.net)。- 成功后保存 JWT 到
preferences('user_data').auth_token,并保存current_user的基础展示信息。 - 提供
getJWT() / clearJWT()等方法。
features/login/src/main/ets/model/UserManager.ets- 管理登录态与用户信息,统一读取
auth_token、current_user等。 loginUser、getCurrentUser、getToken、logout、updateUserInfo等。- 偏好存储:
preferences('user_data'),Key:auth_token、current_user、user_info。
- 管理登录态与用户信息,统一读取
- JWT 工具:
commons/utils/src/JwtUtil.ets:解析与校验 JWT(过期、剩余时间、用户信息等)。- (另有
features/login/src/main/ets/model/JwtManager.ets,提供更完整的 JWT 管理接口,当前主要使用JwtUtil+UserManager实现业务)。
更换登录/注册页首图:替换
features/login/src/main/resources/base/media/logo.png,或新增文件后将页面中的Image($r('app.media.logo'))改为对应资源名(详见“资源与国际化”)。
- 页面:
features/news/src/main/ets/pages/NewsPage.ets- 顶部搜索:复用
commons/uicomponents/src/main/ets/components/CommonSearchBar.ets,热词来自百度热搜解析器。 - 类别 Tab、热门新闻卡片、新闻列表等 UI,支持内嵌 WebView 打开详情浮层。
- 数据:部分示例数据内置类
NewsData;也可结合 utils 中服务(见下)。
- 顶部搜索:复用
- 依赖工具:
commons/utils/src/BaiduHotSearchParser.ets:抓取百度热搜,生成热词/热度,供搜索栏 placeholder 与示例数据。- WebView:
@ohos.web.webview用于详情页浏览。
- 页面入口:
features/video/src/main/ets/pages/VideoPage.ets - 核心实现:
features/video/src/main/ets/view/Video.ets- 顶部搜索:
CommonSearchBar - 文本新闻流:按“推荐/国内/国际/娱乐/…”分类,分页加载。
- 数据来源:
commons/utils/src/NewsHeadlineService.ets- 基于
NewsApiConfig.ets构建聚合数据请求(默认使用聚合数据头条接口,需有效apiKey)。
- 基于
- 登录态与收藏:通过
UserManager获取登录状态(示例包含收藏逻辑接口骨架)。
- 顶部搜索:
- 页面:
features/follows/src/main/ets/view/Follow.ets- 展示已关注新闻列表,支持点击卡片打开浏览器层、取消关注等。
- 页面接收
refreshTrigger属性(来自首页 Index.ets),在切换到关注 Tab 时触发刷新。
- 服务:
features/follows/src/main/ets/view/NewsFollowService.etsfollowNews / unfollowNews / getFollowedNewsList等方法。- 依赖:
commons/utils/src/HttpUtils.ets:HTTP 请求工具(GET/POST/DELETE、Form表单编码)。commons/utils/src/BackendConfig.ets:withBase(path)将接口路径拼接到baseUrl(默认http://hmos.w1.luyouxia.net)。features/login/src/main/ets/model/UserManager.ets:获取 JWT 并写入Authorization: Bearer <token>。
- 页面入口:
features/mine/src/main/ets/pages/MinePage.ets - 核心实现:
features/mine/src/main/ets/view/Mine.ets- 已登录:展示头像区、账户信息、常用功能(宫格)、更多功能(宫格),支持退出登录(调用
UserManager.logout)。 - 未登录:展示提示 UI。
- 通过
onLogout回调将退出事件传回首页,首页刷新全局登录态。
- 已登录:展示头像区、账户信息、常用功能(宫格)、更多功能(宫格),支持退出登录(调用
- UI 组件(可复用):
- 搜索栏:
commons/uicomponents/src/main/ets/components/CommonSearchBar.ets - 其它:
CommonLoading.ets、CommonInput.ets、CommonButton.ets等
- 搜索栏:
- 网络与配置:
- HTTP:
commons/utils/src/HttpUtils.ets(GET/POST/DELETE;JSON/Form;默认 Header) - 后端基址:
commons/utils/src/BackendConfig.ets(统一baseUrl) - 头条新闻 API:
commons/utils/src/NewsApiConfig.ets(endpoint/apiKey可配置;buildNewsApiUrl生成带签名的 URL) - 新闻抓取:
commons/utils/src/NewsHeadlineService.ets - JWT:
commons/utils/src/JwtUtil.ets(解析/过期/剩余时间/用户字段)
- HTTP:
- 应用名称:
AppScope/resources/base/element/string.json:1中app_name(已改为“领先新闻”);引用于AppScope/app.json5:8的label。 - 应用图标:
AppScope/resources/base/media/app_icon.png(AppScope/app.json5:6使用$media:app_icon)。 - 登录/注册页首图:
features/login/src/main/resources/base/media/logo.png(页面Image($r('app.media.logo')))。 - Tab 图标(如有):
products/default/src/main/resources/base/media/ic_01_on.svg等(如需自定义 Tab 图标,可在Index.ets的 TabBuilder 中加入Image并引用这些资源)。 - 语言资源:
- 中文:
products/default/src/main/resources/zh_CN/element/string.json - 英文:
products/default/src/main/resources/en_US/element/string.json
- 中文:
- 主模块配置:
products/default/src/main/module.json5mainElement:EntryAbility- 网络权限:
ohos.permission.INTERNET pages:$profile:main_pages(页面路由见products/default/src/main/resources/base/profile/main_pages.json)
- 应用配置:
AppScope/app.json5(图标、名称、版本等)
- 存储位置:
preferences('user_data')auth_token:JWTcurrent_user:当前用户基础信息(账号、临时昵称等)
- 读取入口:
UserManager.getToken / getCurrentUser(多数业务通过它注入请求头或渲染 UI)
- 关注/取关/列表等:依赖
withBase(path)(见commons/utils/src/BackendConfig.ets)- 修改
baseUrl即可全局切换:BackendConfig.baseUrl
- 修改
- 登录接口:当前在
AuthService.login中硬编码完整地址(features/login/src/main/ets/model/UserService.ets)。- 建议改为:
withBase('/api/users/login'),以与其它接口统一域名管理。
- 建议改为:
- 修改应用名称:
AppScope/resources/base/element/string.json的app_name。 - 更换应用图标:替换
AppScope/resources/base/media/app_icon.png,或新增图片并在AppScope/app.json5修改icon字段。 - 更换登录/注册首图:替换
features/login/src/main/resources/base/media/logo.png,或新增资源并修改两页面的Image($r('app.media.xxx'))引用。 - 调整 Tab 样式:
products/default/src/main/ets/pages/Index.ets中TabBuilder(字号、高度、颜色、可加入图标)。 - 切换后端域名:
commons/utils/src/BackendConfig.ets修改baseUrl。 - 修改聚合头条 API key/接口:
commons/utils/src/NewsApiConfig.ets。
- 入口
products/default/src/main/ets/entryability/EntryAbility.ets:1products/default/src/main/ets/pages/Index.ets:1
- 登录注册
features/login/src/main/ets/pages/LoginPage.etsfeatures/login/src/main/ets/pages/RegisterPage.etsfeatures/login/src/main/ets/model/UserService.etsfeatures/login/src/main/ets/model/UserManager.etscommons/utils/src/JwtUtil.ets
- 新闻
features/news/src/main/ets/pages/NewsPage.etscommons/utils/src/BaiduHotSearchParser.ets
- 视频
features/video/src/main/ets/view/Video.etscommons/utils/src/NewsHeadlineService.etscommons/utils/src/NewsApiConfig.ets
- 关注
features/follows/src/main/ets/view/Follow.etsfeatures/follows/src/main/ets/view/NewsFollowService.ets
- 我的
features/mine/src/main/ets/view/Mine.ets
- 公共组件
commons/uicomponents/src/main/ets/components/CommonSearchBar.ets
- 网络工具/配置
commons/utils/src/HttpUtils.etscommons/utils/src/BackendConfig.ets
- 资源与配置
AppScope/app.json5AppScope/resources/base/element/string.jsonAppScope/resources/base/media/app_icon.pngproducts/default/src/main/resources/base/profile/main_pages.json
- 代码中部分中文字符在 IDE 中可能显示为乱码,来源于拷贝或编码差异,不影响功能。
NewsApiConfig.ets内包含示例 API Key,请在生产环境自行管理密钥并避免明文提交仓库。
- 总体架构
- 多模块分层:入口模块整合页面导航;各功能以 feature 模块独立;公共能力沉淀在
commons(utils、uicomponents)。 - 分层关系:UI(pages/view 组件) → 业务服务/数据层(Service、Utils) → 网络与配置(
HttpUtils、BackendConfig/NewsApiConfig) → 后端接口。
- 多模块分层:入口模块整合页面导航;各功能以 feature 模块独立;公共能力沉淀在
- 模块划分
- 入口模块(Entry)
- Ability 与路由装载:
products/default/src/main/ets/entryability/EntryAbility.ets:1 - 首页与四大 Tab 导航容器:
products/default/src/main/ets/pages/Index.ets:1
- Ability 与路由装载:
- 功能模块(Features)
- 新闻:
features/news/src/main/ets/pages/NewsPage.ets:1 - 视频:
features/video/src/main/ets/view/Video.ets:1(入口features/video/src/main/ets/pages/VideoPage.ets:1) - 关注:
features/follows/src/main/ets/view/Follow.ets:1(服务features/follows/src/main/ets/view/NewsFollowService.ets:1) - 我的:
features/mine/src/main/ets/view/Mine.ets:1(入口features/mine/src/main/ets/pages/MinePage.ets:1) - 登录注册:
features/login/src/main/ets/pages/LoginPage.ets:1、features/login/src/main/ets/pages/RegisterPage.ets:1;登录态/用户在features/login/src/main/ets/model/UserManager.ets:1、features/login/src/main/ets/model/UserService.ets:1
- 新闻:
- 公共模块(Commons)
- UI 组件:
commons/uicomponents/src/main/ets/components/CommonSearchBar.ets:1等 - 工具与配置:
commons/utils/src/HttpUtils.ets:1、commons/utils/src/BackendConfig.ets:1、commons/utils/src/NewsHeadlineService.ets:1、commons/utils/src/NewsApiConfig.ets:1、commons/utils/src/JwtUtil.ets:1
- UI 组件:
- 入口模块(Entry)
- 页面与导航
- App 启动:
EntryAbility在onWindowStageCreate加载pages/Index(products/default/src/main/ets/entryability/EntryAbility.ets:1)。 - 首页容器:
Index.ets:1使用Tabs组织 新闻/视频/关注/我的;TabBuilder控制 Tab 样式与切换逻辑。 - 登录态联动:
Index.ets通过UserManager维护isLoggedIn/currentUser,切到关注 Tab 时用followRefreshTrigger触发刷新。
- App 启动:
- 数据与网络层
- HTTP 抽象:
commons/utils/src/HttpUtils.ets:1(GET/POST/DELETE、JSON/Form、默认 Header、超时)。 - 后端基址:
commons/utils/src/BackendConfig.ets:1提供baseUrl与withBase(path);关注相关 API 统一走此基址。 - 新闻来源:
commons/utils/src/NewsHeadlineService.ets:1基于commons/utils/src/NewsApiConfig.ets:1组合请求(聚合头条接口 + API Key),视频页文本流直接消费该服务。 - 登录鉴权:
features/login/src/main/ets/model/UserService.ets:1(AuthService)提交登录表单,成功后写入preferences('user_data').auth_token/current_user。features/login/src/main/ets/model/UserManager.ets:1统一读取/校验登录态、生成鉴权头;JWT 工具commons/utils/src/JwtUtil.ets:1解析过期与用户字段。
- HTTP 抽象:
- 状态与存储
- 偏好存储:HarmonyOS Preferences,命名空间
user_data;键:auth_token、current_user。 - JWT 校验与解析:
commons/utils/src/JwtUtil.ets:1(过期判断、剩余时间、用户名/手机号提取)。
- 偏好存储:HarmonyOS Preferences,命名空间
- 资源与配置
- 应用清单与资源绑定:
AppScope/app.json5:1(icon、label),应用名在AppScope/resources/base/element/string.json:1。 - 登录/注册首图:页面引用
app.media.logo(素材位于登录模块资源目录)。 - 入口模块资源与多语言:
products/default/src/main/resources/...
- 应用清单与资源绑定:
- 数据流简述
- UI 事件(搜索/分页/关注/登录) → 调服务(
NewsHeadlineService/NewsFollowService/AuthService或UserManager) →HttpUtils发起请求 → 使用BackendConfig/NewsApiConfig组装 URL 与鉴权 → 返回数据渲染到页面状态。
- UI 事件(搜索/分页/关注/登录) → 调服务(