hookehuyr

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

1 <!-- 1 <!--
2 * @Date: 2022-08-30 11:34:19 2 * @Date: 2022-08-30 11:34:19
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-08-22 09:58:37 4 + * @LastEditTime: 2024-11-20 17:52:28
5 * @FilePath: /data-table/src/components/CheckboxField/index.vue 5 * @FilePath: /data-table/src/components/CheckboxField/index.vue
6 * @Description: 多项选择控件 6 * @Description: 多项选择控件
7 --> 7 -->
...@@ -40,6 +40,8 @@ ...@@ -40,6 +40,8 @@
40 <script setup> 40 <script setup>
41 import { styleColor } from "@/constant.js"; 41 import { styleColor } from "@/constant.js";
42 import { useRoute } from "vue-router"; 42 import { useRoute } from "vue-router";
43 +import Cookies from 'js-cookie';
44 +import _ from 'lodash';
43 45
44 const $route = useRoute(); 46 const $route = useRoute();
45 47
...@@ -151,6 +153,33 @@ onMounted(() => { ...@@ -151,6 +153,33 @@ onMounted(() => {
151 props.item.value = { key: props.item.key, value: checkbox_value.value, affix: affix_value.value, type: "checkbox" }; 153 props.item.value = { key: props.item.key, value: checkbox_value.value, affix: affix_value.value, type: "checkbox" };
152 emit("active", props.item.value); 154 emit("active", props.item.value);
153 }) 155 })
156 +
157 +// 适配cookie保存未完成表单
158 +watch(
159 + () => props.item.value,
160 + (v) => {
161 + const checkbox_value = _.cloneDeep(v.value)
162 + checkbox_value?.forEach((element, index) => {
163 + for (const key in v.affix) {
164 + if (v.affix[key] && element === key) {
165 + checkbox_value[index] = v.affix[key]
166 + }
167 + }
168 + });
169 + const currentValue = checkbox_value;
170 + const existingCookie = Cookies.get($route.query.code);
171 +
172 + if (existingCookie) {
173 + // 如果Cookie存在,更新它
174 + let obj = JSON.parse(existingCookie);
175 + obj[props.item.key] = currentValue; // 替换掉旧值
176 + Cookies.set($route.query.code, JSON.stringify(obj));
177 + } else {
178 + // 如果Cookie不存在,新增它
179 + Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }));
180 + }
181 + }
182 + );
154 </script> 183 </script>
155 184
156 <style lang="less" scoped> 185 <style lang="less" scoped>
......