个性化推荐类应用的核心挑战在于“商品标签体系可视化、用户偏好精准映射、跨端交互一致性”,而 React Native 凭借其“一次开发、多端部署”的技术特性,成为连接 iOS、Android 与鸿蒙(HarmonyOS)系统的最优技术选型。本文以个性化商品推荐应用为例,从商品标签体系设计、鸿蒙风格 UI 实现、个性化交互逻辑等核心维度,深度解析 React Native 对接鸿蒙系统的技术内核与商品推荐场景的落地最佳实践。

本个性化推荐应用聚焦商品标签筛选、个性化展示、收藏管理、快速购买等核心电商推荐场景,覆盖推荐、热销、新品、限时、特价等多维度商品标签体系,整体架构遵循 React Native 组件化开发范式,同时深度契合鸿蒙系统的设计语言与电商推荐类应用的交互规范。从技术底层来看,应用基于 React 函数式组件 + TypeScript 构建,这种组合既保证了商品推荐数据的类型安全,又能最大化跨端复用率,是 React Native 适配鸿蒙系统电商推荐类应用的理想技术底座。

1. 数据模型:

个性化推荐类应用涉及多维度的商品属性(标签、收藏状态、价格体系等),统一且精准的强类型数据模型是避免多端行为不一致的关键。代码中通过 TypeScript 严格定义了个性化商品的核心数据结构,覆盖电商推荐全场景的业务属性:

// 个性化商品核心数据模型
type PersonalizedProduct = {
  id: string;
  name: string;
  category: string;
  price: number;
  originalPrice?: number;
  image: string;
  description: string;
  tag?: '推荐' | '热销' | '新品' | '限时' | '特价'; // 商品标签
  isFavorite?: boolean; // 是否收藏
};

这种场景化的强类型定义不仅在开发阶段提供语法校验和智能提示,更关键的是在鸿蒙系统适配时,能够与 ArkTS 的类型系统形成天然映射。相较于纯 JavaScript 开发,TypeScript 可有效规避因数据类型模糊导致的商品标签展示错误、价格计算偏差等问题——尤其是在鸿蒙这类面向全场景智慧终端的操作系统中,类型安全能大幅降低多设备适配的调试成本,确保商品标签、价格、收藏状态等核心推荐数据在不同终端的一致性。

2. 状态管理:

应用采用 React 内置的 useState Hook 管理核心商品推荐状态,结合不可变数据模式实现商品信息的跨端展示与动态交互:

const [products] = useState<PersonalizedProduct[]>([
  {
    id: '1',
    name: '无线蓝牙耳机',
    category: '数码配件',
    price: 199,
    originalPrice: 299,
    image: '🎧',
    description: '主动降噪,续航20小时',
    tag: '推荐',
    isFavorite: true
  },
  // 其他个性化商品数据
]);

这种轻量级状态管理方案完全适配商品推荐类跨端开发场景,相较于 Redux 等重型状态库,useState 无需额外的中间件和适配层,能够直接在 React Native 支持的所有平台(包括鸿蒙)上稳定运行。从鸿蒙系统的视角来看,useState 的状态管理逻辑与 ArkUI 的 @State 装饰器在设计理念上高度契合,开发者无需切换思维模式即可完成跨端状态管理,大幅降低了鸿蒙适配的学习成本。


React Native 的核心价值在于通过统一的组件抽象层,屏蔽不同平台的 UI 实现差异。本应用在 UI 层的设计深度复刻了鸿蒙系统的视觉风格与电商推荐类应用交互规范,同时保证多端体验的一致性,尤其针对商品标签、价格体系、收藏按钮等核心元素做了专属适配。

1. Flex 布局

应用基于 React Native 的 Flex 布局系统构建整体界面,通过 Dimensions.get('window') 获取设备宽高,实现对不同尺寸鸿蒙设备(手机、平板、智慧屏)的自适应:

const { width, height } = Dimensions.get('window');

相较于鸿蒙系统的 DirectionalLayoutGridLayout,React Native 的 Flex 布局具备更强的跨端兼容性,通过 flexDirectionjustifyContentposition 等属性,能够精准还原鸿蒙系统的个性化推荐界面布局逻辑。例如商品卡片的布局实现,结合绝对定位实现收藏按钮的悬浮展示:

