瀏覽代碼

📦 新增简易版字段验证hooks:useValidate(fieldObject)

晓晓晓晓丶vv 4 年之前
父節點
當前提交
019f83c9ab
共有 3 個文件被更改,包括 42 次插入7 次删除
  1. 13 0
      src/helper/utils.ts
  2. 18 0
      src/hooks/useValidate.ts
  3. 11 7
      src/views/put/put-book.vue

+ 13 - 0
src/helper/utils.ts

@@ -0,0 +1,13 @@
+const utilString = Object.prototype.toString;
+
+export const isDate = (date: any): date is Date => {
+  return utilString.call(date) === "[object Date]";
+};
+
+export const isObject = (value: any): value is object => {
+  return utilString.call(value) === "[object Object]";
+};
+
+export const isArray = (array: any[]): array is Array<any> => {
+  return utilString.call(array) === "[object Array]";
+};

+ 18 - 0
src/hooks/useValidate.ts

@@ -0,0 +1,18 @@
+import { isObject } from "@/helper/utils";
+
+// TODO 简单判断字段空值
+const useValidate = <T extends Record<string, any>>(fields: T): T => {
+  const flagArray: boolean[] = [];
+  Object.keys(fields).forEach((field) => {
+    if (isObject(fields[field])) useValidate(fields[field]);
+    else flagArray.push(!!fields[field]);
+  });
+
+  const hasEmpty = flagArray.some((flag) => !flag);
+
+  if (hasEmpty) throw new Error("字段不能为空");
+
+  return fields as T;
+};
+
+export default useValidate;

+ 11 - 7
src/views/put/put-book.vue

@@ -75,6 +75,7 @@ import useDebounceFn from "@/hooks/useDebounceFn";
 import { getDeliveryBookList, getBooksByName, addDeliveryBook } from "@/api";
 import { getDeliveryBookList, getBooksByName, addDeliveryBook } from "@/api";
 import { TableColumnOfPutBooks } from "../_pageOptions/table-put";
 import { TableColumnOfPutBooks } from "../_pageOptions/table-put";
 import { IBookSearchResult, IDeliveryBook } from "@/types/api";
 import { IBookSearchResult, IDeliveryBook } from "@/types/api";
+import useValidate from "../../hooks/useValidate";
 
 
 const PutBooks = defineComponent({
 const PutBooks = defineComponent({
   components: {
   components: {
@@ -150,16 +151,19 @@ const PutBooks = defineComponent({
         // TODO 没做字段校验 字段校验封装
         // TODO 没做字段校验 字段校验封装
         this.addFormState.inConfirm = true;
         this.addFormState.inConfirm = true;
         const { official_id, platform, book } = this.addFormState;
         const { official_id, platform, book } = this.addFormState;
-        await addDeliveryBook({
-          delivery_bid: book.id!,
-          official_id,
-          platform,
-        });
+        // useValidate({ official_id, platform });
+        await addDeliveryBook(
+          useValidate({
+            delivery_bid: book.id!,
+            official_id,
+            platform,
+          })
+        );
         this.open = false;
         this.open = false;
         this.$message.success("添加成功");
         this.$message.success("添加成功");
       } catch (error) {
       } catch (error) {
-        console.log("error while add delivery book", error);
-        this.$message.error("添加失败!");
+        console.log("error while add delivery book");
+        error.message && this.$message.error(error.message);
       } finally {
       } finally {
         this.addFormState.inConfirm = false;
         this.addFormState.inConfirm = false;
       }
       }