鸿蒙应用开发实战【94】— 实战多卡号场景管理最佳实践本文是「号码助手全栈开发系列」第 94 篇持续更新中…开源社区https://openharmonycrossplatform.csdn.net前言多卡管理是号码助手区别于普通通讯录的核心能力。用户可能有工作号、生活号、备用号等多张 SIM 卡需要在各页面中随时切换卡号上下文。本篇讲解多卡号场景下的数据模型设计、UI 联动和数据隔离策略。本篇涵盖CardEntity 设计、多卡切换实现方案、按卡号筛选chip 筛选器、卡号颜色主题、跨页面传递选中卡号。一、CardEntity 数据模型文件features/data/Models.etsexportinterfaceCardEntity{id?:number// 自增主键label:string// 卡号标签工作号 / 生活号 / 卡1phone_number:string// 手机号carrier:string// 运营商中国移动 / 联通 / 电信remark:string// 备注color:string// 卡片颜色blue / orange / green / pink / purplesort_order:number// 排序序号created_at:number// 创建时间updated_at:number// 更新时间}设计要点字段用途说明示例值label用户给卡号起的名字便于区分多张卡“工作号”、“卡1”color卡号主题色列表/卡片中通过颜色区分不同卡号‘blue’、‘orange’sort_order自定义排序常用卡号排前面0、1、2id→app_bindings.card_id外键关联一个卡号关联多个应用绑定主键自增二、多卡号筛选实现2.1 首页 Chip 筛选// HomePage.ets — chip 切换卡号筛选constcardsawaitCardDao.listAll()// chipBar 中展示全部 | 工作号 | 生活号 | 卡3 ...// 筛选逻辑privategetFilteredRows():AppRow[]{returnthis.rows.filter(r{constcatOkthis.filterCatIdx0||r.binding.categoryCATEGORIES[this.filterCatIdx]constcardOkthis.filterCardIdx0||r.cardLabelthis.cards[this.filterCardIdx-1]?.labelconststatOkthis.filterStatIdx0||r.binding.statusSTATUS_FILTERS[this.filterStatIdx]returncatOkcardOkstatOk})}2.2 状态列表页 Chip 筛选文件pages/StatusListPage.ets// chip 二次筛选分类 卡号privategetFilteredRows():AppRow[]{constCATEGORIES[全部,APP,网站,小程序]returnthis.rows.filter(r{constcatOkthis.chipCatIdx0||r.binding.categoryCATEGORIES[this.chipCatIdx]constcardOkthis.chipCardIdx0||r.cardLabelthis.cards[this.chipCardIdx-1]?.labelreturncatOkcardOk})}三、首页卡片上的多卡号统计首页统计卡片展示每个状态的汇总数据点击后跳转到对应的StatusListPage// 跳转到待换绑列表携带 status 参数Text(this.pendingSwitch.toString()).onClick((){router.pushUrl({url:pages/StatusListPage,params:{status:待换绑}asStatusRouteParams})})在 StatusListPage 通过路由参数接收aboutToAppear():void{constparamsthis.getUIContext().getRouter().getParams()if(paramstypeofparams[status]string){this.statusLabelparams[status]}this.loadData()}四、添加应用时选择卡号在AddAppPage和PasteImportPage中用户需要通过 ActionMenu 选择目标卡号// PasteImportPage — 选择绑定的卡号privateasyncpickCard():Promisevoid{constbuttonsthis.cards.map(c({text:${c.label}·${c.phone_number},color:AppColors.TEXT}))constresultawaitpromptAction.showActionMenu({title:选择绑定卡号,buttons:buttons})this.selectedCardIndexresult.index}五、卡号管理页面AddCardPage文件pages/AddCardPage.etsasyncaddCard(){constnowDate.now()constnewCard:CardEntity{label:this.label,// 用户输入的名称phone_number:this.phoneNumber,// 用户输入的号码carrier:this.carrier||未识别,color:this.selectedColor,// 用户选择的主题色remark:this.remark,sort_order:0,created_at:now,updated_at:now}constidawaitCardDao.create(newCard)if(id0){// 刷新首页统计数据AppStorage.Setnumber(cardCount,awaitCardDao.countByStatus())router.back()}}六、卡号颜色主题系统每个卡号可以设置颜色在 UI 中以卡片颜色AppColors映射展示// AppColors.ets — 卡号颜色映射staticcardColor(colorName:string):ResourceColor{constmap:Recordstring,ResourceColor{blue:#4F7CFF,orange:#FF7A1E,green:#3CC463,pink:#FF6B9D,purple:#7B61FF}returnmap[colorName]??#4F7CFF}在 CardDao 反序列化时保留颜色字符串privatestaticparseRow(rs:relationalStore.ResultSet):CardEntity{return{id:rs.getLong(rs.getColumnIndex(id)),label:rs.getString(rs.getColumnIndex(label)),color:rs.getString(rs.getColumnIndex(color))asCardColor,// ...}}七、跨页面当前卡号状态同步多卡号场景下需要一种轻量级「当前选中卡号」共享机制方案AppStorage Watch// 全局状态AppStorage.SetOrCreatenumber(currentCardId,0)AppStorage.SetOrCreatestring(currentCardLabel,)// 页面监听StorageProp(currentCardId)currentCardId:number0// 切换卡号时更新onCardSwitch(card:CardEntity){AppStorage.Set(currentCardId,card.id??0)AppStorage.Set(currentCardLabel,card.label)// 重新加载该卡号下的应用列表this.loadBindings(card.id??0)}八、多卡号场景性能注意事项场景问题优化首页加载CardDao.listAll()AppBindingDao.countByStatus()并行Promise.all()筛选切换内存中 filter 全部数据使用getFilteredRows()计算属性卡号切换重新加载应用列表只在切换时加载不做预加载统计卡片每次 onPageShow 都重新查询配合 AppStorage 缓存统计值小结功能实现方式涉及文件卡号模型CardEntity union typeModels.ets卡号 CRUDCardDao.create / update / deleteByIdCardDao.ets卡号列表CardDao.listAll按 sort_order 排序CardDao.ets颜色映射AppColors.cardColor()AppColors.etsChip 筛选数组 filter分类 卡号 状态HomePage.ets卡号切换AppStorage StorageProp全局状态共享如果这篇文章对你有帮助欢迎点赞、收藏⭐、关注你的支持是我持续创作的动力相关资源开源鸿蒙跨平台社区https://openharmonycrossplatform.csdn.netHarmonyOS 官方文档https://developer.huawei.com/consumer/cn/doc/HarmonyOS hilog文档https://developer.huawei.com/consumer/cn/doc/harmonyos-references/hilogHarmonyOS testinghttps://developer.huawei.com/consumer/cn/doc/harmonyos-guides/unit-testHarmonyOS 安全与权限https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/permissions-overview