瀏覽代碼

添加cookie封装

zouzs 1 月之前
父節點
當前提交
1281a1a734
共有 3 個文件被更改,包括 95 次插入4 次删除
  1. 85 0
      src/utils/cookieStorage.ts
  2. 9 3
      src/views/testTable/pageTable/index.vue
  3. 1 1
      src/views/testTable/splitTable/index.vue

+ 85 - 0
src/utils/cookieStorage.ts

@@ -0,0 +1,85 @@
+/** 配置 */
+interface Options {
+  /** key前缀 */
+  prefix?: string;
+}
+/** 默认配置 */
+const defaultOptions: Options = {
+  prefix: ""
+};
+
+class CookieStorage {
+  private readonly prefix: string;
+
+  constructor(options: Options = defaultOptions) {
+    const { prefix } = options;
+    this.prefix = prefix ?? "";
+  }
+
+  /**
+   * @description: 设置cookie
+   * @param {string} key 键
+   * @param {any} value 值
+   * @param {number} expires 过期时间s毫秒,不传默认2年有效
+   * @Date: 2023-05-17 18:10:34
+   * @Author: mulingyuer
+   */
+  public setItem(key: string, value: string | number, expires?: number): void {
+    const timestamp = Date.now();
+    if (typeof expires === "number") {
+      expires = timestamp + expires;
+    } else {
+      expires = timestamp + 2 * 365 * 24 * 60 * 60 * 1000;
+    }
+    document.cookie = `${this.prefix}${key}=${value};expires=${new Date(expires).toUTCString()}`;
+  }
+
+  /**
+   * @description: 获取cookie
+   * @param {string} key 键
+   * @Date: 2023-05-17 18:31:50
+   * @Author: mulingyuer
+   */
+  public getItem(key: string): string | undefined {
+    const cookies = document.cookie.split("; ");
+    let val: string | undefined = undefined;
+    cookies.find(item => {
+      const [k, v] = item.split("=");
+      if (k === `${this.prefix}${key}`) {
+        val = v;
+        return true;
+      }
+      return false;
+    });
+
+    return val;
+  }
+
+  /**
+   * @description: 删除指定key的cookie
+   * @param {string} key 键
+   * @Date: 2023-05-17 18:32:56
+   * @Author: mulingyuer
+   */
+  public removeItem(key: string): void {
+    this.setItem(key, "", -1);
+  }
+
+  /**
+   * @description: 清空所有cookie
+   * @Date: 2023-05-17 23:13:04
+   * @Author: mulingyuer
+   */
+  public clear(): void {
+    const cookies = document.cookie.split("; ");
+    cookies.forEach(item => {
+      const [k] = item.split("=");
+      this.removeItem(k);
+    });
+  }
+}
+
+const cookieStorage = new CookieStorage();
+
+export default cookieStorage;
+export { CookieStorage };

+ 9 - 3
src/views/testTable/pageTable/index.vue

@@ -8,8 +8,11 @@
       :table="{
         onSortChange: handleSortChange,
         isSelection: true,
-        actionBar: { buttons, type: 'button', width: 240 }
+        actionBar: { buttons, type: 'button', width: 240 },
+        adaptive: { offsetBottom: 50 }
       }"
+      :defaultPageInfo="{ page: 1, pageSize: 10 }"
+      :defaultPageSizeList="[10, 20, 50, 100]"
     />
   </div>
 </template>
@@ -202,14 +205,17 @@ const tableConfig: PlusColumn[] = [
     prop: "rate",
     valueType: "rate",
     hideInSearch: true,
-    editable: true
+    editable: true,
+    minWidth: 130
   },
   {
     label: "开关",
     prop: "switch",
     hideInSearch: true,
     valueType: "switch",
-    editable: true
+    editable: true,
+    maxWidth: 100,
+    minWidth: 40
   },
   {
     label: "时间",

+ 1 - 1
src/views/testTable/splitTable/index.vue

@@ -314,7 +314,7 @@ const handleExport = () => {
           modelValue: pageInfo,
           pageSizeList: [10, 20, 50]
         }"
-        adaptive
+        :adaptive="{ offsetBottom: 50 }"
         @selection-change="handleSelectionChange"
         @paginationChange="handlePaginationChange"
         @sort-change="handleSrotChange"