摘要:本文以一个完整的龙泉宝剑文化展示与交易管理应用"龙泉剑坊"为案例,深入剖析鸿蒙HarmonyOS ArkTS声明式UI开发范式的核心要点。从以"钢蓝·鞘棕·金辉"三色为核心的古风色彩体系的接口化定义,到响应式数据流@Observed/@ObjectLink在多Tab场景下的深度运用,再到自定义模态弹窗体系、条件渲染路由、layoutWeight弹性布局权重等复杂交互模式的完整实现,逐段拆解每一个关键代码块背后的设计思想与技术原理。全文覆盖接口约束、枚举安全、数据建模、状态驱动渲染、组件拆分策略、颜色透明度拼接、ForEach键值策略等20+个技术知识点,适合有一定ArkTS基础、希望深入理解鸿蒙声明式UI架构的开发者进阶阅读。


一、前言:千年剑魂的数字化新生

剑,乃百兵之君。自春秋欧冶子铸龙泉、泰阿以来,三尺青锋便承载了中国人关于勇毅、匠心与美学的全部想象。"十年的汗水,千锤百炼,方得一道寒光",龙泉铸剑技艺更是在2006年被列入第一批国家级非物质文化遗产名录。它不只是冷兵器时代的遗存,更是绵延数千年的手工业精神图腾。

在数字化浪潮席卷全球的今天,将这样一门承载着深厚文化底蕴的传统技艺以移动应用的形式呈现给现代用户,既是对非物质文化遗产传播方式的一次创新探索,也是对鸿蒙开发技术的一次深度实践。本文所剖析的"龙泉剑坊"应用,正是围绕这一主题构建的一个集名剑陈列、剑鞘库、铸剑工序、订单管理与客户评价于一体的综合性移动端展示平台。

在整个应用的开发过程中,我们不仅需要关注UI层面的视觉表现力——如何用代码还原钢刃的冷峻质感、鞘木的温润纹理与炉火的炽烈气息,更需要深入理解ArkTS框架的底层机制:状态管理如何驱动UI刷新、组件间如何高效通信、数据流如何在父子组件间双向传递、自定义弹窗如何优雅地叠加在页面上方。这些问题的答案,都隐藏在接下来的每一行代码之中。本文将从架构总览开始,逐层深入,带你完成一次从"能写"到"写好"的鸿蒙开发进阶之旅。

二、项目架构总览:分层设计与模块化思想

本应用采用经典的单文件声明式架构,所有逻辑集中在一个源码文件中,通过清晰的分层组织实现高内聚低耦合。整体架构可以划分为以下六个层次:

第一层——类型定义层(第1~132行):包含SwordPalette色彩接口、SwordTab枚举、TabMeta标签元数据接口以及全部业务实体接口(SwordItemSheathItemForgeItemSwordOrderItemSwordReviewItem)和图表数据元接口(WeekSwordMetaStyleShareMetaForgeHotMetaSwordTopMeta)。这一层是整个应用的"契约",所有后续的数据结构都必须严格遵循这些接口定义。

第二层——常量实例层(第23~167行):将抽象的类型定义具象化为可用的运行时对象,包括COLORS色彩常量、TAB_LIST标签列表、SWORD_COL剑脊列阵以及四组静态图表数据(WEEK_SOLD周销量、STYLE_SHARE剑式占比、FORGE_HOT工艺热度、SWORD_TOP名剑排行)。

第三层——响应式数据类(第169~245行):@Observed装饰的SwordData类,封装了五组核心业务数据的动态数组,是整个应用的状态中枢。

第四层——工具函数层(第247~300行):五个纯函数式的颜色映射器(getStyleColorgetSheathColorgetSwordColorgetOrderColorgetReviewTagColor),根据业务规则返回语义化的颜色值。

第五层——主入口与容器组件(第302~519行):@Entry标记的SwordApp根组件,负责全局状态管理、页面骨架搭建、Tab路由分发和弹窗条件渲染。

第六层——子组件体系(第521行至文件末尾):包括1个标签组件(SwordTag)、5个内容组件(分别对应5个Tab页)和4个模态弹窗组件,共同构成完整的功能矩阵。

这种分层设计的优势在于:当需要修改某个功能模块时,开发者可以快速定位到对应的层次进行修改,而不会对其他部分产生连锁反应。例如,如果需要调整配色方案,只需修改第一层的接口定义和第二层的常量实例;如果需要新增一个Tab页面,只需在第五层添加路由分支并在第六层新增对应的内容组件即可。

三、色彩体系设计:接口约束与常量定义

3.1 调色板接口定义

interface SwordPalette {
  primary: string;        // 主色调——钢蓝(剑身钢色)
  primaryLight: string;   // 主色浅变体
  primaryDark: string;    // 主色深变体——深墨
  accent: string;         // 强调色——鞘棕(剑鞘木色)
  accentLight: string;    // 强调色浅变体——金辉
  bg: string;             // 页面背景——冷灰宣纸
  cardBg: string;         // 卡片背景——纯白
  cardAlt: string;        // 交替卡片背景——浅灰
  textPrimary: string;    // 一级文本——墨蓝黑
  textSecondary: string;  // 二级文本——灰蓝
  textHint: string;       // 提示文本——淡灰蓝
  border: string;         // 边框色
  line: string;           // 分割线色
  success: string;        // 成功色——青绿
  warning: string;        // 警告色——金辉
  danger: string;         // 危险色——暗红(血光)
  white: string;          // 纯白
  steelBlue: string;      // 钢蓝(剑身专用)
  scabbardBrown: string;  // 鞘棕(剑鞘专用)
}

这段代码看似简单,却蕴含着深刻的设计思想。首先,使用interface而不是type来定义调色板结构,是因为interface在ArkTS中具有更好的类型推断能力和扩展性。接口中定义了19个颜色字段,覆盖了以下几大语义类别:

  • 主色调系列(primary / primaryLight / primaryDark):以钢蓝色#4E6E8A为核心,模拟龙泉宝剑剑身的冷冽钢色。三个梯度分别用于常规主色、次要强调和深色背景区域(如顶部Header)。
  • 强调色系列(accent / accentLight):鞘棕色#5A4030取自传统剑鞘的紫檀、乌木等硬木本色,用于关键操作按钮和高亮元素;其浅变体金辉色#C9A24B则呼应剑装、錾花与鎏金的贵金属装饰效果。
  • 背景色系(bg / cardBg / cardAlt):冷灰宣纸色#EFF1F2作为页面底色营造刚毅冷峻的工业质感;纯白#FFFFFF作为卡片底色保证内容可读性;浅灰#E6E9EC作为交替行背景增强列表视觉节奏。
  • 文本色三级灰度(textPrimary / textSecondary / textHint):从墨蓝黑#26303A到灰蓝#5E6B77再到淡灰蓝#93A0AC,形成清晰的信息层级。
  • 功能语义色(success / warning / danger):青绿#5E8A6E表示成功/完成,金辉表示提醒/待办,暗红#B3362E表示危险/撤下等不可逆操作。
  • 主题专属色(steelBlue / scabbardBrown):钢蓝复用主色但语义独立,专指剑身钢质元素;鞘棕#5A4030精确对应剑鞘木纹装饰。

这种19色的精细划分,使得整个应用的每一个视觉元素都有明确的"色彩归属",避免了随意使用硬编码颜色值带来的视觉混乱。更重要的是,通过接口约束,任何遗漏字段或类型不匹配都会在编译阶段被TypeScript编译器捕获,实现了"编译期安全"。

3.2 色彩常量实例化

const COLORS: SwordPalette = {
  primary: '#4E6E8A',
  primaryLight: '#7E9CB4',
  primaryDark: '#2E3238',
  accent: '#5A4030',
  accentLight: '#C9A24B',
  bg: '#EFF1F2',
  cardBg: '#FFFFFF',
  cardAlt: '#E6E9EC',
  textPrimary: '#26303A',
  textSecondary: '#5E6B77',
  textHint: '#93A0AC',
  border: '#D5DCE2',
  line: '#EBEEF1',
  success: '#5E8A6E',
  warning: '#C9A24B',
  danger: '#B3362E',
  white: '#FFFFFF',
  steelBlue: '#4E6E8A',
  scabbardBrown: '#5A4030'
};

这里有一个非常值得注意的细节:COLORS常量的类型被显式标注为SwordPalette。这意味着TypeScript编译器会在编译阶段检查COLORS对象是否完整实现了接口定义的所有19个字段。如果遗漏了任何一个颜色字段,或者字段类型不匹配(比如不小心把数字赋给了string类型的字段),编译器会立即报错。这种"编译期安全"是大型项目中极其重要的保障,它能在代码运行之前就消灭掉大量潜在的类型错误。

此外,所有颜色值均采用标准的#RRGGBB六位十六进制格式,而非CSS变量名或语义别名。这种选择在ArkTS生态中是务实的——ArkTS的声明式UI直接支持字符串形式的颜色值,无需额外的运行时解析开销。同时,将所有颜色集中在一个常量对象中管理,使得后续的主题切换(如实现"暗色模式"或"炉火红"节日皮肤)只需要替换这个常量对象即可,体现了良好的架构前瞻性。

四、枚举与标签配置:类型安全的页面路由

4.1 Tab枚举定义

enum SwordTab {
  SWORD = 0,
  SHEATH = 1,
  FORGE = 2,
  ORDER = 3,
  REVIEW = 4
}

SwordTab枚举定义了应用底部导航栏的五个页面标识,从0到4依次对应:剑器陈列、剑鞘库、铸剑工序、订单台账、客户评价。使用枚举而非魔术数字(magic number)的好处是多方面的:

第一,类型安全。当我们在代码中编写this.curTab === SwordTab.SWORD时,编辑器会提供自动补全和拼写检查,有效避免将SWORD误写成SWROD之类的低级错误。

第二,语义明确。枚举成员名称本身就是最好的文档——任何阅读代码的人都能立刻理解SwordTab.FORGE代表的是"铸剑"页面,而数字2则需要查阅上下文才能确定含义。

第三,重构友好。如果未来需要调整Tab的顺序(比如把"订单"移到第二个位置),只需修改枚举值的赋值顺序,所有引用该枚举的地方都会自动适配。

4.2 标签元数据配置

interface TabMeta {
  key: string;
  icon: string;
  label: string;
  color: string;
}

const TAB_LIST: TabMeta[] = [
  { key: 'sword', icon: '⚔️', label: '剑器', color: '#4E6E8A' },
  { key: 'sheath', icon: '🛡️', label: '剑鞘', color: '#5A4030' },
  { key: 'forge', icon: '🔥', label: '铸剑', color: '#C9A24B' },
  { key: 'order', icon: '📦', label: '订单', color: '#C9A24B' },
  { key: 'review', icon: '⭐', label: '客评', color: '#B3362E' }
];

这是一个非常优雅的设计模式:将标签的配置信息(唯一标识key、图标emoji、显示文字label、主题色color)抽象为一个数据数组,而非硬编码在组件的build()方法中。这种"数据驱动UI"的思路,使得后续新增或修改标签时,只需要修改TAB_LIST数组即可,无需触碰任何组件代码。

每个标签还拥有自己的主题色(color字段),这为选中态的视觉表现提供了丰富的个性化空间。例如,"剑器"Tab选中时使用钢蓝色高亮,"客评"Tab则使用暗红色,让用户在不同页面间切换时能获得一致的色彩反馈但又各具辨识度。值得注意的是,SWORD_COL数组([0,1,2,...,11]共12个元素)并非业务数据,而是专门驱动顶部Header中那排"剑脊点缀"装饰圆点的循环计数——它用12次迭代渲染出长短不一、钢蓝与暗红交织的剑脊纹理,是纯装饰性的工程巧思。

五、数据模型设计:接口驱动的类型安全

5.1 业务实体接口定义

interface SwordItem {
  name: string;      // 剑名
  style: string;     // 剑式(长剑/短剑/双手剑/软剑)
  steel: string;     // 钢材(百炼钢/陨铁/精钢...)
  price: number;     // 价格(元)
  emoji: string;     // 表情图标
}

interface SheathItem {
  name: string;      // 鞘名
  wood: string;      // 材质(紫檀/乌木/花梨...)
  level: number;     // 工艺等级(0-100)
  craft: string;     // 工艺(素面/嵌银/浮雕...)
  emoji: string;
}

