index.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { Component } from "react";
  2. import { View, Image } from "@tarojs/components";
  3. import "./index.less";
  4. import Taro from "@tarojs/taro";
  5. import seckillImg from "../../../images/seckill/seckillTop.jpg";
  6. import ProductList from "../../../components/index/ProductList";
  7. import { getRecommendProductList, addLog } from "../../../service";
  8. import { formatDate } from "../../../common/time";
  9. import { getShareContent } from "../../../common/share";
  10. export default class Index extends Component {
  11. state = {
  12. productList: [], //推荐商品列表
  13. page: 1, //页数
  14. loading: false, //加载状态
  15. totalPages: 1, // 添加总页数
  16. isNoMore: false, // 是否没有更多
  17. isDirect: false, // 是否直接跳转
  18. isBanner: false, // 是否是banner跳转
  19. isShare: false, // 是否是分享跳转
  20. shareUserId: "", // 分享用户id
  21. };
  22. componentDidMount() {
  23. const { isDirect, isBanner, isShare, shareUserId } =
  24. Taro.getCurrentInstance().router.params || "";
  25. this.setState(
  26. {
  27. isDirect: isDirect || false,
  28. isBanner: isBanner || false,
  29. isShare: isShare || false,
  30. shareUserId: shareUserId || "",
  31. },
  32. () => {
  33. this.handleLog(
  34. isDirect
  35. ? "direct_to_fish_seckill"
  36. : isBanner
  37. ? "banner_to_fish_seckill"
  38. : isShare
  39. ? "share_to_fish_seckill"
  40. : "direct_to_fish_seckill"
  41. );
  42. }
  43. );
  44. this.getRecommendProductList();
  45. }
  46. // 获取推荐商品列表
  47. getRecommendProductList = async () => {
  48. const { page } = this.state;
  49. this.setState({ loading: true });
  50. const res = await getRecommendProductList({
  51. tag_id: 120,
  52. page,
  53. page_size: 10,
  54. });
  55. this.setState((prevState) => ({
  56. productList: [...prevState.productList, ...res.goods_list],
  57. totalPages: res.total_pages,
  58. loading: false,
  59. isNoMore: res.total_pages <= page,
  60. }));
  61. };
  62. // 埋点
  63. handleLog = (event_type) => {
  64. const dayid = formatDate(new Date(), "YYYY-MM-DD");
  65. const event_type_title =
  66. event_type == "banner_to_fish_seckill"
  67. ? "从banner 跳转到鱼市秒杀"
  68. : event_type == "share_to_fish_seckill"
  69. ? "从分享链接跳转到鱼市秒杀"
  70. : event_type == "direct_to_fish_seckill"
  71. ? "直接点击进入鱼市秒杀"
  72. : "直接点击进入鱼市秒杀";
  73. let userId = "";
  74. if (Taro.getStorageSync("loginInfo")) {
  75. userId = Taro.getStorageSync("loginInfo").id;
  76. }
  77. // 构建ext对象,包含share_id
  78. let ext = "";
  79. if (this.state.shareUserId) {
  80. ext = JSON.stringify({
  81. share_users_id: this.state.shareUserId,
  82. });
  83. }
  84. addLog({
  85. users_id: userId||0,
  86. goods_id: 0,
  87. event_type_title,
  88. event_type,
  89. dayid,
  90. ext,
  91. });
  92. };
  93. // 配置分享内容
  94. onShareAppMessage() {
  95. let shareUserId = "";
  96. if (Taro.getStorageSync("loginInfo")) {
  97. shareUserId = Taro.getStorageSync("loginInfo").id;
  98. }
  99. const shareConfig = getShareContent();
  100. if (shareConfig.title == "默认分享标题") {
  101. return {
  102. title: "鱼市秒杀",
  103. path: `/pages/indexSub/seckillIndex/index?isShare=true&shareUserId=${shareUserId}`,
  104. imageUrl: "",
  105. };
  106. } else {
  107. return getShareContent();
  108. }
  109. }
  110. // 页面上拉触底
  111. onReachBottom = () => {
  112. const { page, totalPages, loading } = this.state;
  113. if (page < totalPages && !loading) {
  114. this.setState(
  115. (prevState) => ({ page: prevState.page + 1 }),
  116. () => this.getRecommendProductList()
  117. );
  118. }
  119. };
  120. render() {
  121. return (
  122. <View className="index">
  123. <Image className="seckill" src={seckillImg} mode="aspectFill" />
  124. {/* 商品列表 */}
  125. <View className="product-list">
  126. <ProductList
  127. isSeckill={true}
  128. productList={this.state.productList}
  129. loading={this.state.loading}
  130. isNoMore={this.state.isNoMore}
  131. shareUserId={this.state.shareUserId}
  132. />
  133. </View>
  134. </View>
  135. );
  136. }
  137. }