first commit
This commit is contained in:
69
resources/admin/composables/curd/useCreate.ts
Normal file
69
resources/admin/composables/curd/useCreate.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import http from '/admin/support/http'
|
||||
import { ref, unref } from 'vue'
|
||||
import { Code } from '/admin/enum/app'
|
||||
import Message from '/admin/support/message'
|
||||
import { FormInstance } from 'element-plus'
|
||||
import { AxiosResponse } from 'axios'
|
||||
import { isFunction } from'/admin/support/helper'
|
||||
|
||||
// get table list
|
||||
export function useCreate(path: string, id: string | number | null = null, _formData: object = {}) {
|
||||
const formData = ref<object>(_formData)
|
||||
|
||||
const loading = ref<boolean>()
|
||||
const isClose = ref<boolean>(false)
|
||||
|
||||
// 创建前 hook
|
||||
const beforeCreate = ref()
|
||||
// 更新前 hook
|
||||
const beforeUpdate = ref()
|
||||
|
||||
// store
|
||||
function store(path: string, id: string | number | null = null) {
|
||||
loading.value = true
|
||||
let promise: Promise<AxiosResponse> | null = null
|
||||
if (id) {
|
||||
if (isFunction(beforeUpdate.value)) {
|
||||
beforeUpdate.value()
|
||||
}
|
||||
|
||||
promise = http.put(path + '/' + id, unref(formData))
|
||||
} else {
|
||||
console.log(isFunction(beforeCreate.value), beforeCreate.value)
|
||||
if (isFunction(beforeCreate.value)) {
|
||||
beforeCreate.value()
|
||||
}
|
||||
|
||||
promise = http.post(path, unref(formData))
|
||||
}
|
||||
|
||||
promise
|
||||
.then(r => {
|
||||
if (r.data.code === Code.SUCCESS) {
|
||||
isClose.value = true
|
||||
Message.success(r.data.message)
|
||||
} else {
|
||||
Message.error(r.data.message)
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
loading.value = false
|
||||
})
|
||||
}
|
||||
|
||||
const form = ref<FormInstance>()
|
||||
const submitForm = (formEl: FormInstance | undefined) => {
|
||||
if (!formEl) return
|
||||
formEl
|
||||
.validate(valid => {
|
||||
if (valid) {
|
||||
store(path, id)
|
||||
} else {
|
||||
loading.value = false
|
||||
}
|
||||
})
|
||||
.then(() => {})
|
||||
}
|
||||
|
||||
return { formData, loading, form, submitForm, isClose, beforeCreate, beforeUpdate }
|
||||
}
|
36
resources/admin/composables/curd/useDestroy.ts
Normal file
36
resources/admin/composables/curd/useDestroy.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import http from '/admin/support/http'
|
||||
import { Code } from '/admin/enum/app'
|
||||
import Message from '/admin/support/message'
|
||||
import { ref } from 'vue'
|
||||
import { isFunction } from'/admin/support/helper'
|
||||
|
||||
export function useDestroy(confirm: string = '确认删除吗') {
|
||||
const isDeleted = ref(false)
|
||||
|
||||
const beforeDestroy = ref()
|
||||
|
||||
// fetch list
|
||||
function destroy(path: string, id: string | number) {
|
||||
Message.confirm(confirm + '?', function () {
|
||||
|
||||
// before destroy
|
||||
if (isFunction(beforeDestroy.value)) {
|
||||
beforeDestroy.value()
|
||||
}
|
||||
|
||||
http
|
||||
.delete(path + '/' + id)
|
||||
.then(r => {
|
||||
if (r.data.code === Code.SUCCESS) {
|
||||
Message.success(r.data.message)
|
||||
isDeleted.value = true
|
||||
} else {
|
||||
Message.error(r.data.message)
|
||||
}
|
||||
})
|
||||
.finally(() => {})
|
||||
})
|
||||
}
|
||||
|
||||
return { destroy, isDeleted }
|
||||
}
|
27
resources/admin/composables/curd/useEnabled.ts
Normal file
27
resources/admin/composables/curd/useEnabled.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import http from '/admin/support/http'
|
||||
import { Code } from '/admin/assets/enum/app'
|
||||
import Message from '/admin/support/message'
|
||||
import { ref } from 'vue'
|
||||
|
||||
export function useEnabled() {
|
||||
const success = ref(false)
|
||||
const loading = ref<boolean>(false)
|
||||
function enabled(path: string, id: string | number, data: object = {}) {
|
||||
loading.value = true
|
||||
http
|
||||
.put(path + '/enable/' + id, data)
|
||||
.then(r => {
|
||||
if (r.data.code === Code.SUCCESS) {
|
||||
success.value = true
|
||||
Message.success(r.data.message)
|
||||
} else {
|
||||
Message.error(r.data.message)
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
loading.value = false
|
||||
})
|
||||
}
|
||||
|
||||
return { enabled, success, loading }
|
||||
}
|
75
resources/admin/composables/curd/useGetList.ts
Normal file
75
resources/admin/composables/curd/useGetList.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import http from '/admin/support/http'
|
||||
import { ref, unref } from 'vue'
|
||||
import { Code } from '/admin/enum/app'
|
||||
import Message from '/admin/support/message'
|
||||
|
||||
const initLimit = 10
|
||||
const initPage = 1;
|
||||
|
||||
// get table list
|
||||
export function useGetList(path: string) {
|
||||
const data = ref<object>()
|
||||
const page = ref(initPage)
|
||||
const limit = ref(initLimit)
|
||||
const query = ref<object>({
|
||||
page: page.value,
|
||||
limit: limit.value,
|
||||
})
|
||||
const loading = ref(true)
|
||||
// fetch list
|
||||
function getList() {
|
||||
// when table's data page >= 100, it will loading
|
||||
if (page.value >= 100) {
|
||||
loading.value = true
|
||||
}
|
||||
http
|
||||
.get(path, unref(query))
|
||||
.then(r => {
|
||||
closeLoading()
|
||||
if (r.data.code === Code.SUCCESS) {
|
||||
data.value = r.data
|
||||
} else {
|
||||
Message.error(r.data.message)
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
closeLoading()
|
||||
})
|
||||
}
|
||||
|
||||
// close loading
|
||||
function closeLoading() {
|
||||
loading.value = false
|
||||
}
|
||||
// search
|
||||
function search() {
|
||||
getList()
|
||||
}
|
||||
|
||||
// reset
|
||||
function reset() {
|
||||
query.value = Object.assign({ page: page.value, limit: limit.value })
|
||||
|
||||
getList()
|
||||
}
|
||||
|
||||
// change page
|
||||
function changePage(p: number) {
|
||||
page.value = p
|
||||
// @ts-ignore
|
||||
query.value.page = p
|
||||
search()
|
||||
}
|
||||
|
||||
// change limit
|
||||
function changeLimit(l: number) {
|
||||
limit.value = l
|
||||
// @ts-ignore
|
||||
query.value.page = 1
|
||||
// @ts-ignore
|
||||
query.value.limit = l
|
||||
search()
|
||||
}
|
||||
|
||||
return { data, query, search, reset, changePage, changeLimit, loading }
|
||||
}
|
14
resources/admin/composables/curd/useShow.ts
Normal file
14
resources/admin/composables/curd/useShow.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import http from '/admin/support/http'
|
||||
|
||||
export function useShow(path: string, id: string | number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
http
|
||||
.get(path + '/' + id)
|
||||
.then(response => {
|
||||
resolve(response.data)
|
||||
})
|
||||
.catch(e => {
|
||||
reject(e)
|
||||
})
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user