import {Image, ScrollView, View } from "@tarojs/components" import Popup from "@/components/popup" import classnames from "classnames"; import MCheckbox from "@/components/checkbox"; import LoadingCard from "@/components/loadingCard"; import InfiniteScroll from "@/components/infiniteScroll"; import styles from "./index.module.scss" import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import Taro from "@tarojs/taro"; import { alert, goLink } from "@/common/common"; import {GetShoppingCartApi, DelShoppingCartApi, UpdateShoppingCartApi} from "@/api/shopCart" import { formatHashTag, formatImgUrl, formatPriceDiv } from "@/common/fotmat"; import { setParam } from "@/common/system"; import { debounce } from "@/common/util"; import Counter from "../counter"; type param = { show?: true|false, onClose?: () => void } export default ({show = false, onClose}: param) => { const selectList = [ {value:0, title:'大货', unit:'条', eunit:'kg', step:1, digits:0, minNum:1, maxNum:100000, defaultNum:1}, {value:1,title:'剪板', unit:'米', eunit:'m', step:1, digits:2, minNum:0.5, maxNum:9.99, defaultNum:1}, {value:2,title:'散剪', unit:'米', eunit:'kg', step:1, digits:2, minNum:5, maxNum:100000, defaultNum:10}, ] const [selectIndex, setSelectIndex] = useState(0) const selectProduct = (index:number) => { setSelectIndex(index) } useEffect(() => { resetList() setSelectStatus(true) }, [selectIndex]) //重置勾选数据 const resetList = () => { list?.map(item => { if(selectIndex == item.sale_mode || selectIndex == -1) { item.select = true } else { item.select = false } }) setList([...list]) } //获取数据 const [list, setList] = useState([]) const [loading, setLoading] = useState(false) const {fetchData} = GetShoppingCartApi() const getShoppingCart = async () => { const {data} = await fetchData() let color_list = data.color_list||[] initList(color_list) setList(color_list) setLoading(false) } //初始化全部数据 const initList = (color_list) => { color_list?.map(item => { if(selectIndex == item.sale_mode) item.select = true item.count = formatCount(item) }) } //显示是展示数据 useEffect(() => { if(!show) { setList([]) setSelectIndex(0) } else { setLoading(true) getShoppingCart() } }, [show]) useEffect(() => { return () => { setList([]) } }, []) const [showPopup, setShowPopup] = useState(false) useEffect(() => { setShowPopup(show) }, [show]) //全选反选 const [selectStatus, setSelectStatus] = useState(false) const selectAll = () => { list.map(item => { if(selectIndex == item.sale_mode || selectIndex == -1) item.select = !selectStatus }) setSelectStatus(!selectStatus) setList([...list]) } //checkbox选中回调 const selectCallBack = (item) => { item.select = true checkSelect() setList([...list]) } //checkbox选中判断是否全部选中,全部选中后是全选,否则反选 const checkSelect = () => { let list_count = 0 let select_count = 0 list?.map(item => { if(selectIndex == -1 || selectIndex == item.sale_mode) { list_count ++ if(item.select) select_count++ } }) setSelectStatus(select_count == list_count) } //checkbox关闭回调 const colseCallBack = (item) => { item.select = false checkSelect() setList([...list]) } //popup关闭 const closePopup = () => { onClose?.() setShowPopup(false) } //删除购物车内容 const {fetchData:delShopFetchData} = DelShoppingCartApi() const delSelect = () => { getSelectId() if(selectIds.current.length <= 0) return alert.none('请选择要删除的面料!') Taro.showModal({ content: '删除所选商品?', success: async function (res) { if (res.confirm) { const res = await delShopFetchData({id:selectIds.current}) if(res.success) { getShoppingCart() Taro.showToast({ title: '成功', icon: 'success', }) } else { Taro.showToast({ title: res.msg, icon: 'none', }) } } else if (res.cancel) { console.log('用户点击取消') } } }) } //获取面料选中的id const selectIds = useRef([]) const getSelectId = () => { selectIds.current = [] list?.map(item => { if(selectIndex == -1 || selectIndex == item.sale_mode) { item.select&&selectIds.current.push(item.id) } }) } //格式化金额 const formatPirce = useCallback((price) => { return Number(formatPriceDiv(price)) }, []) //格式化数量 const formatCount = useCallback((item) => { console.log('item:::',item) return item.sale_mode == 0? item.roll : (item.length/100) }, []) //格式化单位 const formatUnit = useCallback((item) => { return item.sale_mode == 0? '条':'米' }, []) //预估金额和总条数 const estimatePrice = useMemo(() => { let estimate_amount = 0 let product_list = new Set() //面料 let color_count = 0 //颜色数量 let all_count = 0 //总数量 list.map(item => { if(item.select) { estimate_amount += item.estimate_amount product_list.add(item.product_id) color_count++ all_count += item.sale_mode == 0?item.roll: (item.length) } }) let all_count_text = selectIndex == 0?all_count + '条': (all_count/100) + '米' return {price: Number(formatPriceDiv(estimate_amount)).toFixed(2), countText: `已选${product_list.size}种面料,${color_count}个颜色,共${all_count_text}`, color_count} },[list]) //去结算 const orderDetail = debounce(() => { getSelectId() if(selectIds.current.length == 0) { alert.error('请选择面料') } else { let ids = selectIds.current.join('-') setParam({ids, sale_mode:selectIndex}) //临时存储 closePopup() goLink('/pages/order/comfirm') } }, 500) //计数组件-当后端修改完成才修改前端显示 const {fetchData: fetchDataUpdateShoppingCart} = UpdateShoppingCartApi() const [UpdateShoppingCartLoading, setUpdateShoppingCartLoading] = useState(false) const getInputValue = debounce(async (num, item) => { let roll = item.sale_mode == 0?parseFloat(num):0 let length = item.sale_mode != 0?(parseFloat(num)*100):0 setUpdateShoppingCartLoading(() => true) let res = await fetchDataUpdateShoppingCart({id: item.id, roll, length}) setUpdateShoppingCartLoading(() => false) if(res.success) { getShoppingCart() } }, 300) return ( closePopup()} > {!selectStatus?'全选':'反选'} 删除所选 {estimatePrice.countText} {selectList.map((item) => { return selectProduct(item.value)} className={classnames(styles.search_item, (selectIndex==item.value)&&styles.search_item_select)}>{item.title} })} {loading&&} {!loading&&list?.length > 0&& {list?.map((item, index) => { return selectCallBack(item)} onClose={() => colseCallBack(item)}/> {formatHashTag(item.product_code, item.product_name)} {item.product_color_code +' ' + item.product_color_name} {item.sale_mode_name} {formatPirce(item.sale_price)}/{selectList[selectIndex].eunit} {/* ×{formatCount(item)}{selectList[selectIndex].unit} */} getInputValue(e, item)} defaultNum={item.count} step={selectList[selectIndex].step} digits={selectList[selectIndex].digits} onClickBtn={(e) => getInputValue(e, item)} unit={formatUnit(item)} minNum={selectList[selectIndex].minNum} maxNum={selectList[selectIndex].maxNum} disable={UpdateShoppingCartLoading} /> })} } {!loading&&list?.length == 0 && 暂未选择商品 去选购 } {estimatePrice.price} 预估金额 orderDetail()}> 去结算({estimatePrice.color_count}) ) }