index.jsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { Component } from "react";
  2. import { View, Image } from "@tarojs/components";
  3. import "./index.less";
  4. import Taro from "@tarojs/taro";
  5. import ProductList from "../../../components/index/ProductList";
  6. import {
  7. getRecommendProductList,
  8. getShareJump,
  9. bindShareJump,
  10. } from "../../../service";
  11. import { handleLog } from "../../../common/track";
  12. import { getShareContent } from "../../../common/share";
  13. export default class Index extends Component {
  14. state = {
  15. title: "", // 页面标题
  16. id: "", // 页面类型id
  17. productList: [], //推荐商品列表
  18. page: 1, //页数
  19. loading: false, //加载状态
  20. totalPages: 1, // 添加总页数
  21. isNoMore: false, // 是否没有更多
  22. isDirect: false, // 是否直接跳转
  23. isBanner: false, // 是否是banner跳转
  24. isShare: false, // 是否是分享跳转
  25. shareUserId: "", // 分享用户id
  26. img: "", // 图片
  27. isShowBack: false, // 是否显示返回
  28. };
  29. componentDidMount() {
  30. const { isDirect, isBanner, isShare, shareUserId, title, id, shareJump,shareType,isShowBack } =
  31. Taro.getCurrentInstance().router.params || "";
  32. let session_key = Taro.getStorageSync("session_key");
  33. this.setState(
  34. {
  35. isDirect: isDirect || false,
  36. isBanner: isBanner || false,
  37. isShare: isShare === "true" || false,
  38. shareUserId: shareUserId || "",
  39. title: title || "",
  40. id: id || "",
  41. isShowBack: isShowBack === "true" || false,
  42. },
  43. () => {
  44. // 设置页面标题
  45. Taro.setNavigationBarTitle({
  46. title: this.state.title || "鱼市",
  47. });
  48. if (isShare) {
  49. Taro.hideHomeButton();//隐藏返回首页
  50. }
  51. handleLog({
  52. event_type: isDirect
  53. ? `direct_to_${this.state.id}_${this.state.title}`
  54. : isBanner
  55. ? `banner_to_${this.state.id}_${this.state.title}`
  56. : isShare
  57. ? `share_to_${this.state.id}_${this.state.title}`
  58. : `direct_to_${this.state.id}_${this.state.title}`,
  59. event_type_title: isDirect
  60. ? `直接点击进入${this.state.title}`
  61. : isBanner
  62. ? `从banner 跳转到${this.state.title}`
  63. : isShare
  64. ? `从分享链接跳转到${this.state.title}`
  65. : `直接点击进入${this.state.title}`,
  66. goods_id: 0,
  67. shareUserId: this.state.shareUserId,
  68. });
  69. this.getRecommendProductList();
  70. // 获取分享跳转标识
  71. shareJump &&
  72. Taro.setStorage({
  73. key: "shareJump",
  74. data: shareJump,
  75. });
  76. shareType &&
  77. Taro.setStorage({
  78. key: "shareType",
  79. data: shareType,
  80. });
  81. if (session_key && shareJump) {
  82. this.bindShareJump(); //绑定分享跳转标识
  83. }
  84. }
  85. );
  86. }
  87. // 获取推荐商品列表
  88. getRecommendProductList = async () => {
  89. const { page } = this.state;
  90. this.setState({ loading: true });
  91. const res = await getRecommendProductList({
  92. tag_id: this.state.id,
  93. page,
  94. page_size: 10,
  95. });
  96. this.setState((prevState) => ({
  97. productList: [...prevState.productList, ...res.goods_list],
  98. totalPages: res.total_pages,
  99. loading: false,
  100. isNoMore: res.total_pages <= page,
  101. img: res.img,
  102. }));
  103. };
  104. // 绑定分享跳转标识
  105. bindShareJump = async () => {
  106. let shareJump = Taro.getStorageSync("shareJump");
  107. await bindShareJump({
  108. share_type: 3,
  109. share_unique_value: shareJump,
  110. });
  111. Taro.removeStorageSync("shareJump");
  112. Taro.removeStorageSync("shareType");
  113. };
  114. // 配置分享内容
  115. onShareAppMessage(res) {
  116. let shareUserId = "";
  117. if (Taro.getStorageSync("loginInfo")) {
  118. shareUserId = Taro.getStorageSync("loginInfo").id;
  119. }
  120. if (res.from == "menu") {
  121. return getShareJump({
  122. share_type: 3,
  123. share_id: this.state.id,
  124. }).then((res) => {
  125. return {
  126. title: `${this.state.title}`,
  127. path: `/pages/indexSub/seckillIndex/index?isShare=true&shareUserId=${shareUserId}&title=${this.state.title}&id=${this.state.id}&&shareJump=${res.share_unique_value}&&shareType=3&&isShowBack=true`,
  128. imageUrl: "",
  129. };
  130. });
  131. } else {
  132. return getShareContent();
  133. }
  134. }
  135. // 页面上拉触底
  136. onReachBottom = () => {
  137. const { page, totalPages, loading } = this.state;
  138. if (page < totalPages && !loading) {
  139. this.setState(
  140. (prevState) => ({ page: prevState.page + 1 }),
  141. () => this.getRecommendProductList()
  142. );
  143. }
  144. };
  145. render() {
  146. return (
  147. <View className="index">
  148. <Image className="seckill" src={this.state.img} mode="widthFix" />
  149. {/* 商品列表 */}
  150. <View className="product-list">
  151. <ProductList
  152. isSeckill={true}
  153. productList={this.state.productList}
  154. loading={this.state.loading}
  155. isNoMore={this.state.isNoMore}
  156. shareUserId={this.state.shareUserId}
  157. tagTitle={this.state.title}
  158. isShare={this.state.isShare}
  159. isShowBack={this.state.isShowBack}
  160. />
  161. </View>
  162. </View>
  163. );
  164. }
  165. }