index.jsx 7.6 KB

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