index.jsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { Component } from "react";
  2. import { View, Image } from "@tarojs/components";
  3. import "./index.less";
  4. import seckillImg from "../../../images/seckill/heighTop.jpg";
  5. import ProductList from "../../../components/index/ProductList";
  6. import { getRecommendProductList } from "../../../service";
  7. import { getShareContent } from "../../../common/share";
  8. export default class Index extends Component {
  9. state = {
  10. productList: [], //推荐商品列表
  11. page: 1, //页数
  12. loading: false, //加载状态
  13. totalPages: 1, // 添加总页数
  14. };
  15. componentDidMount() {
  16. this.getRecommendProductList();
  17. }
  18. // 获取推荐商品列表
  19. getRecommendProductList = async () => {
  20. const { page } = this.state;
  21. this.setState({ loading: true });
  22. const res = await getRecommendProductList({
  23. tag_id: 121,
  24. page,
  25. page_size: 10,
  26. });
  27. this.setState((prevState) => ({
  28. productList: [...prevState.productList, ...res.goods_list],
  29. totalPages: res.total_pages,
  30. loading: false,
  31. }));
  32. };
  33. // 配置分享内容
  34. onShareAppMessage() {
  35. return getShareContent();
  36. }
  37. // 页面上拉触底
  38. onReachBottom = () => {
  39. const { page, totalPages, loading } = this.state;
  40. if (page < totalPages && !loading) {
  41. this.setState(
  42. (prevState) => ({ page: prevState.page + 1 }),
  43. () => this.getRecommendProductList()
  44. );
  45. }
  46. };
  47. render() {
  48. return (
  49. <View className="index">
  50. <Image className="seckill" src={seckillImg} mode="aspectFill" />
  51. {/* 商品列表 */}
  52. <View className="product-list">
  53. <ProductList
  54. isHighCommission={true}
  55. productList={this.state.productList}
  56. loading={this.state.loading}
  57. />
  58. </View>
  59. </View>
  60. );
  61. }
  62. }