magic-backup-file.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <magic-loading :loading="loading">
  3. <div class="magic-backup-file">
  4. <template v-if="backupData.length > 0">
  5. <magic-table :data="backupData" :border="true" @clickRow="clickRow">
  6. <magic-table-column :title="$i('message.date')" width="160" v-slot:default="{ row }" class="selected">{{ formatDate(row.createDate) }}</magic-table-column>
  7. <magic-table-column :title="$i('history.operator')" width="100" v-slot:default="{ row }">{{ row.createBy || 'guest' }}</magic-table-column>
  8. </magic-table>
  9. <div class="magic-backup-file-diff-container">
  10. <ul>
  11. <li>
  12. {{ formatDate(current.createDate) }} by {{ current.createBy || 'guest' }}
  13. <magic-button :value="$i('backup.rollback')" type="active" @click="doRollback"/>
  14. </li>
  15. <li>{{ $i('backup.current') }}({{ formatDate(oldInfo.updateTime || oldInfo.createTime) }} by {{ oldInfo.updateBy || oldInfo.createBy || 'guest' }})</li>
  16. </ul>
  17. <magic-monaco-diff-editor v-if="!scriptLoading" v-model:value="diffValue" :language="language"/>
  18. <magic-loading v-else :loading="scriptLoading" />
  19. </div>
  20. </template>
  21. <magic-empty v-else :text="$i('message.empty', $i('history.name'))"/>
  22. </div>
  23. </magic-loading>
  24. </template>
  25. <script setup>
  26. import { inject, reactive, ref, watch } from 'vue'
  27. import $i from '../../../scripts/i18n.js'
  28. import request from '../../../scripts/request.js'
  29. import { formatDate } from '../../../scripts/utils.js'
  30. import modal from '../../common/dialog/magic-modal.js'
  31. const props = defineProps({
  32. id: String
  33. })
  34. const service = inject('service')
  35. const oldInfo = ref('')
  36. const loading = ref(true)
  37. const scriptLoading = ref(true)
  38. const backupData = reactive([])
  39. const current = ref({})
  40. const diffValue = ref([])
  41. const language = ref('')
  42. const loadDifference = () => {
  43. language.value = service[current.value.type].language || 'magicscript'
  44. const promises = []
  45. scriptLoading.value = true
  46. if(!oldInfo.value){
  47. promises.push(new Promise(r => request.sendGet('/resource/file/' + props.id).success(res => (oldInfo.value = res) && r())))
  48. }
  49. promises.push(new Promise(r => request.sendGet('/backup', { id: props.id, timestamp: current.value.createDate }).success(res => (current.value.script = res) && r())))
  50. Promise.all(promises).then(() => {
  51. scriptLoading.value = false
  52. diffValue.value = [current.value.script, oldInfo.value.script]
  53. }).catch(e => {
  54. console.error(e)
  55. scriptLoading.value = false
  56. })
  57. }
  58. const load = () => {
  59. loading.value = true
  60. request.sendGet('/backup/' + props.id).success(res => {
  61. if(res && res.length > 0){
  62. backupData.push(...res)
  63. current.value = backupData[0] || {}
  64. if(current.value){
  65. loadDifference()
  66. }
  67. }
  68. loading.value = false
  69. })
  70. }
  71. load();
  72. watch(() => props.id, load)
  73. const doRollback = () => {
  74. const msg = `${current.value.name}(${formatDate(current.value.createDate)})`
  75. request.sendPost('/backup/rollback', { id: props.id, timestamp: current.value.createDate}).success(r => {
  76. if(!r){
  77. modal.alert($i('backup.rollbackFailed', msg))
  78. bus.status('backup.rollbackFailed', false, msg)
  79. } else {
  80. modal.alert($i('backup.rollbackSuccess', msg))
  81. bus.status('backup.rollbackSuccess', true, msg)
  82. bus.$emit(Message.LOAD_RESOURCES, current.value.type)
  83. }
  84. })
  85. }
  86. const clickRow = rowIndex => {
  87. current.value = backupData[rowIndex]
  88. loadDifference()
  89. }
  90. </script>
  91. <style scoped>
  92. .magic-table{
  93. width: 270px;
  94. border-right: 1px solid var(--table-border-color);
  95. }
  96. .magic-backup-file{
  97. height: 100%;
  98. position: relative;
  99. overflow: hidden;
  100. display: flex;
  101. }
  102. .magic-backup-file-diff-container{
  103. flex: 1;
  104. overflow: hidden;
  105. height: 100%;
  106. display: flex;
  107. flex-direction: column;
  108. }
  109. .magic-backup-file-diff-container > ul{
  110. display: flex;
  111. }
  112. .magic-backup-file-diff-container > ul li{
  113. flex: 1;
  114. height: 30px;
  115. line-height: 30px;
  116. padding: 0 5px;
  117. }
  118. .magic-backup-file-diff-container > ul li button{
  119. float: right;
  120. margin-right: 10px;
  121. margin-top: 4px;
  122. }
  123. </style>