|
@@ -1,6 +1,9 @@
|
|
|
-import {scanCode} from '@tarojs/taro';
|
|
|
-import {resolving} from '@utils';
|
|
|
-import {useCallback, useState} from 'react';
|
|
|
+import {useMutation} from '@tanstack/react-query';
|
|
|
+import {scanCode, showLoading, hideLoading} from '@tarojs/taro';
|
|
|
+import {resolving, resolvingGoods} from '@utils';
|
|
|
+import {useCallback, useState, useRef} from 'react';
|
|
|
+import {getDays} from '@apis';
|
|
|
+import dayjs from 'dayjs';
|
|
|
|
|
|
export function useScanOrder() {
|
|
|
const [state, setState] = useState({
|
|
@@ -8,14 +11,48 @@ export function useScanOrder() {
|
|
|
customerNo: '',
|
|
|
truckNo: '',
|
|
|
});
|
|
|
+ const [date, setDate] = useState('');
|
|
|
+ const prevCode = useRef('');
|
|
|
|
|
|
- const onScan = useCallback(function () {
|
|
|
- scanCode({scanType: ['qrCode']}).then(function (res) {
|
|
|
- if (res.result) {
|
|
|
- setState(resolving(res.result));
|
|
|
+ const {mutate} = useMutation({
|
|
|
+ mutationFn: getDays,
|
|
|
+ onMutate() {
|
|
|
+ showLoading({title: '正在获取客户信息', mask: true});
|
|
|
+ },
|
|
|
+ onSettled() {
|
|
|
+ hideLoading({noConflict: true});
|
|
|
+ },
|
|
|
+ onSuccess(res) {
|
|
|
+ const goodsList = [];
|
|
|
+ if (res.code === '200') {
|
|
|
+ goodsList.push(
|
|
|
+ ...resolvingGoods(prevCode.current, res.data.states !== '1'),
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ goodsList.push(...resolvingGoods(prevCode.current, true));
|
|
|
}
|
|
|
- });
|
|
|
- }, []);
|
|
|
+ setState(prev => ({...prev, goodsList}));
|
|
|
+ setDate(dayjs().add(Number(res.data.days), 'day').format('YYYY-MM-DD'));
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ const onScan = useCallback(
|
|
|
+ function () {
|
|
|
+ scanCode({scanType: ['qrCode']}).then(function (res) {
|
|
|
+ if (res.result) {
|
|
|
+ prevCode.current = res.result;
|
|
|
+ const {customerNo, truckNo} = resolving(res.result);
|
|
|
+
|
|
|
+ setState({goodsList: [], customerNo, truckNo});
|
|
|
+ mutate(customerNo);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ [mutate],
|
|
|
+ );
|
|
|
|
|
|
- return [state, onScan];
|
|
|
+ return [
|
|
|
+ {...state, date},
|
|
|
+ {onScan, setDate},
|
|
|
+ ];
|
|
|
}
|