eventSource.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!--
  2. * @Author: HTangtang 1539880046@qq.com
  3. * @Date: 2025-01-21 14:41:20
  4. * @LastEditors: HTangtang 1539880046@qq.com
  5. * @LastEditTime: 2025-03-06 17:49:40
  6. * @FilePath: \dam_app_project\pages\smartAi\components\eventSource.vue
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. -->
  9. <template>
  10. <!-- #ifdef H5 -->
  11. <view id="event-source" :props="mergeProps" :change:props="eventSource.renderPropsChange" v-show="false">
  12. </view>
  13. <!-- #endif -->
  14. <!-- #ifdef MP-WEIXIN -->
  15. <view id="event-source" v-show="false">
  16. </view>
  17. <!-- #endif -->
  18. </template>
  19. <script>
  20. export default {
  21. props: {
  22. url: {
  23. type: String,
  24. default: "",
  25. },
  26. options: {
  27. type: Object,
  28. default: () => ({}),
  29. },
  30. },
  31. data() {
  32. return {
  33. isSend: false
  34. }
  35. },
  36. computed: {
  37. // 合并传入renderjs的数据
  38. mergeProps({ url, options, isSend }) {
  39. return {
  40. url,
  41. options,
  42. isSend
  43. };
  44. },
  45. },
  46. methods: {
  47. // 发送
  48. send() {
  49. // #ifdef H5
  50. this.isSend = true;
  51. this.$nextTick(() => {
  52. this.isSend = false;
  53. });
  54. // #endif
  55. // #ifdef MP-WEIXIN
  56. const { url, options = {} } = this;
  57. const { headers, method, body } = options;
  58. this.$emit("callback", { type: "onopen", msg: "Connection opened." });
  59. uni.request({
  60. url: url,
  61. method: method || 'POST',
  62. header: headers,
  63. data: JSON.parse(body),
  64. success: (res) => {
  65. if (res.statusCode === 200) {
  66. // 看起来返回的res.data是一个字符串,需要先处理掉开头的"data:"
  67. const rawData = res.data.replace(/^data:/, '');
  68. // 然后解析JSON
  69. const parsedData = JSON.parse(rawData);
  70. this.$emit("callback", {
  71. type: "onmessage",
  72. msg: "Message received.",
  73. data: JSON.stringify(parsedData) // 发送解析后的数据
  74. });
  75. } else {
  76. this.$emit("callback", { type: "onerror", msg: "Request failed.", data: JSON.stringify(res) });
  77. }
  78. },
  79. fail: (error) => {
  80. this.$emit("callback", { type: "onerror", msg: "Request error.", data: JSON.stringify(error) });
  81. },
  82. complete: () => {
  83. this.$emit("callback", { type: "onclose", msg: "Connection closed." });
  84. }
  85. });
  86. // #endif
  87. },
  88. // 处理renderjs发回的数据
  89. emits(e) {
  90. this.$emit("callback", { ...e });
  91. },
  92. },
  93. };
  94. </script>
  95. <script module="eventSource" lang="renderjs">
  96. // #ifdef H5
  97. import { fetchEventSource } from "@microsoft/fetch-event-source";
  98. export default {
  99. methods: {
  100. // 传入数据变更
  101. renderPropsChange(nVal) {
  102. const { url, isSend } = nVal || {};
  103. if (!isSend) return;
  104. if (!url) return this.handleEmitData({ type: "tip", msg: "URL cannot be empty." });
  105. this.$nextTick(() => {
  106. this.handleSSE(nVal);
  107. });
  108. },
  109. // 发送数据到service层
  110. handleEmitData(data = {}) {
  111. this.$ownerInstance.callMethod('emits', data);
  112. },
  113. // 处理SSE (H5)
  114. handleSSE(opts = {}) {
  115. const that = this;
  116. if (!('EventSource' in window)) return this.handleEmitData({ type: "tip", msg: "The current device does not support EventSource." });
  117. const { url, options = {} } = opts || {};
  118. fetchEventSource(url, {
  119. ...options,
  120. async onopen() {
  121. that.handleEmitData({ type: "onopen", msg: "EventSource onopen." });
  122. },
  123. onmessage(res) {
  124. that.handleEmitData({ type: "onmessage", msg: "EventSource onmessage.", data: res.data });
  125. },
  126. onclose() {
  127. that.handleEmitData({ type: "onclose", msg: "EventSource onclose." });
  128. },
  129. onerror(error) {
  130. that.handleEmitData({ type: "onerror", msg: "EventSource onerror.", data: JSON.stringify(error) });
  131. }
  132. });
  133. }
  134. }
  135. }
  136. // #endif
  137. </script>