hookehuyr

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

1 <!-- 1 <!--
2 * @Date: 2023-03-29 15:27:02 2 * @Date: 2023-03-29 15:27:02
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-08-07 18:43:14 4 + * @LastEditTime: 2024-11-21 11:42:22
5 * @FilePath: /data-table/src/components/AreaPickerField/MyComponent.vue 5 * @FilePath: /data-table/src/components/AreaPickerField/MyComponent.vue
6 * @Description: 文件描述 6 * @Description: 文件描述
7 --> 7 -->
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
22 placeholder="请填写详细地址" 22 placeholder="请填写详细地址"
23 :border="false" 23 :border="false"
24 :readonly="props.component_props.readonly" 24 :readonly="props.component_props.readonly"
25 + @blur="onBlur"
25 /> 26 />
26 <van-divider /> 27 <van-divider />
27 28
...@@ -41,9 +42,14 @@ ...@@ -41,9 +42,14 @@
41 import { ref } from 'vue' 42 import { ref } from 'vue'
42 import { areaList } from "@vant/area-data"; 43 import { areaList } from "@vant/area-data";
43 import { useCustomFieldValue } from '@vant/use'; 44 import { useCustomFieldValue } from '@vant/use';
45 +import Cookies from 'js-cookie';
46 +import { useRoute } from "vue-router";
47 +
48 +const $route = useRoute();
44 49
45 // 获取父组件传值 50 // 获取父组件传值
46 const props = inject('props'); 51 const props = inject('props');
52 +const emit = defineEmits(["active"]);
47 const show_address = ref(!props.component_props.no_street); 53 const show_address = ref(!props.component_props.no_street);
48 54
49 const address = ref(props.component_props.default?.input_value); 55 const address = ref(props.component_props.default?.input_value);
...@@ -66,10 +72,31 @@ const result_value = computed(() => { ...@@ -66,10 +72,31 @@ const result_value = computed(() => {
66 } 72 }
67 }) 73 })
68 74
75 +const province = ref(null);
76 +const city = ref(null);
77 +const district = ref(null);
78 +
69 const onConfirm = ({ selectedOptions }) => { 79 const onConfirm = ({ selectedOptions }) => {
70 fieldValue.value = selectedOptions.map((option) => option.text).join(" "); 80 fieldValue.value = selectedOptions.map((option) => option.text).join(" ");
71 city_code.value = selectedOptions[2]?.value; 81 city_code.value = selectedOptions[2]?.value;
72 showPicker.value = false; 82 showPicker.value = false;
83 + //
84 + province.value = selectedOptions[0].text;
85 + city.value = selectedOptions[1].text;
86 + district.value = selectedOptions[2].text;
87 + emit("active", {
88 + city_code: city_code.value,
89 + province: province.value,
90 + city: city.value,
91 + district: district.value,
92 + street: address.value
93 + });
94 +};
95 +
96 +const onBlur = () => {
97 + emit("active", {
98 + street: address.value
99 + });
73 }; 100 };
74 101
75 // 此处传入的值会替代 Field 组件内部的 value 102 // 此处传入的值会替代 Field 组件内部的 value
......
1 <!-- 1 <!--
2 * @Date: 2022-08-30 14:32:11 2 * @Date: 2022-08-30 14:32:11
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-08-22 09:58:15 4 + * @LastEditTime: 2024-11-21 12:17:06
5 * @FilePath: /data-table/src/components/AreaPickerField/index.vue 5 * @FilePath: /data-table/src/components/AreaPickerField/index.vue
6 * @Description: 省市区选择控件 6 * @Description: 省市区选择控件
7 --> 7 -->
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 </div> 14 </div>
15 <van-field :name="item.key" :rules="rules" style="padding: 0;"> 15 <van-field :name="item.key" :rules="rules" style="padding: 0;">
16 <template #input> 16 <template #input>
17 - <my-component /> 17 + <my-component @active="onActive" />
18 </template> 18 </template>
19 </van-field> 19 </van-field>
20 </div> 20 </div>
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
24 import { useRoute } from "vue-router"; 24 import { useRoute } from "vue-router";
25 import MyComponent from './MyComponent.vue'; 25 import MyComponent from './MyComponent.vue';
26 import _ from 'lodash' 26 import _ from 'lodash'
27 +import Cookies from 'js-cookie';
27 28
28 const $route = useRoute(); 29 const $route = useRoute();
29 const props = defineProps({ 30 const props = defineProps({
...@@ -82,6 +83,34 @@ const validatorMessage = (val, rule) => { ...@@ -82,6 +83,34 @@ const validatorMessage = (val, rule) => {
82 } 83 }
83 }; 84 };
84 const rules = [{ validator, message: validatorMessage }]; 85 const rules = [{ validator, message: validatorMessage }];
86 +
87 +// 子组件通信,适配规则触发
88 +const onActive = (val) => {
89 + // 适配cookie保存未完成表单
90 + const currentValue = JSON.stringify(val);
91 + const existingCookie = Cookies.get($route.query.code);
92 +
93 + if (existingCookie) {
94 + // 如果Cookie存在,更新它
95 + let obj = JSON.parse(existingCookie);
96 + if (val.city_code) { // 修改了
97 + let temp = JSON.parse(obj[props.item.key]);
98 + temp.city_code = val.city_code;
99 + temp.province = val.province;
100 + temp.city = val.city;
101 + temp.district = val.district;
102 + obj[props.item.key] = JSON.stringify(temp); // 替换掉旧值
103 + } else { // 只修改了地址
104 + let temp = JSON.parse(obj[props.item.key]);
105 + temp.street = val.street;
106 + obj[props.item.key] = JSON.stringify(temp); // 替换掉旧值
107 + }
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 + }
113 +};
85 </script> 114 </script>
86 115
87 <style lang="less" scoped> 116 <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:13:59 4 + * @LastEditTime: 2024-11-21 10:50:09
5 * @FilePath: /data-table/src/components/EmailField/index.vue 5 * @FilePath: /data-table/src/components/EmailField/index.vue
6 * @Description: 邮箱输入框 6 * @Description: 邮箱输入框
7 --> 7 -->
...@@ -22,12 +22,15 @@ ...@@ -22,12 +22,15 @@
22 :disabled="item.component_props.disabled" 22 :disabled="item.component_props.disabled"
23 :readonly="item.component_props.readonly" 23 :readonly="item.component_props.readonly"
24 clearable 24 clearable
25 + @blur="onBlur(item)"
25 /> 26 />
26 </div> 27 </div>
27 </template> 28 </template>
28 29
29 <script setup> 30 <script setup>
30 import { useRoute } from "vue-router"; 31 import { useRoute } from "vue-router";
32 +import Cookies from 'js-cookie';
33 +
31 const $route = useRoute(); 34 const $route = useRoute();
32 const props = defineProps({ 35 const props = defineProps({
33 item: Object, 36 item: Object,
...@@ -62,6 +65,22 @@ const validatorMessage = (val, rule) => { ...@@ -62,6 +65,22 @@ const validatorMessage = (val, rule) => {
62 } 65 }
63 }; 66 };
64 const rules = [{ validator, message: validatorMessage }]; 67 const rules = [{ validator, message: validatorMessage }];
68 +
69 +// 适配cookie保存未完成表单
70 +const onBlur = (item) => {
71 + const currentValue = item.value;
72 + const existingCookie = Cookies.get($route.query.code);
73 +
74 + if (existingCookie) {
75 + // 如果Cookie存在,更新它
76 + let obj = JSON.parse(existingCookie);
77 + obj[props.item.key] = currentValue; // 替换掉旧值
78 + Cookies.set($route.query.code, JSON.stringify(obj));
79 + } else {
80 + // 如果Cookie不存在,新增它
81 + Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }));
82 + }
83 +}
65 </script> 84 </script>
66 85
67 <style lang="less" scoped> 86 <style lang="less" scoped>
......
1 <!-- 1 <!--
2 * @Date: 2022-09-02 10:46:03 2 * @Date: 2022-09-02 10:46:03
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-08-19 15:17:46 4 + * @LastEditTime: 2024-11-21 10:49:36
5 * @FilePath: /data-table/src/components/PhoneField/index.vue 5 * @FilePath: /data-table/src/components/PhoneField/index.vue
6 * @Description: 手机输入框 6 * @Description: 手机输入框
7 --> 7 -->
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
41 :required="item.component_props.required" 41 :required="item.component_props.required"
42 :readonly="item.component_props.readonly" 42 :readonly="item.component_props.readonly"
43 :border="false" 43 :border="false"
44 + @blur="onBlur(item)"
44 > 45 >
45 </van-field> 46 </van-field>
46 <van-field 47 <van-field
...@@ -72,6 +73,7 @@ import { useRoute } from "vue-router"; ...@@ -72,6 +73,7 @@ import { useRoute } from "vue-router";
72 import { wxInfo } from "@/utils/tools"; 73 import { wxInfo } from "@/utils/tools";
73 import $ from "jquery"; 74 import $ from "jquery";
74 import { storeToRefs, mainStore } from "@/utils/generatePackage"; 75 import { storeToRefs, mainStore } from "@/utils/generatePackage";
76 +import Cookies from 'js-cookie';
75 77
76 const $route = useRoute(); 78 const $route = useRoute();
77 const props = defineProps({ 79 const props = defineProps({
...@@ -171,6 +173,22 @@ const blurKeyboard = () => { ...@@ -171,6 +173,22 @@ const blurKeyboard = () => {
171 // 还原border颜色 173 // 还原border颜色
172 content.css("border-color", "#eaeaea"); 174 content.css("border-color", "#eaeaea");
173 }; 175 };
176 +
177 +// 适配cookie保存未完成表单
178 +const onBlur = (item) => {
179 + const currentValue = item.value;
180 + const existingCookie = Cookies.get($route.query.code);
181 +
182 + if (existingCookie) {
183 + // 如果Cookie存在,更新它
184 + let obj = JSON.parse(existingCookie);
185 + obj[props.item.key] = currentValue; // 替换掉旧值
186 + Cookies.set($route.query.code, JSON.stringify(obj));
187 + } else {
188 + // 如果Cookie不存在,新增它
189 + Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }));
190 + }
191 +}
174 </script> 192 </script>
175 193
176 <style lang="less" scoped> 194 <style lang="less" scoped>
......
1 <!-- 1 <!--
2 * @Date: 2022-07-18 10:22:22 2 * @Date: 2022-07-18 10:22:22
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-11-21 10:20:47 4 + * @LastEditTime: 2024-11-21 12:15:57
5 * @FilePath: /data-table/src/views/index.vue 5 * @FilePath: /data-table/src/views/index.vue
6 * @Description: 首页 6 * @Description: 首页
7 --> 7 -->
...@@ -502,19 +502,28 @@ onMounted(async () => { ...@@ -502,19 +502,28 @@ onMounted(async () => {
502 // TAG:创建组件类型 502 // TAG:创建组件类型
503 createComponentType(formData.value); 503 createComponentType(formData.value);
504 504
505 + const isJSON = (value) => {
506 + try {
507 + JSON.parse(value);
508 + return true;
509 + } catch (e) {
510 + return false;
511 + }
512 + }
513 +
505 // TAG:不同类型提交表单处理 514 // TAG:不同类型提交表单处理
506 if (page_type === 'add' && model === 'edit') { // 表单为新增状态, 检查是否有未完成的表单信息 515 if (page_type === 'add' && model === 'edit') { // 表单为新增状态, 检查是否有未完成的表单信息
507 const existingCookie = Cookies.get($route.query.code); 516 const existingCookie = Cookies.get($route.query.code);
508 if (existingCookie) { 517 if (existingCookie) {
509 // 如果Cookie存在,更新它 518 // 如果Cookie存在,更新它
510 let object = JSON.parse(existingCookie); 519 let object = JSON.parse(existingCookie);
511 - console.warn(object);
512 // 默认值 520 // 默认值
513 const objectMap = new Map(Object.entries(object)); // 将 object 转换为 Map,Object.entries() 方法用于返回一个给定对象自身可枚举属性的键值对数组,数组中的每个元素是一个包含键值对的数组,[ ["name", "Alice"], ["age", 30], ["city", "New York"] ] 521 const objectMap = new Map(Object.entries(object)); // 将 object 转换为 Map,Object.entries() 方法用于返回一个给定对象自身可枚举属性的键值对数组,数组中的每个元素是一个包含键值对的数组,[ ["name", "Alice"], ["age", 30], ["city", "New York"] ]
514 formData.value.forEach((item) => { 522 formData.value.forEach((item) => {
515 if (objectMap.has(item.key)) { 523 if (objectMap.has(item.key)) {
516 - console.warn(objectMap.get(item.key)); 524 + // 适配双重json字符串问题,比如地址
517 - item.component_props.default = objectMap.get(item.key); 525 + const value = isJSON(objectMap.get((item.key))) ? JSON.parse(objectMap.get((item.key))) : objectMap.get((item.key));
526 + item.component_props.default = value;
518 } 527 }
519 }); 528 });
520 } 529 }
......