index.jsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. getFishMarketTags,
  11. } from "../../../service";
  12. import { handleLog } from "../../../common/track";
  13. import { getShareContent } from "../../../common/share";
  14. import { AtTabs } from "taro-ui";
  15. export default class Index extends Component {
  16. state = {
  17. title: "", // 页面标题
  18. id: "", // 页面类型id
  19. productList: [], //推荐商品列表
  20. page: 1, //页数
  21. loading: false, //加载状态
  22. totalPages: 1, // 添加总页数
  23. isNoMore: false, // 是否没有更多
  24. isDirect: false, // 是否直接跳转
  25. isBanner: false, // 是否是banner跳转
  26. isShare: false, // 是否是分享跳转
  27. shareUserId: "", // 分享用户id
  28. img: "", // 图片
  29. isShowBack: false, // 是否显示返回
  30. current: 0, // 当前选中的tab索引
  31. tabList: [], // 标签列表
  32. isTabFixed: false, // 添加新的状态来控制tab是否固定
  33. };
  34. componentDidMount() {
  35. const {
  36. isDirect,
  37. isBanner,
  38. isShare,
  39. shareUserId,
  40. title,
  41. id,
  42. shareJump,
  43. shareType,
  44. isShowBack,
  45. } = Taro.getCurrentInstance().router.params || "";
  46. let session_key = Taro.getStorageSync("session_key");
  47. this.setState(
  48. {
  49. isDirect: isDirect || false,
  50. isBanner: isBanner || false,
  51. isShare: isShare === "true" || false,
  52. shareUserId: shareUserId || "",
  53. title: title || "",
  54. id: id || "",
  55. isShowBack: isShowBack === "true" || false,
  56. },
  57. () => {
  58. // 设置页面标题
  59. Taro.setNavigationBarTitle({
  60. title: this.state.title || "鱼市",
  61. });
  62. if (isShare) {
  63. Taro.hideHomeButton(); //隐藏返回首页
  64. }
  65. handleLog({
  66. event_type: isDirect
  67. ? `direct_to_${this.state.id}_${this.state.title}`
  68. : isBanner
  69. ? `banner_to_${this.state.id}_${this.state.title}`
  70. : isShare
  71. ? `share_to_${this.state.id}_${this.state.title}`
  72. : `direct_to_${this.state.id}_${this.state.title}`,
  73. event_type_title: isDirect
  74. ? `直接点击进入${this.state.title}`
  75. : isBanner
  76. ? `从banner 跳转到${this.state.title}`
  77. : isShare
  78. ? `从分享链接跳转到${this.state.title}`
  79. : `直接点击进入${this.state.title}`,
  80. goods_id: 0,
  81. shareUserId: this.state.shareUserId,
  82. });
  83. this.getFishMarketTags(); //获取标签
  84. // 获取分享跳转标识
  85. shareJump &&
  86. Taro.setStorage({
  87. key: "shareJump",
  88. data: shareJump,
  89. });
  90. shareType &&
  91. Taro.setStorage({
  92. key: "shareType",
  93. data: shareType,
  94. });
  95. if (session_key && shareJump) {
  96. this.bindShareJump(); //绑定分享跳转标识
  97. }
  98. }
  99. );
  100. }
  101. // 获取标签
  102. getFishMarketTags = async () => {
  103. let res = await getFishMarketTags({
  104. tag_id: this.state.id,
  105. });
  106. res = res.map((item, index) => ({
  107. title: item.tab_name
  108. }));
  109. this.setState({ tabList: res }, () => {
  110. this.getRecommendProductList(); //获取商品
  111. });
  112. };
  113. // 获取推荐商品列表
  114. getRecommendProductList = async (isRefresh = false) => {
  115. const { page } = this.state;
  116. this.setState({ loading: true });
  117. const res = await getRecommendProductList({
  118. tag_id: this.state.id,
  119. page,
  120. page_size: 10,
  121. tag_name: this.state.tabList[this.state.current].title,
  122. });
  123. this.setState((prevState) => ({
  124. productList: isRefresh
  125. ? res.goods_list
  126. : [...prevState.productList, ...res.goods_list],
  127. totalPages: res.total_pages,
  128. loading: false,
  129. isNoMore: res.total_pages <= page,
  130. img: res.img,
  131. }));
  132. };
  133. // 绑定分享跳转标识
  134. bindShareJump = async () => {
  135. let shareJump = Taro.getStorageSync("shareJump");
  136. await bindShareJump({
  137. share_type: 3,
  138. share_unique_value: shareJump,
  139. });
  140. Taro.removeStorageSync("shareJump");
  141. Taro.removeStorageSync("shareType");
  142. };
  143. // 点击标签页
  144. handleClick(value) {
  145. if (this.state.current == value) return;
  146. this.setState(
  147. {
  148. current: value,
  149. page: 1,
  150. productList: [],
  151. },
  152. () => {
  153. this.getRecommendProductList(true);
  154. }
  155. );
  156. }
  157. // 添加页面滚动监听方法
  158. onPageScroll = (e) => {
  159. const { isTabFixed } = this.state;
  160. // 获取 product-list-box 到顶部的距离
  161. const query = Taro.createSelectorQuery();
  162. query
  163. .select(".product-list")
  164. .boundingClientRect((rect) => {
  165. if (rect) {
  166. // 当元素顶部接触到屏幕顶部时切换状态
  167. const shouldFix = rect.top <= 0;
  168. if (shouldFix !== isTabFixed) {
  169. this.setState({ isTabFixed: shouldFix });
  170. }
  171. }
  172. })
  173. .exec();
  174. };
  175. // 配置分享内容
  176. onShareAppMessage(res) {
  177. let shareUserId = "";
  178. if (Taro.getStorageSync("loginInfo")) {
  179. shareUserId = Taro.getStorageSync("loginInfo").id;
  180. }
  181. if (res.from == "menu") {
  182. return getShareJump({
  183. share_type: 3,
  184. share_id: this.state.id,
  185. }).then((res) => {
  186. return {
  187. title: `${this.state.title}`,
  188. 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`,
  189. imageUrl: "",
  190. };
  191. });
  192. } else {
  193. return getShareContent();
  194. }
  195. }
  196. // 页面上拉触底
  197. onReachBottom = () => {
  198. const { page, totalPages, loading } = this.state;
  199. if (page < totalPages && !loading) {
  200. this.setState(
  201. (prevState) => ({ page: prevState.page + 1 }),
  202. () => this.getRecommendProductList()
  203. );
  204. }
  205. };
  206. render() {
  207. const { isTabFixed } = this.state;
  208. return (
  209. <View className="index">
  210. <Image className="seckill" src={this.state.img} mode="widthFix" />
  211. {/* 商品列表 */}
  212. <View className="product-list">
  213. {/* tabs切换 */}
  214. <View className={`tabs ${isTabFixed ? "fixed" : ""}`}>
  215. <AtTabs
  216. scroll
  217. current={this.state.current}
  218. tabList={this.state.tabList}
  219. onClick={this.handleClick.bind(this)}
  220. ></AtTabs>
  221. </View>
  222. <ProductList
  223. isSeckill={true}
  224. productList={this.state.productList}
  225. loading={this.state.loading}
  226. isNoMore={this.state.isNoMore}
  227. shareUserId={this.state.shareUserId}
  228. tagTitle={this.state.title}
  229. isShare={this.state.isShare}
  230. isShowBack={this.state.isShowBack}
  231. />
  232. </View>
  233. </View>
  234. );
  235. }
  236. }