Skip to content

Commit 1386406

Browse files
feat(model):The model driver is optimized, and built-in interfaces for adding, deleting, modifying, and querying are added (opentiny#1756)
1 parent be00161 commit 1386406

11 files changed

Lines changed: 153 additions & 178 deletions

File tree

packages/builtinComponent/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"preview": "vite preview"
2323
},
2424
"dependencies": {
25-
"axios": "^0.28.0",
25+
"@opentiny/tiny-engine-meta-register": "workspace:*",
2626
"vite-plugin-css-injected-by-js": "^3.3.1"
2727
},
2828
"devDependencies": {

packages/builtinComponent/src/components/BaseForm.vue

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import {
6262
Row as TinyRow,
6363
Col as TinyCol
6464
} from '@opentiny/vue'
65-
import axios from 'axios'
65+
import { getMetaApi, META_SERVICE } from '@opentiny/tiny-engine-meta-register'
6666
6767
const props = defineProps({
6868
style: {
@@ -115,14 +115,8 @@ const insertApi = (data = modelData.value) => {
115115
if (!apiInfo) {
116116
return undefined
117117
}
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-
})
118+
return getMetaApi(META_SERVICE.Http)
119+
.post(apiInfo.url, { nameEn: formModel.value.nameEn, params: data })
126120
.catch((err) => {
127121
throw new Error(err)
128122
})
@@ -133,13 +127,13 @@ const updateApi = (data = modelData.value) => {
133127
if (!apiInfo) {
134128
return undefined
135129
}
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-
}
130+
const id = data.id
131+
delete data.id
132+
return getMetaApi(META_SERVICE.Http)
133+
.post(apiInfo.url, {
134+
nameEn: formModel.value.nameEn,
135+
data: data,
136+
params: { id }
143137
})
144138
.catch((err) => {
145139
throw new Error(err)
@@ -151,33 +145,31 @@ const queryApi = ({ currentPage, pageSize, data } = {}) => {
151145
if (!apiInfo) {
152146
return undefined
153147
}
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
148+
// 处理查询参数
149+
const params = Object.fromEntries(pageModel.value.parameters.map((item) => [item.prop, null]))
150+
return getMetaApi(META_SERVICE.Http)
151+
.post(apiInfo.url, {
152+
currentPage: currentPage || 1,
153+
pageSize: pageSize || 10,
154+
nameEn: formModel.value.nameEn,
155+
nameCn: formModel.value.nameCn,
156+
params: {
157+
...params,
158+
...(data || modelData.value)
160159
}
161-
throw new Error('request fail')
162160
})
163161
.catch((err) => {
164162
throw new Error(err)
165163
})
166164
}
167165
168-
const deleteApi = (evidence = { id: modelData.value?.id }) => {
166+
const deleteApi = () => {
169167
const apiInfo = props.modelApis.find((api) => api.nameEn === 'deleteApi')
170168
if (!apiInfo) {
171169
return undefined
172170
}
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-
})
171+
return getMetaApi(META_SERVICE.Http)
172+
.post(apiInfo.url, { id: modelData.value?.id, nameEn: formModel.value.nameEn })
181173
.catch((err) => {
182174
throw new Error(err)
183175
})

packages/builtinComponent/src/components/BasePage.vue

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ import {
179179
Notify
180180
} from '@opentiny/vue'
181181
import * as tinyVueIcon from '@opentiny/vue-icon'
182-
import axios from 'axios'
182+
import { getMetaApi, META_SERVICE } from '@opentiny/tiny-engine-meta-register'
183183
184184
const props = defineProps({
185185
style: {
@@ -302,18 +302,15 @@ const insertApi = (data = addFormData.value) => {
302302
if (!apiInfo) {
303303
return undefined
304304
}
305-
return axios[apiInfo.method](apiInfo.url, data)
305+
return getMetaApi(META_SERVICE.Http)
306+
.post(apiInfo.url, { nameEn: pageModel.value.nameEn, params: data })
306307
.then((res) => {
307-
if (res.status === 200) {
308-
Notify({
309-
type: 'success',
310-
message: res.data.message,
311-
position: 'top-right'
312-
})
313-
return res.data
314-
} else {
315-
throw new Error('request fail')
316-
}
308+
Notify({
309+
type: 'success',
310+
message: '新增成功',
311+
position: 'top-right'
312+
})
313+
return res
317314
})
318315
.catch((err) => {
319316
throw new Error(err)
@@ -325,18 +322,21 @@ const updateApi = (data = addFormData.value) => {
325322
if (!apiInfo) {
326323
return undefined
327324
}
328-
return axios[apiInfo.method](apiInfo.url, data)
325+
const id = data.id
326+
delete data.id
327+
return getMetaApi(META_SERVICE.Http)
328+
.post(apiInfo.url, {
329+
nameEn: pageModel.value.nameEn,
330+
data: data,
331+
params: { id }
332+
})
329333
.then((res) => {
330-
if (res.status === 200) {
331-
Notify({
332-
type: 'success',
333-
message: res.data.message,
334-
position: 'top-right'
335-
})
336-
return res.data
337-
} else {
338-
throw new Error('request fail')
339-
}
334+
Notify({
335+
type: 'success',
336+
message: '修改成功',
337+
position: 'top-right'
338+
})
339+
return res
340340
})
341341
.catch((err) => {
342342
throw new Error(err)
@@ -348,17 +348,24 @@ const queryApi = ({ currentPage, pageSize, data } = {}) => {
348348
if (!apiInfo) {
349349
return undefined
350350
}
351-
return axios[apiInfo.method](`${apiInfo.url}?currentPage=${currentPage || 1}&pageSize=${pageSize || 10}`, {
352-
params: data || formData.value
353-
})
354-
.then((res) => {
355-
if (res.status === 200) {
356-
tableData.value = res.data.data
357-
pagerState.total = res.data.total
358-
emit('update:tableData', tableData.value)
359-
return res.data
351+
// 处理查询参数
352+
const params = Object.fromEntries(pageModel.value.parameters.map((item) => [item.prop, null]))
353+
return getMetaApi(META_SERVICE.Http)
354+
.post(apiInfo.url, {
355+
currentPage: currentPage || 1,
356+
pageSize: pageSize || 10,
357+
nameEn: pageModel.value.nameEn,
358+
nameCn: pageModel.value.nameCn,
359+
params: {
360+
...params,
361+
...data
360362
}
361-
throw new Error('request fail')
363+
})
364+
.then((res) => {
365+
tableData.value = res.list
366+
pagerState.total = res.total
367+
emit('update:tableData', tableData.value)
368+
return res
362369
})
363370
.catch((err) => {
364371
throw new Error(err)
@@ -370,18 +377,15 @@ const deleteApi = (evidence) => {
370377
if (!apiInfo) {
371378
return undefined
372379
}
373-
return axios[apiInfo.method](apiInfo.url, { params: evidence })
380+
return getMetaApi(META_SERVICE.Http)
381+
.post(apiInfo.url, { ...evidence, nameEn: pageModel.value.nameEn })
374382
.then((res) => {
375-
if (res.status === 200) {
376-
Notify({
377-
type: 'success',
378-
message: res.data.message,
379-
position: 'top-right'
380-
})
381-
return res.data
382-
} else {
383-
throw new Error('request fail')
384-
}
383+
Notify({
384+
type: 'success',
385+
message: '已删除',
386+
position: 'top-right'
387+
})
388+
return res
385389
})
386390
.catch((err) => {
387391
throw new Error(err)

packages/builtinComponent/src/components/BaseTable.vue

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import {
6363
Popover as TinyPopover
6464
} from '@opentiny/vue'
6565
import * as tinyVueIcon from '@opentiny/vue-icon'
66-
import axios from 'axios'
66+
import { getMetaApi, META_SERVICE } from '@opentiny/tiny-engine-meta-register'
6767
6868
const props = defineProps({
6969
style: {
@@ -169,14 +169,8 @@ const insertApi = (data = {}) => {
169169
if (!apiInfo) {
170170
return undefined
171171
}
172-
return axios[apiInfo.method](apiInfo.url, data)
173-
.then((res) => {
174-
if (res.status === 200) {
175-
return res.data
176-
} else {
177-
throw new Error('request fail')
178-
}
179-
})
172+
return getMetaApi(META_SERVICE.Http)
173+
.post(apiInfo.url, { nameEn: tableModel.value.nameEn, params: data })
180174
.catch((err) => {
181175
throw new Error(err)
182176
})
@@ -187,13 +181,13 @@ const updateApi = (data) => {
187181
if (!apiInfo) {
188182
return undefined
189183
}
190-
return axios[apiInfo.method](apiInfo.url, data)
191-
.then((res) => {
192-
if (res.status === 200) {
193-
return res.data
194-
} else {
195-
throw new Error('request fail')
196-
}
184+
const id = data.id
185+
delete data.id
186+
return getMetaApi(META_SERVICE.Http)
187+
.post(apiInfo.url, {
188+
nameEn: tableModel.value.nameEn,
189+
data: data,
190+
params: { id }
197191
})
198192
.catch((err) => {
199193
throw new Error(err)
@@ -207,16 +201,23 @@ const queryApi = (
207201
if (!apiInfo) {
208202
return undefined
209203
}
210-
return axios[apiInfo.method](`${apiInfo.url}?currentPage=${currentPage}&pageSize=${pageSize}`, { params: data })
211-
.then((res) => {
212-
if (res.status === 200) {
213-
if (res.data.code === 200) {
214-
tableData.value = res.data.data
215-
pagerState.total = res.data.total
216-
return res.data
217-
}
204+
// 处理查询参数
205+
const params = Object.fromEntries(tableModel.value.parameters.map((item) => [item.prop, null]))
206+
return getMetaApi(META_SERVICE.Http)
207+
.post(apiInfo.url, {
208+
currentPage: currentPage || 1,
209+
pageSize: pageSize || 10,
210+
nameEn: tableModel.value.nameEn,
211+
nameCn: tableModel.value.nameCn,
212+
params: {
213+
...params,
214+
...data
218215
}
219-
throw new Error('request fail')
216+
})
217+
.then((res) => {
218+
tableData.value = res.list
219+
pagerState.total = res.total
220+
return res
220221
})
221222
.catch((err) => {
222223
throw new Error(err)
@@ -228,14 +229,8 @@ const deleteApi = (evidence) => {
228229
if (!apiInfo) {
229230
return undefined
230231
}
231-
return axios[apiInfo.method](apiInfo.url, { params: evidence })
232-
.then((res) => {
233-
if (res.status === 200) {
234-
return res.data
235-
} else {
236-
throw new Error('request fail')
237-
}
238-
})
232+
return getMetaApi(META_SERVICE.Http)
233+
.post(apiInfo.url, { ...evidence, nameEn: tableModel.value.nameEn })
239234
.catch((err) => {
240235
throw new Error(err)
241236
})

0 commit comments

Comments
 (0)