You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
3.8 KiB

12 months ago
  1. <template>
  2. <view>
  3. <div ref="terminalContainer" style="height: auto;width: 100%;"></div>
  4. <view class="imagesBox">
  5. <image :src="imgbas64" mode=""></image>
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. import {
  11. Terminal
  12. } from 'xterm';
  13. import {
  14. FitAddon
  15. } from 'xterm-addon-fit';
  16. import 'xterm/css/xterm.css'
  17. import {
  18. fileTail
  19. } from '@/config/api.js'
  20. import {
  21. LOG_TAIL_STATUS
  22. } from './logConfig.js'
  23. export default {
  24. props: ['relId'],
  25. data() {
  26. return {
  27. imgbas64: '',
  28. terminal: null,
  29. fitAddon: null,
  30. token: '',
  31. socketTask: null,
  32. termConfig: {
  33. rightClickSelectsWord: true,
  34. disableStdin: true,
  35. cursorStyle: 'bar',
  36. cursorBlink: false,
  37. fastScrollModifier: 'shift',
  38. fontSize: 13,
  39. rendererType: 'canvas',
  40. fontFamily: 'courier-new, courier, monospace',
  41. lineHeight: 1.08,
  42. convertEol: true,
  43. theme: {
  44. foreground: '#FFFFFF',
  45. background: '#212529'
  46. }
  47. },
  48. };
  49. },
  50. mounted(options) {
  51. this.fileTailFn()
  52. },
  53. methods: {
  54. async fileTailFn() {
  55. let obj = {
  56. relId: this.relId,
  57. type: 10
  58. }
  59. const {
  60. data: res
  61. } = await fileTail(obj)
  62. this.token = res.token
  63. this.$nextTick(() => {
  64. this.initializeTerminal();
  65. })
  66. },
  67. initializeTerminal() {
  68. // 创建终端实例
  69. this.terminal = new Terminal({
  70. ...this.termConfig
  71. });
  72. // 创建适应插件
  73. this.fitAddon = new FitAddon();
  74. this.terminal.loadAddon(this.fitAddon);
  75. // 将终端挂载到页面
  76. this.terminal.open(this.$refs.terminalContainer);
  77. // 设置终端大小适应窗口
  78. this.fitAddon.fit();
  79. // 你可以添加其他配置,例如主题等
  80. // this.terminal.setOption('theme', { background: '#282c34' });
  81. // ...
  82. this.terminal.write('\x1b[?25l')
  83. // 示例:在终端中输出一行文本
  84. // this.terminal.write('Hello, UniApp xterm!');
  85. this.initSocket()
  86. },
  87. initSocket(data) {
  88. // 打开websocket
  89. // let wsUrl =`${window.location.protocol === 'https:' ? 'wss' : 'ws'}://${window.location.host}/orion/keep-alive/tail/${this.token}`
  90. let wsUrl = `ws://192.168.1.15:35/orion/keep-alive/tail/${this.token}`
  91. console.log(wsUrl)
  92. let _this = this
  93. this.socketTask = uni.connectSocket({
  94. url: wsUrl,
  95. header: {
  96. 'content-type': 'application/json'
  97. },
  98. responseType: 'String',
  99. success: (res) => {
  100. console.log('1,链接已建立')
  101. console.log(res)
  102. },
  103. complete: () => {
  104. console.log('WebSocket connected');
  105. },
  106. });
  107. uni.onSocketOpen(function(res) {
  108. console.log('WebSocket连接已打开!');
  109. });
  110. uni.onSocketMessage(async (event) => {
  111. let data = event.data
  112. let bob = new Blob([data])
  113. console.log(bob)
  114. console.log(data)
  115. // console.log(await bob.text())
  116. _this.terminal.write(await bob.text())
  117. _this.terminal.scrollToBottom()
  118. });
  119. uni.onSocketError(function(res) {
  120. console.log(res)
  121. console.log('WebSocket连接打开失败,请检查!');
  122. });
  123. return
  124. this.client = new WebSocket(wsUrl)
  125. this.client.onopen = () => {
  126. this.status = LOG_TAIL_STATUS.RUNNABLE.value
  127. this.$emit('open')
  128. }
  129. this.client.onerror = () => {
  130. this.status = LOG_TAIL_STATUS.ERROR.value
  131. }
  132. this.client.onclose = (e) => {
  133. this.status = LOG_TAIL_STATUS.CLOSE.value
  134. if (e.code > 4000 && e.code < 5000) {
  135. // 自定义错误信息
  136. this.term.write(`\x1b[93m${e.reason}\x1b[0m`)
  137. }
  138. this.$emit('close')
  139. }
  140. this.client.onmessage = async event => {
  141. console.log('来这了吗?')
  142. this.term.write(await event.data.text())
  143. if (!this.fixedLog) {
  144. this.term.scrollToBottom()
  145. }
  146. }
  147. },
  148. },
  149. };
  150. </script>
  151. <style>
  152. /* @import 'xterm/dist/xterm.css'; */
  153. .imagesBox {
  154. width: 450rpx;
  155. height: 450rpx;
  156. }
  157. </style>