import { Component } from "react"; import { View, Input } from "@tarojs/components"; import { AtTabs, AtIcon } from "taro-ui"; import "./index.less"; import Taro from "@tarojs/taro"; import ProductList from "../../../components/index/ProductList"; import { getBrowseShopProductList, addShopProduct, getAllTags, } from "../../../service"; import { getShareContent } from "../../../common/share"; export default class Index extends Component { state = { current: 0, // 当前选中的标签页索引 tabList: [], // 标签页列表 productList: [], // 商品列表 page: 1, //页数 loading: false, //加载状态 totalPages: 1, // 添加总页数 tabList: [], // 所有闲鱼tags value: "", // 搜索 }; handleClick(value) { this.setState( { current: value, page: 1, value: "", }, () => { Taro.pageScrollTo({ scrollTop: 0, duration: 300, }); this.getBrowseShopProductList(true); } ); } componentDidMount() { this.getAllTags(); } // 获取商品列表 getBrowseShopProductList = async (isAdd = false) => { const { page } = this.state; this.setState({ loading: true }); const res = await getBrowseShopProductList({ tag_name: this.state.tabList[this.state.current].title, page, page_size: 10, search_name: this.state.value, }); this.setState((prevState) => ({ productList: isAdd ? res.goods_list : [...prevState.productList, ...res.goods_list], totalPages: res.total_pages, loading: false, })); }; // 获取所有闲鱼tags getAllTags = async () => { let res = await getAllTags(); res = res.map((item, index) => ({ title: item.name, })); this.setState( { tabList: res, }, () => { this.getBrowseShopProductList(true); } ); }; // 输入 handleChange = (e) => { this.setState({ value: e.target.value }); }; // 搜索 handleSearch = () => { this.getBrowseShopProductList(true); }; // 添加商品 onAddProduct = (productId, index) => { addShopProduct({ goods_ids: [productId], }).then((res) => { if (res.success === true) { this.setState((prevState) => { const newList = [...prevState.productList]; newList[index] = { ...newList[index], is_collected: true }; return { productList: newList }; }); Taro.showToast({ title: "添加成功", icon: "none", }); } }); }; // 配置分享内容 onShareAppMessage() { return getShareContent(); } // 页面上拉触底 onReachBottom = () => { const { page, totalPages, loading } = this.state; if (page < totalPages && !loading) { this.setState( (prevState) => ({ page: prevState.page + 1 }), () => this.getBrowseShopProductList() ); } }; render() { return ( {/* 顶部固定 */} {/* 搜索 */} 搜索 {/* 商品列表 */} this.onAddProduct(productId, index) } /> {/* 底部按钮 */} Taro.navigateBack()} className="bottom-button"> 返回店铺首页 ); } }