interface ForgeItem {
  name: string;      // 工序名
  style: string;     // 工序流派(折叠锻/双液淬...)
  level: number;     // 工级(0-100)
  tool: string;      // 工具(大锤/火炉/磨石...)
  emoji: string;
}

interface SwordOrderItem {
  name: string;      // 客户名称
  region: string;    // 所属区域
  count: number;     // 订购数量
  amount: number;    // 订单金额
  emoji: string;
}

interface SwordReviewItem {
  name: string;      // 评价人姓名
  score: number;     // 评分(1-5星)
  date: string;      // 评价日期
  content: string;   // 评价内容
  tag: string;       // 评价标签
  emoji: string;
}

这里展示了五种业务实体的接口定义。每个接口都精心设计了字段,紧密围绕龙泉铸剑行业的实际业务场景:

  • SwordItem(名剑条目):包含剑名(如"七星龙渊"、"湛卢"、"干将")、剑式(长剑、短剑、双手剑、软剑)、钢材(百炼钢、陨铁、精钢、花纹钢、合金钢)、价格和表情图标。其中style字段不仅用于显示,还是后续getStyleColor()颜色映射函数的输入参数;price字段则驱动getSwordColor()的阶梯阈值着色。
  • SheathItem(剑鞘条目):记录鞘名、材质(紫檀、乌木、花梨、金丝楠、牛皮、鲨鱼皮等)、工艺等级(level数值型,用于进度条可视化)、工艺(素面、嵌银、浮雕、描金等)。level字段是剑鞘页面的核心数据维度,直接影响每具鞘的"工艺等级"进度条长度和颜色。
  • ForgeItem(铸剑工序条目):记录工序名(千锤锻打、淬火入水、磨砺开锋等十二道核心工序)、流派、工级和所用工具(大锤、火炉、磨石、夹钢、錾刀等)。level同样驱动进度条。
  • SwordOrderItem(订单条目):记录客户名称(武术馆联盟、影视道具组、收藏家协会等)、所属区域、订购数量和金额。amount字段是getOrderColor()的颜色映射依据——金额越高颜色越"危险"(暗红),形成直观的业务警示。
  • SwordReviewItem(评价条目):包含评分(1-5星)、日期、评价内容和标签。tag字段是getReviewTagColor()的映射输入,不同类型的评价标签被赋予不同的主题色。

这五组接口构成了整个应用的数据模型基石。值得注意的是,所有接口中的emoji字段虽然不是严格的"业务数据",但在ArkTS的Text组件中可以直接渲染表情符号,为每个条目提供了轻量级的视觉标识,增强了列表的可读性和趣味性。

5.2 图表数据元接口

interface WeekSwordMeta {
  day: string;   // 星期几
  value: number; // 销量
}

interface StyleShareMeta {
  name: string;   // 剑式名称
  count: number;  // 数量
  color: string;  // 主题色
}

interface ForgeHotMeta {
  name: string;   // 工艺名称
  heat: number;   // 热度值
  color: string;  // 主题色
}

interface SwordTopMeta {
  name: string;   // 剑名
  sold: number;   // 销量
  color: string;  // 主题色
}

这四个接口专门服务于应用中的四种图表/排行榜可视化组件。它们的设计遵循了一个共同的范式:名称 + 数值 + 主题色的三元组结构。这种统一的数据形状使得四种图表可以共享同一套渲染逻辑(ForEach + layoutWeight条形图),只是数据源不同而已。

WeekSwordMeta用于垂直柱状图(周销量走势),StyleShareMeta/ForgeHotMeta/SwordTopMeta用于水平条形图(占比/热度/排行)。每种接口都自带color字段,使得每个数据条都可以拥有独立的颜色,而不是强制使用统一的系列色。

六、静态数据集:应用的基础数据源

6.1 图表静态数据

const WEEK_SOLD: WeekSwordMeta[] = [
  { day: '周一', value: 12 },
  { day: '周二', value: 16 },
  { day: '周三', value: 14 },
  { day: '周四', value: 20 },
  { day: '周五', value: 24 },
  { day: '周六', value: 33 },
  { day: '周日', value: 29 }
];

这里展示了周销量数据。在实际项目中,这些数据通常来自后端API接口,但本应用作为展示型Demo采用了静态数据的方式。数据的设计颇具巧思——销量从周一到周六逐步递增(12→16→14→20→24→33),周日略有回落(29),这完全符合零售业"工作日平稳、周末高峰、周日略降"的实际规律,让数据看起来真实可信。图表中value >= 29的柱子使用主色钢蓝、其余使用金辉,使得周六峰值在视觉上一目了然。

const STYLE_SHARE: StyleShareMeta[] = [
  { name: '长剑', count: 5, color: '#4E6E8A' },
  { name: '短剑', count: 4, color: '#5A4030' },
  { name: '双手剑', count: 3, color: '#C9A24B' },
  { name: '软剑', count: 2, color: '#B3362E' }
];

剑式构成数据反映了龙泉剑最常见的四种形制及其市场占比。长剑(5款)居首,这是最经典、受众最广的剑式;短剑(4款)便于随身携带与仪仗;双手剑(3款)威猛厚重,多见于收藏与舞台;软剑(2款)最为灵巧罕见,因韧性与工艺要求极高而数量最少。

6.2 工艺热度与排行榜数据

const FORGE_HOT: ForgeHotMeta[] = [
  { name: '千锤锻打', heat: 97, color: '#4E6E8A' },
  { name: '淬火成钢', heat: 95, color: '#5A4030' },
  { name: '磨砺开锋', heat: 94, color: '#C9A24B' },
  { name: '装配剑具', heat: 93, color: '#5E8A6E' },
  { name: '折叠锻纹', heat: 92, color: '#B3362E' },
  { name: '养剑防锈', heat: 90, color: '#7E9CB4' }
];

const SWORD_TOP: SwordTopMeta[] = [
  { name: '七星龙渊', sold: 97, color: '#4E6E8A' },
  { name: '湛卢', sold: 93, color: '#5A4030' },
  { name: '泰阿', sold: 90, color: '#C9A24B' },
  { name: '鱼肠', sold: 87, color: '#5E8A6E' },
  { name: '秋霜', sold: 84, color: '#B3362E' },
  { name: '青锋', sold: 81, color: '#7E9CB4' }
];

铸剑工艺热度数据涵盖了龙泉铸剑的六道核心工序,从"千锤锻打"(97分)到"养剑防锈"(90分),热度值递减但差距不大,说明每一道工序在铸剑师心中都至关重要。名剑销量TOP6则列出了最受欢迎的六款剑,"七星龙渊"以97的销量遥遥领先——这款剑名源自古代名剑谱,是龙泉剑坊的镇店之作。

七、响应式数据类:@Observed装饰器的深度运用

@Observed
export class SwordData {
  swords: SwordItem[] = [
    { name: '七星龙渊', style: '长剑', steel: '百炼钢', price: 2880, emoji: '⚔️' },
    { name: '湛卢', style: '长剑', steel: '陨铁', price: 3880, emoji: '🌑' },
    // ... 共12款名剑
  ];

  sheaths: SheathItem[] = [
    { name: '紫檀剑鞘', wood: '紫檀', level: 96, craft: '素面', emoji: '🪵' },
    // ... 共12具剑鞘
  ];

  forges: ForgeItem[] = [
    { name: '千锤锻打', style: '折叠锻', level: 96, tool: '大锤', emoji: '🔨' },
    // ... 共12道工序
  ];

  orders: SwordOrderItem[] = [
    { name: '武术馆联盟', region: '华东', count: 300, amount: 540000, emoji: '🥋' },
    // ... 共12条订单
  ];

  reviews: SwordReviewItem[] = [
    { name: '武馆馆长', score: 5, date: '09-03',
      content: '七星龙渊重心合度,劈刺迅疾,练了半月虎口不震。',
      tag: '重心合度', emoji: '🥋' },
    // ... 共12条评价
  ];
}

这是整个应用最核心的数据层代码@Observed装饰器是ArkTS框架提供的一个关键装饰器,它的作用是将一个普通的类标记为"可观察的"。当一个类被@Observed标记后,该类实例的属性变化会被ArkTS框架自动追踪,任何引用了该实例属性的@ObjectLink@State变量都会在属性变化时自动触发UI重新渲染。

SwordData类封装了五组核心业务数据:

  • swords(12款名剑):涵盖从"七星龙渊"(¥2880,长剑·百炼钢)到"青锋"(¥1580,短剑·精钢)的全价位段产品矩阵,钢材维度横跨文化百炼钢、陨铁、花纹钢、合金钢、精钢。
  • sheaths(12具剑鞘):材质涵盖紫檀、乌木、花梨、金丝楠、牛皮、鲨鱼皮、红酸枝、竹片、铜饰、银饰、大漆、藤条等,工艺等级89-96不等。
  • forges(12道工序):从千锤锻打(大锤法,工级96)到试斩调校(草席靶,工级93),完整还原龙泉铸剑的十二道核心工序。
  • orders(12条订单):客户涵盖武术馆联盟、影视道具组、收藏家协会、国术学院、海外武馆、汉服品牌、景区演艺团、文旅街区、游戏周边、博物馆文创、私人定制、庆典仪仗等多元渠道,区域分布华东/华南/华北/华中/西南/海外。
  • reviews(12条评价):全部4-5星好评,标签涵盖"重心合度"、"锻纹如云"、"舞台炸场"、"独一无二"等多维度评价维度。

这种将所有业务数据集中在一个@Observed类中的设计,带来了一个重要的架构优势:单一数据源原则(Single Source of Truth)。无论哪个子组件需要访问名剑列表、剑鞘数据还是订单信息,它们都通过@ObjectLink引用同一个SwordData实例。当任何一个数据发生变化(比如新增了一款剑、删除了一具鞘),所有引用该数据的组件都会自动收到通知并刷新UI,无需手动编写繁琐的事件监听和数据同步逻辑。

八、颜色工具函数:纯函数的设计哲学

function getStyleColor(style: string): string {
  if (style === '长剑') return '#4E6E8A';
  else if (style === '短剑') return '#5A4030';
  else if (style === '双手剑') return '#C9A24B';
  return '#B3362E';  // 软剑及其他 → 暗红
}

function getSheathColor(level: number): string {
  if (level >= 95) return '#4E6E8A';
  else if (level >= 92) return '#5A4030';
  return '#C9A24B';
}

function getSwordColor(price: number): string {
  if (price >= 3000) return '#B3362E';   // 高价 → 暗红(珍品)
  else if (price >= 2200) return '#4E6E8A';
  else if (price >= 1700) return '#5A4030';
  return '#7E9CB4';                       // 低价 → 浅钢蓝
}

function getOrderColor(amount: number): string {
  if (amount >= 400000) return '#B3362E';
  else if (amount >= 280000) return '#4E6E8A';
  else if (amount >= 200000) return '#5A4030';
  return '#7E9CB4';
}

function getReviewTagColor(tag: string): string {
  if (tag === '重心合度' || tag === '锻纹如云' || tag === '舞台炸场' || tag === '独一无二')
    return '#4E6E8A';
  else if (tag === '上镜凛凛' || tag === '剑脊挺直' || tag === '包装严实')
    return '#5A4030';
  else if (tag === '气场全开' || tag === '复刻形准' || tag === '整齐划一')
    return '#5E8A6E';
  else if (tag === '样式丰富' || tag === '卖得飞快')
    return '#C9A24B';
  return '#B3362E';
}

这五个函数构成了应用的颜色映射层,它们都是纯函数(Pure Function)——相同的输入永远产生相同的输出,没有任何副作用。这种函数式的设计哲学在ArkTS开发中具有多重价值:

第一,可测试性极强。每个函数都可以独立编写单元测试,传入已知输入、断言输出颜色,无需mock任何组件或状态。

第二,逻辑集中且可复用。颜色映射规则分散在多个组件中是维护噩梦——如果要调整"高价剑"的阈值从3000改为3200,需要在每个使用处逐一修改。而现在只需改getSwordColor一处。

第三,语义化表达业务规则。函数名本身就是最好的文档——getSwordColor(price)一眼就能看出是根据价格返回名剑卡片的主题色;getReviewTagColor(tag)则是根据评价标签返回标签颜色。

