H5GamesPreview.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!-- eslint-disable vue/no-v-html -->
  2. <template>
  3. <div class="imageText-preview" :style="getAreaStyle()">
  4. <SerialNumberPosition v-if="isEnable(data.property.sn_display_mode)" :property="data.property" />
  5. <div ref="fullscreenElement" :class="[full_type ? 'full-screen-games' : '']">
  6. <el-button
  7. type="primary"
  8. :class="[full_type ? 'exit-btn' : '']"
  9. style="margin-bottom: 10px"
  10. :style="{
  11. background:
  12. data.unified_attrib && data.unified_attrib.topic_color ? data.unified_attrib.topic_color : '#165dff',
  13. borderColor:
  14. data.unified_attrib && data.unified_attrib.topic_color ? data.unified_attrib.topic_color : '#165dff',
  15. }"
  16. @click="toggleFullScreen"
  17. >{{ full_type ? convertText('退出全屏') : convertText('进入全屏') }}</el-button
  18. >
  19. <iframe
  20. :src="games_url"
  21. width="100%"
  22. :height="full_type ? '100%' : isMobile ? '400px' : '580px'"
  23. style="border: none"
  24. ></iframe>
  25. </div>
  26. </div>
  27. </template>
  28. <script>
  29. import PreviewMixin from '../common/PreviewMixin';
  30. import { getH5GamesData } from '@/views/book/courseware/data/h5Games';
  31. import { H5StartupFile } from '@/api/app';
  32. export default {
  33. name: 'H5GamesPreview',
  34. components: {},
  35. mixins: [PreviewMixin],
  36. props: {
  37. isMobile: {
  38. type: Boolean,
  39. default: false,
  40. },
  41. },
  42. data() {
  43. return {
  44. data: getH5GamesData(),
  45. games_url: '',
  46. full_type: false,
  47. scroll_top: 0, // 预览时滚动的高度 切换全屏时使用
  48. };
  49. },
  50. watch: {
  51. 'data.file_list.length': {
  52. handler() {
  53. this.initData();
  54. },
  55. },
  56. },
  57. created() {
  58. this.initData();
  59. },
  60. mounted() {
  61. document.addEventListener('keyup', (e) => {
  62. if (event.key === 'Escape' || event.key === 'Esc' || event.keyCode === 27) {
  63. this.full_type = false;
  64. this.$nextTick(() => {
  65. const div = document.querySelector('.main-container');
  66. if (div) {
  67. div.scrollTop = this.scroll_top;
  68. }
  69. });
  70. }
  71. });
  72. },
  73. methods: {
  74. initData() {
  75. this.data.file_list.forEach((item) => {
  76. const suffix = item.file_url.slice(item.file_url.lastIndexOf('.') + 1, item.file_url.length).toLowerCase();
  77. if (suffix === 'html') {
  78. this.games_url = item.file_url;
  79. } else {
  80. H5StartupFile({ file_id: item.file_id, index_file_name: 'index.html' }).then((res) => {
  81. this.games_url = res.file_url;
  82. });
  83. }
  84. });
  85. },
  86. toggleFullScreen() {
  87. const div = document.querySelector('.main-container');
  88. if (div) {
  89. if (this.full_type) {
  90. this.full_type = false;
  91. this.$nextTick(() => {
  92. div.scrollTop = this.scroll_top;
  93. });
  94. } else {
  95. this.scroll_top = div.scrollTop;
  96. this.full_type = true;
  97. }
  98. } else {
  99. this.full_type = !this.full_type;
  100. }
  101. },
  102. /**
  103. * 获取无文本内容的数据结构,用于保存为个人模板时的样式模板
  104. */
  105. getNoTextContentData() {
  106. let noTextContentData = JSON.parse(JSON.stringify(this.data));
  107. const resetFieldMap = {
  108. file_list: [],
  109. file_id_list: [],
  110. file_info: {}, // 存放文件序号
  111. };
  112. Object.assign(noTextContentData, resetFieldMap);
  113. if (noTextContentData.answer) {
  114. noTextContentData.answer.answer_list = [];
  115. noTextContentData.answer.reference_answer = '';
  116. }
  117. return noTextContentData;
  118. },
  119. },
  120. };
  121. </script>
  122. <style lang="scss" scoped>
  123. @use '@/styles/mixin.scss' as *;
  124. .exit-btn {
  125. position: fixed;
  126. top: 10px;
  127. left: 10px;
  128. }
  129. .full-screen-games {
  130. position: fixed;
  131. top: 0;
  132. left: 0;
  133. z-index: 99999;
  134. width: 100%;
  135. height: 100vh;
  136. }
  137. </style>