hookehuyr

✨ feat: 适配cookie保存未完成表单

1 <!-- 1 <!--
2 * @Date: 2023-03-29 13:09:02 2 * @Date: 2023-03-29 13:09:02
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2023-03-29 13:22:01 4 + * @LastEditTime: 2024-11-21 09:53:59
5 * @FilePath: /data-table/src/components/RatePickerField/MyComponent.vue 5 * @FilePath: /data-table/src/components/RatePickerField/MyComponent.vue
6 * @Description: 文件描述 6 * @Description: 文件描述
7 --> 7 -->
...@@ -22,6 +22,10 @@ ...@@ -22,6 +22,10 @@
22 import { ref } from 'vue' 22 import { ref } from 'vue'
23 import { useCustomFieldValue } from '@vant/use'; 23 import { useCustomFieldValue } from '@vant/use';
24 import { styleColor } from "@/constant.js"; 24 import { styleColor } from "@/constant.js";
25 +import Cookies from 'js-cookie';
26 +import { useRoute } from "vue-router";
27 +
28 +const $route = useRoute();
25 29
26 // 获取父组件传值 30 // 获取父组件传值
27 const props = inject('props'); 31 const props = inject('props');
...@@ -29,6 +33,25 @@ const rate_value = ref(props.component_props.default); ...@@ -29,6 +33,25 @@ const rate_value = ref(props.component_props.default);
29 33
30 // 此处传入的值会替代 Field 组件内部的 value 34 // 此处传入的值会替代 Field 组件内部的 value
31 useCustomFieldValue(() => rate_value.value); 35 useCustomFieldValue(() => rate_value.value);
36 +
37 +// 适配cookie保存未完成表单
38 +watch(
39 + () => rate_value.value,
40 + (v) => {
41 + const currentValue = v;
42 + const existingCookie = Cookies.get($route.query.code);
43 +
44 + if (existingCookie) {
45 + // 如果Cookie存在,更新它
46 + let obj = JSON.parse(existingCookie);
47 + obj[props.key] = currentValue; // 替换掉旧值
48 + Cookies.set($route.query.code, JSON.stringify(obj));
49 + } else {
50 + // 如果Cookie不存在,新增它
51 + Cookies.set($route.query.code, JSON.stringify({ [props.key]: currentValue }));
52 + }
53 + }
54 +);
32 </script> 55 </script>
33 56
34 <style lang="less" scoped> 57 <style lang="less" scoped>
......
1 <!-- 1 <!--
2 * @Date: 2022-08-29 14:31:20 2 * @Date: 2022-08-29 14:31:20
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-08-07 18:15:18 4 + * @LastEditTime: 2024-11-21 10:25:09
5 * @FilePath: /data-table/src/components/TableField/index.vue 5 * @FilePath: /data-table/src/components/TableField/index.vue
6 * @Description: 表格组件 6 * @Description: 表格组件
7 --> 7 -->
...@@ -37,6 +37,7 @@ import { showSuccessToast, showFailToast, showToast } from 'vant'; ...@@ -37,6 +37,7 @@ import { showSuccessToast, showFailToast, showToast } from 'vant';
37 import wx from 'weixin-js-sdk' 37 import wx from 'weixin-js-sdk'
38 import $ from 'jquery' 38 import $ from 'jquery'
39 import TEditor from "@/components/TEditor/index.vue"; 39 import TEditor from "@/components/TEditor/index.vue";
40 +import Cookies from 'js-cookie';
40 41
41 const $route = useRoute(); 42 const $route = useRoute();
42 const props = defineProps({ 43 const props = defineProps({
...@@ -96,6 +97,19 @@ const onBlur = (html) => { ...@@ -96,6 +97,19 @@ const onBlur = (html) => {
96 value: html, 97 value: html,
97 }; 98 };
98 emit("active", props.item.value); 99 emit("active", props.item.value);
100 + // 适配cookie保存未完成表单
101 + const currentValue = html;
102 + const existingCookie = Cookies.get($route.query.code);
103 +
104 + if (existingCookie) {
105 + // 如果Cookie存在,更新它
106 + let obj = JSON.parse(existingCookie);
107 + obj[props.item.key] = currentValue; // 替换掉旧值
108 + Cookies.set($route.query.code, JSON.stringify(obj));
109 + } else {
110 + // 如果Cookie不存在,新增它
111 + Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }));
112 + }
99 } 113 }
100 114
101 // const table = ` 115 // const table = `
......