特别值得分析的是getSwordColor阶梯式阈值设计:价格≥3000返回暗红(danger色,暗示"高端珍品"),2200-2999返回主色钢蓝,1700-2199返回鞘棕,<1700返回浅钢蓝(success色,暗示"性价比之选")。这种将业务语义(价格区间)映射到视觉语义(颜色情感)的模式,是数据可视化中的经典手法。

九、主入口组件:应用骨架与路由管理

9.1 状态声明

@Entry
@Component
struct SwordApp {
  @State curTab: number = 0;
  @State data: SwordData = new SwordData();
  @State showAddSword: boolean = false;
  @State showEditOrder: boolean = false;
  @State showDeleteSheath: boolean = false;
  @State showDetailForge: boolean = false;
  @State delSheathName: string = '';
  @State detailForgeName: string = '';
  @State bladeRotate: boolean = false;
  @State shineScale: boolean = false;

SwordApp是整个应用的根组件,由@Entry装饰器标记为应用入口,由@Component标记为可复用组件。它声明了10个@State状态变量,可以分为三类:

路由与数据状态(2个):

  • curTab:当前选中的Tab索引(0-4),控制底部导航栏的高亮和内容区的切换。
  • dataSwordData实例,作为全局唯一的数据源传递给所有子组件。

弹窗显隐状态(4个布尔值 + 2个字符串):

  • showAddSword / showEditOrder / showDeleteSheath / showDetailForge:分别控制四种弹窗的显示/隐藏。
  • delSheathName / detailForgeName:传递给删除确认弹窗和详情弹窗的上下文参数(要删除的鞘名、要查看详情的工序名)。

动画交互状态(2个布尔值):

  • bladeRotate:控制头部剑形图标的旋转动画(点击切换90°旋转)。
  • shineScale:控制闪亮图标的缩放动画(点击切换1.6倍放大)。

这种细粒度的状态拆分,使得每个UI交互都有独立的状态变量驱动,互不干扰。当需要添加新的弹窗或交互效果时,只需新增一个@State变量即可,不会影响现有逻辑。

9.2 构建方法与页面结构

build()方法是ArkTS声明式UI的核心——它描述了"UI应该长什么样",框架负责在状态变化时高效地 diff 和更新真实DOM。SwordAppbuild()方法构建了以下三层结构:

第一层:主内容区(外层Column)。包含两个子区域:

  • 顶部Header区域:深墨色(primaryDark)背景,包含应用标题"⚔️ 龙泉剑坊"、英文副标题"Sword · 千锤百炼 剑气如虹"、右侧操作图标区(剑形旋转、闪亮缩放、剑尖图标),以及下方的12个剑脊点缀点(用三种尺寸和颜色的矩形/圆形模拟剑身上的锻造纹理与血槽排列),再下方是"🗡️ 剑出龙泉 · 霜刃试锋 ⚔️"的分隔标题。
  • 内容切换区:根据curTab的值,使用if-else if-else链条件渲染对应的内容组件(SwordContent / SheathContent / ForgeContent / SwordOrderContent / SwordReviewContent)。

第二层:底部导航栏(固定在底部)。使用ForEach遍历TAB_LIST数组渲染5个Tab项,每个Tab项根据是否为当前选中项来决定:文字颜色(主题色 vs 提示色)、字重(Bold vs Normal)、背景色(半透明主题色 vs 透明)、边框(有 vs 无)。点击时更新curTab状态触发切换。

第三层:弹窗叠加层。四个if条件分别控制四种弹窗的渲染,弹窗以绝对定位的方式覆盖在主内容区上方。

9.3 弹窗的条件渲染

if (this.showAddSword) {
  AddSwordModal({
    onClose: () => { this.showAddSword = false; }
  })
}
if (this.showEditOrder) {
  EditSwordOrderModal({
    onClose: () => { this.showEditOrder = false; }
  })
}
if (this.showDeleteSheath) {
  DeleteSheathModal({
    title: this.delSheathName,
    onClose: () => { this.showDeleteSheath = false; }
  })
}
if (this.showDetailForge) {
  DetailForgeModal({
    name: this.detailForgeName,
    onClose: () => { this.showDetailForge = false; }
  })
}

弹窗的渲染采用了条件渲染 + 回调关闭的模式。每个弹窗组件接收一个onClose回调函数,当用户点击弹窗内的"取消"/"确认"/"知道了"等按钮时,调用onClose()将对应的showXxx状态设为false,从而触发弹窗从DOM中移除。

DeleteSheathModalDetailForgeModal额外接收了title/name属性,用于在弹窗内显示具体的操作对象(要删除哪具鞘、查看哪道工序的详情)。这种"状态提升"(Lifting State Up)的模式——将弹窗所需的上下文数据存储在父组件的状态中、通过props传递给弹窗——是React/ArkTS中处理跨组件数据流的经典方案。

十、内容组件体系:@ObjectLink与数据双向绑定

10.1 标签组件 SwordTag

@Component
struct SwordTag {
  @Prop text: string;
  @Prop color: string;

  build() {
    Text(this.text)
      .fontSize(9)
      .fontColor(this.color)
      .padding({ left: 7, right: 7, top: 2, bottom: 2 })
      .backgroundColor(this.color + '14')  // 8% 透明度
      .borderRadius(10)
      .border({ width: 1, color: this.color + '40' })  // 25% 透明度边框
  }
}

SwordTag是一个极简但精心设计的标签组件,仅接收两个@Prop属性:text(标签文字)和color(主题色)。它的设计精髓在于颜色透明度的十六进制拼接技巧

  • 背景色使用this.color + '14',即在原色hex后面追加14(即8%不透明度的alpha通道),生成类似#4E6E8A14的8位颜色值。ArkTS原生支持这种8位hex颜色格式,前6位是RGB,后2位是Alpha(00=完全透明,FF=完全不透明)。
  • 边框色使用this.color + '40'(25%不透明度),比背景更明显但不抢眼。

这种"同色系多层透明度叠加"的技法,使得标签在任何背景下都能保持和谐的视觉效果——背景色淡淡的(8%)提供区域感,边框稍深(25%)勾勒轮廓,文字用原色(100%)保证可读性。三者来自同一个color变量,天然协调,永远不会出现"背景是蓝色但文字是红色"的色彩冲突。

10.2 名剑内容组件 SwordContent

@Component
struct SwordContent {
  @ObjectLink data: SwordData;
  onAdd: () => void = () => {};

  build() {
    Scroll() {
      Column() {
        // 销量图表卡片
        Column() { /* 本周剑器销量柱状图 + 名剑陈列标题 */ }
        // 名剑列表
        ForEach(this.data.swords, (p: SwordItem, i: number) => {
          Row() { /* 卡片内容 */ }
        }, (p: SwordItem, i: number) => 'sw' + p.name + i)
      }
    }
    .scrollable(ScrollDirection.Vertical)
    .scrollBar(BarState.Off)
  }
}

SwordContent是剑器Tab页的主体内容组件。它使用了@ObjectLink装饰器来接收父组件传递的SwordData实例。@ObjectLink@Prop的关键区别在于:@ObjectLink建立的是引用关系,而非值的拷贝——当SwordData实例的属性(如swords数组)发生变化时,SwordContent中引用这些属性的部分会自动刷新。这使得数据在父子组件间实现了"双向绑定"的效果。

组件内部由两大部分组成:

第一部分:本周剑器销量图表卡片。顶部有一排装饰性的剑脊点缀(模仿剑身锻造纹理的简化版),下方是标题"📈 本周剑器销量"和副标题"周六33柄"。柱状图使用ForEach遍历WEEK_SOLD数据,每根柱子的高度value决定(直接以数值作为像素高度),颜色由阈值判断决定(≥29用主色钢蓝,否则用金辉)。柱子使用borderRadius(8)实现圆角,底部对齐(alignItems(VerticalAlign.Bottom))模拟真实柱状图的基线对齐效果。

第二部分:名剑陈列列表。使用ForEach遍历this.data.swords数组,每张卡片是一个Row横向布局,包含三个区域:

  • 左侧图标区(宽56):圆角头像区显示emoji,下方有一条短横线模拟剑脊纹理。整体背景色由getSwordColor(p.price)决定并附加极低透明度(0D=5%),边框用33%透明度。
  • 中间信息区layoutWeight(1)占据剩余空间):第一行是剑名(粗体)+ 价格(带颜色),第二行是剑式标签(SwordTag)+ 钢材标签(SwordTag)+ "龙泉铸造"提示文字。
  • 卡片背景色使用i % 2 === 0实现斑马纹交替(白色/浅灰),增强列表的可读性。

10.3 其他内容组件

SheathContent(剑鞘库):结构与SwordContent类似,但顶部图表替换为"名剑销量TOP6"水平条形图(使用SWORD_TOP数据和layoutWeight实现比例条),列表项改为剑鞘卡片——左侧显示emoji+工艺等级圆点+金辉点缀,中间显示鞘名+工艺,下方显示"材质"进度条(同样用layoutWeight(c.level)实现比例填充)。右侧有删除图标(🗑️),点击触发onDel回调。

ForgeContent(铸剑工序):顶部图表为"铸剑工艺热度"水平条形图(FORGE_HOT数据),列表项为工序卡片——显示工序名+流派+工级+工具标签,左侧用getSheathColor(d.level)着色,右侧有详情图标(👁️)。

SwordOrderContent(订单台账):顶部图表为"剑式构成占比"水平条形图(STYLE_SHARE数据),列表项为订单卡片——显示客户emoji+名称+区域+数量+金额(金额颜色由getOrderColor决定),右侧有编辑图标(✏️)。

SwordReviewContent(客户评价):无顶部图表,直接是评价列表。每条评价显示用户emoji+姓名+日期+星级(用.repeat(score)重复渲染)+ 标签(SwordTag)+ 评价正文。星级使用repeat()方法根据score值动态生成对应数量的星星字符,简洁而巧妙。

十一、弹窗组件体系:自定义模态对话框

应用包含四个自定义弹窗组件,它们共享一致的结构模式但各有不同的内容和用途:

11.1 定制剑器弹窗 AddSwordModal

弹窗采用全屏遮罩 + 居中卡片的经典模态布局:

  • 外层Column:width('100%') height('100%'),背景色#66000000(40%黑色半透明遮罩),justifyContent(FlexAlign.Center)使内容居中。
  • 内层卡片:宽度86%、最大高度78%、圆角18px、白色背景、16px内边距。
  • 标题栏:"⚔️ 定制剑器" + 右侧"✕"关闭按钮,两端对齐。
  • 内容区:三行信息展示(剑名、剑式、预算),每行使用cardAlt背景色+圆角的统一样式。
  • 操作栏:左"取消"(描边按钮)+ 右"确认定制"(实心主色按钮),右对齐。

11.2 其他弹窗组件

  • EditOrderModal(修改订单):结构与AddSwordModal完全对称,内容改为客户/数量/金额三行信息,确认按钮文案为"保存修改"。
  • DeleteSheathModal(撤下剑鞘):接收title属性显示要删除的鞘名,内容为确认提示文字("确定撤下「xxx」吗?"),确认按钮使用危险色danger = 暗红)以警示操作的不可逆性。
  • DetailForgeModal(工序详情):接收name属性,内容包含大号emoji+工序名展示区、龙泉铸剑文化介绍文字、技法信息和匠人评级,单按钮"知道了"关闭。其中那段"龙泉铸剑,钢入炉中烧至杏黄……"的介绍文字,是对千锤百炼工艺的诗意还原。

四个弹窗的一致性设计(相同的外层遮罩、卡片尺寸、圆角、内边距、标题栏布局)确保了用户在使用不同功能时获得统一的交互体验。差异仅在内容区和按钮文案/颜色上体现各自的功能特性。

十二、关键技术点深度解析

12.1 layoutWeight布局权重的妙用

layoutWeight是ArkTS弹性布局中最强大的属性之一。在本应用中被广泛用于两类场景:

水平条形图:在SWORD_TOPFORGE_HOTSTYLE_SHARE的渲染中,每行的彩色条使用layoutWeight(dataValue)设置宽度权重,后面的灰色占位条使用layoutWeight(maxValue - dataValue)补足剩余空间。两者之和等于最大值(100或20),从而精确实现按比例分配宽度的效果——无需手动计算像素值,框架自动处理。

