index.jsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. complete: () => {
  70. this.getProductList(true);
  71. },
  72. });
  73. } else {
  74. console.warn("未找到 .product-list-wrap 元素");
  75. }
  76. });
  77. }
  78. );
  79. }
  80. componentDidShow() {
  81. this.getHomeData(); //获取首页数据
  82. }
  83. // 添加回到顶部方法
  84. handleBackTop = () => {
  85. Taro.pageScrollTo({
  86. scrollTop: 0,
  87. duration: 300,
  88. });
  89. };
  90. // 页面上拉触底
  91. onReachBottom = () => {
  92. const { page, totalPages, loading } = this.state;
  93. if (page < totalPages && !loading) {
  94. this.setState(
  95. (prevState) => ({ page: prevState.page + 1 }),
  96. () => this.getProductList()
  97. );
  98. }
  99. };
  100. // 获取商品列表
  101. getProductList = async (isRefresh) => {
  102. const { page } = this.state;
  103. this.setState({ loading: true });
  104. const res = await getProductList({
  105. tag_name: this.state.homeData.tags[this.state.current].title,
  106. page,
  107. page_size: 10,
  108. });
  109. this.setState((prevState) => ({
  110. productList: isRefresh
  111. ? res.goods_list
  112. : [...prevState.productList, ...res.goods_list],
  113. totalPages: res.total_pages,
  114. loading: false,
  115. }));
  116. };
  117. // 配置分享内容
  118. onShareAppMessage() {
  119. return getShareContent();
  120. }
  121. // ooo = () => {
  122. // Taro.requestSubscribeMessage({
  123. // tmplIds: ["vAYG7hbNbsD75YHpmGvF8O_Pve_1Q5AvnLBd-2JF9ys"],
  124. // success: function (res) {
  125. // console.log(res);
  126. // },
  127. // });
  128. // };
  129. render() {
  130. const { homeData, productList, loading } = this.state;
  131. return (
  132. <View className="index">
  133. <View
  134. className="header"
  135. style={{ paddingTop: Taro.navigationBarHeight + "px" }}
  136. >
  137. <View className="header-content">
  138. <Image
  139. mode="heightFix"
  140. className="header-img"
  141. src="https://video-img.fyshark.com/1731495433480WechatIMG674.jpg"
  142. />
  143. </View>
  144. {/* 搜索框包裹盒子 */}
  145. <View
  146. className="search-bar-container"
  147. onClick={() => Taro.navigateTo({ url: "/pages/search/index" })}
  148. >
  149. <SearchBar value="" disabled={true} placeholder="点击搜索商品" />
  150. </View>
  151. {/* tab分类 */}
  152. <View className="tabs-list">
  153. {homeData.tags && (
  154. <AtTabs
  155. current={this.state.current}
  156. scroll
  157. tabList={homeData.tags}
  158. onClick={this.handleClick.bind(this)}
  159. />
  160. )}
  161. <View className="tab-more">
  162. <Image className="tab-more-img" />
  163. </View>
  164. </View>
  165. {/* 轮播图 */}
  166. <View className="banner-wrap">
  167. <Swiper
  168. className="banner-swiper"
  169. circular
  170. autoplay
  171. indicatorDots
  172. indicatorColor="#e8e8e8"
  173. banner
  174. indicatorActiveColor="#ffffff"
  175. >
  176. {homeData.banner &&
  177. homeData.banner.map((item) => (
  178. <SwiperItem
  179. onClick={() => this.handleBannerClick(item)}
  180. className="banner-item"
  181. key={item.id}
  182. >
  183. <Image
  184. className="banner-img"
  185. src={item.img}
  186. mode="widthFix"
  187. />
  188. </SwiperItem>
  189. ))}
  190. </Swiper>
  191. </View>
  192. </View>
  193. {/* <Button onClick={this.ooo}>订阅</Button> */}
  194. {/* 骨架屏 */}
  195. <Skeleton
  196. isSkeletonShow={!homeData.banner && !productList.length > 0}
  197. />
  198. {/* 分类列表组件 */}
  199. {homeData.icon && <CategoryList categoryList={homeData.icon} />}
  200. {/* 运营区域 */}
  201. {homeData.recommend && <OperationArea recommend={homeData.recommend} />}
  202. {/* 邀请人推荐 */}
  203. {/* <RecommendList /> */}
  204. {/* 商品列表 */}
  205. <View className="product-list-wrap">
  206. <ProductList
  207. productList={productList}
  208. loading={loading}
  209. onShareProduct={this.handleShare}
  210. />
  211. </View>
  212. {/* 添加浮动按钮 */}
  213. <View className="float-buttons">
  214. <View
  215. className="float-btn"
  216. onClick={() =>
  217. Taro.navigateTo({
  218. url: `/pages/memberSub/storeManagement/index?isManager=${true}`,
  219. })
  220. }
  221. >
  222. <Image className="my-shop-bg" src={myStoreIcon} mode="aspectFit" />
  223. <Text>我的{"\n"}小店</Text>
  224. </View>
  225. <View className="float-btn" onClick={this.handleBackTop}>
  226. <Image className="back-top" src={backTopIcon} mode="aspectFit" />
  227. </View>
  228. </View>
  229. {/* 分享弹窗 */}
  230. </View>
  231. );
  232. }
  233. }