应用层PC端前端服务

onlyoffice.vue 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // this.config.callbackUrl = this.config.callbackUrl + `/${this.userId}`;
  43. var docEditor = new DocsAPI.DocEditor("placeholder", this.config);
  44. });
  45. },
  46. //初始化weosocket
  47. initWebSocket() {
  48. if (typeof WebSocket === "undefined") {
  49. alert("您的浏览器不支持WebSocket");
  50. return false;
  51. }
  52. const wsuri = `ws://121.40.189.20:9090/api/websocket/${this.userId}`; // websocket地址
  53. this.websock = new WebSocket(wsuri);
  54. this.websock.onopen = this.websocketonopen;
  55. this.websock.onmessage = this.websocketonmessage;
  56. this.websock.onerror = this.websocketonerror;
  57. this.websock.onclose = this.websocketclose;
  58. },
  59. //连接成功
  60. websocketonopen() {
  61. console.log("WebSocket连接成功");
  62. },
  63. //接收后端返回的数据
  64. websocketonmessage(e) {
  65. let dataJson = e.data;
  66. if (dataJson != "conn_success") {
  67. dataJson = JSON.parse(dataJson);
  68. }
  69. if (dataJson.caseId) {
  70. saveOnlyOfficeFile({
  71. caseAppliId: Number(dataJson.caseId),
  72. annexName: dataJson.fileName,
  73. annexPath: dataJson.filePath,
  74. onlyOfficeFileId: dataJson.fileId,
  75. }).then((res) => {
  76. this.websock.send();
  77. });
  78. }
  79. // 在这里使用后端返回的数据,对数据进行处理渲染
  80. },
  81. //连接建立失败重连
  82. websocketonerror(e) {
  83. console.log(`连接失败的信息:`, e);
  84. this.initWebSocket(); // 连接失败后尝试重新连接
  85. },
  86. //关闭连接
  87. websocketclose(e) {
  88. console.log("断开连接", e);
  89. },
  90. /**是否从视频会议跳转,是处理token */
  91. async getToken() {
  92. setToken(this.$route.query.token);
  93. },
  94. },
  95. async mounted() {
  96. if (this.$route.query.token) {
  97. await this.getToken();
  98. }
  99. let id = this.$route.query.id;
  100. this.flag = this.$route.query.flag;
  101. this.getConfig(id);
  102. this.initWebSocket();
  103. },
  104. watch: {},
  105. };
  106. </script>
  107. <style lang="scss" scoped></style>