列表卡片内部布局:名剑卡片、剑鞘卡片等信息区的中间列使用layoutWeight(1)占据所有剩余空间,左右固定宽度的图标区和操作区保持不变。这种"两头固定、中间弹性"的布局模式在移动端UI中极为常见。

12.2 颜色透明度的十六进制拼接

本应用大量使用了hex颜色 + 两位alpha的8位颜色格式,例如:

  • COLORS.accentLight + '2E' → #C9A24B2E(约18%不透明度,用于按钮背景)
  • COLORS.accentLight + '66' → #C9A24B66(约40%不透明度,用于分割线)
  • COLORS.primary + '55' → #4E6E8A55(约33%不透明度,用于卡片边框)
  • t.color + '14' → 如#4E6E8A14(约8%不透明度,用于标签背景)

这种技法的优势在于:不需要预先定义几十个半透明色变量,而是根据运行时的主题色动态生成任意透明度的衍生色。只要知道基础色和目标透明度,字符串拼接一行搞定。需要注意的是,alpha值的范围是00(全透明)到FF(完全不透明),常用参考值:14≈8%,2E≈18%,40≈25%,55≈33%,66≈40%,80≈50%。

12.3 ForEach的键值生成策略

应用中所有的ForEach循环都精心设计了键值生成函数(key generator)

// 名剑列表
(p: SwordItem, i: number) => 'sw' + p.name + i
// 剑鞘列表
(c: SheathItem, i: number) => 'sh' + c.name + i
// 铸剑列表
(d: ForgeItem, i: number) => 'fg' + d.name + i
// 订单列表
(o: SwordOrderItem, i: number) => 'od' + o.name + i
// 评价列表
(r: SwordReviewItem, i: number) => 'rv' + r.name + i
// Tab栏
(t: TabMeta) => t.key
// 图表数据
(w: WeekSwordMeta) => w.day
(t: SwordTopMeta) => t.name
(d: ForgeHotMeta) => d.name
(s: StyleShareMeta) => s.name

每个键值都采用了**"前缀 + 唯一标识 + 索引"**的三段式命名:

  • 前缀(sw/sh/fg/od/rv)区分不同的ForEach作用域,避免跨列表键值冲突。
  • 唯一标识(p.name/c.name等)保证同一条数据始终映射到同一个键。
  • 索引(i)作为兜底,即使两条数据name相同(理论上不应发生但有防御价值),索引也能保证唯一性。

正确的键值策略对于ArkTS的ForEach性能至关重要——框架通过键值来判断哪些节点是"新增"、哪些是"移动"、哪些是"删除",从而执行最小量的DOM操作。错误的键值(如使用Math.random())会导致每次渲染都销毁重建所有节点,造成严重的性能问题。

12.4 @Observed/@ObjectLink响应式机制

本应用的响应式数据流遵循以下路径:

SwordData (@Observed 类)
    ↓ @ObjectLink 引用传递
SwordContent / SheathContent / ForgeContent / SwordOrderContent / SwordReviewContent
    ↓ 组件内部读取属性
ForEach / Text / 等UI节点

SwordData的某个数组属性发生变化(如swords.push(newItem)),ArkTS框架的响应式系统会:

  1. 检测到@Observed类实例的属性变更。
  2. 通知所有持有该实例@ObjectLink引用的组件。
  3. 这些组件重新执行build()方法。
  4. 框架diff新旧虚拟DOM树,只更新实际变化的部分。

这种机制比传统的"手动调用setState/refresh"模式更加声明式和自动化,开发者只需描述"数据是什么样子",框架负责"如何高效地更新UI"。

12.5 条件渲染与状态驱动UI

应用的Tab切换和弹窗显示/隐藏完全由状态变量驱动,没有使用任何命令式的DOM操作(如appendChild/removeChild):

  • Tab切换:if (this.curTab === SwordTab.SWORD) 渲染SwordContent,否则渲染其他组件。curTab的变化由底部Tab栏的onClick事件触发。
  • 弹窗显示:if (this.showAddSword) 渲染AddSwordModalshowAddSword由"➕铸剑"按钮的onClick设为true,由弹窗内部的"取消"/"确认"按钮的onClick通过onClose回调设为false

这种"状态即UI"(State is UI)的范式是现代前端框架的核心思想——UI是状态的纯函数,给定相同的状态必然产生相同的UI。这使得应用的行为完全可预测、可调试、可测试。

十三、技术要点对比表

技术维度 本应用实现方案 设计意图与优势
状态管理 @State(10个) + @Observed类 + @ObjectLink引用 单一数据源 + 自动响应式更新,避免手动同步
组件通信 Props单向数据流 + 回调函数(onClose/onAdd/onDel/onDetail/onEdit) 父→子通过Props,子→父通过回调,清晰的单向数据流
路由方案 if-else if-else条件渲染 + 枚举索引 适合Tab数量固定(5个)的场景,简单直接无额外依赖
弹窗模式 条件渲染 + 全屏遮罩 + 居中卡片 + 回调关闭 统一的模态交互规范,遮罩阻止背景交互
列表渲染 ForEach + 三段式键值('prefix'+name+index) 高效diff、防冲突、可预测的节点复用
布局策略 layoutWeight弹性权重 + 固定像素结合 两头固定中间弹性的经典移动端布局
颜色系统 19色接口约束 + hex+alpha透明度拼接 编译期类型安全 + 运行时动态透明度
数据建模 5个业务接口 + 4个图表接口 + 纯函数颜色映射 类型安全 + 逻辑集中 + 可独立测试
动画交互 .rotate() + .scale() + .animation() 声明式动画 状态驱动动画,无需手动管理动画生命周期
斑马纹列表 i % 2 === 0 ? COLORS.cardBg : COLORS.cardAlt 偶数行白色、奇数行浅灰,增强列表可读性

十四、总结与展望

14.1 架构设计总结

"龙泉剑坊"应用虽然是一个单文件Demo,但其内部蕴含的架构思想值得认真总结:

分层清晰,职责分明。从顶部的类型定义层到底部的子组件层,每一层都有明确的单一职责。类型定义层管"契约",常量实例层管"数据",响应式类管"状态",工具函数层管"逻辑",入口组件管"路由",子组件管"展示"。这种分层使得无论是添加新功能、修bug、还是做性能优化,都能快速定位到正确的层次。

接口先行,类型安全。所有数据结构都先定义接口,再实现具体数据。TypeScript编译器在编译阶段就能捕获大部分类型错误,大大降低了运行时出错的概率。19色调色板接口、5组业务实体接口、4组图表接口——合计超过10个接口定义,构筑了坚实的类型安全防线。

数据驱动,声明式渲染。UI完全由状态驱动,没有命令式的DOM操作。ForEach遍历数据数组生成列表,if条件根据状态渲染不同组件,@Observed/@ObjectLink自动追踪数据变化并刷新UI。这种范式让代码更接近"描述UI应该是什么样子"而非"如何一步步构建UI"。

14.2 技术亮点回顾

回顾全文,以下几个技术点尤其值得关注和借鉴:

  1. 19色古风色彩体系:以"钢蓝·鞘棕·金辉"为核心的三色系设计,配合精细的透明度梯度(8%/18%/25%/33%/40%),在视觉上完美还原了龙泉宝剑的冷峻钢魂与鎏金华彩。接口约束 + 常量实例化的双层设计,兼顾了类型安全和运行效率。
  2. @Observed + @ObjectLink的深度运用:将全部业务数据封装在一个可观察类中,通过@ObjectLink引用传递给5个子组件,实现了真正的"单一数据源 + 双向绑定"。任何数据变化都会自动级联刷新所有相关UI,无需手动编写事件监听。
  3. 纯函数颜色映射层:5个无副作用的颜色映射函数,将业务规则(价格区间、工艺等级、评价标签等)映射到视觉语义(颜色情感)。逻辑集中、可测试、可复用。
  4. layoutWeight驱动的数据可视化:不依赖任何图表库,仅用ArkTS内置的layoutWeight属性就实现了4种精美的条形图/柱状图。这种"零依赖"的数据可视化方案在轻量级应用中极具实用价值。
  5. 统一的弹窗交互规范:4个弹窗共享相同的结构模板(遮罩+居中卡片+标题栏+内容区+操作栏),仅在内容和按钮细节上差异化。这种一致性设计大幅降低了用户的认知负荷。

14.3 优化建议与展望

尽管本应用在架构和实现上已经相当成熟,但从工程实践的角度,仍有以下几点可以进一步优化:

第一,状态管理的精细化。当前10个@State变量全部集中在根组件中,随着功能增长可能导致"状态爆炸"。建议引入轻量级状态管理方案(如ArkTS的@Provide/@ConsumeAppStorage),将弹窗状态下沉到各自的父组件中,根组件只保留全局路由状态。

第二,网络数据对接的准备。当前所有数据均为静态硬编码。SwordData类的数组属性已经为未来对接真实后端API留出了清晰的接口——只需将静态初始化改为异步fetch调用,UI层无需任何修改。这种"函数化的封装方式"体现了良好的架构前瞻性。

第三,组件的进一步拆分。当前每个内容组件(如SwordContent)内部同时包含了图表卡片和列表卡片两种UI形态。如果未来需要单独复用"周销量图表"或"名剑列表",可以考虑将它们拆分为独立的子组件(WeeklyChartSwordList),提升复用性。

第四,无障碍访问(Accessibility)的支持。当前组件未设置accessibilityText等无障碍属性。对于一款面向文化传播的应用,确保视障用户也能通过屏幕阅读器获取剑坊信息,是产品完善度的重要维度。

第五,国际化(i18n)的预留。虽然"龙泉剑坊"面向中文用户,但如果未来需要推出多语言版本(如面向海外剑友的英文版),建议将所有硬编码的中文字符串提取到资源文件(string.json)中,通过$r()引用。当前的TAB_LIST、静态数据中的中文字符串都需要做相应改造。

总而言之,"龙泉剑坊"应用以其精巧的古风色彩体系、严谨的类型安全设计、高效的响应式数据流和优雅的组件化架构,为我们提供了一个学习鸿蒙ArkTS声明式UI开发的优秀范本。从"能写出能跑的代码"到"写出架构清晰、可维护、可扩展的代码",这正是每一位鸿蒙开发者应当追求的成长路径。愿这篇深度解析能为你的进阶之路提供一份有价值的参考。


十五、附录:DevEco Studio 安装与环境搭建

为方便读者将本文分析的"龙泉剑坊"代码真正跑起来,下面补充一份 DevEco Studio 的安装与环境配置简明教程(配图来源于官方文档)。

安装DevEco Studio程序

在这里插入图片描述

选择目标安装目录:

在这里插入图片描述

设置环境变量,设置完务必重启:

在这里插入图片描述

新建一个空白模板:

在这里插入图片描述

设置API为24的模板项目:

在这里插入图片描述

初始化项目,自动下载相关依赖:

在这里插入图片描述

完整代码

interface SwordPalette {
  primary: string;
  primaryLight: string;
  primaryDark: string;
  accent: string;
  accentLight: string;
  bg: string;
  cardBg: string;
  cardAlt: string;
  textPrimary: string;
  textSecondary: string;
  textHint: string;
  border: string;
  line: string;
  success: string;
  warning: string;
  danger: string;
  white: string;
  steelBlue: string;
  scabbardBrown: string;
}

const COLORS: SwordPalette = {
  primary: '#4E6E8A',
  primaryLight: '#7E9CB4',
  primaryDark: '#2E3238',
  accent: '#5A4030',
  accentLight: '#C9A24B',
  bg: '#EFF1F2',
  cardBg: '#FFFFFF',
  cardAlt: '#E6E9EC',
  textPrimary: '#26303A',
  textSecondary: '#5E6B77',
  textHint: '#93A0AC',
  border: '#D5DCE2',
  line: '#EBEEF1',
  success: '#5E8A6E',
  warning: '#C9A24B',
  danger: '#B3362E',
  white: '#FFFFFF',
  steelBlue: '#4E6E8A',
  scabbardBrown: '#5A4030'
};

enum SwordTab {
  SWORD = 0,
  SHEATH = 1,
  FORGE = 2,
  ORDER = 3,
  REVIEW = 4
}

interface TabMeta {
  key: string;
  icon: string;
  label: string;
  color: string;
}

