index.vue
4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!--
* @Date: 2022-08-30 14:32:11
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-11-21 13:34:36
* @FilePath: /data-table/src/components/AreaPickerField/index.vue
* @Description: 省市区选择控件
-->
<template>
<div v-if="HideShow" class="area-picker-field">
<div class="label">
<span v-if="item.component_props.disabled_show"><van-icon name="https://cdn.ipadbiz.cn/custom_form/icon/closed-eye1.png" /></span>
<span v-if="item.component_props.required" style="color: red">* </span>
<span :class="[ReadonlyShow ? 'readonly-show' : '']">{{ item.component_props.label }}</span>
</div>
<van-field :name="item.key" :rules="rules" style="padding: 0;">
<template #input>
<my-component @active="onActive" />
</template>
</van-field>
</div>
</template>
<script setup>
import { useRoute } from "vue-router";
import MyComponent from './MyComponent.vue';
import _ from 'lodash'
import Cookies from 'js-cookie';
const $route = useRoute();
const props = defineProps({
item: Object,
});
// 只读显示-流程模式
const ReadonlyShow = computed(() => {
return ($route.query.page_type === 'flow' || $route.query.page_type === 'edit') && !props.item.component_props.readonly;
});
if (props.item.component_props.default) { // 存在默认值时业务逻辑
if (!props.item.component_props.default?.city_code) { // 默认值为空
props.item.component_props.default.picker_value = '';
props.item.component_props.default.input_value = '';
props.item.city_code = '';
} else {
props.item.component_props.default.picker_value = `${props.item.component_props.default.province} ${props.item.component_props.default.city} ${props.item.component_props.default.district}`;
props.item.component_props.default.input_value = props.item.component_props.default.street;
props.item.city_code = props.item.component_props.default.city_code;
}
} else { // 默认值为空, 处理数据显示问题 - 详细地址不填写可能存在undefined
props.item.component_props.default = {
picker_value: '',
input_value: '',
city_code: '',
}
}
// 注入子组件属性
provide('props', props.item);
// 隐藏显示
const HideShow = computed(() => {
return !props.item.component_props.disabled
});
// 规则校验
const required = props.item.component_props.required;
const show_street = !props.item.component_props.no_street; // 显示详细地址
const validator = (val) => {
if (required && show_street && (!val.picker_value || !val.input_value)) {
return false;
} else if (required && !show_street && !val.picker_value) {
return false;
} else {
return true;
}
};
// 错误提示文案
const validatorMessage = (val, rule) => {
if (required && show_street && (!val.picker_value || !val.input_value)) {
return "必填项不能为空";
} else if (required && !show_street && !val.picker_value) {
return "必填项不能为空";
}
};
const rules = [{ validator, message: validatorMessage }];
const isJSON = (value) => {
try {
JSON.parse(value);
return true;
} catch (e) {
return false;
}
}
// 子组件通信,适配规则触发
const onActive = (val) => {
// 适配cookie保存未完成表单
const currentValue = JSON.stringify(val);
const existingCookie = Cookies.get($route.query.code);
if (existingCookie) {
// 如果Cookie存在,更新它
let obj = JSON.parse(existingCookie);
if (val.city_code) { // 修改了
let temp = null;
if (isJSON(obj[props.item.key])) {
temp = JSON.parse(obj[props.item.key]);
temp.city_code = val.city_code;
temp.province = val.province;
temp.city = val.city;
temp.district = val.district;
} else {
temp = {
city_code: val.city_code,
province: val.province,
city: val.city,
district: val.district,
street: val.street,
};
}
obj[props.item.key] = JSON.stringify(temp); // 替换掉旧值
} else { // 只修改了地址
let temp = JSON.parse(obj[props.item.key]);
temp.street = val.street;
obj[props.item.key] = JSON.stringify(temp); // 替换掉旧值
}
Cookies.set($route.query.code, JSON.stringify(obj), { expires: 1 });
} else {
// 如果Cookie不存在,新增它
Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }), { expires: 1 });
}
};
</script>
<style lang="less" scoped>
.area-picker-field {
.label {
padding: 1rem 1rem 0 1rem;
font-size: 0.9rem;
font-weight: bold;
}
}
:deep(.van-field__error-message) {
padding-left: 1rem;
}
</style>