index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { reactive, ref } from 'vue'
  2. import { batchDelete as admin_batchDelete, list as admin_list, remove as admin_remove } from '@/api/share_record'
  3. import { batchDelete as my_batchDelete, list as my_list, remove as my_remove } from '@/api/my/share_record'
  4. import { ElMessage, ElMessageBox } from 'element-plus'
  5. import { T } from '@/utils/i18n'
  6. const apis = {
  7. admin: { batchDelete: admin_batchDelete, list: admin_list, remove: admin_remove },
  8. my: { batchDelete: my_batchDelete, list: my_list, remove: my_remove },
  9. }
  10. export function useRepositories (api_type = 'my') {
  11. const listRes = reactive({
  12. list: [], total: 0, loading: false,
  13. })
  14. const listQuery = reactive({
  15. page: 1,
  16. page_size: 10,
  17. })
  18. const getList = async () => {
  19. listRes.loading = true
  20. const res = await apis[api_type].list(listQuery).catch(_ => false)
  21. listRes.loading = false
  22. if (res) {
  23. listRes.list = res.data.list
  24. listRes.total = res.data.total
  25. }
  26. }
  27. const handlerQuery = () => {
  28. if (listQuery.page === 1) {
  29. getList()
  30. } else {
  31. listQuery.page = 1
  32. }
  33. }
  34. const del = async (row) => {
  35. const cf = await ElMessageBox.confirm(T('Confirm?', { param: T('Delete') }), {
  36. confirmButtonText: T('Confirm'),
  37. cancelButtonText: T('Cancel'),
  38. type: 'warning',
  39. }).catch(_ => false)
  40. if (!cf) {
  41. return false
  42. }
  43. const res = await apis[api_type].remove({ id: row.id }).catch(_ => false)
  44. if (res) {
  45. ElMessage.success(T('OperationSuccess'))
  46. getList()
  47. }
  48. }
  49. const multipleSelection = ref([])
  50. const toBatchDelete = async () => {
  51. if (multipleSelection.value.length === 0) {
  52. return
  53. }
  54. const ids = multipleSelection.value.map(r => r.id)
  55. if (!ids.length) {
  56. ElMessage.warning(T('PleaseSelectData'))
  57. return false
  58. }
  59. const cf = await ElMessageBox.confirm(T('Confirm?', { param: T('BatchDelete') }), {
  60. confirmButtonText: T('Confirm'),
  61. cancelButtonText: T('Cancel'),
  62. type: 'warning',
  63. }).catch(_ => false)
  64. if (!cf) {
  65. return false
  66. }
  67. const res = await apis[api_type].batchDelete({ ids }).catch(_ => false)
  68. if (res) {
  69. ElMessage.success(T('OperationSuccess'))
  70. getList()
  71. }
  72. }
  73. const expired = (row) => {
  74. if (row.expire === 0) {
  75. return false
  76. }
  77. const now = new Date().getTime()
  78. const created_at = new Date(row.created_at).getTime()
  79. return row.expire * 1000 + created_at < now
  80. }
  81. return {
  82. listRes,
  83. listQuery,
  84. getList,
  85. handlerQuery,
  86. del,
  87. multipleSelection,
  88. toBatchDelete,
  89. expired,
  90. }
  91. }