const TAB_LIST: TabMeta[] = [
  { key: 'sword', icon: '⚔️', label: '剑器', color: '#4E6E8A' },
  { key: 'sheath', icon: '🛡️', label: '剑鞘', color: '#5A4030' },
  { key: 'forge', icon: '🔥', label: '铸剑', color: '#C9A24B' },
  { key: 'order', icon: '📦', label: '订单', color: '#C9A24B' },
  { key: 'review', icon: '⭐', label: '客评', color: '#B3362E' }
];

const SWORD_COL: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

interface SwordItem {
  name: string;
  style: string;
  steel: string;
  price: number;
  emoji: string;
}

interface SheathItem {
  name: string;
  wood: string;
  level: number;
  craft: string;
  emoji: string;
}

interface ForgeItem {
  name: string;
  style: string;
  level: number;
  tool: string;
  emoji: string;
}

interface SwordOrderItem {
  name: string;
  region: string;
  count: number;
  amount: number;
  emoji: string;
}

interface SwordReviewItem {
  name: string;
  score: number;
  date: string;
  content: string;
  tag: string;
  emoji: string;
}

interface WeekSwordMeta {
  day: string;
  value: number;
}

interface StyleShareMeta {
  name: string;
  count: number;
  color: string;
}

interface ForgeHotMeta {
  name: string;
  heat: number;
  color: string;
}

interface SwordTopMeta {
  name: string;
  sold: number;
  color: string;
}

const WEEK_SOLD: WeekSwordMeta[] = [
  { day: '周一', value: 12 },
  { day: '周二', value: 16 },
  { day: '周三', value: 14 },
  { day: '周四', value: 20 },
  { day: '周五', value: 24 },
  { day: '周六', value: 33 },
  { day: '周日', value: 29 }
];

const STYLE_SHARE: StyleShareMeta[] = [
  { name: '长剑', count: 5, color: '#4E6E8A' },
  { name: '短剑', count: 4, color: '#5A4030' },
  { name: '双手剑', count: 3, color: '#C9A24B' },
  { name: '软剑', count: 2, color: '#B3362E' }
];

const FORGE_HOT: ForgeHotMeta[] = [
  { name: '千锤锻打', heat: 97, color: '#4E6E8A' },
  { name: '淬火成钢', heat: 95, color: '#5A4030' },
  { name: '磨砺开锋', heat: 94, color: '#C9A24B' },
  { name: '装配剑具', heat: 93, color: '#5E8A6E' },
  { name: '折叠锻纹', heat: 92, color: '#B3362E' },
  { name: '养剑防锈', heat: 90, color: '#7E9CB4' }
];

const SWORD_TOP: SwordTopMeta[] = [
  { name: '七星龙渊', sold: 97, color: '#4E6E8A' },
  { name: '湛卢', sold: 93, color: '#5A4030' },
  { name: '泰阿', sold: 90, color: '#C9A24B' },
  { name: '鱼肠', sold: 87, color: '#5E8A6E' },
  { name: '秋霜', sold: 84, color: '#B3362E' },
  { name: '青锋', sold: 81, color: '#7E9CB4' }
];

@Observed
export class SwordData {
  swords: SwordItem[] = [
    { name: '七星龙渊', style: '长剑', steel: '百炼钢', price: 2880, emoji: '⚔️' },
    { name: '湛卢', style: '长剑', steel: '陨铁', price: 3880, emoji: '🌑' },
    { name: '泰阿', style: '长剑', steel: '百炼钢', price: 2680, emoji: '🔥' },
    { name: '鱼肠', style: '短剑', steel: '精钢', price: 1980, emoji: '🐟' },
    { name: '纯钧', style: '长剑', steel: '百炼钢', price: 3280, emoji: '💠' },
    { name: '承影', style: '软剑', steel: '合金钢', price: 2180, emoji: '🌫️' },
    { name: '干将', style: '双手剑', steel: '百炼钢', price: 2980, emoji: '⚡' },
    { name: '莫邪', style: '双手剑', steel: '百炼钢', price: 2980, emoji: '🌙' },
    { name: '龙泉太阿', style: '长剑', steel: '花纹钢', price: 3480, emoji: '🌀' },
    { name: '秋霜', style: '长剑', steel: '精钢', price: 1780, emoji: '❄️' },
    { name: '青锋', style: '短剑', steel: '精钢', price: 1580, emoji: '💙' },
    { name: '倚天', style: '双手剑', steel: '花纹钢', price: 3180, emoji: '⛰️' }
  ];

  sheaths: SheathItem[] = [
    { name: '紫檀剑鞘', wood: '紫檀', level: 96, craft: '素面', emoji: '🪵' },
    { name: '乌木剑鞘', wood: '乌木', level: 95, craft: '嵌银', emoji: '⚫' },
    { name: '花梨剑鞘', wood: '花梨', level: 94, craft: '浮雕', emoji: '🌸' },
    { name: '楠木剑鞘', wood: '金丝楠', level: 95, craft: '描金', emoji: '✨' },
    { name: '牛皮剑鞘', wood: '牛皮', level: 93, craft: '缝线', emoji: '🟤' },
    { name: '鲨皮剑鞘', wood: '鲨鱼皮', level: 94, craft: '包边', emoji: '🦈' },
    { name: '红木剑鞘', wood: '红酸枝', level: 92, craft: '素面', emoji: '🔴' },
    { name: '竹编剑鞘', wood: '竹片', level: 90, craft: '编织', emoji: '🎋' },
    { name: '铜装剑鞘', wood: '铜饰', level: 91, craft: '鎏金', emoji: '🟡' },
    { name: '银装剑鞘', wood: '银饰', level: 93, craft: '錾花', emoji: '🥈' },
    { name: '漆器剑鞘', wood: '大漆', level: 94, craft: '描彩', emoji: '🎨' },
    { name: '缠藤剑鞘', wood: '藤条', level: 89, craft: '缠绕', emoji: '🍃' }
  ];

  forges: ForgeItem[] = [
    { name: '千锤锻打', style: '折叠锻', level: 96, tool: '大锤', emoji: '🔨' },
    { name: '淬火入水', style: '双液淬', level: 95, tool: '火炉', emoji: '💧' },
    { name: '磨砺开锋', style: '镜面磨', level: 94, tool: '磨石', emoji: '🪨' },
    { name: '折叠锻纹', style: '云纹锻', level: 95, tool: '夹钢', emoji: '🌀' },
    { name: '装配剑具', style: '错金装', level: 93, tool: '錾刀', emoji: '🔧' },
    { name: '回火调性', style: '回火', level: 92, tool: '炉膛', emoji: '🔥' },
    { name: '雕刻剑首', style: '镂雕', level: 94, tool: '刻刀', emoji: '🗿' },
    { name: '缠绕剑柄', style: '缠丝', level: 91, tool: '丝绳', emoji: '🪢' },
    { name: '鞘口镶边', style: '包边', level: 92, tool: '锤', emoji: '🛠️' },
    { name: '剑穗编结', style: '编穗', level: 90, tool: '丝绦', emoji: '🎗️' },
    { name: '养剑上油', style: '养剑', level: 89, tool: '油布', emoji: '🧴' },
    { name: '试斩调校', style: '试斩', level: 93, tool: '草席', emoji: '🎯' }
  ];

  orders: SwordOrderItem[] = [
    { name: '武术馆联盟', region: '华东', count: 300, amount: 540000, emoji: '🥋' },
    { name: '影视道具组', region: '华南', count: 120, amount: 300000, emoji: '🎬' },
    { name: '收藏家协会', region: '华北', count: 80, amount: 264000, emoji: '🏛️' },
    { name: '国术学院', region: '华中', count: 200, amount: 360000, emoji: '🎓' },
    { name: '海外武馆', region: '海外', count: 150, amount: 390000, emoji: '🌏' },
    { name: '汉服品牌', region: '华东', count: 180, amount: 288000, emoji: '👘' },
    { name: '景区演艺团', region: '西南', count: 160, amount: 256000, emoji: '🏞️' },
    { name: '文旅街区', region: '西南', count: 220, amount: 264000, emoji: '⛩️' },
    { name: '游戏周边', region: '华南', count: 400, amount: 360000, emoji: '🎮' },
    { name: '博物馆文创', region: '华中', count: 90, amount: 180000, emoji: '🏺' },
    { name: '私人定制', region: '华东', count: 60, amount: 240000, emoji: '💎' },
    { name: '庆典仪仗', region: '华北', count: 100, amount: 150000, emoji: '🎉' }
  ];

  reviews: SwordReviewItem[] = [
    { name: '武馆馆长', score: 5, date: '09-03', content: '七星龙渊重心合度,劈刺迅疾,练了半月虎口不震。', tag: '重心合度', emoji: '🥋' },
    { name: '道具总监', score: 5, date: '09-02', content: '剑身开刃圆润安全,上镜寒光凛凛,剧组反复用。', tag: '上镜凛凛', emoji: '🎬' },
    { name: '藏家', score: 5, date: '09-01', content: '折叠锻纹如云如浪,装具鎏金考究,案头镇宅。', tag: '锻纹如云', emoji: '🏛️' },
    { name: '国术教授', score: 5, date: '08-31', content: '剑脊挺直剑锋薄厚均匀,学生刺靶走位顺。', tag: '剑脊挺直', emoji: '🎓' },
    { name: '海外武师', score: 5, date: '08-30', content: '漂洋过海包装严实,剑鞘木纹温润,物超所值。', tag: '包装严实', emoji: '🌏' },
    { name: '汉服主理', score: 5, date: '08-29', content: '佩剑拍照气场全开,客片预订量翻了一番。', tag: '气场全开', emoji: '👘' },
    { name: '演艺团长', score: 5, date: '08-28', content: '舞台灯光下剑光流转,演出效果炸场。', tag: '舞台炸场', emoji: '🏞️' },
    { name: '街区店主', score: 4, date: '08-27', content: '装饰剑样式足,若附剑架更显档次。', tag: '样式丰富', emoji: '⛩️' },
    { name: '周边策划', score: 4, date: '08-26', content: '复刻游戏剑形准,若出限定色更佳。', tag: '复刻形准', emoji: '🎮' },
    { name: '文创经理', score: 5, date: '08-25', content: '小剑摆件配书签,文创店卖得飞快。', tag: '卖得飞快', emoji: '🏺' },
    { name: '定制客户', score: 5, date: '08-24', content: '刻了名号与生辰,独一无二,爱不释手。', tag: '独一无二', emoji: '💎' },
    { name: '仪仗队长', score: 5, date: '08-23', content: '仪仗剑整齐划一,开合有气势,典礼效果好。', tag: '整齐划一', emoji: '🎉' }
  ];
}

function getStyleColor(style: string): string {
  if (style === '长剑') {
    return '#4E6E8A';
  } else if (style === '短剑') {
    return '#5A4030';
  } else if (style === '双手剑') {
    return '#C9A24B';
  }
  return '#B3362E';
}

function getSheathColor(level: number): string {
  if (level >= 95) {
    return '#4E6E8A';
  } else if (level >= 92) {
    return '#5A4030';
  }
  return '#C9A24B';
}

function getSwordColor(price: number): string {
  if (price >= 3000) {
    return '#B3362E';
  } else if (price >= 2200) {
    return '#4E6E8A';
  } else if (price >= 1700) {
    return '#5A4030';
  }
  return '#7E9CB4';
}

function getOrderColor(amount: number): string {
  if (amount >= 400000) {
    return '#B3362E';
  } else if (amount >= 280000) {
    return '#4E6E8A';
  } else if (amount >= 200000) {
    return '#5A4030';
  }
  return '#7E9CB4';
}

function getReviewTagColor(tag: string): string {
  if (tag === '重心合度' || tag === '锻纹如云' || tag === '舞台炸场' || tag === '独一无二') {
    return '#4E6E8A';
  } else if (tag === '上镜凛凛' || tag === '剑脊挺直' || tag === '包装严实') {
    return '#5A4030';
  } else if (tag === '气场全开' || tag === '复刻形准' || tag === '整齐划一') {
    return '#5E8A6E';
  } else if (tag === '样式丰富' || tag === '卖得飞快') {
    return '#C9A24B';
  }
  return '#B3362E';
}

@Entry
@Component
struct SwordApp {
  @State curTab: number = 0;
  @State data: SwordData = new SwordData();
  @State showAddSword: boolean = false;
  @State showEditOrder: boolean = false;
  @State showDeleteSheath: boolean = false;
  @State showDetailForge: boolean = false;
  @State delSheathName: string = '';
  @State detailForgeName: string = '';
  @State bladeRotate: boolean = false;
  @State shineScale: boolean = false;

