index.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { Component } from "react";
  2. import { View, Input } from "@tarojs/components";
  3. import { AtTabs, AtIcon } from "taro-ui";
  4. import "./index.less";
  5. import Taro from "@tarojs/taro";
  6. import ProductList from "../../../components/index/ProductList";
  7. import {
  8. getBrowseShopProductList,
  9. addShopProduct,
  10. getAllTags,
  11. } from "../../../service";
  12. import { getShareContent } from "../../../common/share";
  13. export default class Index extends Component {
  14. state = {
  15. current: 0, // 当前选中的标签页索引
  16. tabList: [], // 标签页列表
  17. productList: [], // 商品列表
  18. page: 1, //页数
  19. loading: false, //加载状态
  20. totalPages: 1, // 添加总页数
  21. tabList: [], // 所有闲鱼tags
  22. value: "", // 搜索
  23. };
  24. handleClick(value) {
  25. this.setState(
  26. {
  27. current: value,
  28. page: 1,
  29. value: "",
  30. },
  31. () => {
  32. Taro.pageScrollTo({
  33. scrollTop: 0,
  34. duration: 300,
  35. });
  36. this.getBrowseShopProductList(true);
  37. }
  38. );
  39. }
  40. componentDidMount() {
  41. this.getAllTags();
  42. }
  43. // 获取商品列表
  44. getBrowseShopProductList = async (isAdd = false) => {
  45. const { page } = this.state;
  46. this.setState({ loading: true });
  47. const res = await getBrowseShopProductList({
  48. tag_name: this.state.tabList[this.state.current].title,
  49. page,
  50. page_size: 10,
  51. search_name: this.state.value,
  52. });
  53. this.setState((prevState) => ({
  54. productList: isAdd
  55. ? res.goods_list
  56. : [...prevState.productList, ...res.goods_list],
  57. totalPages: res.total_pages,
  58. loading: false,
  59. }));
  60. };
  61. // 获取所有闲鱼tags
  62. getAllTags = async () => {
  63. let res = await getAllTags();
  64. res = res.map((item, index) => ({
  65. title: item.name,
  66. }));
  67. this.setState(
  68. {
  69. tabList: res,
  70. },
  71. () => {
  72. this.getBrowseShopProductList(true);
  73. }
  74. );
  75. };
  76. // 输入
  77. handleChange = (e) => {
  78. this.setState({ value: e.target.value });
  79. };
  80. // 搜索
  81. handleSearch = () => {
  82. this.getBrowseShopProductList(true);
  83. };
  84. // 添加商品
  85. onAddProduct = (productId, index) => {
  86. addShopProduct({
  87. goods_ids: [productId],
  88. }).then((res) => {
  89. if (res.success === true) {
  90. this.setState((prevState) => {
  91. const newList = [...prevState.productList];
  92. newList[index] = { ...newList[index], is_collected: true };
  93. return { productList: newList };
  94. });
  95. Taro.showToast({
  96. title: "添加成功",
  97. icon: "none",
  98. });
  99. }
  100. });
  101. };
  102. // 配置分享内容
  103. onShareAppMessage() {
  104. return getShareContent();
  105. }
  106. // 页面上拉触底
  107. onReachBottom = () => {
  108. const { page, totalPages, loading } = this.state;
  109. if (page < totalPages && !loading) {
  110. this.setState(
  111. (prevState) => ({ page: prevState.page + 1 }),
  112. () => this.getBrowseShopProductList()
  113. );
  114. }
  115. };
  116. render() {
  117. return (
  118. <View className="index">
  119. {/* 顶部固定 */}
  120. <View className="top-fixed">
  121. <AtTabs
  122. scroll
  123. current={this.state.current}
  124. tabList={this.state.tabList}
  125. onClick={this.handleClick.bind(this)}
  126. ></AtTabs>
  127. {/* 搜索 */}
  128. <View className="search-container">
  129. <View className="search-bar">
  130. <AtIcon
  131. className="search-icon"
  132. value="search"
  133. size="20"
  134. color="#ACACAC"
  135. ></AtIcon>
  136. <Input
  137. type="text"
  138. className="search-input"
  139. placeholder=""
  140. value={this.state.value}
  141. onInput={this.handleChange}
  142. />
  143. <View onClick={this.handleSearch} className="search-button">
  144. 搜索
  145. </View>
  146. </View>
  147. </View>
  148. </View>
  149. {/* 商品列表 */}
  150. <ProductList
  151. loading={this.state.loading}
  152. productList={this.state.productList}
  153. isProductClassify={true}
  154. onAddProduct={(productId, index) =>
  155. this.onAddProduct(productId, index)
  156. }
  157. />
  158. {/* 底部按钮 */}
  159. <View onClick={() => Taro.navigateBack()} className="bottom-button">
  160. 返回店铺首页
  161. </View>
  162. </View>
  163. );
  164. }
  165. }