useEnabled.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 useEnabled() {
  7. const isSuccess = ref(false)
  8. const loading = ref<boolean>(false)
  9. const afterEnabled = ref()
  10. function enabled(path: string, id: string | number, data: object = {}) {
  11. loading.value = true
  12. http
  13. .put(path + '/enable/' + id, data)
  14. .then(r => {
  15. if (r.data.code === Code.SUCCESS) {
  16. isSuccess.value = true
  17. Message.success(r.data.message)
  18. if (isFunction(afterEnabled.value)) {
  19. afterEnabled.value()
  20. }
  21. } else {
  22. Message.error(r.data.message)
  23. }
  24. })
  25. .finally(() => {
  26. loading.value = false
  27. })
  28. }
  29. const success = (func: Function) => {
  30. watch(isSuccess, function () {
  31. isSuccess.value = false
  32. func()
  33. })
  34. }
  35. return { enabled, success, loading, afterEnabled }
  36. }