useDestroy.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import http from '/admin/support/http'
  2. import { Code } from '/admin/enum/app'
  3. import Message from '/admin/support/message'
  4. import { ref, watch } from 'vue'
  5. import { isFunction } from '/admin/support/helper'
  6. export function useDestroy(confirm: string = '确认删除吗') {
  7. const isDeleted = ref(false)
  8. const beforeDestroy = ref()
  9. // fetch list
  10. function destroy(path: string, id: string | number) {
  11. Message.confirm(confirm + '?', function () {
  12. // before destroy
  13. if (isFunction(beforeDestroy.value)) {
  14. beforeDestroy.value()
  15. }
  16. http
  17. .delete(path + '/' + id)
  18. .then(r => {
  19. if (r.data.code === Code.SUCCESS) {
  20. Message.success(r.data.message)
  21. isDeleted.value = true
  22. } else {
  23. Message.error(r.data.message)
  24. }
  25. })
  26. .finally(() => {})
  27. })
  28. }
  29. const deleted = (reset: Function) => {
  30. watch(isDeleted, function (value) {
  31. if (value) {
  32. isDeleted.value = false
  33. reset()
  34. }
  35. })
  36. }
  37. return { destroy, deleted }
  38. }