From c4917e540f7e9c1723208d04b739985c22939c6d Mon Sep 17 00:00:00 2001
From: czm <2192718639@qq.com>
Date: Fri, 13 Jan 2023 20:18:02 +0800
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(ID1001140=E8=B4=AD=E7=89=A9?=
=?UTF-8?q?=E8=BD=A6=E7=9A=84=E9=9D=A2=E6=96=99=E5=87=8F=E5=B0=91=E5=88=B0?=
=?UTF-8?q?0=E7=9A=84=E6=97=B6=E5=80=99=E6=8F=90=E7=A4=BA=E5=88=A0?=
=?UTF-8?q?=E9=99=A4=E8=AF=A5=E5=95=86=E5=93=81):?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/components/counter/index.tsx | 3 +-
src/context/ContextShop/index.tsx | 9 ++--
.../components/RecommendOpen/index.tsx | 3 +-
src/pages/shopCar/components/main/index.tsx | 10 ++++-
.../shopCar/components/productItem/index.tsx | 43 +++++++++++++++----
5 files changed, 53 insertions(+), 15 deletions(-)
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
/>