index.jsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import Taro from "@tarojs/taro"; // 导入 Taro
  2. import { Component } from "react";
  3. import {
  4. View,
  5. Image,
  6. Swiper,
  7. SwiperItem,
  8. Text,
  9. Button,
  10. } from "@tarojs/components";
  11. import { AtTabs } from "taro-ui";
  12. import { getHomeData, getProductList } from "../../service";
  13. import { getShareContent } from "../../common/share";
  14. import "./index.less";
  15. import Skeleton from "../../components/skeleton"; //骨架屏
  16. import SearchBar from "../../components/index/SearchBar"; //搜索框
  17. import CategoryList from "../../components/index/CategoryList"; //分类列表
  18. import OperationArea from "../../components/index/OperationArea"; //运营区域
  19. import RecommendList from "../../components/index/RecommendList"; //邀请人推荐
  20. import ProductList from "../../components/index/ProductList"; //商品列表
  21. import myStoreIcon from "../../images/index/myStore.png";
  22. import backTopIcon from "../../images/index/back-top.png";
  23. export default class Index extends Component {
  24. state = {
  25. current: 0, // 添加当前选中的tab索引
  26. homeData: {}, // 首页数据
  27. bannerList: [],
  28. productList: [], // 添加商品列表
  29. page: 1, // 添加页码
  30. totalPages: 1, // 添加总页数
  31. loading: false, // 添加加载状态
  32. };
  33. // 获取首页数据
  34. getHomeData = async () => {
  35. const res = await getHomeData();
  36. res.tags = res.tags.map((item, index) => ({
  37. title: item.name,
  38. }));
  39. this.setState({ homeData: res }, () => {
  40. this.getProductList(true);
  41. });
  42. };
  43. // 轮播图点击
  44. handleBannerClick = (item) => {
  45. if(item.type_jpath==1){
  46. Taro.navigateTo({
  47. url: `${item.jpath}?isBanner=true`
  48. })
  49. }
  50. };
  51. // 修改 tab 切换处理函数
  52. handleClick(value) {
  53. this.setState(
  54. {
  55. current: value,
  56. page: 1,
  57. },
  58. () => {
  59. // 跳转到列表顶部
  60. Taro.createSelectorQuery()
  61. .select(".product-list-wrap")
  62. .boundingClientRect()
  63. .exec((res) => {
  64. if (res[0]) {
  65. const top = res[0].top;
  66. Taro.pageScrollTo({
  67. scrollTop: top - 40,
  68. duration: 300,
  69. });
  70. this.getProductList(true);
  71. } else {
  72. console.warn("未找到 .product-list-wrap 元素");
  73. }
  74. });
  75. }
  76. );
  77. }
  78. componentDidShow() {
  79. this.getHomeData(); //获取首页数据
  80. }
  81. // 添加回到顶部方法
  82. handleBackTop = () => {
  83. Taro.pageScrollTo({
  84. scrollTop: 0,
  85. duration: 300,
  86. });
  87. };
  88. // 页面上拉触底
  89. onReachBottom = () => {
  90. const { page, totalPages, loading } = this.state;
  91. if (page < totalPages && !loading) {
  92. this.setState(
  93. (prevState) => ({ page: prevState.page + 1 }),
  94. () => this.getProductList()
  95. );
  96. }
  97. };
  98. // 获取商品列表
  99. getProductList = async (isRefresh) => {
  100. const { page } = this.state;
  101. this.setState({ loading: true });
  102. const res = await getProductList({
  103. tag_name: this.state.homeData.tags[this.state.current].title,
  104. page,
  105. page_size: 10,
  106. });
  107. this.setState((prevState) => ({
  108. productList: isRefresh
  109. ? res.goods_list
  110. : [...prevState.productList, ...res.goods_list],
  111. totalPages: res.total_pages,
  112. loading: false,
  113. }));
  114. };
  115. // 配置分享内容
  116. onShareAppMessage() {
  117. return getShareContent();
  118. }
  119. render() {
  120. const { homeData, productList, loading } = this.state;
  121. return (
  122. <View className="index">
  123. <View
  124. className="header"
  125. style={{ paddingTop: Taro.navigationBarHeight + "px" }}
  126. >
  127. <View className="header-content">
  128. <Image
  129. mode="heightFix"
  130. className="header-img"
  131. src="https://video-img.fyshark.com/1731495433480WechatIMG674.jpg"
  132. />
  133. </View>
  134. {/* 搜索框包裹盒子 */}
  135. <View
  136. className="search-bar-container"
  137. onClick={() => Taro.navigateTo({ url: "/pages/search/index" })}
  138. >
  139. <SearchBar value="" disabled={true} placeholder="点击搜索商品" />
  140. </View>
  141. {/* tab分类 */}
  142. <View className="tabs-list">
  143. {homeData.tags && (
  144. <AtTabs
  145. current={this.state.current}
  146. scroll
  147. tabList={homeData.tags}
  148. onClick={this.handleClick.bind(this)}
  149. />
  150. )}
  151. <View className="tab-more">
  152. <Image className="tab-more-img" />
  153. </View>
  154. </View>
  155. {/* 轮播图 */}
  156. <View className="banner-wrap">
  157. <Swiper
  158. className="banner-swiper"
  159. circular
  160. autoplay
  161. indicatorDots
  162. indicatorColor="#e8e8e8"banner
  163. indicatorActiveColor="#ffffff"
  164. >
  165. {homeData.banner &&
  166. homeData.banner.map((item) => (
  167. <SwiperItem onClick={() => this.handleBannerClick(item)} className="banner-item" key={item.id}>
  168. <Image
  169. className="banner-img"
  170. src={item.img}
  171. mode="aspectFill"
  172. />
  173. </SwiperItem>
  174. ))}
  175. </Swiper>
  176. </View>
  177. </View>
  178. {/* 骨架屏 */}
  179. <Skeleton
  180. isSkeletonShow={!homeData.banner && !productList.length > 0}
  181. />
  182. {/* 分类列表组件 */}
  183. {homeData.icon && <CategoryList categoryList={homeData.icon} />}
  184. {/* 运营区域 */}
  185. {homeData.recommend && <OperationArea recommend={homeData.recommend} />}
  186. {/* 邀请人推荐 */}
  187. {/* <RecommendList /> */}
  188. {/* 商品列表 */}
  189. <View className="product-list-wrap">
  190. <ProductList
  191. productList={productList}
  192. loading={loading}
  193. onShareProduct={this.handleShare}
  194. />
  195. </View>
  196. {/* 添加浮动按钮 */}
  197. <View className="float-buttons">
  198. <View
  199. className="float-btn"
  200. onClick={() =>
  201. Taro.navigateTo({
  202. url: `/pages/memberSub/storeManagement/index?isManager=${true}`,
  203. })
  204. }
  205. >
  206. <Image className="my-shop-bg" src={myStoreIcon} mode="aspectFit" />
  207. <Text>我的{"\n"}小店</Text>
  208. </View>
  209. <View className="float-btn" onClick={this.handleBackTop}>
  210. <Image className="back-top" src={backTopIcon} mode="aspectFit" />
  211. </View>
  212. </View>
  213. {/* 分享弹窗 */}
  214. </View>
  215. );
  216. }
  217. }