From ab213186c28d74f0bca078f092952f3382ebf871 Mon Sep 17 00:00:00 2001 From: czm <2192718639@qq.com> Date: Fri, 8 Jul 2022 19:04:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=8B=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/bluetooth/LinkBlueTooth.tsx | 27 +- .../details/components/counter/index.tsx | 34 +- .../components/orderCount/index copy.tsx | 326 ++++++++++++++++++ .../details/components/orderCount/index.tsx | 21 +- src/pages/orderList/index.tsx | 4 +- src/pages/searchList/hightSearchList.tsx | 4 +- 6 files changed, 372 insertions(+), 44 deletions(-) create mode 100644 src/pages/details/components/orderCount/index copy.tsx diff --git a/src/components/bluetooth/LinkBlueTooth.tsx b/src/components/bluetooth/LinkBlueTooth.tsx index 66f01a7..ed4b864 100644 --- a/src/components/bluetooth/LinkBlueTooth.tsx +++ b/src/components/bluetooth/LinkBlueTooth.tsx @@ -1,15 +1,18 @@ import { View } from "@tarojs/components"; import { memo, useEffect, useMemo, useState } from "react"; -import Taro from "@tarojs/taro"; +import Taro, { useDidShow } from "@tarojs/taro"; import {useBluetooth} from "@/use/contextBlueTooth" import SearchInput from "@/components/searchInput"; import Popup from "@/components/bluetooth/Popup" import classnames from "classnames"; import styles from "./css/linkBlueTooth.module.scss" +import useCheckAuthorize from "@/use/useCheckAuthorize"; export default memo(() => { const {state, init, startScan, connect, disconnect} = useBluetooth() + const {check} = useCheckAuthorize({scope:'scope.bluetooth', msg:'请开启小程序蓝牙权限'}) + useEffect(() => { init() }, []) @@ -23,7 +26,6 @@ export default memo(() => { } else { setLinkStatus(2) } - console.log('aaa:::',state.connected) }, [state.available, state.connected]) const linkName = useMemo(() => { @@ -39,16 +41,17 @@ export default memo(() => { const [popupShow, setPopupShow] = useState(false) //显示设备列表 const onFindDevice = () => { - if(linkStatus == 1) { - Taro.showToast({ - title:'请打开蓝牙', - icon:'none' - }) - } else { - setPopupShow(true) - onFindEven() - } - + check().then(res => { + if(linkStatus == 1) { + Taro.showToast({ + title:'请打开手机蓝牙', + icon:'none' + }) + } else { + setPopupShow(true) + onFindEven() + } + }) } const onFindEven = () => { if(!state.discovering&&!state.connected&&!state.connecting) diff --git a/src/pages/details/components/counter/index.tsx b/src/pages/details/components/counter/index.tsx index d7f632d..3bc52fe 100644 --- a/src/pages/details/components/counter/index.tsx +++ b/src/pages/details/components/counter/index.tsx @@ -8,30 +8,32 @@ type params = { step?: number, //步长 defaultNum?: number, //默认值 digits?: number //多少位小数 - onChange?:(val:number) => void, - onBlue?:(val:number) => void, //失去焦点触发 - onClickBtn?:(val:number) => void, - unit?: string + onChange?:(val: number, obj?: any) => void, + onBlue?:(val:number, obj?: any) => void, //失去焦点触发 + onClickBtn?:(val:number, obj?: any) => void, + unit?: string, + otherData?: any } -export default ({minNum = 0, maxNum = 100, step=1, digits = 0, defaultNum = 0, onChange, onBlue, onClickBtn, unit = ''}: params) => { +export default ({minNum = 0, maxNum = 100, step=1, digits = 0, defaultNum = 0, onChange, onBlue, onClickBtn, unit = '', otherData}: params) => { const [value, setValue] = useState({count:defaultNum}) - + + const onPlus = () => { let {count} = value let num_res = Big(count).add(step).toNumber() num_res = num_res >= maxNum?maxNum:num_res num_res = formatDigits(num_res) setValue({...value, count:num_res}) - onChange?.(parseFloat(num_res)) - onClickBtn?.(parseFloat(num_res)) + onChange?.(parseFloat(num_res), otherData) + onClickBtn?.(parseFloat(num_res), otherData) } const minus = () => { let {count} = value let num_res = Big(count).minus(step).toNumber() num_res = num_res < minNum?0:num_res setValue({...value, count:num_res}) - onChange?.(parseFloat(num_res)) - onClickBtn?.(parseFloat(num_res)) + onChange?.(parseFloat(num_res), otherData) + onClickBtn?.(parseFloat(num_res), otherData) } //保留小数 @@ -59,23 +61,23 @@ export default ({minNum = 0, maxNum = 100, step=1, digits = 0, defaultNum = 0, o let res = e.detail.value if(res === '') { setValue({...value, count:minNum}) - onChange?.(minNum) + onChange?.(minNum, otherData) } else if(!isNaN(Number(res))) { let count = formatDigits(res) count = checkData(count) setValue({...value, count}) - onChange?.(parseFloat(count as string)) + onChange?.(parseFloat(count as string), otherData) } else { let num = parseFloat(res) if(!isNaN(num)) { let count = formatDigits(num) count = checkData(count) setValue({...value, count}) - onChange?.(count as number) + onChange?.(count as number, otherData) } else { setValue({...value, count:defaultNum}) - onChange?.(defaultNum) + onChange?.(defaultNum, otherData) } } @@ -87,10 +89,10 @@ export default ({minNum = 0, maxNum = 100, step=1, digits = 0, defaultNum = 0, o let count = formatDigits(num) count = checkData(count) setValue({...value, count}) - onBlue?.(count as number) + onBlue?.(count as number, otherData) } else { setValue({...value, count:defaultNum}) - onBlue?.(defaultNum) + onBlue?.(defaultNum, otherData) } } return ( diff --git a/src/pages/details/components/orderCount/index copy.tsx b/src/pages/details/components/orderCount/index copy.tsx new file mode 100644 index 0000000..988754c --- /dev/null +++ b/src/pages/details/components/orderCount/index copy.tsx @@ -0,0 +1,326 @@ +import {Image, ScrollView, View, Text, CustomWrapper } from "@tarojs/components" +import Popup from "@/components/popup" +import LoadingCard from "@/components/loadingCard"; +import Search from "@/components/search"; +import InfiniteScroll from "@/components/infiniteScroll"; +import Counter from "../counter"; +import Big from 'big.js' +import classnames from "classnames"; +import styles from "./index.module.scss" +import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react"; +import { useSelector } from "@/reducers/hooks" +import {GetColorList} from "@/api/materialColor" +import {AddShoppingCartApi} from "@/api/shopCart" +import Taro, { useRouter } from "@tarojs/taro"; +import UseLogin from "@/use/useLogin" +import { formatHashTag, formatPriceDiv } from "@/common/fotmat"; +import { debounce, getFilterData } from "@/common/util"; +import LabAndImg from "@/components/LabAndImg"; +import VirtualList from '@tarojs/components/virtual-list' +import useCommonData from "@/use/useCommonData"; + + + +type param = { + show?: true|false, + onClose?: () => void, + title?: string, + productId?: number +} +export default memo(({show = false, onClose, title = '', productId = 0}: param) => { + const {adminUserInfo} = useSelector(state => state.userInfo) + + const [selectList, _] = useState([ + {id: 0, step:1, digits:0, maxNum:100000, defaultNum:1, title:'大货', unit:'条', eunit:'kg', priceField:'bulk_price'}, + {id: 1, step:1, digits:2, maxNum:9.99, defaultNum:1, title:'剪板', unit:'米', eunit:'m', priceField:'length_cut_price'}, + {id: 2, step:1, digits:2, minNum:10, maxNum:100000, defaultNum:10, title:'散剪', unit:'米', eunit:'kg', priceField:'weight_cut_price'}, + ]) + const [selectIndex, setSelectIndex] = useState(0) + const selectProduct = (index:number) => { + setSelectIndex(() => index) + } + + //重置数据 + useEffect(() => { + const newList = initList(list) + setList([...newList]) + condition.current.code_or_name = null + setSearchShow(false) + }, [selectIndex]) + + //获取面料颜色列表 + const {fetchData:colorFetchData, state: colorState} = GetColorList() + const [list, setList] = useState([]) + const condition = useRef({physical_warehouse:adminUserInfo?.physical_warehouse, sale_mode:selectIndex, product_id:0, code_or_name:null}) + const getColorList = async () => { + let {data} = await colorFetchData(getFilterData(condition.current)) + let lists = initList(data.list) + setList(() => [...lists]) + } + const [showPopup, setShowPopup] = useState(false) + + + //显示获取 + useEffect(() => { + if(show) { + setSelectIndex(0) + condition.current.code_or_name = null + setSearchShow(false) + condition.current.product_id = productId + getColorList() + } + setShowPopup(show) + }, [show]) + + //初始化列表数据 + const initList = useCallback((list) => { + const newList = list.map(item => { + item.count = 0 + item.show = false + return item + }) + return newList + }, []) + + //卸载数据 + useEffect(() => { + return () => { + setList([]) + } + }, []) + + //popup关闭 + const closePopup = () => { + onClose?.() + setShowPopup(false) + setList([]) + } + + //计算总数量和总米/件数 + const [selectCount, setSelectCount] = useState<{sumCount:number, kindCount:number, color_list:any[]}>({ + sumCount: 0, + kindCount: 0, + color_list: [] + }) + useEffect(() => { + let sumCount = 0, kindCount = 0, color_list:any[] = [] + let color_list_info = {} + list.map(item => { + if(item.count > 0) { + sumCount = Big(sumCount).add(item.count).toNumber() + kindCount ++ + color_list_info = selectIndex == 0? {product_color_id:item.id, roll:item.count}:{product_color_id:item.id, length:(item.count*100)} + color_list.push(color_list_info) + + } + }) + setSelectCount({...selectCount, sumCount, kindCount, color_list}) + }, [list]) + + //计数组件 + const getInputValue = (num, item) => { + item.count = parseFloat(num) + if(num == 0) item.show = false + setList([...list]) + console.log(num) + + } + + + const onAdd = (item) => { + item.show = true + item.count = selectList[selectIndex].defaultNum + setList((list) => [...list]) + } + + //搜索显示与隐藏 + const [searchShow, setSearchShow] = useState(false) + const changeSearchShow = () => { + setSearchShow(true) + } + + //添加购物车 + const {getShopCount} = useCommonData() + const {getSelfUserInfo} = UseLogin() + const {fetchData:addFetchData} = AddShoppingCartApi() + const addShopCart = async () => { + try { + await getSelfUserInfo() + } catch(msg) { + Taro.showToast({ + icon:'none', + title:'授权失败,请求完善授权' + }) + return false + } + if(selectCount.sumCount == 0) { + Taro.showToast({ + icon:'none', + title:'请选择面料颜色!' + }) + return false + } + + const state = await addFetchData({ + "sale_mode": selectIndex, + color_list: selectCount.color_list + }) + if(state.success) { + Taro.showToast({ + title:'添加成功' + }) + getShopCount() + onClose?.() + } else { + Taro.showToast({ + icon:'none', + title: state.msg + }) + } + + } + + //筛选数据 + const searchInput = useCallback((e) => { + condition.current.code_or_name = e + getColorList() + }, []) + + //清空搜索内容 + const searchRef = useRef(null) + const clearSearch = () => { + searchRef.current.clearInput() + setSearchShow(false) + } + + //格式化金额 + const formatPrice = useCallback((item) => { + const price = Number(formatPriceDiv(item[selectList[selectIndex].priceField])) + return ¥{price} /{selectList[selectIndex].eunit} + }, [selectIndex]) + + //虚拟滚动 + const Rows = memo(({id, index, style, data}:any) => { + let item = data[index] + return ( + <> + {item&& + + + + + {formatHashTag(item.code, item.name)} + + {formatPrice(item)} + + + + {!item.show&& onAdd(item)}>添加 + || + getInputValue(e, item)} + defaultNum={item.count} + step={selectList[selectIndex].step} + digits={selectList[selectIndex].digits} + onClickBtn={(e) => getInputValue(e, item)} + unit={selectList[selectIndex].unit} + minNum={selectList[selectIndex].minNum} + maxNum={selectList[selectIndex].maxNum} + /> + } + + || + } + + ) + }) + + return ( + + closePopup()} > + + {title} + + 下单类型: + + {selectList.map((item, index) => { + return selectProduct(index)} className={classnames(styles.search_item, (selectIndex==index)&&styles.search_item_select)}>{item.title} + })} + + + {searchShow&& + + + + clearSearch()}>取消 + } + + 颜色分类 ({list.length}) + {!searchShow&& changeSearchShow()}>} + + + + {(list.length <= 0 && colorState.loading)&&} + {(list.length > 0&& !colorState.loading)&& + // + // + // {list.map(item => { + // return + // + // + // + // + // {formatHashTag(item.code, item.name)} + // + // {formatPrice(item)} + // + // + // + // {!item.show&& onAdd(item)}>添加 + // || + // getInputValue(e, item)} + // defaultNum={item.count} + // step={selectList[selectIndex].step} + // digits={selectList[selectIndex].digits} + // onClickBtn={(e) => getInputValue(e, item)} + // unit={selectList[selectIndex].unit} + // minNum={selectList[selectIndex].minNum} + // maxNum={selectList[selectIndex].maxNum} + // /> + // } + // + // + // })} + // + // } + + + {Rows} + + + } + + {(list.length <= 0 && !colorState.loading)&&暂无此商品} + + + + 当前已选{selectCount.kindCount}种,共{selectCount.sumCount}{selectList[selectIndex].unit} + addShopCart()}>加入购物车 + + + + + + + ) +}) + diff --git a/src/pages/details/components/orderCount/index.tsx b/src/pages/details/components/orderCount/index.tsx index 988754c..f149806 100644 --- a/src/pages/details/components/orderCount/index.tsx +++ b/src/pages/details/components/orderCount/index.tsx @@ -118,14 +118,12 @@ export default memo(({show = false, onClose, title = '', productId = 0}: param) }, [list]) //计数组件 - const getInputValue = (num, item) => { + const getInputValue = useCallback((num, item) => { item.count = parseFloat(num) if(num == 0) item.show = false - setList([...list]) - console.log(num) - - } - + setList(() => [...list]) + }, [list]) + const onAdd = (item) => { item.show = true @@ -200,8 +198,9 @@ export default memo(({show = false, onClose, title = '', productId = 0}: param) }, [selectIndex]) //虚拟滚动 - const Rows = memo(({id, index, style, data}:any) => { + const Rows = useCallback(({id, index, style, data}:any) => { let item = data[index] + console.log('123456') return ( <> {item&& @@ -218,11 +217,12 @@ export default memo(({show = false, onClose, title = '', productId = 0}: param) {!item.show&& onAdd(item)}>添加 || getInputValue(e, item)} + otherData={item} + onBlue={getInputValue} defaultNum={item.count} step={selectList[selectIndex].step} digits={selectList[selectIndex].digits} - onClickBtn={(e) => getInputValue(e, item)} + onClickBtn={getInputValue} unit={selectList[selectIndex].unit} minNum={selectList[selectIndex].minNum} maxNum={selectList[selectIndex].maxNum} @@ -233,7 +233,7 @@ export default memo(({show = false, onClose, title = '', productId = 0}: param) } ) - }) + }, [list]) return ( @@ -308,7 +308,6 @@ export default memo(({show = false, onClose, title = '', productId = 0}: param) } - {(list.length <= 0 && !colorState.loading)&&暂无此商品} diff --git a/src/pages/orderList/index.tsx b/src/pages/orderList/index.tsx index 1ec9815..5210ec6 100644 --- a/src/pages/orderList/index.tsx +++ b/src/pages/orderList/index.tsx @@ -56,7 +56,7 @@ export default () => { const [orderData, setOrderData] = useState<{list:any[], total:number}>({list:[], total:0}) const getOrderList = async () => { let res = await listFetchData(getFilterData(searchField)) - setOrderData((e) => ({...e, list: res.data.list, total: res.data.total})) + setOrderData((e) => ({...e, list: res.data?.list, total: res.data?.total})) setRefresherTriggeredStatus(() => false) } @@ -187,7 +187,7 @@ export default () => { - {orderData?.list.map(item => { + {orderData?.list?.map(item => { return })} diff --git a/src/pages/searchList/hightSearchList.tsx b/src/pages/searchList/hightSearchList.tsx index 7a696e0..5388baf 100644 --- a/src/pages/searchList/hightSearchList.tsx +++ b/src/pages/searchList/hightSearchList.tsx @@ -18,12 +18,11 @@ import useLogin from "@/use/useLogin"; import { goLink } from "@/common/common"; import SelectData, {ListProps} from "./components/selectData"; import LabAndImg from "@/components/LabAndImg"; +import useCheckAuthorize from "@/use/useCheckAuthorize"; export default () => { useLogin() - const [showFilter, setShowFilter] = useState(false) - //搜索参数 const [searchField, setSearchField] = useState({ l: '', @@ -53,7 +52,6 @@ export default () => { //上拉加载数据 const pageNum = useRef({size: searchField.size, page: searchField.page}) - const [hasMore, setHasMore] = useState(true) const getScrolltolower = () => { if(materialList.list.length < materialList.total) { pageNum.current.page++