对接购物车列表
This commit is contained in:
@@ -5,11 +5,11 @@ import MCheckbox from "@/components/checkbox";
|
||||
import LoadingCard from "@/components/loadingCard";
|
||||
import InfiniteScroll from "@/components/infiniteScroll";
|
||||
import styles from "./index.module.scss"
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import Taro from "@tarojs/taro";
|
||||
import { goLink } from "@/common/common";
|
||||
import {GetShoppingCartApi} from "@/api/shopCart"
|
||||
import { formatHashTag, formatMillionYuan } from "@/common/fotmat";
|
||||
import {GetShoppingCartApi, DelShoppingCartApi} from "@/api/shopCart"
|
||||
import { formatHashTag, formatImgUrl, formatMillionYuan } from "@/common/fotmat";
|
||||
|
||||
type param = {
|
||||
show?: true|false,
|
||||
@@ -17,17 +17,35 @@ type param = {
|
||||
}
|
||||
export default ({show = false, onClose}: param) => {
|
||||
const selectList = [
|
||||
{title:'不限', unit:'', eunit:''},
|
||||
{title:'大货', unit:'件', eunit:'kg'},
|
||||
{title:'剪板', unit:'米', eunit:'m'},
|
||||
{title:'散剪', unit:'米', eunit:'kg'},
|
||||
{value:-1, title:'不限', unit:'', eunit:''},
|
||||
{value:0, title:'大货', unit:'件', eunit:'kg'},
|
||||
{value:1,title:'剪板', unit:'米', eunit:'m'},
|
||||
{value:2,title:'散剪', unit:'米', eunit:'kg'},
|
||||
|
||||
]
|
||||
|
||||
const [selectIndex, setSelectIndex] = useState(0)
|
||||
const [selectIndex, setSelectIndex] = useState(-1)
|
||||
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<any[]>([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
@@ -35,15 +53,29 @@ export default ({show = false, onClose}: param) => {
|
||||
const getShoppingCart = async () => {
|
||||
setLoading(true)
|
||||
const {data} = await fetchData()
|
||||
console.log('color_list::', data)
|
||||
setList(data.color_list)
|
||||
let color_list = data.color_list||[]
|
||||
initList(color_list)
|
||||
setList(color_list)
|
||||
setLoading(false)
|
||||
}
|
||||
|
||||
//初始化全部数据
|
||||
const initList = (color_list) => {
|
||||
if(selectIndex == -1) {
|
||||
color_list?.map(item => {
|
||||
item.select = true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//显示是展示数据
|
||||
useEffect(() => {
|
||||
if(!show) return
|
||||
getShoppingCart()
|
||||
if(!show) {
|
||||
setList([])
|
||||
setSelectIndex(-1)
|
||||
} else {
|
||||
getShoppingCart()
|
||||
}
|
||||
}, [show])
|
||||
|
||||
useEffect(() => {
|
||||
@@ -57,11 +89,12 @@ export default ({show = false, onClose}: param) => {
|
||||
setShowPopup(show)
|
||||
}, [show])
|
||||
|
||||
|
||||
//全选反选
|
||||
const [selectStatus, setSelectStatus] = useState(false)
|
||||
const selectAll = () => {
|
||||
list.map(item => {
|
||||
item.select = !selectStatus
|
||||
if(selectIndex == item.sale_mode || selectIndex == -1)
|
||||
item.select = !selectStatus
|
||||
})
|
||||
setSelectStatus(!selectStatus)
|
||||
setList([...list])
|
||||
@@ -70,12 +103,29 @@ export default ({show = false, onClose}: param) => {
|
||||
//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++
|
||||
}
|
||||
})
|
||||
console.log('list_count::',list_count)
|
||||
console.log('select_count::',select_count)
|
||||
setSelectStatus(select_count == list_count)
|
||||
}
|
||||
|
||||
//checkbox关闭回调
|
||||
const colseCallBack = (item) => {
|
||||
item.select = false
|
||||
checkSelect()
|
||||
setList([...list])
|
||||
}
|
||||
|
||||
@@ -85,16 +135,28 @@ export default ({show = false, onClose}: param) => {
|
||||
setShowPopup(false)
|
||||
}
|
||||
|
||||
//删除购物车内容
|
||||
const {fetchData:delShopFetchData} = DelShoppingCartApi()
|
||||
const delSelect = () => {
|
||||
Taro.showModal({
|
||||
content: '删除所选商品?',
|
||||
success: function (res) {
|
||||
success: async function (res) {
|
||||
if (res.confirm) {
|
||||
Taro.showToast({
|
||||
title: '成功',
|
||||
icon: 'success',
|
||||
duration: 2000
|
||||
})
|
||||
getSelectId()
|
||||
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('用户点击取消')
|
||||
}
|
||||
@@ -102,6 +164,16 @@ export default ({show = false, onClose}: param) => {
|
||||
})
|
||||
}
|
||||
|
||||
//获取面料选中的id
|
||||
const selectIds = useRef<number[]>([])
|
||||
const getSelectId = () => {
|
||||
list?.map(item => {
|
||||
if(selectIndex == -1 || selectIndex == item.sale_mode) {
|
||||
item.select&&selectIds.current.push(item.id)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
//格式化金额
|
||||
const formatPirce = useCallback((price) => {
|
||||
const {num} = formatMillionYuan(price, 100)
|
||||
@@ -115,7 +187,7 @@ export default ({show = false, onClose}: param) => {
|
||||
|
||||
return (
|
||||
<View className={styles.shop_cart_main}>
|
||||
<Popup showTitle={false} show={showPopup} onClose={() => closePopup()}>
|
||||
<Popup showTitle={false} show={showPopup} onClose={() => closePopup()} >
|
||||
<View className={styles.popup_con}>
|
||||
<View className={styles.header}>
|
||||
<View onClick={selectAll}>{!selectStatus?'全选':'反选'}</View>
|
||||
@@ -125,22 +197,21 @@ export default ({show = false, onClose}: param) => {
|
||||
</View>
|
||||
</View>
|
||||
<View className={styles.search}>
|
||||
{selectList.map((item, index) => {
|
||||
return <View key={index} onClick={() => selectProduct(index)} className={classnames(styles.search_item, (selectIndex==index)&&styles.search_item_select)}>{item.title}</View>
|
||||
{selectList.map((item) => {
|
||||
return <View key={item.value} onClick={() => selectProduct(item.value)} className={classnames(styles.search_item, (selectIndex==item.value)&&styles.search_item_select)}>{item.title}</View>
|
||||
})}
|
||||
|
||||
</View>
|
||||
<View className={styles.con}>
|
||||
{loading&&<LoadingCard/>}
|
||||
{!loading&&list?.length > 0&&<InfiniteScroll refresherTriggered={true} refresherEnabled={true} moreStatus={false} >
|
||||
<View className={styles.product_list}>
|
||||
{list?.map((item, index) => {
|
||||
return <View key={index} className={styles.product_item}>
|
||||
return <View key={index} className={classnames(styles.product_item, (selectIndex!=-1&&selectIndex!= item.sale_mode)&&styles.no_product_item_select)}>
|
||||
<View className={styles.checkbox}>
|
||||
<MCheckbox status={item.select} onSelect={() => selectCallBack(item)} onClose={() => colseCallBack(item)}/>
|
||||
<MCheckbox disabled={selectIndex!=-1&&selectIndex!=item.sale_mode} status={item.select} onSelect={() => selectCallBack(item)} onClose={() => colseCallBack(item)}/>
|
||||
</View>
|
||||
<View className={styles.img}>
|
||||
<Image src="https://bpic.588ku.com//back_origin_min_pic/22/01/11/aa3da17bab9a6556564028e4f1d77874.jpg!/fw/750/quality/99/unsharp/true/compress/true"/>
|
||||
<Image mode="aspectFill" src={formatImgUrl(item.texture_url)}/>
|
||||
</View>
|
||||
<View className={styles.des}>
|
||||
<View className={styles.title}>{formatHashTag(item.product_code, item.product_name)}</View>
|
||||
|
||||
Reference in New Issue
Block a user