|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <div class="placeholder-layer" v-if="!formModel || !formModel?.id">请选择表单模型</div> |
| 4 | + <tiny-form v-else ref="formRef" label-width="120" label-position="left" :model="modelData" v-bind="$attrs"> |
| 5 | + <tiny-row> |
| 6 | + <tiny-col :span="colNumber" v-for="(item, index) in formModel.parameters" :key="index"> |
| 7 | + <tiny-form-item :prop="item.prop"> |
| 8 | + <template #label> |
| 9 | + <div class="custom-label" v-auto-tip> |
| 10 | + {{ item.label }} |
| 11 | + </div> |
| 12 | + </template> |
| 13 | + <div v-if="item.isModel && item.defaultValue !== null"> |
| 14 | + <tiny-form label-width="100" label-position="left" :model="modelData[item.prop]"> |
| 15 | + <tiny-row> |
| 16 | + <tiny-col |
| 17 | + :span="insideColNumber" |
| 18 | + v-for="(insideItem, insideIndex) in item.defaultValue" |
| 19 | + :key="insideIndex" |
| 20 | + > |
| 21 | + <tiny-form-item :prop="insideItem.prop"> |
| 22 | + <template #label> |
| 23 | + <div class="custom-label" v-auto-tip> |
| 24 | + {{ insideItem.label }} |
| 25 | + </div> |
| 26 | + </template> |
| 27 | + <component |
| 28 | + :is="componentsMap[insideItem.component]" |
| 29 | + v-model="modelData[item.prop][insideItem.prop]" |
| 30 | + :disabled="viewOnly" |
| 31 | + v-bind="insideItem" |
| 32 | + ></component> |
| 33 | + </tiny-form-item> |
| 34 | + </tiny-col> |
| 35 | + </tiny-row> |
| 36 | + </tiny-form> |
| 37 | + </div> |
| 38 | + <component |
| 39 | + v-else |
| 40 | + :is="componentsMap[item.component]" |
| 41 | + v-model="modelData[item.prop]" |
| 42 | + :disabled="viewOnly" |
| 43 | + v-bind="item" |
| 44 | + ></component> |
| 45 | + </tiny-form-item> |
| 46 | + </tiny-col> |
| 47 | + </tiny-row> |
| 48 | + </tiny-form> |
| 49 | + </div> |
| 50 | +</template> |
| 51 | +<script setup> |
| 52 | +import { ref, defineProps, defineExpose, computed, watch, reactive } from 'vue' |
| 53 | +import { |
| 54 | + Form as TinyForm, |
| 55 | + FormItem as TinyFormItem, |
| 56 | + Input as TinyInput, |
| 57 | + Select as TinySelect, |
| 58 | + Checkbox as TinyCheckbox, |
| 59 | + Radio as TinyRadio, |
| 60 | + DatePicker as TinyDatePicker, |
| 61 | + Numeric as TinyNumeric, |
| 62 | + Row as TinyRow, |
| 63 | + Col as TinyCol |
| 64 | +} from '@opentiny/vue' |
| 65 | +import axios from 'axios' |
| 66 | +
|
| 67 | +const props = defineProps({ |
| 68 | + style: { |
| 69 | + type: String |
| 70 | + }, |
| 71 | + className: { |
| 72 | + type: String |
| 73 | + }, |
| 74 | + layout: { |
| 75 | + type: Number, |
| 76 | + default: 2 |
| 77 | + }, |
| 78 | + viewOnly: { |
| 79 | + type: Boolean, |
| 80 | + default: false |
| 81 | + }, |
| 82 | + modelValue: { |
| 83 | + type: Object |
| 84 | + }, |
| 85 | + serviceModel: { |
| 86 | + type: Object |
| 87 | + }, |
| 88 | + modelApis: { |
| 89 | + type: Array, |
| 90 | + default: () => [] |
| 91 | + } |
| 92 | +}) |
| 93 | +
|
| 94 | +const modelData = ref() |
| 95 | +
|
| 96 | +const formRef = ref(null) |
| 97 | +
|
| 98 | +const colNumber = computed(() => 12 / props.layout) |
| 99 | +
|
| 100 | +const insideColNumber = computed(() => (props.layout === 1 ? 6 : 12)) |
| 101 | +
|
| 102 | +const formModel = computed(() => props.serviceModel) |
| 103 | +
|
| 104 | +const componentsMap = reactive({ |
| 105 | + TinyInput, |
| 106 | + TinySelect, |
| 107 | + TinyCheckbox, |
| 108 | + TinyRadio, |
| 109 | + TinyDatePicker, |
| 110 | + TinyNumeric |
| 111 | +}) |
| 112 | +
|
| 113 | +const insertApi = (data = modelData.value) => { |
| 114 | + const apiInfo = props.modelApis.find((api) => api.nameEn === 'insertApi') |
| 115 | + if (!apiInfo) { |
| 116 | + return undefined |
| 117 | + } |
| 118 | + return axios[apiInfo.method](apiInfo.url, data) |
| 119 | + .then((res) => { |
| 120 | + if (res.status === 200) { |
| 121 | + return res.data |
| 122 | + } else { |
| 123 | + throw new Error('request fail') |
| 124 | + } |
| 125 | + }) |
| 126 | + .catch((err) => { |
| 127 | + throw new Error(err) |
| 128 | + }) |
| 129 | +} |
| 130 | +
|
| 131 | +const updateApi = (data = modelData.value) => { |
| 132 | + const apiInfo = props.modelApis.find((api) => api.nameEn === 'updateApi') |
| 133 | + if (!apiInfo) { |
| 134 | + return undefined |
| 135 | + } |
| 136 | + return axios[apiInfo.method](apiInfo.url, data) |
| 137 | + .then((res) => { |
| 138 | + if (res.status === 200) { |
| 139 | + return res.data |
| 140 | + } else { |
| 141 | + throw new Error('request fail') |
| 142 | + } |
| 143 | + }) |
| 144 | + .catch((err) => { |
| 145 | + throw new Error(err) |
| 146 | + }) |
| 147 | +} |
| 148 | +
|
| 149 | +const queryApi = ({ currentPage, pageSize, data } = {}) => { |
| 150 | + const apiInfo = props.modelApis.find((api) => api.nameEn === 'queryApi') |
| 151 | + if (!apiInfo) { |
| 152 | + return undefined |
| 153 | + } |
| 154 | + return axios[apiInfo.method](`${apiInfo.url}?currentPage=${currentPage || 1}&pageSize=${pageSize || 10}`, { |
| 155 | + params: data || modelData.value |
| 156 | + }) |
| 157 | + .then((res) => { |
| 158 | + if (res.status === 200) { |
| 159 | + return res.data |
| 160 | + } |
| 161 | + throw new Error('request fail') |
| 162 | + }) |
| 163 | + .catch((err) => { |
| 164 | + throw new Error(err) |
| 165 | + }) |
| 166 | +} |
| 167 | +
|
| 168 | +const deleteApi = (evidence = { id: modelData.value?.id }) => { |
| 169 | + const apiInfo = props.modelApis.find((api) => api.nameEn === 'deleteApi') |
| 170 | + if (!apiInfo) { |
| 171 | + return undefined |
| 172 | + } |
| 173 | + return axios[apiInfo.method](apiInfo.url, { params: evidence }) |
| 174 | + .then((res) => { |
| 175 | + if (res.status === 200) { |
| 176 | + return res.data |
| 177 | + } else { |
| 178 | + throw new Error('request fail') |
| 179 | + } |
| 180 | + }) |
| 181 | + .catch((err) => { |
| 182 | + throw new Error(err) |
| 183 | + }) |
| 184 | +} |
| 185 | +
|
| 186 | +const initFormData = () => { |
| 187 | + modelData.value = Object.fromEntries( |
| 188 | + (formModel.value.parameters || []).map((item) => { |
| 189 | + return [ |
| 190 | + item.prop, |
| 191 | + item?.isModel |
| 192 | + ? Object.fromEntries( |
| 193 | + item.defaultValue.map((insideItem) => [insideItem.prop, insideItem.defaultValue || null]) |
| 194 | + ) |
| 195 | + : item.defaultValue || null |
| 196 | + ] |
| 197 | + }) |
| 198 | + ) |
| 199 | +} |
| 200 | +
|
| 201 | +watch( |
| 202 | + () => props.modelValue, |
| 203 | + (value) => { |
| 204 | + if (value) { |
| 205 | + modelData.value = props.modelValue |
| 206 | + } |
| 207 | + }, |
| 208 | + { deep: true, immediate: true } |
| 209 | +) |
| 210 | +
|
| 211 | +watch( |
| 212 | + () => props.serviceModel, |
| 213 | + () => { |
| 214 | + if (!modelData.value) { |
| 215 | + initFormData() |
| 216 | + } |
| 217 | + }, |
| 218 | + { immediate: true } |
| 219 | +) |
| 220 | +
|
| 221 | +const exposedData = { |
| 222 | + formData: () => modelData.value, |
| 223 | + formRef, |
| 224 | + insertApi, |
| 225 | + updateApi, |
| 226 | + queryApi, |
| 227 | + deleteApi |
| 228 | +} |
| 229 | +
|
| 230 | +defineExpose({ |
| 231 | + ...exposedData |
| 232 | +}) |
| 233 | +</script> |
| 234 | +<style lang="less" scoped> |
| 235 | +.placeholder-layer { |
| 236 | + height: 40px; |
| 237 | + background-color: #f5f5f5; |
| 238 | + width: 100%; |
| 239 | + text-align: center; |
| 240 | + line-height: 40px; |
| 241 | +} |
| 242 | +</style> |
0 commit comments