  @Builder modalOverlay(onClose: () => void) {
    Column() {
      Text('')
        .width(0)
        .height(0)
        .opacity(0)
      Button('')
        .width(1)
        .height(1)
        .opacity(0)
        .onClick(() => {
          onClose();
        })
    }
    .width(1)
    .height(1)
  }

  build() {
    Column() {
      Column() {
        Column() {
          Row() {
            Column() {
              Text('⚔️ 龙泉剑坊')
                .fontSize(20)
                .fontWeight(FontWeight.Bold)
                .fontColor(COLORS.white)
              Text('Sword · 千锤百炼 剑气如虹')
                .fontSize(10)
                .fontColor(COLORS.accentLight)
                .margin({ top: 3 })
            }
            .alignItems(HorizontalAlign.Start)
            .layoutWeight(1)
            Row() {
              Text('⚔️')
                .fontSize(20)
                .onClick(() => {
                  this.bladeRotate = !this.bladeRotate;
                })
                .rotate({ angle: this.bladeRotate ? 90 : 0 })
                .animation({ duration: 700, curve: Curve.EaseOut })
              Text('✨')
                .fontSize(16)
                .margin({ left: 10 })
                .onClick(() => {
                  this.shineScale = !this.shineScale;
                })
                .scale({ x: this.shineScale ? 1.6 : 1, y: this.shineScale ? 1.6 : 1 })
                .animation({ duration: 620, curve: Curve.EaseOut })
              Text('🗡️')
                .fontSize(14)
                .margin({ left: 6 })
            }
            .padding({ left: 10, right: 10, top: 6, bottom: 6 })
            .backgroundColor(COLORS.accentLight + '2E')
            .borderRadius(24)
            .border({ width: 1, color: COLORS.accentLight + '80' })
          }
          .width('100%')

          Row() {
            ForEach(SWORD_COL, (r: number) => {
              Row() {
                if (r % 3 === 0) {
                  Column()
                    .width(4)
                    .height(14)
                    .backgroundColor(COLORS.steelBlue)
                    .borderRadius(2)
                } else if (r % 3 === 1) {
                  Column()
                    .width(8)
                    .height(8)
                    .backgroundColor(COLORS.danger)
                    .borderRadius(4)
                } else {
                  Column()
                    .width(5)
                    .height(5)
                    .backgroundColor(COLORS.white)
                    .borderRadius(3)
                }
              }
              .width(14)
              .justifyContent(FlexAlign.Center)
            }, (r: number) => 'sword' + r)
          }
          .width('100%')
          .justifyContent(FlexAlign.SpaceBetween)
          .margin({ top: 12 })

          Row() {
            Column()
              .layoutWeight(1)
              .height(1)
              .backgroundColor(COLORS.accentLight + '66')
            Text('🗡️ 剑出龙泉 · 霜刃试锋 ⚔️')
              .fontSize(9)
              .fontColor(COLORS.accentLight)
              .margin({ left: 8, right: 8 })
            Column()
              .layoutWeight(1)
              .height(1)
              .backgroundColor(COLORS.accentLight + '66')
          }
          .width('100%')
          .margin({ top: 10 })
        }
        .width('100%')
        .padding({ left: 16, right: 16, top: 12, bottom: 12 })
        .backgroundColor(COLORS.primaryDark)

        if (this.curTab === SwordTab.SWORD) {
          SwordContent({ data: this.data, onAdd: () => {
            this.showAddSword = true;
          } })
        } else if (this.curTab === SwordTab.SHEATH) {
          SheathContent({ data: this.data, onDel: (n: string) => {
            this.delSheathName = n;
            this.showDeleteSheath = true;
          } })
        } else if (this.curTab === SwordTab.FORGE) {
          ForgeContent({ data: this.data, onDetail: (n: string) => {
            this.detailForgeName = n;
            this.showDetailForge = true;
          } })
        } else if (this.curTab === SwordTab.ORDER) {
          SwordOrderContent({ data: this.data, onEdit: () => {
            this.showEditOrder = true;
          } })
        } else {
          SwordReviewContent({ data: this.data })
        }
      }
      .width('100%')
      .height('100%')

      Column() {
        Column()
          .width('100%')
          .height(3)
          .backgroundColor(COLORS.primary)
        Row() {
          ForEach(TAB_LIST, (t: TabMeta) => {
            Column() {
              Text(t.icon)
                .fontSize(19)
              Text(t.label)
                .fontSize(10)
                .fontColor(this.curTab === TAB_LIST.indexOf(t) ? t.color : COLORS.textHint)
                .fontWeight(this.curTab === TAB_LIST.indexOf(t) ? FontWeight.Bold : FontWeight.Normal)
                .margin({ top: 2 })
            }
            .layoutWeight(1)
            .justifyContent(FlexAlign.Center)
            .padding({ top: 7, bottom: 7 })
            .backgroundColor(this.curTab === TAB_LIST.indexOf(t) ? t.color + '14' : '#00000000')
            .borderRadius(12)
            .border(this.curTab === TAB_LIST.indexOf(t) ? { width: 1, color: t.color } : { width: 0 })
            .onClick(() => {
              this.curTab = TAB_LIST.indexOf(t);
            })
          }, (t: TabMeta) => t.key)
        }
        .width('100%')
        .height(60)
        .padding({ left: 8, right: 8 })
        .backgroundColor(COLORS.cardBg)
      }
      .width('100%')

      if (this.showAddSword) {
        AddSwordModal({
          onClose: () => {
            this.showAddSword = false;
          }
        })
      }
      if (this.showEditOrder) {
        EditSwordOrderModal({
          onClose: () => {
            this.showEditOrder = false;
          }
        })
      }
      if (this.showDeleteSheath) {
        DeleteSheathModal({
          title: this.delSheathName, onClose: () => {
            this.showDeleteSheath = false;
          }
        })
      }
      if (this.showDetailForge) {
        DetailForgeModal({
          name: this.detailForgeName, onClose: () => {
            this.showDetailForge = false;
          }
        })
      }
    }
  }
}

@Component
struct SwordTag {
  @Prop text: string;
  @Prop color: string;

  build() {
    Text(this.text)
      .fontSize(9)
      .fontColor(this.color)
      .padding({ left: 7, right: 7, top: 2, bottom: 2 })
      .backgroundColor(this.color + '14')
      .borderRadius(10)
      .border({ width: 1, color: this.color + '40' })
  }
}

@Component
struct SwordContent {
  @ObjectLink data: SwordData;
  onAdd: () => void = () => {};

  build() {
    Scroll() {
      Column() {
        Column() {
          Row() {
            Column()
              .width(4)
              .height(16)
              .backgroundColor(COLORS.steelBlue)
              .borderRadius(2)
            Column()
              .layoutWeight(1)
              .height(2)
              .backgroundColor(COLORS.primary + '55')
              .margin({ left: 4, right: 4 })
            Column()
              .width(8)
              .height(8)
              .backgroundColor(COLORS.danger)
              .borderRadius(4)
              .margin({ right: 4 })
            Column()
              .width(4)
              .height(16)
              .backgroundColor(COLORS.steelBlue)
              .borderRadius(2)
            Column()
              .layoutWeight(1)
              .height(2)
              .backgroundColor(COLORS.primary + '55')
              .margin({ left: 4, right: 4 })
            Column()
              .width(4)
              .height(16)
              .backgroundColor(COLORS.steelBlue)
              .borderRadius(2)
          }
          .width('100%')
          .margin({ bottom: 10 })
          Row() {
            Text('📈 本周剑器销量')
              .fontSize(13)
              .fontWeight(FontWeight.Bold)
              .fontColor(COLORS.textPrimary)
            Text('周六33柄')
              .fontSize(9)
              .fontColor(COLORS.primary)
              .margin({ left: 8 })
          }
          .width('100%')
          .justifyContent(FlexAlign.SpaceBetween)
          Row() {
            ForEach(WEEK_SOLD, (w: WeekSwordMeta) => {
              Column() {
                Text(w.value + '')
                  .fontSize(8)
                  .fontColor(w.value >= 29 ? COLORS.primary : COLORS.accentLight)
                Column()
                  .width(16)
                  .height(w.value)
                  .backgroundColor(w.value >= 29 ? COLORS.primary : COLORS.accentLight)
                  .borderRadius(8)
                  .margin({ top: 4 })
                Text(w.day)
                  .fontSize(9)
                  .fontColor(COLORS.textSecondary)
                  .margin({ top: 4 })
              }
              .layoutWeight(1)
              .alignItems(HorizontalAlign.Center)
            }, (w: WeekSwordMeta) => w.day)
          }
          .width('100%')
          .height(150)
          .alignItems(VerticalAlign.Bottom)
          .margin({ top: 10 })
        }
        .width('100%')
        .padding(14)
        .backgroundColor(COLORS.cardBg)
        .borderRadius(20)
        .border({ width: 1, color: COLORS.primary + '55' })

        Column() {
          Row() {
            Text('⚔️ 名剑陈列')
              .fontSize(13)
              .fontWeight(FontWeight.Bold)
              .fontColor(COLORS.textPrimary)
            Text('点击➕铸剑')
              .fontSize(9)
              .fontColor(COLORS.textHint)
              .margin({ left: 8 })
            Text('➕')
              .fontSize(14)
              .margin({ left: 8 })
              .onClick(() => {
                this.onAdd();
              })
          }
          .width('100%')
          .justifyContent(FlexAlign.SpaceBetween)
          .margin({ bottom: 10 })
          ForEach(this.data.swords, (p: SwordItem, i: number) => {
            Row() {
              Column() {
                Column() {
                  Text(p.emoji)
                    .fontSize(18)
                    .textAlign(TextAlign.Center)
                }
                .width(40)
                .height(44)
                .justifyContent(FlexAlign.Center)
                .alignItems(HorizontalAlign.Center)
                .backgroundColor(getSwordColor(p.price) + '14')
                .borderRadius(4)
                .border({ width: 1, color: getSwordColor(p.price) + '66' })
                Column()
                  .width(20)
                  .height(2)
                  .backgroundColor(getSwordColor(p.price))
                  .borderRadius(1)
                  .margin({ top: 5 })
              }
              .width(56)
              .padding({ top: 10, bottom: 6 })
              .backgroundColor(getSwordColor(p.price) + '0D')
              .borderRadius(4)
              .border({ width: 1, color: getSwordColor(p.price) + '33' })

              Column() {
                Row() {
                  Text(p.name)
                    .fontSize(13)
                    .fontWeight(FontWeight.Bold)
                    .fontColor(COLORS.textPrimary)
                  Text('¥' + p.price)
                    .fontSize(10)
                    .fontWeight(FontWeight.Bold)
                    .fontColor(getSwordColor(p.price))
                    .margin({ left: 8 })
                }
                .width('100%')
                .justifyContent(FlexAlign.SpaceBetween)
                Row() {
                  SwordTag({ text: p.style, color: getStyleColor(p.style) })
                  SwordTag({ text: p.steel, color: COLORS.accentLight })
                  Text('龙泉铸造')
                    .fontSize(9)
                    .fontColor(COLORS.textHint)
                    .margin({ left: 8 })
                }
                .width('100%')
                .justifyContent(FlexAlign.Start)
                .margin({ top: 6 })
              }
              .layoutWeight(1)
              .alignItems(HorizontalAlign.Start)
              .margin({ left: 10 })
            }
            .width('100%')
            .padding(10)
            .backgroundColor(i % 2 === 0 ? COLORS.cardBg : COLORS.cardAlt)
            .borderRadius(18)
            .border({ width: 1, color: COLORS.line })
            .margin({ top: 8 })
          }, (p: SwordItem, i: number) => 'sw' + p.name + i)
        }
        .width('100%')
        .margin({ top: 12 })
      }
      .width('100%')
      .constraintSize({ maxHeight: '100%' })
    }
    .width('100%')
    .constraintSize({ maxHeight: '100%' })
    .scrollable(ScrollDirection.Vertical)
    .scrollBar(BarState.Off)
  }
}

@Component
struct SheathContent {
  @ObjectLink data: SwordData;
  onDel: (n: string) => void = () => {};

