magic-backup-file.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. console.log(diffValue)
  54. }).catch(e => {
  55. console.error(e)
  56. scriptLoading.value = false
  57. })
  58. }
  59. const load = () => {
  60. loading.value = true
  61. request.sendGet('/backup/' + props.id).success(res => {
  62. backupData.push(...res)
  63. current.value = backupData[0] || {}
  64. if(current.value){
  65. loadDifference()
  66. }
  67. loading.value = false
  68. })
  69. }
  70. load();
  71. watch(() => props.id, load)
  72. const doRollback = () => {
  73. const msg = `${current.value.name}(${formatDate(current.value.createDate)})`
  74. request.sendPost('/backup/rollback', { id: props.id, timestamp: current.value.createDate}).success(r => {
  75. if(!r){
  76. modal.alert($i('backup.rollbackFailed', msg))
  77. bus.status('backup.rollbackFailed', false, msg)
  78. } else {
  79. modal.alert($i('backup.rollbackSuccess', msg))
  80. bus.status('backup.rollbackSuccess', true, msg)
  81. bus.$emit(Message.LOAD_RESOURCES, current.value.type)
  82. }
  83. })
  84. }
  85. const clickRow = rowIndex => {
  86. current.value = backupData[rowIndex]
  87. loadDifference()
  88. }
  89. </script>
  90. <style scoped>
  91. .magic-table{
  92. width: 270px;
  93. border-right: 1px solid var(--table-border-color);
  94. }
  95. .magic-backup-file{
  96. height: 100%;
  97. position: relative;
  98. overflow: hidden;
  99. display: flex;
  100. }
  101. .magic-backup-file-diff-container{
  102. flex: 1;
  103. overflow: hidden;
  104. height: 100%;
  105. display: flex;
  106. flex-direction: column;
  107. }
  108. .magic-backup-file-diff-container > ul{
  109. display: flex;
  110. }
  111. .magic-backup-file-diff-container > ul li{
  112. flex: 1;
  113. height: 30px;
  114. line-height: 30px;
  115. padding: 0 5px;
  116. }
  117. .magic-backup-file-diff-container > ul li button{
  118. float: right;
  119. margin-right: 10px;
  120. margin-top: 4px;
  121. }
  122. </style>