@longhongguo/form-create-ant-design-vue 3.3.20 → 3.3.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@longhongguo/form-create-ant-design-vue",
3
- "version": "3.3.20",
3
+ "version": "3.3.23",
4
4
  "description": "AntDesignVue版本低代码表单|FormCreate 是一个可以通过 JSON 生成具有动态渲染、数据收集、验证和提交功能的低代码表单生成组件。支持6个UI框架,适配移动端,并且支持生成任何 Vue 组件。内置20种常用表单组件和自定义组件,再复杂的表单都可以轻松搞定。",
5
5
  "main": "./dist/form-create.min.js",
6
6
  "module": "./dist/form-create.esm.js",
@@ -80,6 +80,19 @@ export default defineComponent({
80
80
  fileName: {
81
81
  type: String,
82
82
  default: ''
83
+ },
84
+ // 请求前置处理函数
85
+ beforeFetch: {
86
+ type: Function,
87
+ default: null
88
+ },
89
+ // 数据类型:json 或 formData
90
+ dataType: {
91
+ type: String,
92
+ default: 'json',
93
+ validator(value) {
94
+ return ['json', 'formData'].includes(value)
95
+ }
83
96
  }
84
97
  },
85
98
  data() {
@@ -101,15 +114,44 @@ export default defineComponent({
101
114
  this.loading = true
102
115
 
103
116
  try {
104
- // 使用自定义的fetch来处理blob响应
105
- const result = await this.fetchBlob({
117
+ // 构建请求配置
118
+ let config = {
106
119
  action: this.action,
107
120
  method: this.method,
108
121
  data: this.data,
109
122
  query: this.query,
110
123
  headers: this.headers,
111
- withCredentials: this.withCredentials
112
- })
124
+ withCredentials: this.withCredentials,
125
+ dataType: this.dataType
126
+ }
127
+
128
+ // 如果有 beforeFetch,先调用它来修改配置
129
+ if (this.beforeFetch && typeof this.beforeFetch === 'function') {
130
+ const formCreateInject = this.formCreateInject || {}
131
+ const api = formCreateInject.api
132
+ const rule = formCreateInject.rule
133
+
134
+ // beforeFetch 的第二个参数是 { api, rule }
135
+ const result = this.beforeFetch(config, { api, rule })
136
+
137
+ // 如果 beforeFetch 返回 Promise,等待它完成
138
+ if (result && typeof result.then === 'function') {
139
+ await result
140
+ } else if (result === false) {
141
+ // 如果返回 false,取消请求
142
+ this.loading = false
143
+ return
144
+ } else if (result && typeof result === 'object') {
145
+ // 如果返回了新的配置对象,使用它
146
+ config = result
147
+ }
148
+
149
+ // beforeFetch 可能直接修改了 config,或者返回了新的 config
150
+ // 这里假设它会直接修改传入的 config 对象(符合 form-create 的惯例)
151
+ }
152
+
153
+ // 使用自定义的fetch来处理blob响应
154
+ const result = await this.fetchBlob(config)
113
155
 
114
156
  // 获取文件名
115
157
  let fileName =
@@ -264,12 +306,33 @@ export default defineComponent({
264
306
  }
265
307
 
266
308
  // 处理请求体
267
- if (config.data) {
268
- const formData = new FormData()
269
- Object.keys(config.data).forEach((key) => {
270
- formData.append(key, config.data[key])
271
- })
272
- xhr.send(formData)
309
+ const dataType = config.dataType || 'json'
310
+ if (config.data && Object.keys(config.data).length > 0) {
311
+ if (dataType === 'formData') {
312
+ // 使用 FormData
313
+ const formData = new FormData()
314
+ Object.keys(config.data).forEach((key) => {
315
+ const value = config.data[key]
316
+ if (value !== null && value !== undefined) {
317
+ if (Array.isArray(value)) {
318
+ // 如果是数组,需要特殊处理
319
+ value.forEach((item) => {
320
+ formData.append(key, item)
321
+ })
322
+ } else {
323
+ formData.append(key, value)
324
+ }
325
+ }
326
+ })
327
+ xhr.send(formData)
328
+ } else {
329
+ // 使用 JSON
330
+ xhr.setRequestHeader(
331
+ 'Content-Type',
332
+ 'application/json;charset=UTF-8'
333
+ )
334
+ xhr.send(JSON.stringify(config.data))
335
+ }
273
336
  } else {
274
337
  xhr.send()
275
338
  }