  build() {
    Scroll() {
      Column() {
        Column() {
          Row() {
            Text('🏆 名剑销量 TOP6')
              .fontSize(13)
              .fontWeight(FontWeight.Bold)
              .fontColor(COLORS.textPrimary)
            Text('七星龙渊居首')
              .fontSize(9)
              .fontColor(COLORS.textHint)
              .margin({ left: 8 })
          }
          .width('100%')
          .justifyContent(FlexAlign.SpaceBetween)
          .margin({ bottom: 10 })
          ForEach(SWORD_TOP, (t: SwordTopMeta) => {
            Row() {
              Text(t.name)
                .fontSize(10)
                .fontColor(COLORS.textPrimary)
                .width(90)
              Row() {
                Column()
                  .layoutWeight(t.sold)
                  .height(12)
                  .backgroundColor(t.color)
                  .borderRadius(6)
                Column()
                  .layoutWeight(100 - t.sold)
                  .height(12)
                  .backgroundColor(COLORS.line)
                  .borderRadius(6)
              }
              .layoutWeight(1)
              Text(t.sold + '柄')
                .fontSize(9)
                .fontColor(t.color)
                .width(40)
                .textAlign(TextAlign.End)
            }
            .width('100%')
            .margin({ top: 6 })
          }, (t: SwordTopMeta) => t.name)
        }
        .width('100%')
        .padding(14)
        .backgroundColor(COLORS.cardBg)
        .borderRadius(20)
        .border({ width: 1, color: COLORS.primary + '55' })

        Column() {
          Row() {
            Text('🛡️ 剑鞘库')
              .fontSize(13)
              .fontWeight(FontWeight.Bold)
              .fontColor(COLORS.textPrimary)
            Text('点击🗑撤鞘')
              .fontSize(9)
              .fontColor(COLORS.textHint)
              .margin({ left: 8 })
          }
          .width('100%')
          .justifyContent(FlexAlign.SpaceBetween)
          .margin({ bottom: 10 })
          ForEach(this.data.sheaths, (c: SheathItem, i: number) => {
            Row() {
              Column() {
                Text(c.emoji)
                  .fontSize(18)
                  .textAlign(TextAlign.Center)
                Row() {
                  Column()
                    .width(10)
                    .height(10)
                    .backgroundColor(getSheathColor(c.level))
                    .borderRadius(5)
                    .border({ width: 2, color: COLORS.white })
                  Column()
                    .width(8)
                    .height(8)
                    .backgroundColor(COLORS.accentLight)
                    .borderRadius(4)
                    .margin({ left: 4 })
                }
                .width(28)
                .justifyContent(FlexAlign.Center)
                .margin({ top: 5 })
              }
              .width(56)
              .padding({ top: 10, bottom: 6 })
              .backgroundColor(getSheathColor(c.level) + '0D')
              .borderRadius(16)
              .border({ width: 1, color: getSheathColor(c.level) + '33' })

              Column() {
                Row() {
                  Text(c.name)
                    .fontSize(13)
                    .fontWeight(FontWeight.Bold)
                    .fontColor(COLORS.textPrimary)
                  Text(c.craft)
                    .fontSize(9)
                    .fontColor(COLORS.textSecondary)
                    .margin({ left: 8 })
                }
                .width('100%')
                .justifyContent(FlexAlign.SpaceBetween)
                Row() {
                  Text('材质')
                    .fontSize(9)
                    .fontColor(COLORS.textHint)
                  Row() {
                    Column()
                      .layoutWeight(c.level)
                      .height(6)
                      .backgroundColor(getSheathColor(c.level))
                      .borderRadius(3)
                    Column()
                      .layoutWeight(100 - c.level)
                      .height(6)
                      .backgroundColor(COLORS.line)
                      .borderRadius(3)
                  }
                  .layoutWeight(1)
                  .margin({ left: 6 })
                }
                .width('100%')
                .margin({ top: 6 })
              }
              .layoutWeight(1)
              .alignItems(HorizontalAlign.Start)
              .margin({ left: 10 })

              Text('🗑️')
                .fontSize(14)
                .onClick(() => {
                  this.onDel(c.name);
                })
            }
            .width('100%')
            .padding(10)
            .backgroundColor(i % 2 === 0 ? COLORS.cardBg : COLORS.cardAlt)
            .borderRadius(18)
            .border({ width: 1, color: COLORS.line })
            .margin({ top: 8 })
          }, (c: SheathItem, i: number) => 'sh' + c.name + i)
        }
        .width('100%')
        .margin({ top: 12 })
      }
      .width('100%')
      .constraintSize({ maxHeight: '100%' })
    }
    .width('100%')
    .constraintSize({ maxHeight: '100%' })
    .scrollable(ScrollDirection.Vertical)
    .scrollBar(BarState.Off)
  }
}

@Component
struct ForgeContent {
  @ObjectLink data: SwordData;
  onDetail: (n: string) => void = () => {};

  build() {
    Scroll() {
      Column() {
        Column() {
          Row() {
            Text('🔥 铸剑工艺热度')
              .fontSize(13)
              .fontWeight(FontWeight.Bold)
              .fontColor(COLORS.textPrimary)
            Text('千锤锻打97')
              .fontSize(9)
              .fontColor(COLORS.primary)
              .margin({ left: 8 })
          }
          .width('100%')
          .justifyContent(FlexAlign.SpaceBetween)
          .margin({ bottom: 10 })
          ForEach(FORGE_HOT, (d: ForgeHotMeta) => {
            Row() {
              Text(d.name)
                .fontSize(10)
                .fontColor(COLORS.textPrimary)
                .width(80)
              Row() {
                Column()
                  .layoutWeight(d.heat)
                  .height(14)
                  .backgroundColor(d.color)
                  .borderRadius(7)
                Column()
                  .layoutWeight(100 - d.heat)
                  .height(14)
                  .backgroundColor(COLORS.line)
                  .borderRadius(7)
              }
              .layoutWeight(1)
              Text(d.heat + '')
                .fontSize(9)
                .fontColor(d.color)
                .width(32)
                .textAlign(TextAlign.End)
            }
            .width('100%')
            .margin({ top: 6 })
          }, (d: ForgeHotMeta) => d.name)
        }
        .width('100%')
        .padding(14)
        .backgroundColor(COLORS.cardBg)
        .borderRadius(20)
        .border({ width: 1, color: COLORS.primary + '55' })

        Column() {
          Row() {
            Text('🔥 铸剑工序')
              .fontSize(13)
              .fontWeight(FontWeight.Bold)
              .fontColor(COLORS.textPrimary)
            Text('点击👁️了解')
              .fontSize(9)
              .fontColor(COLORS.textHint)
              .margin({ left: 8 })
          }
          .width('100%')
          .justifyContent(FlexAlign.SpaceBetween)
          .margin({ bottom: 10 })
          ForEach(this.data.forges, (d: ForgeItem, i: number) => {
            Row() {
              Column() {
                Text(d.emoji)
                  .fontSize(18)
                  .textAlign(TextAlign.Center)
                Column()
                  .width(14)
                  .height(14)
                  .backgroundColor(getSheathColor(d.level))
                  .borderRadius(7)
                  .border({ width: 2, color: COLORS.white })
                  .margin({ top: 5 })
              }
              .width(56)
              .padding({ top: 10, bottom: 6 })
              .backgroundColor(getSheathColor(d.level) + '0D')
              .borderRadius(16)
              .border({ width: 1, color: getSheathColor(d.level) + '33' })

              Column() {
                Row() {
                  Text(d.name)
                    .fontSize(13)
                    .fontWeight(FontWeight.Bold)
                    .fontColor(COLORS.textPrimary)
                  Text(d.style)
                    .fontSize(9)
                    .fontColor(getSheathColor(d.level))
                    .margin({ left: 8 })
                }
                .width('100%')
                .justifyContent(FlexAlign.SpaceBetween)
                Row() {
                  SwordTag({ text: d.tool + '法', color: COLORS.textSecondary })
                  Text('工级' + d.level)
                    .fontSize(9)
                    .fontColor(COLORS.textHint)
                    .margin({ left: 8 })
                }
                .width('100%')
                .justifyContent(FlexAlign.Start)
                .margin({ top: 6 })
              }
              .layoutWeight(1)
              .alignItems(HorizontalAlign.Start)
              .margin({ left: 10 })

              Text('👁️')
                .fontSize(14)
                .onClick(() => {
                  this.onDetail(d.name);
                })
            }
            .width('100%')
            .padding(10)
            .backgroundColor(i % 2 === 0 ? COLORS.cardBg : COLORS.cardAlt)
            .borderRadius(18)
            .border({ width: 1, color: COLORS.line })
            .margin({ top: 8 })
          }, (d: ForgeItem, i: number) => 'fg' + d.name + i)
        }
        .width('100%')
        .margin({ top: 12 })
      }
      .width('100%')
      .constraintSize({ maxHeight: '100%' })
    }
    .width('100%')
    .constraintSize({ maxHeight: '100%' })
    .scrollable(ScrollDirection.Vertical)
    .scrollBar(BarState.Off)
  }
}

@Component
struct SwordOrderContent {
  @ObjectLink data: SwordData;
  onEdit: () => void = () => {};

  build() {
    Scroll() {
      Column() {
        Column() {
          Row() {
            Text('🧩 剑式构成')
              .fontSize(13)
              .fontWeight(FontWeight.Bold)
              .fontColor(COLORS.textPrimary)
            Text('长剑占比最高')
              .fontSize(9)
              .fontColor(COLORS.textHint)
              .margin({ left: 8 })
          }
          .width('100%')
          .justifyContent(FlexAlign.SpaceBetween)
          .margin({ bottom: 10 })
          ForEach(STYLE_SHARE, (s: StyleShareMeta) => {
            Row() {
              Column()
                .width(8)
                .height(8)
                .backgroundColor(s.color)
                .borderRadius(4)
              Text(s.name)
                .fontSize(10)
                .fontColor(COLORS.textPrimary)
                .margin({ left: 6 })
              Text('×' + s.count)
                .fontSize(9)
                .fontColor(COLORS.textSecondary)
                .margin({ left: 4 })
              Row() {
                Column()
                  .layoutWeight(s.count)
                  .height(10)
                  .backgroundColor(s.color)
                  .borderRadius(5)
                Column()
                  .layoutWeight(20 - s.count)
                  .height(10)
                  .backgroundColor(COLORS.line)
                  .borderRadius(5)
              }
              .layoutWeight(1)
              .margin({ left: 10 })
            }
            .width('100%')
            .margin({ top: 6 })
          }, (s: StyleShareMeta) => s.name)
        }
        .width('100%')
        .padding(14)
        .backgroundColor(COLORS.cardBg)
        .borderRadius(20)
        .border({ width: 1, color: COLORS.primary + '55' })

        Column() {
          Row() {
            Text('📦 订单台账')
              .fontSize(13)
              .fontWeight(FontWeight.Bold)
              .fontColor(COLORS.textPrimary)
            Text('点击✏️改单')
              .fontSize(9)
              .fontColor(COLORS.textHint)
              .margin({ left: 8 })
            Text('✏️')
              .fontSize(14)
              .margin({ left: 8 })
              .onClick(() => {
                this.onEdit();
              })
          }
          .width('100%')
          .justifyContent(FlexAlign.SpaceBetween)
          .margin({ bottom: 10 })
          ForEach(this.data.orders, (o: SwordOrderItem, i: number) => {
            Row() {
              Text(o.emoji)
                .fontSize(16)
              Column() {
                Text(o.name)
                  .fontSize(12)
                  .fontWeight(FontWeight.Bold)
                  .fontColor(COLORS.textPrimary)
                Text(o.region + ' · ' + o.count + '柄')
                  .fontSize(9)
                  .fontColor(COLORS.textSecondary)
                  .margin({ top: 2 })
              }
              .alignItems(HorizontalAlign.Start)
              .layoutWeight(1)
              .margin({ left: 10 })
              Text('¥' + o.amount)
                .fontSize(12)
                .fontWeight(FontWeight.Bold)
                .fontColor(getOrderColor(o.amount))
              Text('✏️')
                .fontSize(12)
                .margin({ left: 10 })
                .onClick(() => {
                  this.onEdit();
                })
            }
            .width('100%')
            .padding(10)
            .backgroundColor(i % 2 === 0 ? COLORS.cardBg : COLORS.cardAlt)
            .borderRadius(18)
            .border({ width: 1, color: COLORS.line })
            .margin({ top: 8 })
          }, (o: SwordOrderItem, i: number) => 'od' + o.name + i)
        }
        .width('100%')
        .margin({ top: 12 })
      }
      .width('100%')
      .constraintSize({ maxHeight: '100%' })
    }
    .width('100%')
    .constraintSize({ maxHeight: '100%' })
    .scrollable(ScrollDirection.Vertical)
    .scrollBar(BarState.Off)
  }
}

