2022-12-05 23:01:12 +08:00
|
|
|
import http from '/admin/support/http'
|
2023-03-10 17:07:19 +08:00
|
|
|
import { Code } from '/admin/enum/app'
|
2022-12-05 23:01:12 +08:00
|
|
|
import Message from '/admin/support/message'
|
2022-12-07 19:28:57 +08:00
|
|
|
import { ref, watch } from 'vue'
|
|
|
|
import { isFunction } from '/admin/support/helper'
|
2022-12-05 23:01:12 +08:00
|
|
|
|
|
|
|
export function useEnabled() {
|
2022-12-07 19:28:57 +08:00
|
|
|
const isSuccess = ref(false)
|
2022-12-05 23:01:12 +08:00
|
|
|
const loading = ref<boolean>(false)
|
2022-12-07 19:28:57 +08:00
|
|
|
const afterEnabled = ref()
|
2022-12-05 23:01:12 +08:00
|
|
|
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) {
|
2022-12-07 19:28:57 +08:00
|
|
|
isSuccess.value = true
|
2022-12-05 23:01:12 +08:00
|
|
|
Message.success(r.data.message)
|
2022-12-07 19:28:57 +08:00
|
|
|
if (isFunction(afterEnabled.value)) {
|
|
|
|
afterEnabled.value()
|
|
|
|
}
|
2022-12-05 23:01:12 +08:00
|
|
|
} else {
|
|
|
|
Message.error(r.data.message)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
loading.value = false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-07 19:28:57 +08:00
|
|
|
const success = (func: Function) => {
|
|
|
|
watch(isSuccess, function () {
|
|
|
|
isSuccess.value = false
|
|
|
|
func()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return { enabled, success, loading, afterEnabled }
|
2022-12-05 23:01:12 +08:00
|
|
|
}
|