EmrWebSocket.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <div class="chat_room"
  3. ref="roomRef"
  4. @click="dialog = true">
  5. <div class="count">
  6. {{ userSize }}
  7. </div>
  8. </div>
  9. <emr-chat-box
  10. v-if="dialog"
  11. ref="dialogRef"
  12. :current-editor-user="props.currentEditorUser"
  13. @closed="dialog = false"
  14. :user-list="userList"
  15. :sid="sid"/>
  16. <el-dialog v-model="errDialog" title="与服务器断开连接"
  17. :show-close="false"
  18. :close-on-press-escape="false"
  19. :close-on-click-modal="false">
  20. <div v-loading="errDialog"
  21. element-loading-text="正在尝试重新连接..."
  22. style="width: 100%;height: 400px"/>
  23. </el-dialog>
  24. </template>
  25. <script setup lang="ts">
  26. import {stringIsBlank} from "@/utils/blank-utils";
  27. import {ref, defineProps, onMounted, nextTick, computed, onBeforeUnmount} from 'vue'
  28. import {xcMessage} from '@/utils/xiaochan-element-plus'
  29. import {getRoomPeople, sendAMessage} from '@/api/zhu-yuan-yi-sheng/emr-socket'
  30. import {ElNotification} from "element-plus";
  31. import EmrChatBox from "@/components/zhu-yuan-yi-sheng/emr/web-socket/EmrChatBox.vue";
  32. import {$ref} from "vue/macros";
  33. import {
  34. emrMitt
  35. } from '@/views/hospitalization/zhu-yuan-yi-sheng/electronic-medical-record/emr-editor/emr-init'
  36. import {userInfoStore} from "@/utils/store-public";
  37. import {
  38. forceRefreshDialog
  39. } from "@/views/hospitalization/zhu-yuan-yi-sheng/electronic-medical-record/emr-editor/force-refresh-func";
  40. const props = defineProps({
  41. patInfo: {
  42. type: Object,
  43. default: null
  44. },
  45. currentEditorUser: {
  46. type: Object,
  47. default: null
  48. }
  49. })
  50. const roomRef = ref<HTMLElement | null>(null)
  51. const dialog = ref<boolean>(false)
  52. const errDialog = ref<any>(true)
  53. const userMap = ref<any>({})
  54. const dialogRef = ref(null)
  55. const SOCKET_URL = import.meta.env.VITE_SOCKET_URL
  56. let webSocket = null
  57. let userData = userInfoStore.value
  58. let sid = $ref(null)
  59. const onmessageFunc = {
  60. "connect": async (data) => {
  61. await queryTheNumberOfPeopleInTheRoom()
  62. xcMessage.success('连接成功')
  63. },
  64. "connectToJoin": async (val) => {
  65. await queryTheNumberOfPeopleInTheRoom()
  66. let userInfo = userMap.value[val]
  67. ElNotification({
  68. type: 'success',
  69. title: '有新的连接加入',
  70. dangerouslyUseHTMLString: true,
  71. message: `<span>姓名:${userInfo.name}</span>
  72. <br>
  73. <span>科室:${userInfo.deptName}</span>`
  74. })
  75. console.log("连接加入 %s, 数据 %o", val, userMap.value)
  76. },
  77. "exitEmrEditor": async (val) => {
  78. let userInfo = userMap.value[val]
  79. ElNotification({
  80. type: 'error',
  81. title: '有连接退出',
  82. dangerouslyUseHTMLString: true,
  83. message: `<span>姓名:${userInfo.name}</span>
  84. <br>
  85. <span>科室:${userInfo.deptName}</span>`
  86. })
  87. console.log("连接退出 %s,数据 %o", val, userMap.value)
  88. await queryTheNumberOfPeopleInTheRoom()
  89. },
  90. "message": async (val) => {
  91. if (dialog.value) {
  92. dialogRef.value.queryJump();
  93. }
  94. dialog.value = true
  95. console.log("接受消息 %o", val)
  96. },
  97. "notice": (val) => {
  98. ElNotification({
  99. type: 'success',
  100. title: '查看或编辑',
  101. message: val
  102. })
  103. },
  104. "closeSoctek": (val) => {
  105. console.log(val)
  106. if (navigator.userAgent.indexOf("Firefox") !== -1 || navigator.userAgent.indexOf("Chrome") !== -1) {
  107. window.location.href = "about:blank";
  108. window.close();
  109. } else {
  110. window.opener = null;
  111. window.open("", "_self");
  112. window.close();
  113. }
  114. },
  115. "forceRefresh": (val) => {
  116. emrMitt.emit('forceRefresh', val)
  117. },
  118. "receivedMessage": (val) => {
  119. forceRefreshDialog.value.message.push(val.message)
  120. if (val.flag && val.flag === 1) {
  121. emrMitt.emit('closeForceRefreshDialog')
  122. }
  123. }
  124. }
  125. const userSize = computed(() => {
  126. return Object.keys(userMap.value).length
  127. })
  128. const userList = computed(() => {
  129. return Object.values(userMap.value)
  130. })
  131. const queryTheNumberOfPeopleInTheRoom = async () => {
  132. userMap.value = await getRoomPeople(sid) as any
  133. }
  134. function initWebSocket(patNo, times) {
  135. if (stringIsBlank(patNo)) {
  136. return null
  137. }
  138. if ('WebSocket' in window) {
  139. sid = 'emr_' + patNo.trim() + '_' + times;
  140. const url = SOCKET_URL + sid + '?userCode=' + userData.code;
  141. webSocket = new WebSocket(url);
  142. } else {
  143. alert('该浏览器不支持websocket!');
  144. webSocket = 'unsupport';
  145. }
  146. webSocket.onopen = async () => {
  147. errDialog.value = false
  148. }
  149. webSocket.onmessage = async function (e) {
  150. let data = JSON.parse(e.data)
  151. for (let key in data) {
  152. onmessageFunc[key](data[key])
  153. }
  154. }
  155. webSocket.onclose = () => {
  156. errDialog.value = true
  157. initWebSocket(props.patInfo.inpatientNo, props.patInfo.admissTimes)
  158. }
  159. }
  160. const medicalRecordSwitching = async (name) => {
  161. await sendAMessage(sid, "notice", name)
  162. }
  163. let documentSocket = null
  164. let documentSid = null
  165. const documentChange = (id) => {
  166. documentSid = 'documentEmr_' + id + '_' + userData.code
  167. if (documentSocket != null) {
  168. documentSocket.close()
  169. documentSocket = null
  170. } else {
  171. if ('WebSocket' in window) {
  172. initDocumentSocket()
  173. } else {
  174. alert('该浏览器不支持websocket!');
  175. documentSocket = null;
  176. }
  177. }
  178. }
  179. const initDocumentSocket = () => {
  180. if (documentSid == null) return
  181. let temp = SOCKET_URL + documentSid
  182. documentSocket = new WebSocket(temp)
  183. documentSocket.onopen = () => {
  184. console.log('连接成功')
  185. }
  186. documentSocket.onmessage = async function (e) {
  187. let data = JSON.parse(e.data)
  188. for (let key in data) {
  189. onmessageFunc[key](data[key])
  190. }
  191. }
  192. documentSocket.onclose = () => {
  193. initDocumentSocket()
  194. }
  195. }
  196. const clearDocument = () => {
  197. if (documentSocket != null) {
  198. documentSocket.close()
  199. documentSocket = null
  200. documentSid = null
  201. }
  202. }
  203. const clearSocket = () => {
  204. if (webSocket != null) {
  205. webSocket.close()
  206. webSocket = null
  207. }
  208. }
  209. onMounted(async () => {
  210. await nextTick()
  211. initWebSocket(props.patInfo.inpatientNo, props.patInfo.admissTimes)
  212. })
  213. onBeforeUnmount(() => {
  214. clearDocument()
  215. clearSocket()
  216. })
  217. defineExpose({
  218. medicalRecordSwitching,
  219. documentChange,
  220. clearDocument,
  221. clearSocket
  222. })
  223. </script>
  224. <style scoped lang="scss">
  225. .chat_room {
  226. bottom: 0;
  227. right: 0;
  228. position: absolute;
  229. width: 40px;
  230. height: 40px;
  231. color: white;
  232. display: flex;
  233. align-items: center;
  234. justify-content: center;
  235. z-index: 999;
  236. background-color: rgb(0 0 0 / 63%);
  237. border-radius: 9px;
  238. cursor: all-scroll;
  239. .count {
  240. width: 25px;
  241. text-align: center;
  242. line-height: 25px;
  243. background-color: rgba(255, 255, 255, 0.48);
  244. border-radius: 12px;
  245. position: relative;
  246. }
  247. }
  248. </style>