<View key={product.id} style={styles.productCard}>
  <TouchableOpacity 
    style={styles.favoriteButton}
    onPress={() => toggleFavorite(product.id)}
  >
    <Text style={styles.favoriteIcon}>
      {product.isFavorite ? '❤️' : '🤍'}
    </Text>
  </TouchableOpacity>
  
  <Text style={styles.productImage}>{product.image}</Text>
  <View style={styles.productInfo}>
    {/* 商品信息、价格、操作按钮 */}
  </View>
</View>

这段代码通过 position: 'absolute' 实现了鸿蒙系统特有的收藏按钮悬浮效果,结合 flexDirection: 'row' 的横向布局,在不同尺寸的鸿蒙设备上都能保持商品信息的合理展示比例,无需针对鸿蒙系统做额外的布局适配。

2. 样式

应用通过 StyleSheet.create 定义样式表,深度适配鸿蒙系统的商品推荐设计规范,核心体现在以下几个维度:

  • 商品推荐色彩体系:采用鸿蒙系统的中性色系主色调(#1e293b#f8fafc),搭配功能性色彩区分商品标签(推荐-蓝色 #3b82f6、热销-红色 #ef4444、新品-绿色 #10b981 等),符合鸿蒙系统电商推荐类应用“清晰、高效”的视觉设计理念
  • 圆角与阴影:使用 borderRadius: 12 实现鸿蒙风格的大圆角设计,通过 elevation(Android)和 shadow(iOS)属性适配鸿蒙系统的阴影效果,兼顾商品推荐界面的层次感与跨端一致性
  • 商品标签样式:通过动态样式绑定实现鸿蒙风格的商品标签展示,同时强化价格体系的视觉层级:
<View style={[
  styles.tagBadge,
  { backgroundColor: getTagColor(product.tag) }
]}>
  <Text style={styles.tagBadgeText}>{product.tag}</Text>
</View>

<Text style={styles.currentPrice}>¥{product.price}</Text>
{product.originalPrice && (
  <Text style={styles.originalPrice}>¥{product.originalPrice}</Text>
)}

这种样式设计方案完全基于 React Native 的标准 API 实现,在鸿蒙系统中能够通过 React Native 的渲染层自动转换为原生样式——backgroundColor 对应鸿蒙的 background-colortextDecorationLine 对应鸿蒙的 text-decoration,无需编写平台特定代码。

3. 交互

应用中所有交互组件均基于 React Native 基础组件封装,同时适配鸿蒙系统的商品推荐交互规范:

  • SafeAreaView:对应鸿蒙系统的 SafeArea 组件,适配刘海屏、挖孔屏等异形屏,保证推荐界面在鸿蒙不同终端设备上的完整性
  • ScrollView:与鸿蒙的 List 组件逻辑一致,实现商品推荐内容的纵向滚动展示,适配鸿蒙系统的滑动交互逻辑
  • TouchableOpacity:替代鸿蒙的 Button 组件,实现商品收藏、查看详情、快速购买等核心推荐交互,结合 disabled 属性控制交互状态,符合鸿蒙的交互规范
  • Alert:对应鸿蒙的 TextDialog 组件,实现商品详情展示、购买确认、收藏反馈等弹窗交互,保持与鸿蒙原生电商推荐应用一致的交互体验

以商品详情交互为例,代码通过状态判断实现鸿蒙风格的详情展示逻辑:

const handleProductDetail = (productId: string) => {
  const product = products.find(p => p.id === productId);
  if (product) {
    Alert.alert(
      '商品详情',
      `名称: ${product.name}\n` +
      `分类: ${product.category}\n` +
      `${product.originalPrice ? `原价: ¥${product.originalPrice}\n` : ''}` +
      `现价: ¥${product.price}\n` +
      `描述: ${product.description}\n` +
      `${product.tag ? `标签: ${product.tag}\n` : ''}` +
      `${product.isFavorite ? '❤️ 已收藏' : ''}`,
      [{ text: '确定', style: 'cancel' }]
    );
  }
};

这种交互逻辑完全基于 React Native 的跨端 API 实现,在鸿蒙系统中能够保持与原生电商推荐应用一致的交互体验,无需针对鸿蒙系统做特殊处理。

除了 UI 层的适配,商品推荐业务逻辑的跨端兼容性是 React Native 开发的核心。本应用的核心业务逻辑包括商品标签色彩映射、收藏状态切换、快速购买、详情展示等,这些逻辑完全基于 JavaScript/TypeScript 实现,天然具备跨端运行能力。

1. 商品标签

应用实现了基于标签的色彩映射逻辑,这是个性化推荐场景的核心能力,通过纯函数实现跨端兼容:

const getTagColor = (tag?: string) => {
  switch (tag) {
    case '推荐': return '#3b82f6';
    case '热销': return '#ef4444';
    case '新品': return '#10b981';
    case '限时': return '#f59e0b';
    case '特价': return '#8b5cf6';
    default: return '#64748b';
  }
};

该逻辑完全基于纯函数实现,不依赖任何平台特定 API,在 React Native 支持的所有平台(包括鸿蒙)上都能稳定运行。值得注意的是,代码中使用 switch 语句实现标签色彩映射,这种逻辑在鸿蒙系统的 JS 引擎中能够无缝执行,体现了 React Native 跨端开发“一次编写,多端复用”的核心价值。

2. 商品交互

应用实现了基于商品状态的动态交互逻辑,区分收藏、详情、购买等不同交互场景,适配鸿蒙系统的交互规范:

const toggleFavorite = (productId: string) => {
  Alert.alert(
    '收藏成功',
    '已添加到您的收藏夹',
    [{ text: '确定', style: 'cancel' }]
  );
};

const handleQuickPurchase = (productId: string) => {
  const product = products.find(p => p.id === productId);
  if (product) {
    Alert.alert(
      '立即购买',
      `确认购买 ${product.name} 吗?\n价格: ¥${product.price}`,
      [
        { text: '取消', style: 'cancel' },
        { text: '确认购买', onPress: () => Alert.alert('成功', '订单已提交') }
      ]
    );
  }
};

这种交互逻辑不仅符合 React 的设计理念,更重要的是在跨端场景下,能够避免因不同平台的运行时差异导致的商品交互行为不一致。对于鸿蒙系统而言,这类纯逻辑代码无需任何适配即可直接运行,是商品推荐类跨端开发的最优实践。

3. 商品筛选

应用针对个性化推荐场景实现了鸿蒙风格的商品标签筛选展示逻辑,模拟用户偏好的精准映射:

{['推荐', '热销', '新品', '限时', '特价'].map(tag => (
  <TouchableOpacity 
    key={tag}
    style={[
      styles.tagButton,
      { borderColor: getTagColor(tag) }
    ]}
  >
    <Text style={[
      styles.tagText,
      { color: getTagColor(tag) }
    ]}>{tag}</Text>
  </TouchableOpacity>
))}

这种筛选展示逻辑基于纯 JavaScript 实现,能够有效保证商品推荐体验的跨端一致性,同时在鸿蒙系统中,动态样式绑定会被转换为鸿蒙的原生样式属性,确保标签按钮的视觉一致性。


从本应用的实现来看,React Native 对接鸿蒙系统商品推荐场景的核心在于“抽象层适配 + 推荐体验兼容”,具体体现在以下几个维度:

  1. JS 引擎层推荐兼容:鸿蒙系统内置了符合 ECMAScript 标准的 JavaScript 引擎,能够直接执行 React Native 的 JS 代码,这是跨端运行的底层基础。本应用中所有的商品推荐业务逻辑代码(标签映射、收藏切换、快速购买)均运行在 JS 引擎层,无需任何修改即可在鸿蒙系统中执行,保证了商品推荐逻辑的跨端一致性。

  2. 组件映射层推荐适配:React Native 通过自定义渲染器,将 React 组件(View、Text、TouchableOpacity 等)映射为鸿蒙系统的原生组件。例如 ScrollView 会被转换为鸿蒙的 List 组件,TouchableOpacity 会被转换为鸿蒙的 Button 组件,这种映射关系由 React Native 的鸿蒙适配层自动完成,开发者无需关注底层实现细节,只需专注于商品推荐业务逻辑开发。

  3. 样式转换层推荐规范适配:React Native 的 StyleSheet 样式会被自动转换为鸿蒙系统的原生样式,本应用中定义的所有鸿蒙商品推荐风格样式(中性系主调、标签分类色彩、价格体系样式)都能通过这一层完成自动转换,保证了商品推荐 UI 风格在鸿蒙系统中的一致性。

  4. 推荐体验跨端兼容:应用中所有交互逻辑均基于 React Native 的标准 API 实现,这些 API 在鸿蒙系统中会被替换为对应的原生 API 调用,例如 Alert.alert 对应鸿蒙的 TextDialogTouchableOpacity 对应鸿蒙的 Button,确保了商品推荐体验在不同平台的一致性。

本个性化推荐应用的实现完整展现了 React Native 在鸿蒙跨端商品推荐开发领域的技术优势,核心要点可总结为:

  1. 强类型设计保障推荐数据一致性:TypeScript 场景化类型定义不仅提升代码质量,更能与鸿蒙 ArkTS 形成类型映射,降低商品推荐场景跨端适配成本,是电商推荐类应用跨端开发的基础保障
  2. 纯逻辑开发最大化推荐代码复用率:标签映射、收藏切换、快速购买等核心商品推荐逻辑采用纯 JavaScript/TypeScript 实现,无需针对鸿蒙系统做特殊修改,大幅提升开发效率
  3. 标准 API 适配鸿蒙商品推荐生态:基于 React Native 标准组件和 API 开发,通过底层适配层自动对接鸿蒙原生能力,兼顾开发效率与商品推荐场景的原生体验

本文将深入分析一个基于 React Native 构建的个性化推荐应用,该应用采用了现代化的函数式组件架构,同时兼顾了 React Native 与 HarmonyOS 的跨端兼容性。

数据结构

首先,我们来看一下核心数据结构的定义:

// 个性化商品类型
type PersonalizedProduct = {
  id: string;
  name: string;
  category: string;
  price: number;
  originalPrice?: number;
  image: string;
  description: string;
  tag?: '推荐' | '热销' | '新品' | '限时' | '特价'; // 商品标签
  isFavorite?: boolean; // 是否收藏
};

这个类型定义体现了良好的 TypeScript 实践,通过可选属性(originalPricetagisFavorite)和字面量类型(tag 的取值范围)提供了严格的类型约束。这种设计不仅增强了代码的可读性,也为后续的业务逻辑处理提供了类型安全保障。

状态管理

应用采用了 React 的 useState Hook 进行状态管理:

const [products] = useState<PersonalizedProduct[]>([
  {
    id: '1',
    name: '无线蓝牙耳机',
    category: '数码配件',
    price: 199,
    originalPrice: 299,
    image: '🎧',
    description: '主动降噪,续航20小时',
    tag: '推荐',
    isFavorite: true
  },
  // 更多商品数据...
]);

这里将商品数据直接初始化在 useState 中,虽然在实际生产环境中通常会通过 API 获取数据,但这种方式对于演示和本地开发非常便捷。值得注意的是,这里只获取了状态值而没有解构出更新函数,因为在这个组件中商品数据是静态的。

UI 组件

应用的 UI 架构清晰明了,主要包含以下几个部分:

  1. 安全区域容器:使用 SafeAreaView 确保内容在各种设备上都能正确显示,避免被刘海屏或系统导航栏遮挡。
  2. 头部区域:包含应用标题和副标题,采用了层级分明的排版设计。
  3. 标签导航:实现了可点击的标签筛选功能,通过动态计算标签颜色增强视觉效果。
  4. 商品列表:使用 ScrollView 实现了可滚动的商品卡片列表。

响应式布局

应用使用了 StyleSheet 进行样式管理,同时结合 Dimensions API 实现了响应式布局:

const { width, height } = Dimensions.get('window');

这种方式使得应用能够根据不同设备的屏幕尺寸自动调整布局,提高了跨设备的兼容性。特别值得一提的是,商品卡片的设计采用了 flexbox 布局,确保了在不同屏幕宽度下的良好显示效果。

业务逻辑

应用实现了以下核心业务逻辑:

  1. 商品详情查看:通过 handleProductDetail 函数实现,点击商品时弹出包含详细信息的对话框。
  2. 收藏功能:通过 toggleFavorite 函数实现,点击收藏按钮时弹出成功提示。
  3. 快速购买:通过 handleQuickPurchase 函数实现,点击购买按钮时弹出确认对话框,确认后提交订单。

标签颜色动态计算

应用通过 getTagColor 函数实现了标签颜色的动态计算:

const getTagColor = (tag?: string) => {
  switch (tag) {
    case '推荐': return '#3b82f6';
    case '热销': return '#ef4444';
    case '新品': return '#10b981';
    case '限时': return '#f59e0b';
    case '特价': return '#8b5cf6';
    default: return '#64748b';
  }
};

这种设计使得不同标签具有不同的视觉标识,提高了用户体验的同时,也增强了代码的可维护性。


  1. 组件兼容性:代码中使用的 SafeAreaViewScrollViewTouchableOpacity 等组件在 React Native 和 HarmonyOS 中都有对应实现,确保了跨平台的一致性。
  2. 样式兼容性StyleSheet API 在两个平台上的使用方式基本一致,确保了样式的跨平台兼容。
  3. API 兼容性Dimensions API 和 Alert API 在两个平台上都可用,保证了基础功能的正常运行。

性能

  1. 组件结构优化:应用采用了扁平的组件结构,减少了组件嵌套层级,有利于渲染性能的提升。
  2. 状态管理优化:使用 useState 进行局部状态管理,避免了全局状态管理带来的不必要渲染。
  3. 渲染优化:商品列表使用 map 函数进行渲染,每个商品卡片都有唯一的 key 属性,有助于 React 进行高效的虚拟 DOM diff。

代码质量

  1. TypeScript 类型定义:通过严格的类型定义,提高了代码的可读性和可维护性。
  2. 函数模块化:将不同功能的逻辑封装在独立的函数中,提高了代码的模块化程度。
  3. 样式分离:使用 StyleSheet 将样式与业务逻辑分离,提高了代码的可维护性。

  1. 个性化推荐系统架构:应用展示了一个完整的个性化推荐系统前端架构,包括标签分类、商品展示、详情查看、收藏和购买功能。
  2. 动态视觉效果:通过动态计算标签颜色,实现了丰富的视觉效果,提高了用户体验。
  3. 用户交互设计:应用实现了流畅的用户交互,包括标签点击、商品详情查看、收藏和购买确认等操作。
  4. 代码组织:代码结构清晰,逻辑分明,采用了现代化的 React 开发实践。

  1. 数据获取优化:在实际生产环境中,建议通过 API 获取商品数据,并实现数据缓存机制,减少网络请求。
  2. 状态管理扩展:当应用规模扩大时,建议使用 useReducer 或 Redux 等状态管理库,提高状态管理的可扩展性。
  3. 组件拆分:建议将商品卡片、标签按钮等可复用的 UI 元素拆分为独立的组件,提高代码的复用性。
  4. 动画效果增强:可以添加适当的动画效果,如商品卡片的加载动画、收藏按钮的点击动画等,进一步提高用户体验。
  5. 性能监控:建议添加性能监控,及时发现并解决性能瓶颈。

本文分析的个性化推荐应用展示了如何使用 React Native 构建一个功能完整、用户体验良好的跨端应用。通过合理的架构设计、清晰的代码组织和现代化的开发实践,实现了一个具有个性化推荐、标签筛选、商品详情查看、收藏和购买功能的应用。


真实演示案例代码:






// App.tsx
import React, { useState } from 'react';
import { SafeAreaView, View, Text, StyleSheet, TouchableOpacity, ScrollView, Dimensions, Alert } from 'react-native';

// Base64 图标库
const ICONS_BASE64 = {
  recommend: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
  history: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
  favorite: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
  trending: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
  flash: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
  new: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
  hot: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
  bargain: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
};

const { width, height } = Dimensions.get('window');

// 个性化商品类型
type PersonalizedProduct = {
  id: string;
  name: string;
  category: string;
  price: number;
  originalPrice?: number;
  image: string;
  description: string;
  tag?: '推荐' | '热销' | '新品' | '限时' | '特价'; // 商品标签
  isFavorite?: boolean; // 是否收藏
};

// 个性化推荐应用组件
const PersonalizedRecommendationApp: React.FC = () => {
  const [products] = useState<PersonalizedProduct[]>([
    {
      id: '1',
      name: '无线蓝牙耳机',
      category: '数码配件',
      price: 199,
      originalPrice: 299,
      image: '🎧',
      description: '主动降噪,续航20小时',
      tag: '推荐',
      isFavorite: true
    },
    {
      id: '2',
      name: '智能手环',
      category: '穿戴设备',
      price: 299,
      image: '⌚',
      description: '心率监测,运动数据记录',
      tag: '热销'
    },
    {
      id: '3',
      name: '便携充电宝',
      category: '数码配件',
      price: 89,
      originalPrice: 129,
      image: '🔋',
      description: '20000mAh,支持快充',
      tag: '特价'
    },
    {
      id: '4',
      name: '机械键盘',
      category: '电脑外设',
      price: 399,
      image: '⌨️',
      description: '青轴手感,RGB背光',
      tag: '新品'
    },
    {
      id: '5',
      name: '高清摄像头',
      category: '数码配件',
      price: 159,
      originalPrice: 199,
      image: '📷',
      description: '1080P高清录制,广角镜头',
      tag: '限时'
    }
  ]);

  const getTagColor = (tag?: string) => {
    switch (tag) {
      case '推荐': return '#3b82f6';
      case '热销': return '#ef4444';
      case '新品': return '#10b981';
      case '限时': return '#f59e0b';
      case '特价': return '#8b5cf6';
      default: return '#64748b';
    }
  };

  const handleProductDetail = (productId: string) => {
    const product = products.find(p => p.id === productId);
    if (product) {
      Alert.alert(
        '商品详情',
        `名称: ${product.name}\n` +
        `分类: ${product.category}\n` +
        `${product.originalPrice ? `原价: ¥${product.originalPrice}\n` : ''}` +
        `现价: ¥${product.price}\n` +
        `描述: ${product.description}\n` +
        `${product.tag ? `标签: ${product.tag}\n` : ''}` +
        `${product.isFavorite ? '❤️ 已收藏' : ''}`,
        [{ text: '确定', style: 'cancel' }]
      );
    }
  };

  const toggleFavorite = (productId: string) => {
    Alert.alert(
      '收藏成功',
      '已添加到您的收藏夹',
      [{ text: '确定', style: 'cancel' }]
    );
  };

  const handleQuickPurchase = (productId: string) => {
    const product = products.find(p => p.id === productId);
    if (product) {
      Alert.alert(
        '立即购买',
        `确认购买 ${product.name} 吗?\n价格: ¥${product.price}`,
        [
          { text: '取消', style: 'cancel' },
          { text: '确认购买', onPress: () => Alert.alert('成功', '订单已提交') }
        ]
      );
    }
  };

  return (
    <SafeAreaView style={styles.container}>
      {/* 头部 */}
      <View style={styles.header}>
        <Text style={styles.title}>个性化推荐</Text>
        <Text style={styles.subtitle}>基于您的喜好精心挑选</Text>
      </View>

      <ScrollView style={styles.content}>
        {/* 推荐标签导航 */}
        <View style={styles.tagNavigation}>
          {['推荐', '热销', '新品', '限时', '特价'].map(tag => (
            <TouchableOpacity 
              key={tag}
              style={[
                styles.tagButton,
                { borderColor: getTagColor(tag) }
              ]}
            >
              <Text style={[
                styles.tagText,
                { color: getTagColor(tag) }
              ]}>{tag}</Text>
            </TouchableOpacity>
          ))}
        </View>

        {/* 商品列表 */}
        <View style={styles.productsSection}>
          <Text style={styles.sectionTitle}>为您推荐</Text>
          
          {products.map(product => (
            <View key={product.id} style={styles.productCard}>
              <TouchableOpacity 
                style={styles.favoriteButton}
                onPress={() => toggleFavorite(product.id)}
              >
                <Text style={styles.favoriteIcon}>
                  {product.isFavorite ? '❤️' : '🤍'}
                </Text>
              </TouchableOpacity>
              
              <Text style={styles.productImage}>{product.image}</Text>
              
              <View style={styles.productInfo}>
                <Text style={styles.productName}>{product.name}</Text>
                <Text style={styles.productCategory}>{product.category}</Text>
                <Text style={styles.productDesc}>{product.description}</Text>
                
                <View style={styles.priceRow}>
                  <Text style={styles.currentPrice}>¥{product.price}</Text>
                  {product.originalPrice && (
                    <Text style={styles.originalPrice}>¥{product.originalPrice}</Text>
                  )}
                  {product.tag && (
                    <View style={[
                      styles.tagBadge,
                      { backgroundColor: getTagColor(product.tag) }
                    ]}>
                      <Text style={styles.tagBadgeText}>{product.tag}</Text>
                    </View>
                  )}
                </View>
                
                <View style={styles.productActions}>
                  <TouchableOpacity 
                    style={styles.detailButton}
                    onPress={() => handleProductDetail(product.id)}
                  >
                    <Text style={styles.detailText}>查看详情</Text>
                  </TouchableOpacity>
                  <TouchableOpacity 
                    style={styles.buyButton}
                    onPress={() => handleQuickPurchase(product.id)}
                  >
                    <Text style={styles.buyText}>立即购买</Text>
                  </TouchableOpacity>
                </View>
              </View>
            </View>
          ))}
        </View>

        {/* 个性化贴士 */}
        <View style={styles.tipsCard}>
          <Text style={styles.sectionTitle}>💡 个性化小贴士</Text>
          
          <View style={styles.tipItem}>
            <Text style={styles.tipEmoji}>🎯</Text>
            <View style={styles.tipContent}>
              <Text style={styles.tipTitle}>精准推荐</Text>
              <Text style={styles.tipDesc}>基于浏览和购买历史智能推荐</Text>
            </View>
          </View>
          
          <View style={styles.tipItem}>
            <Text style={styles.tipEmoji}></Text>
            <View style={styles.tipContent}>
              <Text style={styles.tipTitle}>限时优惠</Text>
              <Text style={styles.tipDesc}>关注倒计时,不错过心仪商品</Text>
            </View>
          </View>
          
          <View style={styles.tipItem}>
            <Text style={styles.tipEmoji}>❤️</Text>
            <View style={styles.tipContent}>
              <Text style={styles.tipTitle}>收藏管理</Text>
              <Text style={styles.tipDesc}>收藏商品,方便后续购买</Text>
            </View>
          </View>
        </View>

        {/* 使用说明 */}
        <View style={styles.infoCard}>
          <Text style={styles.sectionTitle}>📘 使用说明</Text>
          <Text style={styles.infoText}>• 系统根据您的浏览记录智能推荐</Text>
          <Text style={styles.infoText}>• 点击标签可筛选同类商品</Text>
          <Text style={styles.infoText}>• 收藏商品可在个人中心查看</Text>
          <Text style={styles.infoText}>• 限时商品数量有限,先到先得</Text>
        </View>
      </ScrollView>

      {/* 底部导航 */}
      <View style={styles.bottomNav}>
        <TouchableOpacity style={styles.navItem}>
          <Text style={styles.navIcon}>🏠</Text>
          <Text style={styles.navText}>首页</Text>
        </TouchableOpacity>
        <TouchableOpacity style={styles.navItem}>
          <Text style={styles.navIcon}>🔍</Text>
          <Text style={styles.navText}>搜索</Text>
        </TouchableOpacity>
        <TouchableOpacity style={styles.navItem}>
          <Text style={styles.navIcon}>❤️</Text>
          <Text style={styles.navText}>收藏</Text>
        </TouchableOpacity>
        <TouchableOpacity style={[styles.navItem, styles.activeNavItem]}>
          <Text style={styles.navIcon}>👤</Text>
          <Text style={styles.navText}>我的</Text>
        </TouchableOpacity>
      </View>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#f8fafc',
  },
  header: {
    flexDirection: 'column',
    padding: 16,
    backgroundColor: '#ffffff',
    borderBottomWidth: 1,
    borderBottomColor: '#e2e8f0',
  },
  title: {
    fontSize: 20,
    fontWeight: 'bold',
    color: '#1e293b',
    marginBottom: 4,
  },
  subtitle: {
    fontSize: 14,
    color: '#64748b',
  },
  content: {
    flex: 1,
    marginTop: 12,
  },
  tagNavigation: {
    flexDirection: 'row',
    justifyContent: 'space-around',
    backgroundColor: '#ffffff',
    marginHorizontal: 16,
    marginBottom: 12,
    paddingVertical: 12,
    borderRadius: 12,
    elevation: 2,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.1,
    shadowRadius: 4,
  },
  tagButton: {
    borderWidth: 1,
    paddingHorizontal: 16,
    paddingVertical: 6,
    borderRadius: 16,
  },
  tagText: {
    fontSize: 12,
    fontWeight: '500',
  },
  productsSection: {
    backgroundColor: '#ffffff',
    marginHorizontal: 16,
    marginBottom: 12,
    borderRadius: 12,
    padding: 16,
    elevation: 2,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.1,
    shadowRadius: 4,
  },
  sectionTitle: {
    fontSize: 18,
    fontWeight: '600',
    color: '#1e293b',
    marginBottom: 16,
  },
  productCard: {
    flexDirection: 'row',
    backgroundColor: '#f8fafc',
    borderRadius: 12,
    padding: 12,
    marginBottom: 12,
    position: 'relative',
  },
  favoriteButton: {
    position: 'absolute',
    top: 8,
    right: 8,
    zIndex: 1,
  },
  favoriteIcon: {
    fontSize: 20,
  },
  productImage: {
    fontSize: 40,
    marginRight: 12,
  },
  productInfo: {
    flex: 1,
  },
  productName: {
    fontSize: 16,
    fontWeight: '600',
    color: '#1e293b',
    marginBottom: 4,
  },
  productCategory: {
    fontSize: 12,
    color: '#64748b',
    marginBottom: 4,
  },
  productDesc: {
    fontSize: 12,
    color: '#94a3b8',
    marginBottom: 8,
  },
  priceRow: {
    flexDirection: 'row',
    alignItems: 'center',
    marginBottom: 12,
  },
  currentPrice: {
    fontSize: 18,
    fontWeight: 'bold',
    color: '#ef4444',
    marginRight: 8,
  },
  originalPrice: {
    fontSize: 14,
    color: '#94a3b8',
    textDecorationLine: 'line-through',
    marginRight: 8,
  },
  tagBadge: {
    paddingHorizontal: 8,
    paddingVertical: 2,
    borderRadius: 12,
  },
  tagBadgeText: {
    fontSize: 10,
    color: '#ffffff',
    fontWeight: '600',
  },
  productActions: {
    flexDirection: 'row',
    justifyContent: 'space-between',
  },
  detailButton: {
    backgroundColor: '#e2e8f0',
    paddingHorizontal: 16,
    paddingVertical: 6,
    borderRadius: 16,
  },
  detailText: {
    color: '#64748b',
    fontSize: 12,
    fontWeight: '500',
  },
  buyButton: {
    backgroundColor: '#3b82f6',
    paddingHorizontal: 16,
    paddingVertical: 6,
    borderRadius: 16,
  },
  buyText: {
    color: '#ffffff',
    fontSize: 12,
    fontWeight: '500',
  },
  tipsCard: {
    backgroundColor: '#ffffff',
    marginHorizontal: 16,
    marginBottom: 12,
    borderRadius: 12,
    padding: 16,
    elevation: 2,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.1,
    shadowRadius: 4,
  },
  tipItem: {
    flexDirection: 'row',
    alignItems: 'center',
    paddingVertical: 12,
    borderBottomWidth: 1,
    borderBottomColor: '#e2e8f0',
  },
  tipEmoji: {
    fontSize: 20,
    width: 30,
  },
  tipContent: {
    flex: 1,
  },
  tipTitle: {
    fontSize: 14,
    fontWeight: '600',
    color: '#1e293b',
    marginBottom: 2,
  },
  tipDesc: {
    fontSize: 12,
    color: '#64748b',
  },
  infoCard: {
    backgroundColor: '#ffffff',
    marginHorizontal: 16,
    marginBottom: 80,
    borderRadius: 12,
    padding: 16,
    elevation: 2,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.1,
    shadowRadius: 4,
  },
  infoText: {
    fontSize: 14,
    color: '#64748b',
    lineHeight: 20,
    marginBottom: 4,
  },
  bottomNav: {
    flexDirection: 'row',
    justifyContent: 'space-around',
    backgroundColor: '#ffffff',
    borderTopWidth: 1,
    borderTopColor: '#e2e8f0',
    paddingVertical: 12,
    position: 'absolute',
    bottom: 0,
    left: 0,
    right: 0,
  },
  navItem: {
    alignItems: 'center',
    flex: 1,
  },
  activeNavItem: {
    paddingTop: 4,
    borderTopWidth: 2,
    borderTopColor: '#3b82f6',
  },
  navIcon: {
    fontSize: 20,
    color: '#94a3b8',
    marginBottom: 4,
  },
  activeNavIcon: {
    color: '#3b82f6',
  },
  navText: {
    fontSize: 12,
    color: '#94a3b8',
  },
  activeNavText: {
    color: '#3b82f6',
    fontWeight: '500',
  },
});

export default PersonalizedRecommendationApp;

请添加图片描述


打包

接下来通过打包命令npn run harmony将reactNative的代码打包成为bundle,这样可以进行在开源鸿蒙OpenHarmony中进行使用。

在这里插入图片描述

打包之后再将打包后的鸿蒙OpenHarmony文件拷贝到鸿蒙的DevEco-Studio工程目录去:

在这里插入图片描述

最后运行效果图如下显示:

请添加图片描述
本文介绍了基于React Native开发的个性化商品推荐应用如何适配鸿蒙系统。通过TypeScript定义商品数据模型,使用React状态管理核心业务逻辑,并采用Flex布局和StyleSheet实现鸿蒙风格的UI设计。应用涵盖了商品标签展示、收藏管理、快速购买等电商推荐功能,通过纯JavaScript/TypeScript代码确保跨端兼容性,包括商品标签色彩映射、交互逻辑等核心功能。文章展示了React Native在连接iOS、Android与鸿蒙系统方面的优势,为开发者提供了电商推荐类应用跨平台开发的技术方案和最佳实践。

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