diff --git a/src/components/counter/index.tsx b/src/components/counter/index.tsx index f11539b..628a7e0 100644 --- a/src/components/counter/index.tsx +++ b/src/components/counter/index.tsx @@ -159,7 +159,6 @@ const CounterDisplayName = (props: params) => { ) } const Counter = memo(CounterDisplayName) -const CounterWithMemo = (props: params) => { +export default (props: params) => { return } -export default memo(CounterWithMemo) diff --git a/src/context/ContextShop/index.tsx b/src/context/ContextShop/index.tsx index 2c86241..e6cb2b3 100644 --- a/src/context/ContextShop/index.tsx +++ b/src/context/ContextShop/index.tsx @@ -57,13 +57,14 @@ export interface StateType { sale_mode?: saleModeType checkList?: { [key in number]: boolean } recommendId?: number - + delCommendId?: number } export interface ActionsMap { setInitData: StateType updateProduct: StateType setRecommendId: StateType + setDelCommendId: StateType } export type Actions = { @@ -95,7 +96,7 @@ export default (props: PropsType) => { price_number: 0, }, recommendId: 0, - + delCommendId: 0, } function reducer(state: StateType, action: Actions) { switch (action.type) { @@ -105,6 +106,8 @@ export default (props: PropsType) => { return onUpdateData(state, action) case 'setRecommendId': return { ...state, recommendId: action.data.recommendId } + case 'setDelCommendId': + return { ...state, delCommendId: action.data.delCommendId } default: return state } @@ -127,7 +130,7 @@ const onInitData = (state: StateType, action: Actions) => { citem.checked = false }) }) - return { ...state, list: data.list, sale_mode: data.sale_mode, recommendId: data.recommendId, statistics } + return { ...state, list: data.list, sale_mode: data.sale_mode, recommendId: 0, statistics, delCommendId: 0 } } // 更新数据 diff --git a/src/pages/shopCar/components/RecommendOpen/index.tsx b/src/pages/shopCar/components/RecommendOpen/index.tsx index 17df52e..fab7857 100644 --- a/src/pages/shopCar/components/RecommendOpen/index.tsx +++ b/src/pages/shopCar/components/RecommendOpen/index.tsx @@ -44,7 +44,7 @@ export default memo((props: ParamType) => { ...res.data, sale_mode_type: filter.sale_mode, } - onSubmitData(res.data.defaultNum, res.data.screw_color_id) + onSubmitData(SALE_MODE_SETTING[filter.sale_mode].defaultNum, res.data.screw_color_id) setProductData(res.data) } } @@ -69,6 +69,7 @@ export default memo((props: ParamType) => { // 整理提交数据 const onSubmitData = (num = 0, product_color_id = 0) => { const data = { product_color_id, length: 0, roll: 0 } + console.log('num::::', num) if (filter.sale_mode === 0) { data.roll = num } diff --git a/src/pages/shopCar/components/main/index.tsx b/src/pages/shopCar/components/main/index.tsx index 619ee90..e520d8d 100644 --- a/src/pages/shopCar/components/main/index.tsx +++ b/src/pages/shopCar/components/main/index.tsx @@ -46,7 +46,7 @@ export default () => { const res = await getShoppingCartFetchData(filter) if (res.success) { startTransition(() => { - dispatch({ type: 'setInitData', data: { list: JSON.parse(JSON.stringify(res?.data?.product_list)), sale_mode: filter.sale_mode, recommendId: 0 } }) + dispatch({ type: 'setInitData', data: { list: JSON.parse(JSON.stringify(res?.data?.product_list)), sale_mode: filter.sale_mode } }) }) } } @@ -216,6 +216,14 @@ export default () => { getShoppingCart() }, []) + // 当减到0时删除商品 + useEffect(() => { + if (state.delCommendId) { + getShoppingCart() + getProductColorNum() + } + }, [state.delCommendId]) + return diff --git a/src/pages/shopCar/components/productItem/index.tsx b/src/pages/shopCar/components/productItem/index.tsx index 07e813b..82720df 100644 --- a/src/pages/shopCar/components/productItem/index.tsx +++ b/src/pages/shopCar/components/productItem/index.tsx @@ -1,6 +1,6 @@ import { CustomWrapper, Text, View } from '@tarojs/components' import Taro from '@tarojs/taro' -import { memo, useCallback, useEffect, useMemo } from 'react' +import { memo, useCallback, useEffect, useMemo, useState } from 'react' import Big from 'big.js' import ColorCheckbox from '../colorCheckbox' import RecommendProduct from '../recommendProduct' @@ -10,7 +10,7 @@ import Counter from '@/components/counter' import type { ColorType } from '@/context/ContextShop' import { useCurrenShop } from '@/context/ContextShop' import { formatPriceDiv } from '@/common/fotmat' -import { GetShoppingCartApi, UpdateShoppingCartApi } from '@/api/shopCart' +import { DelShoppingCartApi, GetShoppingCartApi, UpdateShoppingCartApi } from '@/api/shopCart' import { debounce } from '@/common/util' import { SALE_MODE_SETTING } from '@/common/enum' @@ -27,16 +27,37 @@ type IndexItemType = { export default memo((props: ColorItemType) => { const { fetchData: updateShoppingCartFetchData } = UpdateShoppingCartApi() const { fetchData: getShoppingCartFetchData } = GetShoppingCartApi() + const { fetchData: delShopFetchData } = DelShoppingCartApi() const { colorItem } = props const { state, dispatch } = useCurrenShop() const indexs = colorItem.index_str.split(',') const colorItemNew: ColorType = state.list[indexs[0]].color_list[indexs[1]] const onChangeNum = useCallback(async(val) => { - await updateShoppingCartFetchData(val) - const res = await getShoppingCartFetchData({ id: colorItem.id }) - if (res.success) { - state.list[indexs[0]].color_list[indexs[1]] = { ...state.list[indexs[0]].color_list[indexs[1]], ...res.data.color_list[0] } - dispatch({ type: 'updateProduct', data: { list: state.list } }) + if (val.roll == 0 && val.length == 0) { + Taro.showModal({ + content: '确定删除该商品?', + confirmText: '删除', + async success(res) { + if (res.confirm) { + const res = await delShopFetchData({ id: [colorItemNew.id] }) + if (res.success) { + dispatch({ type: 'setDelCommendId', data: { list: state.list, delCommendId: colorItemNew.id } }) + } + } + else if (res.cancel) { + state.list[indexs[0]].color_list[indexs[1]] = { ...state.list[indexs[0]].color_list[indexs[1]] } + dispatch({ type: 'updateProduct', data: { list: state.list } }) + } + }, + }) + } + else { + await updateShoppingCartFetchData(val) + const res = await getShoppingCartFetchData({ id: colorItem.id }) + if (res.success) { + state.list[indexs[0]].color_list[indexs[1]] = { ...state.list[indexs[0]].color_list[indexs[1]], ...res.data.color_list[0] } + dispatch({ type: 'updateProduct', data: { list: state.list } }) + } } }, [colorItemNew]) @@ -51,7 +72,12 @@ const Index = memo((props: IndexItemType) => { const rga = useMemo(() => { return { lab: colorItem.lab, rgb: colorItem.rgb, texture_url: colorItem.texture_url, title: colorItem.product_color_code } }, [colorItem]) + const [count, setCount] = useState(0) + useEffect(() => { + setCount(() => colorItem.sale_mode === 0 ? colorItem.roll : colorItem.length / 100) + }, [colorItem]) const getCount = debounce(async(num) => { + setCount(num) const data = { id: colorItem.id, length: 0, roll: 0 } if (colorItem.sale_mode === 0) { data.roll = num @@ -98,9 +124,10 @@ const Index = memo((props: IndexItemType) => { minNum={saleSetting.minNum} maxNum={saleSetting.maxNum} onBlue={getCount} - defaultNum={colorItem.sale_mode === 0 ? colorItem.roll : colorItem.length / 100} + defaultNum={count} unit={colorItem.sale_mode === 0 ? '条' : '米'} digits={saleSetting.digits} + returnZero />