eventSource.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. console.log('Raw data:', res.data.split('data:').filter(item => item.trim() !== ''));
  70. res.data.split('data:').filter(item => item.trim() !== '').forEach(item => {
  71. const parsedData = JSON.parse(item);
  72. console.log(item, parsedData, 5454545)
  73. this.$emit("callback", { type: "onmessage", msg: "Message received.", data: JSON.stringify(parsedData) }); // 发送解析后的数据
  74. });
  75. // const parsedData = JSON.parse(rawData);
  76. // this.$emit("callback", {
  77. // type: "onmessage",
  78. // msg: "Message received.",
  79. // data: JSON.stringify(parsedData) // 发送解析后的数据
  80. // });
  81. } else {
  82. this.$emit("callback", { type: "onerror", msg: "Request failed.", data: JSON.stringify(res) });
  83. }
  84. },
  85. fail: (error) => {
  86. this.$emit("callback", { type: "onerror", msg: "Request error.", data: JSON.stringify(error) });
  87. },
  88. complete: () => {
  89. this.$emit("callback", { type: "onclose", msg: "Connection closed." });
  90. }
  91. });
  92. // #endif
  93. },
  94. // 处理renderjs发回的数据
  95. emits(e) {
  96. this.$emit("callback", { ...e });
  97. },
  98. },
  99. };
  100. </script>
  101. <script module="eventSource" lang="renderjs">
  102. // #ifdef H5
  103. import { fetchEventSource } from "@microsoft/fetch-event-source";
  104. export default {
  105. methods: {
  106. // 传入数据变更
  107. renderPropsChange(nVal) {
  108. const { url, isSend } = nVal || {};
  109. if (!isSend) return;
  110. if (!url) return this.handleEmitData({ type: "tip", msg: "URL cannot be empty." });
  111. this.$nextTick(() => {
  112. this.handleSSE(nVal);
  113. });
  114. },
  115. // 发送数据到service层
  116. handleEmitData(data = {}) {
  117. this.$ownerInstance.callMethod('emits', data);
  118. },
  119. // 处理SSE (H5)
  120. handleSSE(opts = {}) {
  121. const that = this;
  122. if (!('EventSource' in window)) return this.handleEmitData({ type: "tip", msg: "The current device does not support EventSource." });
  123. const { url, options = {} } = opts || {};
  124. fetchEventSource(url, {
  125. ...options,
  126. async onopen() {
  127. that.handleEmitData({ type: "onopen", msg: "EventSource onopen." });
  128. },
  129. onmessage(res) {
  130. that.handleEmitData({ type: "onmessage", msg: "EventSource onmessage.", data: res.data });
  131. },
  132. onclose() {
  133. that.handleEmitData({ type: "onclose", msg: "EventSource onclose." });
  134. },
  135. onerror(error) {
  136. that.handleEmitData({ type: "onerror", msg: "EventSource onerror.", data: JSON.stringify(error) });
  137. }
  138. });
  139. }
  140. }
  141. }
  142. // #endif
  143. </script>