@Component
struct SwordReviewContent {
  @ObjectLink data: SwordData;

  build() {
    Scroll() {
      Column() {
        Row() {
          Text('⭐ 客评口碑')
            .fontSize(13)
            .fontWeight(FontWeight.Bold)
            .fontColor(COLORS.textPrimary)
          Text('12条好评 · 平均4.9')
            .fontSize(9)
            .fontColor(COLORS.textHint)
            .margin({ left: 8 })
        }
        .width('100%')
        .justifyContent(FlexAlign.SpaceBetween)
        .margin({ bottom: 6 })
        ForEach(this.data.reviews, (r: SwordReviewItem, i: number) => {
          Column() {
            Row() {
              Text(r.emoji)
                .fontSize(15)
              Column() {
                Text(r.name)
                  .fontSize(12)
                  .fontWeight(FontWeight.Bold)
                  .fontColor(COLORS.textPrimary)
                Text(r.date)
                  .fontSize(9)
                  .fontColor(COLORS.textHint)
                  .margin({ top: 1 })
              }
              .alignItems(HorizontalAlign.Start)
              .layoutWeight(1)
              .margin({ left: 8 })
              Text('⭐'.repeat(r.score))
                .fontSize(10)
                .fontColor(COLORS.warning)
              SwordTag({ text: r.tag, color: getReviewTagColor(r.tag) })
            }
            .width('100%')
            Text(r.content)
              .fontSize(11)
              .fontColor(COLORS.textSecondary)
              .lineHeight(16)
              .margin({ top: 6 })
          }
          .width('100%')
          .padding(12)
          .backgroundColor(i % 2 === 0 ? COLORS.cardBg : COLORS.cardAlt)
          .borderRadius(18)
          .border({ width: 1, color: COLORS.line })
          .margin({ top: 8 })
        }, (r: SwordReviewItem, i: number) => 'rv' + r.name + i)
      }
      .width('100%')
      .constraintSize({ maxHeight: '100%' })
    }
    .width('100%')
    .constraintSize({ maxHeight: '100%' })
    .scrollable(ScrollDirection.Vertical)
    .scrollBar(BarState.Off)
  }
}

@Component
struct AddSwordModal {
  onClose: () => void = () => {};

  build() {
    Column() {
      Column() {
        Row() {
          Text('⚔️ 定制剑器')
            .fontSize(16)
            .fontWeight(FontWeight.Bold)
            .fontColor(COLORS.textPrimary)
          Text('✕')
            .fontSize(16)
            .fontColor(COLORS.textHint)
            .onClick(() => {
              this.onClose();
            })
        }
        .width('100%')
        .justifyContent(FlexAlign.SpaceBetween)
        Row() {
          Text('剑名')
            .fontSize(11)
            .fontColor(COLORS.textSecondary)
            .width(56)
          Text('七星龙渊')
            .fontSize(11)
            .fontColor(COLORS.textPrimary)
        }
        .width('100%')
        .padding(10)
        .backgroundColor(COLORS.cardAlt)
        .borderRadius(10)
        .margin({ top: 14 })
        Row() {
          Text('剑式')
            .fontSize(11)
            .fontColor(COLORS.textSecondary)
            .width(56)
          Text('长剑 · 百炼钢 · 龙泉铸造')
            .fontSize(11)
            .fontColor(COLORS.textPrimary)
        }
        .width('100%')
        .padding(10)
        .backgroundColor(COLORS.cardAlt)
        .borderRadius(10)
        .margin({ top: 8 })
        Row() {
          Text('预算')
            .fontSize(11)
            .fontColor(COLORS.textSecondary)
            .width(56)
          Text('¥2880')
            .fontSize(11)
            .fontColor(COLORS.primary)
        }
        .width('100%')
        .padding(10)
        .backgroundColor(COLORS.cardAlt)
        .borderRadius(10)
        .margin({ top: 8 })
        Row() {
          Text('取消')
            .fontSize(12)
            .fontColor(COLORS.textSecondary)
            .padding({ left: 16, right: 16, top: 7, bottom: 7 })
            .borderRadius(12)
            .border({ width: 1, color: COLORS.border })
            .onClick(() => {
              this.onClose();
            })
          Text('确认定制')
            .fontSize(12)
            .fontWeight(FontWeight.Bold)
            .fontColor(COLORS.white)
            .padding({ left: 16, right: 16, top: 7, bottom: 7 })
            .backgroundColor(COLORS.primary)
            .borderRadius(12)
            .onClick(() => {
              this.onClose();
            })
        }
        .width('100%')
        .justifyContent(FlexAlign.End)
        .margin({ top: 14 })
      }
      .width('86%')
      .padding(16)
      .backgroundColor(COLORS.cardBg)
      .borderRadius(18)
      .constraintSize({ maxHeight: '78%' })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .backgroundColor('#66000000')
  }
}

@Component
struct EditSwordOrderModal {
  onClose: () => void = () => {};

  build() {
    Column() {
      Column() {
        Row() {
          Text('✏️ 修改订单')
            .fontSize(16)
            .fontWeight(FontWeight.Bold)
            .fontColor(COLORS.textPrimary)
          Text('✕')
            .fontSize(16)
            .fontColor(COLORS.textHint)
            .onClick(() => {
              this.onClose();
            })
        }
        .width('100%')
        .justifyContent(FlexAlign.SpaceBetween)
        Row() {
          Text('客户')
            .fontSize(11)
            .fontColor(COLORS.textSecondary)
            .width(56)
          Text('武术馆联盟')
            .fontSize(11)
            .fontColor(COLORS.textPrimary)
        }
        .width('100%')
        .padding(10)
        .backgroundColor(COLORS.cardAlt)
        .borderRadius(10)
        .margin({ top: 14 })
        Row() {
          Text('数量')
            .fontSize(11)
            .fontColor(COLORS.textSecondary)
            .width(56)
          Text('300 → 330 柄')
            .fontSize(11)
            .fontColor(COLORS.primary)
        }
        .width('100%')
        .padding(10)
        .backgroundColor(COLORS.cardAlt)
        .borderRadius(10)
        .margin({ top: 8 })
        Row() {
          Text('金额')
            .fontSize(11)
            .fontColor(COLORS.textSecondary)
            .width(56)
          Text('¥594000')
            .fontSize(11)
            .fontColor(COLORS.accent)
        }
        .width('100%')
        .padding(10)
        .backgroundColor(COLORS.cardAlt)
        .borderRadius(10)
        .margin({ top: 8 })
        Row() {
          Text('取消')
            .fontSize(12)
            .fontColor(COLORS.textSecondary)
            .padding({ left: 16, right: 16, top: 7, bottom: 7 })
            .borderRadius(12)
            .border({ width: 1, color: COLORS.border })
            .onClick(() => {
              this.onClose();
            })
          Text('保存修改')
            .fontSize(12)
            .fontWeight(FontWeight.Bold)
            .fontColor(COLORS.white)
            .padding({ left: 16, right: 16, top: 7, bottom: 7 })
            .backgroundColor(COLORS.primary)
            .borderRadius(12)
            .onClick(() => {
              this.onClose();
            })
        }
        .width('100%')
        .justifyContent(FlexAlign.End)
        .margin({ top: 14 })
      }
      .width('86%')
      .padding(16)
      .backgroundColor(COLORS.cardBg)
      .borderRadius(18)
      .constraintSize({ maxHeight: '78%' })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .backgroundColor('#66000000')
  }
}

@Component
struct DeleteSheathModal {
  @Prop title: string;
  onClose: () => void = () => {};

  build() {
    Column() {
      Column() {
        Text('🗑️ 撤下剑鞘')
          .fontSize(16)
          .fontWeight(FontWeight.Bold)
          .fontColor(COLORS.textPrimary)
        Text('确定撤下「' + this.title + '」吗?')
          .fontSize(12)
          .fontColor(COLORS.textSecondary)
          .margin({ top: 10 })
        Text('撤下的剑鞘将从鞘库移除。')
          .fontSize(10)
          .fontColor(COLORS.textHint)
          .margin({ top: 4 })
        Row() {
          Text('再想想')
            .fontSize(12)
            .fontColor(COLORS.textSecondary)
            .padding({ left: 16, right: 16, top: 7, bottom: 7 })
            .borderRadius(12)
            .border({ width: 1, color: COLORS.border })
            .onClick(() => {
              this.onClose();
            })
          Text('确认撤下')
            .fontSize(12)
            .fontWeight(FontWeight.Bold)
            .fontColor(COLORS.white)
            .padding({ left: 16, right: 16, top: 7, bottom: 7 })
            .backgroundColor(COLORS.danger)
            .borderRadius(12)
            .onClick(() => {
              this.onClose();
            })
        }
        .width('100%')
        .justifyContent(FlexAlign.End)
        .margin({ top: 16 })
      }
      .width('86%')
      .padding(16)
      .backgroundColor(COLORS.cardBg)
      .borderRadius(18)
      .constraintSize({ maxHeight: '78%' })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .backgroundColor('#66000000')
  }
}

@Component
struct DetailForgeModal {
  @Prop name: string;
  onClose: () => void = () => {};

  build() {
    Column() {
      Column() {
        Row() {
          Text('👁️ 工序详情')
            .fontSize(16)
            .fontWeight(FontWeight.Bold)
            .fontColor(COLORS.textPrimary)
          Text('✕')
            .fontSize(16)
            .fontColor(COLORS.textHint)
            .onClick(() => {
              this.onClose();
            })
        }
        .width('100%')
        .justifyContent(FlexAlign.SpaceBetween)
        Column() {
          Text('🔥')
            .fontSize(30)
          Text(this.name)
            .fontSize(14)
            .fontWeight(FontWeight.Bold)
            .fontColor(COLORS.textPrimary)
            .margin({ top: 6 })
        }
        .width('100%')
        .padding(14)
        .backgroundColor(COLORS.cardAlt)
        .borderRadius(12)
        .margin({ top: 12 })
        Text('龙泉铸剑,钢入炉中烧至杏黄,师徒对锤千余锻,去杂存精。淬火凭水温之变定剑之刚柔,磨砺则用粗细砥石层层递进,直至刃口映出寒光。折叠锻纹如流水千层,剑成之日悬于堂前,待霜锋养定。')
          .fontSize(11)
          .fontColor(COLORS.textSecondary)
          .lineHeight(17)
          .margin({ top: 10 })
        Row() {
          Text('技法')
            .fontSize(11)
            .fontColor(COLORS.textSecondary)
            .width(56)
          Text('折叠锻 · 大锤法')
            .fontSize(11)
            .fontColor(COLORS.textPrimary)
        }
        .width('100%')
        .padding(10)
        .backgroundColor(COLORS.cardAlt)
        .borderRadius(10)
        .margin({ top: 8 })
        Row() {
          Text('匠人评级')
            .fontSize(11)
            .fontColor(COLORS.textSecondary)
            .width(56)
          Text('甲级 · 铸剑卅载')
            .fontSize(11)
            .fontColor(COLORS.primary)
        }
        .width('100%')
        .padding(10)
        .backgroundColor(COLORS.cardAlt)
        .borderRadius(10)
        .margin({ top: 8 })
        Text('知道了')
          .fontSize(12)
          .fontWeight(FontWeight.Bold)
          .fontColor(COLORS.white)
          .padding({ left: 18, right: 18, top: 7, bottom: 7 })
          .backgroundColor(COLORS.primary)
          .borderRadius(12)
          .margin({ top: 12 })
          .onClick(() => {
            this.onClose();
          })
      }
      .width('86%')
      .padding(16)
      .backgroundColor(COLORS.cardBg)
      .borderRadius(18)
      .constraintSize({ maxHeight: '78%' })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .backgroundColor('#66000000')
  }
}
Logo

作为“人工智能6S店”的官方数字引擎,为AI开发者与企业提供一个覆盖软硬件全栈、一站式门户。

更多推荐