浏览代码

update: 增加新的二维码解析规则

xyh 2 年之前
父节点
当前提交
6422b259d7

+ 4 - 0
.eslintrc.js

@@ -1,6 +1,10 @@
 module.exports = {
   extends: ['proste/reactJS.js', 'prettier'],
   root: true,
+  plugins: ['jest'],
+  env: {
+    'jest/globals': true,
+  },
   globals: {
     definePageConfig: 'readonly',
     defineAppConfig: 'readonly',

+ 1 - 1
config/index.js

@@ -22,7 +22,7 @@ const config = {
   framework: 'react',
   compiler: 'webpack5',
   cache: {
-    enable: true, // Webpack 持久化缓存配置,建议开启。默认配置请参考:https://docs.taro.zone/docs/config-detail#cache
+    enable: false, // Webpack 持久化缓存配置,建议开启。默认配置请参考:https://docs.taro.zone/docs/config-detail#cache
   },
   alias: {
     '@styles': resolve(__dirname, '..', 'src/styles'),

+ 9 - 0
jest.config.json

@@ -0,0 +1,9 @@
+{
+  "roots": [
+    "<rootDir>"
+  ],
+  "testMatch": [
+    "<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
+    "<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
+  ]
+}

+ 4 - 1
package.json

@@ -19,7 +19,8 @@
     "prepare": "husky install",
     "postinstall": "husky install weapp-tw patch",
     "pre-commit": "lint-staged",
-    "commit": "cz"
+    "commit": "cz",
+    "test": "jest"
   },
   "config": {
     "commitizen": {
@@ -140,9 +141,11 @@
     "eslint-config-prettier": "^8.8.0",
     "eslint-config-proste": "^5.1.0",
     "eslint-plugin-import": "^2.27.5",
+    "eslint-plugin-jest": "^27.2.1",
     "eslint-plugin-react": "^7.32.2",
     "eslint-plugin-react-hooks": "^4.6.0",
     "husky": "^8.0.3",
+    "jest": "^29.5.0",
     "lint-staged": "^13.2.1",
     "postcss": "^8.4.22",
     "postcss-loader": "^7.2.4",

文件差异内容过多而无法显示
+ 1118 - 4
pnpm-lock.yaml


+ 47 - 10
src/hooks/use-scan-order/index.js

@@ -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},
+  ];
 }

+ 6 - 4
src/pages/deliver/index.jsx

@@ -2,14 +2,16 @@ import {Image, Text, View, Picker} from '@tarojs/components';
 import icon from '@assets/goods/sending.svg';
 import {Button} from '@antmjs/vantui';
 import {TextGroup as Item} from '@components';
-import {useDate, useSubmit} from './hooks';
+import {useSubmit} from './hooks';
 import dayjs from 'dayjs';
 import {useScanOrder} from '@hooks';
 import {BTN_LOADING_SIZE} from '@utils';
 
 export default function DeliverGoods() {
-  const [{goodsList, customerNo, truckNo}, onScan] = useScanOrder();
-  const [date, onDateChagne] = useDate(customerNo);
+  const [
+    {goodsList, customerNo, truckNo, date},
+    {onScan, setDate: onDateChange},
+  ] = useScanOrder();
   const [isLoading, onSubmit] = useSubmit({
     goodsList,
     customerNo,
@@ -33,7 +35,7 @@ export default function DeliverGoods() {
         <Picker
           mode='date'
           value={date}
-          onChange={onDateChagne}
+          onChange={onDateChange}
           start={dayjs().format('YYYY-MM-DD')}
         >
           <Item title='到货时间' content={date} />

+ 1 - 1
src/pages/receive/index.jsx

@@ -9,7 +9,7 @@ import Exception from './exception';
 import {BTN_LOADING_SIZE} from '@utils';
 
 export default function Receive() {
-  const [{goodsList, customerNo, truckNo}, onScan] = useScanOrder();
+  const [{goodsList, customerNo, truckNo}, {onScan}] = useScanOrder();
   const [files, {onRemove, onAdd, setFiles}] = useUpload();
   const [{anomaly, note}, {setAnomaly, setNote}] = useAnomaly();
   const [isLoading, onSubmit] = useSubmit({

+ 26 - 22
src/utils/resolving/index.js

@@ -1,30 +1,34 @@
 /**
- * 识别二维码中的货品信息
+ * code -- 04170064500140000
+ * 11SW042400-15121M0001860SW042400-15821M0000840SW042400-18211M0001920SW042400-19021M0000080SW447140-12421M0000257
+ *
+ * 0417006 卡车号
+ * 45001400 客户号 最后00是里程 不需要管
+ *
+ * 11 忽略不需要
+ *
+ * SW042400-15121M0001860
+ *
+ * SW042400-15121M 品号
+ * 0001860 数量
+ */
+
+/**
+ * 识别二维码中的车号和客户号
  * @param {string} code 二维码识别内容
  */
 export function resolving(code) {
-  /**
-   * code -- 04170064500140000
-   * 11SW042400-15121M0001860SW042400-15821M0000840SW042400-18211M0001920SW042400-19021M0000080SW447140-12421M0000257
-   *
-   * 0417006 卡车号
-   * 4500140000 客户号
-   *
-   * 11 忽略不需要
-   *
-   * SW042400-15121M0001860
-   *
-   * SW042400-15121M 品号
-   * 0001860 数量
-   */
+  const str = code.slice(0, 15);
+  const truckNo = str.slice(0, 7);
+  const customerNo = str.slice(7, 15);
+
+  return {truckNo, customerNo};
+}
 
-  const [first, last] = code.split(' ');
-  const truckNo = first.slice(0, 7);
-  const customerNo = first.slice(7, 15);
+export function resolvingGoods(code, isNormal) {
+  const str = code.slice(isNormal ? 20 : 24);
 
-  // 排除掉一开始的两位数
-  const goodsStr = last.slice(2);
-  const goodsList = goodsStr.match(/(.{22})/g).map(function (el) {
+  const goodsList = str.match(/(.{22})/g).map(function (el) {
     // 前15位是品号 后7位是数量
     const no = el.slice(0, 15),
       num = Number(el.slice(15));
@@ -32,5 +36,5 @@ export function resolving(code) {
     return {no, num};
   });
 
-  return {truckNo, customerNo, goodsList};
+  return goodsList;
 }

+ 26 - 0
src/utils/resolving/index.test.js

@@ -0,0 +1,26 @@
+import {resolving, resolvingGoods} from './index';
+
+describe('resolving', function () {
+  it('test', function () {
+    const str1 = '09150032601590000 11SW447110-15900G0001920';
+    const str2 =
+      '04240011000190000  1 411SW447150-83050A0005760SW447151-83050A0005761';
+
+    expect(resolving(str1)).toEqual({
+      truckNo: '0915003',
+      customerNo: '26015900',
+    });
+    expect(resolving(str2)).toEqual({
+      truckNo: '0424001',
+      customerNo: '10001900',
+    });
+
+    expect(resolvingGoods(str1, true)).toEqual([
+      {no: 'SW447110-15900G', num: 1920},
+    ]);
+    expect(resolvingGoods(str2, false)).toEqual([
+      {no: 'SW447150-83050A', num: 5760},
+      {no: 'SW447151-83050A', num: 5761},
+    ]);
+  });
+});