index.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import React, { Component, createRef } from "react";
  2. import { View, Picker, Image, Text } from "@tarojs/components";
  3. import { AtTabs, AtTabsPane, AtCheckbox } from "taro-ui";
  4. import Taro from "@tarojs/taro";
  5. import "./index.less";
  6. import {
  7. getMyShopList,
  8. deleteShopProduct,
  9. updateProductSort,
  10. getAllTags
  11. } from "../../../service";
  12. import ProductList from "../../../components/index/ProductList"; //商品列表
  13. import add from "../../../images/productManagement/add.png";
  14. import back from "../../../images/productManagement/back.png";
  15. import manger from "../../../images/productManagement/manger.png";
  16. import selectIcon from "../../../images/productManagement/selectIcon.png";
  17. import deleteIcon from "../../../images/productManagement/delete.png";
  18. import toTop from "../../../images/productManagement/toTop.png";
  19. export default class Index extends Component {
  20. childComponentRef = React.createRef(); // 创建 ref
  21. state = {
  22. isManagementStatus: false, //是否是管理状态
  23. current: 0, //tabs坐标
  24. isSelectAll: false, //是否全选
  25. selectedOption: "首页", //商品默认名称
  26. tabList: [{ title: "在售中 (0)" }], //tabs标题
  27. options: ["首页"], //商品下拉选项
  28. checkedList: [""], //全选
  29. checkboxOption: [
  30. {
  31. value: "allValue",
  32. label: "全选",
  33. },
  34. ], //全选
  35. productList: [], // 商品列表
  36. page: 1, //页数
  37. loading: false, //加载状态
  38. totalPages: 1, // 添加总页数
  39. selectedProducts: [], // 删除商品选中id数组
  40. tags: [], // 所有闲鱼tags
  41. isNoMore: false, // 是否没有更多
  42. };
  43. componentDidShow() {
  44. this.getAllTags();
  45. }
  46. // 获取所有闲鱼tags
  47. getAllTags = async () => {
  48. const res = await getAllTags();
  49. const arr = Object.values(res).map(item => item.name);
  50. this.setState({
  51. options: arr,
  52. },()=>{
  53. this.getMyShopList(true);
  54. });
  55. }
  56. // 获取商品列表
  57. getMyShopList = async (isDelete) => {
  58. const { page } = this.state;
  59. this.setState({ loading: true });
  60. const res = await getMyShopList({
  61. tag_name: this.state.selectedOption,
  62. page,
  63. page_size: 10,
  64. });
  65. this.setState(
  66. (prevState) => ({
  67. productList: isDelete
  68. ? res.goods_list
  69. : [...prevState.productList, ...res.goods_list],
  70. totalPages: res.total_pages,
  71. loading: false,
  72. isNoMore: res.total_pages <= page,
  73. }),
  74. () => {
  75. this.setState((prevState) => ({
  76. tabList: [{ title: `在售中 (${res.total_count})` }],
  77. }));
  78. }
  79. );
  80. };
  81. // 商品下拉选项
  82. handleSelectOption = (e) => {
  83. const selectedOption = this.state.options[e.detail.value];
  84. this.setState({ selectedOption, page: 1, isNoMore: false },()=>{
  85. Taro.pageScrollTo({
  86. scrollTop: 0,
  87. duration: 300
  88. });
  89. this.getMyShopList(true);
  90. });
  91. };
  92. // 切换管理状态
  93. changeManagement = (type) => {
  94. if (type === "management") {
  95. this.setState({
  96. isManagementStatus: true,
  97. });
  98. } else {
  99. this.setState(
  100. {
  101. isManagementStatus: false,
  102. isSelectAll: false, // 重置全选状态
  103. checkedList: [], // 重置选中状态
  104. },
  105. () => {
  106. // 在清空后通过回调清除子组件中的选中状态
  107. this.childComponentRef.current.clearSelectedItems();
  108. }
  109. );
  110. }
  111. };
  112. // 全选
  113. handleChange(value) {
  114. this.setState({
  115. checkedList: value,
  116. isSelectAll: !this.state.isSelectAll,
  117. });
  118. }
  119. // 删除商品
  120. onDeleteProduct = async () => {
  121. if (this.state.selectedProducts.length == 0) {
  122. Taro.showToast({
  123. title: "请选择商品",
  124. icon: "none",
  125. });
  126. return;
  127. }
  128. const res = await deleteShopProduct({
  129. goods_ids: this.state.selectedProducts,
  130. });
  131. if (res.success == true) {
  132. this.setState((prevState) => ({
  133. productList: prevState.productList.filter(
  134. (item) => !this.state.selectedProducts.includes(item.id)
  135. ),
  136. isSelectAll: false, // 重置全选状态
  137. checkedList: [], // 重置选中状态
  138. }),
  139. () => {
  140. // 在清空后通过回调清除子组件中的选中状态
  141. this.childComponentRef.current.clearSelectedItems();
  142. this.setState((prevState) => ({
  143. tabList: [{ title: `在售中 (${prevState.productList.length})` }],
  144. }));
  145. }
  146. );
  147. Taro.showToast({
  148. title: "删除成功",
  149. icon: "none",
  150. });
  151. }
  152. };
  153. // 更新商品排序
  154. updateProductSort = async () => {
  155. if (this.state.selectedProducts.length === 0) {
  156. Taro.showToast({
  157. title: "请选择商品",
  158. icon: "none",
  159. });
  160. return;
  161. } else if (this.state.selectedProducts.length > 1) {
  162. Taro.showToast({
  163. title: "只能选择一个商品",
  164. icon: "none",
  165. });
  166. return;
  167. }
  168. const res = await updateProductSort({
  169. goods_id: this.state.selectedProducts.join(","),
  170. });
  171. if (res.success === "排序完成") {
  172. this.setState(
  173. (prevState) => {
  174. // 从 productList 中移除选中的商品
  175. const remainingProducts = prevState.productList.filter(
  176. (item) => !this.state.selectedProducts.includes(item.id)
  177. );
  178. // 将选中的商品添加到列表的开头
  179. const sortedProducts = this.state.selectedProducts.map((id) =>
  180. prevState.productList.find((item) => item.id === id)
  181. );
  182. return {
  183. productList: [...sortedProducts, ...remainingProducts],
  184. selectedProducts: [],
  185. };
  186. },
  187. () => {
  188. // 在清空后通过回调清除子组件中的选中状态
  189. this.childComponentRef.current.clearSelectedItems();
  190. }
  191. );
  192. Taro.showToast({
  193. title: "排序完成",
  194. icon: "none",
  195. });
  196. }
  197. };
  198. // 页面上拉触底
  199. onReachBottom = () => {
  200. const { page, totalPages, loading } = this.state;
  201. if (page < totalPages && !loading) {
  202. this.setState(
  203. (prevState) => ({ page: prevState.page + 1 }),
  204. () => this.getMyShopList()
  205. );
  206. }
  207. };
  208. // 添加处理选中商品的方法
  209. onDeleteProductSelect = (selectedProducts) => {
  210. // 可以将选中的商品保存到父组件的状态中
  211. this.setState({ selectedProducts });
  212. };
  213. render() {
  214. return (
  215. <View
  216. className={`index ${
  217. this.state.isManagementStatus ? "management-active" : ""
  218. }`}
  219. >
  220. {/* tab分类 */}
  221. <View className="tabs-list">
  222. <AtTabs
  223. tabList={this.state.tabList}
  224. current={this.state.current}
  225. scroll
  226. >
  227. <AtTabsPane current={this.state.current} index={0}>
  228. {/* 内容 */}
  229. <View className="tabs-content">
  230. {/* 商品下拉分类 */}
  231. <View className="product-category">
  232. <Picker
  233. mode="selector"
  234. range={this.state.options}
  235. onChange={this.handleSelectOption}
  236. >
  237. <View className="product-category-left">
  238. {this.state.selectedOption}
  239. <Image
  240. className="selectIcon"
  241. src={selectIcon}
  242. mode="aspectFit"
  243. />
  244. </View>
  245. </Picker>
  246. <View className="product-category-right">
  247. 前6款商品将推荐给直属会员
  248. </View>
  249. </View>
  250. {/* 商品列表 */}
  251. {this.state.productList && (
  252. <ProductList
  253. ref={this.childComponentRef} // 将 ref 传递给子组件
  254. productList={this.state.productList}
  255. isSelectAll={this.state.isSelectAll}
  256. isManagement={true}
  257. isManagementStatus={this.state.isManagementStatus}
  258. loading={this.state.loading}
  259. onDeleteProductSelect={this.onDeleteProductSelect}
  260. isNoMore={this.state.isNoMore}
  261. />
  262. )}
  263. </View>
  264. </AtTabsPane>
  265. </AtTabs>
  266. </View>
  267. {/* 底部购买模块 */}
  268. {!this.state.isManagementStatus ? (
  269. <View className="bottom-buy">
  270. <View
  271. onClick={() => Taro.navigateBack()}
  272. className="bottom-buy-left"
  273. >
  274. <Image src={back} mode="aspectFit" />
  275. <Text className="bottom-buy-text">返回店铺</Text>
  276. </View>
  277. <View className="bottom-buy-right">
  278. <View
  279. onClick={() => this.changeManagement("management")}
  280. className="bottom-buy-right-self"
  281. >
  282. <Image src={manger} mode="aspectFit" />
  283. <Text className="bottom-buy-text">批量管理</Text>
  284. </View>
  285. <View
  286. onClick={() =>
  287. Taro.navigateTo({
  288. url: "/pages/memberSub/productClassify/index",
  289. })
  290. }
  291. className="bottom-buy-right-share"
  292. >
  293. <Image src={add} mode="aspectFit" />
  294. <Text className="bottom-buy-text">添加商品</Text>
  295. </View>
  296. </View>
  297. </View>
  298. ) : (
  299. <View className="operation-product">
  300. <View className="operation-product-top">
  301. <View className="left">
  302. <AtCheckbox
  303. options={this.state.checkboxOption}
  304. selectedList={this.state.checkedList}
  305. onChange={this.handleChange.bind(this)}
  306. />
  307. </View>
  308. <View className="right">
  309. <View className="item" onClick={this.updateProductSort}>
  310. <Image src={toTop} mode="aspectFit" />
  311. <Text>移至顶部</Text>
  312. </View>
  313. <View onClick={this.onDeleteProduct} className="item">
  314. <Image src={deleteIcon} mode="aspectFit" />
  315. <Text>删除</Text>
  316. </View>
  317. </View>
  318. </View>
  319. <View
  320. onClick={() => this.changeManagement("finish")}
  321. className="operation-product-bottom"
  322. >
  323. 完成
  324. </View>
  325. </View>
  326. )}
  327. </View>
  328. );
  329. }
  330. }