reponsitories.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { reactive } from 'vue'
  2. import { list, remove, fileList, fileRemove, batchDelete, fileBatchDelete } from '@/api/audit'
  3. import { ElMessage, ElMessageBox } from 'element-plus'
  4. import { formatTime } from '@/utils/time'
  5. import { T } from '@/utils/i18n'
  6. import { downBlob, jsonToCsv } from '@/utils/file'
  7. export function useRepositories () {
  8. const listRes = reactive({
  9. list: [], total: 0, loading: false,
  10. })
  11. const listQuery = reactive({
  12. page: 1,
  13. page_size: 10,
  14. peer_id: null,
  15. from_peer: null,
  16. })
  17. const getList = async () => {
  18. listRes.loading = true
  19. const res = await list(listQuery).catch(_ => false)
  20. listRes.loading = false
  21. if (res) {
  22. listRes.list = res.data.list.map(item => {
  23. item.close_time = item.close_time ? formatTime(item.close_time * 1000) : '-'
  24. return item
  25. })
  26. listRes.total = res.data.total
  27. }
  28. }
  29. const handlerQuery = () => {
  30. if (listQuery.page === 1) {
  31. getList()
  32. } else {
  33. listQuery.page = 1
  34. }
  35. }
  36. const del = async (row) => {
  37. const cf = await ElMessageBox.confirm(T('Confirm?', { param: T('Delete') }), {
  38. confirmButtonText: T('Confirm'),
  39. cancelButtonText: T('Cancel'),
  40. type: 'warning',
  41. }).catch(_ => false)
  42. if (!cf) {
  43. return false
  44. }
  45. const res = await remove({ id: row.id }).catch(_ => false)
  46. if (res) {
  47. ElMessage.success(T('OperationSuccess'))
  48. getList()
  49. }
  50. }
  51. const batchdel = async (rows) => {
  52. const ids = rows.map(r => r.id)
  53. if (!ids.length) {
  54. ElMessage.warning(T('PleaseSelectData'))
  55. return false
  56. }
  57. const cf = await ElMessageBox.confirm(T('Confirm?', { param: T('BatchDelete') }), {
  58. confirmButtonText: T('Confirm'),
  59. cancelButtonText: T('Cancel'),
  60. type: 'warning',
  61. }).catch(_ => false)
  62. if (!cf) {
  63. return false
  64. }
  65. const res = await batchDelete({ ids }).catch(_ => false)
  66. if (res) {
  67. ElMessage.success(T('OperationSuccess'))
  68. getList()
  69. }
  70. }
  71. const toExport = async () => {
  72. const q = { ...listQuery }
  73. q.page_size = 1000000
  74. q.page = 1
  75. const res = await list(q).catch(_ => false)
  76. if (res) {
  77. const csv = jsonToCsv(res.data.list)
  78. downBlob(csv, 'connectLog.csv')
  79. }
  80. }
  81. return {
  82. listRes,
  83. listQuery,
  84. getList,
  85. handlerQuery,
  86. del,
  87. batchdel,
  88. toExport,
  89. }
  90. }
  91. export function useFileRepositories () {
  92. const listRes = reactive({
  93. list: [], total: 0, loading: false,
  94. })
  95. const listQuery = reactive({
  96. page: 1,
  97. page_size: 10,
  98. peer_id: null,
  99. from_peer: null,
  100. })
  101. const getList = async () => {
  102. listRes.loading = true
  103. const res = await fileList(listQuery).catch(_ => false)
  104. listRes.loading = false
  105. if (res) {
  106. listRes.list = res.data.list.map(item => {
  107. item.info = item.info ? JSON.parse(item.info) : '-'
  108. return item
  109. })
  110. listRes.total = res.data.total
  111. }
  112. }
  113. const handlerQuery = () => {
  114. if (listQuery.page === 1) {
  115. getList()
  116. } else {
  117. listQuery.page = 1
  118. }
  119. }
  120. const del = async (row) => {
  121. const cf = await ElMessageBox.confirm(T('Confirm?', { param: T('Delete') }), {
  122. confirmButtonText: T('Confirm'),
  123. cancelButtonText: T('Cancel'),
  124. type: 'warning',
  125. }).catch(_ => false)
  126. if (!cf) {
  127. return false
  128. }
  129. const res = await fileRemove({ id: row.id }).catch(_ => false)
  130. if (res) {
  131. ElMessage.success(T('OperationSuccess'))
  132. getList()
  133. }
  134. }
  135. const batchdel = async (rows) => {
  136. const ids = rows.map(r => r.id)
  137. if (!ids.length) {
  138. ElMessage.warning(T('PleaseSelectData'))
  139. return false
  140. }
  141. const cf = await ElMessageBox.confirm(T('Confirm?', { param: T('BatchDelete') }), {
  142. confirmButtonText: T('Confirm'),
  143. cancelButtonText: T('Cancel'),
  144. type: 'warning',
  145. }).catch(_ => false)
  146. if (!cf) {
  147. return false
  148. }
  149. const res = await fileBatchDelete({ ids }).catch(_ => false)
  150. if (res) {
  151. ElMessage.success(T('OperationSuccess'))
  152. getList()
  153. }
  154. }
  155. const toExport = async () => {
  156. const q = { ...listQuery }
  157. q.page_size = 1000000
  158. q.page = 1
  159. const res = await fileList(q).catch(_ => false)
  160. if (res) {
  161. const csv = jsonToCsv(res.data.list)
  162. downBlob(csv, 'fileTransformLog.csv')
  163. }
  164. }
  165. return {
  166. listRes,
  167. listQuery,
  168. getList,
  169. handlerQuery,
  170. del,
  171. batchdel,
  172. toExport,
  173. }
  174. }