index.jsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import React, { useState, useEffect } from "react";
  2. import { View, Text, Image } from "@tarojs/components";
  3. import { AtCheckbox } from "taro-ui";
  4. import Taro from "@tarojs/taro";
  5. import "./index.less";
  6. import joinStoreBg from "../../../images/index/jionStore.png";
  7. import saveMoneyBg from "../../../images/index/save-money.png";
  8. import shareEarnBg from "../../../images/index/share-earn.png";
  9. import seckillIcon from "../../../images/seckill/seckillIcon.png";
  10. const ProductList = (props) => {
  11. const { isSeckill } = props; //是否是秒杀产品引用
  12. const { isManagement } = props; //是否是管理产品引用
  13. const { isManagementStatus } = props; //是否是管理状态引用
  14. const { isSelectAll } = props; //是否是全选
  15. const [selectedProducts, setSelectedProducts] = useState([]); // 管理选中的商品列表
  16. // 模拟商品数据
  17. const products = [
  18. {
  19. id: 1,
  20. image:
  21. "https://img-s-msn-com.akamaized.net/tenant/amp/entityid/AA1saL6x.img?w=768&h=411&m=6",
  22. name: "这是一个商品名称这是一个商品名称这是一个商品名称这是一个商品名称这是一个商品名称",
  23. price: 99.99,
  24. originalPrice: 199.99,
  25. saveMoney: 10,
  26. shareEarn: 20,
  27. },
  28. {
  29. id: 2,
  30. image:
  31. "https://img-s-msn-com.akamaized.net/tenant/amp/entityid/AA1saL6x.img?w=768&h=411&m=6",
  32. name: "这是一个商品名称这是一个商品名称这是一个商品名称这是一个商品名称这是一个商品名称",
  33. price: 99.99,
  34. originalPrice: 199.99,
  35. saveMoney: 10,
  36. shareEarn: 20,
  37. },
  38. {
  39. id: 3,
  40. image:
  41. "https://img-s-msn-com.akamaized.net/tenant/amp/entityid/AA1saL6x.img?w=768&h=411&m=6",
  42. name: "这是一个商品名称这是一个商品名称这是一个商品名称这是一个商品名称这是一个商品名称",
  43. price: 99.99,
  44. originalPrice: 199.99,
  45. saveMoney: 10,
  46. shareEarn: 20,
  47. },
  48. ];
  49. // 跳转产品详情
  50. const toDetail = (id) => {
  51. if (isManagementStatus) {
  52. selectProduct(id);
  53. } else {
  54. Taro.navigateTo({
  55. url: `/pages/productDetail/index?id=${id}`,
  56. });
  57. }
  58. };
  59. // 监听 isSelectAll 的变化
  60. useEffect(() => {
  61. if (isSelectAll) {
  62. // 如果 isSelectAll 为 true,选中所有商品
  63. setSelectedProducts(products.map((product) => product.id));
  64. } else {
  65. // 如果 isSelectAll 为 false,取消所有选中
  66. setSelectedProducts([]);
  67. }
  68. }, [isSelectAll]); // 依赖项是 isSelectAll
  69. // 单选
  70. const selectProduct = (productId) => {
  71. setSelectedProducts((prevSelected) => {
  72. const isSelected = prevSelected.includes(productId);
  73. if (isSelected) {
  74. // 如果已经选中,则取消选择
  75. return prevSelected.filter((id) => id !== productId);
  76. } else {
  77. // 如果未选中,则添加到选中列表
  78. return [...prevSelected, productId];
  79. }
  80. });
  81. };
  82. return (
  83. <View className="product-list-wrap">
  84. {isSeckill && (
  85. <View className="seckill-icon">
  86. <Image src={seckillIcon} mode="aspectFit" />
  87. <Text>每日疯抢 限时秒杀</Text>
  88. </View>
  89. )}
  90. {products.map((product, index) => (
  91. <View key={product.id}>
  92. <View onClick={() => toDetail(product.id)} className="product-item">
  93. {isManagement && isManagementStatus && (
  94. <AtCheckbox
  95. options={[{ value: product.id.toString(), label: "" }]} // 确保每个复选框的 options 是独立的
  96. selectedList={selectedProducts.map((id) => id.toString())}
  97. onChange={(selectedList) => selectProduct(product.id)} // 传递当前 product.id
  98. />
  99. )}
  100. <View className="right">
  101. <Image
  102. className="product-img"
  103. src={product.image}
  104. mode="aspectFill"
  105. />
  106. <View className="product-info">
  107. <View
  108. className="product-name"
  109. style={{ WebkitBoxOrient: "vertical" }}
  110. >
  111. <Text className="self-tag">
  112. {!isSeckill ? "自营" : "鱼市秒杀"}
  113. </Text>
  114. {product.name}
  115. </View>
  116. <View className="price-line">
  117. <View className="price-info">
  118. <Text className="symbol">¥</Text>
  119. <Text className="price">{product.price}</Text>
  120. <Text className="original-price">
  121. ¥{product.originalPrice}
  122. </Text>
  123. </View>
  124. {!isSeckill && !isManagement && (
  125. <View className="add-btn">
  126. <Image
  127. className="join-store-bg"
  128. src={joinStoreBg}
  129. mode="aspectFit"
  130. />
  131. <Text className="btn-text">加入小店</Text>
  132. </View>
  133. )}
  134. </View>
  135. <View className="profit-line">
  136. <View className="save-money">
  137. <Image className="bg" src={saveMoneyBg} mode="aspectFit" />
  138. <View className="content">
  139. <Text className="label">自购省</Text>
  140. <Text className="money">¥{product.saveMoney}</Text>
  141. </View>
  142. </View>
  143. <View className="share-earn">
  144. <Image className="bg" src={shareEarnBg} mode="aspectFit" />
  145. <View className="content">
  146. <Text className="label">分享赚</Text>
  147. <Text className="money">¥{product.shareEarn}</Text>
  148. </View>
  149. </View>
  150. </View>
  151. </View>
  152. </View>
  153. </View>
  154. {index !== products.length - 1 && <View className="divider"></View>}
  155. </View>
  156. ))}
  157. </View>
  158. );
  159. };
  160. ProductList.defaultProps = {
  161. isSeckill: false,
  162. };
  163. export default ProductList;