应用层PC端前端服务

onlyoffice.vue 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <div style="width: 100%; height: 100%">
  3. <div id="placeholder"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import { getOnlyoffice, saveOnlyOfficeFile } from "@/api/onlyoffice/onlyoffice";
  8. import { setToken } from "@/utils/auth";
  9. import store from "@/store";
  10. import axios from "axios";
  11. export default {
  12. props: [],
  13. data() {
  14. return {
  15. config: {},
  16. flag: 0,
  17. userId: store.getters.userId,
  18. websock: null,
  19. };
  20. },
  21. destroyed() {
  22. //页面销毁时关闭ws连接
  23. if (this.websock) {
  24. this.websock.close(); // 关闭websocket
  25. }
  26. },
  27. methods: {
  28. getConfig(id) {
  29. let edit = "edit";
  30. if (this.flag == 0) {
  31. edit = "view";
  32. } else {
  33. edit = "edit";
  34. }
  35. axios
  36. .get(
  37. `http://121.40.189.20:9090/onlyOfficeConfig/${edit}/${id}/${this.userId}/desktop/true/zhongcai`
  38. )
  39. .then((res) => {
  40. this.config = res.data;
  41. this.config.editorConfig.callbackUrl = this.config.editorConfig.callbackUrl + "/" + id;
  42. console.log(this.config,"OOOOOOOOOOOOOOOOOOOOOOOOOOO");
  43. // this.config.callbackUrl = this.config.callbackUrl + `/${this.userId}`;
  44. var docEditor = new DocsAPI.DocEditor("placeholder", this.config);
  45. });
  46. },
  47. //初始化weosocket
  48. initWebSocket() {
  49. if (typeof WebSocket === "undefined") {
  50. alert("您的浏览器不支持WebSocket");
  51. return false;
  52. }
  53. const wsuri = `ws://121.40.189.20:9090/api/websocket/${this.userId}`; // websocket地址
  54. this.websock = new WebSocket(wsuri);
  55. this.websock.onopen = this.websocketonopen;
  56. this.websock.onmessage = this.websocketonmessage;
  57. this.websock.onerror = this.websocketonerror;
  58. this.websock.onclose = this.websocketclose;
  59. },
  60. //连接成功
  61. websocketonopen() {
  62. console.log("WebSocket连接成功");
  63. },
  64. //接收后端返回的数据
  65. websocketonmessage(e) {
  66. let dataJson = e.data;
  67. if (dataJson != "conn_success") {
  68. dataJson = JSON.parse(dataJson);
  69. }
  70. if (dataJson.caseId) {
  71. saveOnlyOfficeFile({
  72. caseAppliId: Number(dataJson.caseId),
  73. annexName: dataJson.fileName,
  74. annexPath: dataJson.filePath,
  75. onlyOfficeFileId: dataJson.fileId,
  76. }).then((res) => {
  77. this.websock.send();
  78. });
  79. }
  80. // 在这里使用后端返回的数据,对数据进行处理渲染
  81. },
  82. //连接建立失败重连
  83. websocketonerror(e) {
  84. console.log(`连接失败的信息:`, e);
  85. this.initWebSocket(); // 连接失败后尝试重新连接
  86. },
  87. //关闭连接
  88. websocketclose(e) {
  89. console.log("断开连接", e);
  90. },
  91. /**是否从视频会议跳转,是处理token */
  92. async getToken() {
  93. setToken(this.$route.query.token);
  94. },
  95. },
  96. async mounted() {
  97. if (this.$route.query.token) {
  98. await this.getToken();
  99. }
  100. let id = this.$route.query.id;
  101. this.flag = this.$route.query.flag;
  102. this.getConfig(id);
  103. this.initWebSocket();
  104. },
  105. watch: {},
  106. };
  107. </script>
  108. <style lang="scss" scoped></style>