socket-com.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //# sourceURL=socket-com.js
  2. var prescriptionPrintWebsocket = null;
  3. var lockReconnect = false; //避免ws重复连接
  4. function openSocket(type) {
  5. //判断当前浏览器是否支持WebSocket
  6. try {
  7. if ('WebSocket' in window) {
  8. prescriptionPrintWebsocket = new WebSocket("ws://" + window.location.host + "/thmz/prescriptionPrintSocket/" + type);
  9. } else {
  10. alert('当前浏览器不支持WebSocket,请更换浏览器');
  11. }
  12. }catch (e) {
  13. reconnect(type);
  14. console.log(e);
  15. }
  16. //连接发生错误的回调方法
  17. prescriptionPrintWebsocket.onerror = function () {
  18. console.log(type+"连接prescriptionPrintSocket发生错误")
  19. };
  20. //连接成功建立的回调方法
  21. prescriptionPrintWebsocket.onopen = function (event) {
  22. console.log(type+"连接prescriptionPrintSocket连接成功");
  23. };
  24. //接收到消息的回调方法
  25. prescriptionPrintWebsocket.onmessage = function (event) {
  26. if (type == 'PY') {
  27. dispensingMessage(JSON.parse(event.data));
  28. }else if(type == 'FY'){
  29. sendMedicineMessage(JSON.parse(event.data));
  30. }else if(type == 'JH'){
  31. cellNumberMessage(JSON.parse(event.data));
  32. }
  33. };
  34. //连接关闭的回调方法
  35. prescriptionPrintWebsocket.onclose = function (event) {
  36. console.log(type+"连接prescriptionPrintSocket连接关闭 code:"+event.code+",原因:"+event.reason+",是否正常关闭:"+event.wasClean);
  37. if(event.code == '1001' || event.code == '1006'){//1001:终端离开, 可能因为服务端错误 1006:没有发送关闭帧
  38. reconnect(type);
  39. }
  40. };
  41. //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
  42. window.onbeforeunload = function () {
  43. prescriptionPrintWebsocket.close();
  44. }
  45. }
  46. //重新连接
  47. function reconnect(type) {
  48. if(lockReconnect) return;
  49. lockReconnect = true;
  50. //没连接上会一直重连,设置延迟避免请求过多
  51. setTimeout(function () {
  52. openSocket(type);
  53. lockReconnect = false;
  54. }, 500);
  55. }
  56. /**
  57. * 接收到配药完成消息回调方法
  58. * @param data
  59. */
  60. function dispensingMessage(data) {
  61. var dispensingStatus = $("#dispensingStatusSearch").val();
  62. if (data.type == 'JF') {//来自缴费成功后的消息
  63. if (dispensingStatus == 0) {
  64. initTable();
  65. }
  66. if ($("#aotuPrint").is(':checked')) {//自动打印
  67. for (var i = 0; i < data.orderNos.length; i++) {
  68. printPrescription(data.patient_id, data.times,data.orderNos[i]);
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * 接收到发药完成消息回调方法
  75. * @param data
  76. */
  77. function sendMedicineMessage(data){
  78. var confirmFlag = $("#confirmFlagSearch").val();
  79. if (data.type == 'PY' && confirmFlag == 0) {//来自配药处理成功后的消息,未发药列表刷新
  80. initTbTable();
  81. }
  82. }
  83. /**
  84. * 接收到叫号消息回调方法
  85. * @param data
  86. */
  87. function cellNumberMessage(data){
  88. if(data.type == 'JF'){//来自缴费后的消息
  89. dispenseListClear(data);
  90. //添加正在配药列表
  91. dispenseList[dispenseList.length] = {name: data.name, patient_id: data.patient_id};
  92. }else if (data.type == 'PY') {//来自配药处理成功后的消息
  93. dispenseListClear(data);
  94. currentListClear(data);
  95. //添加等待取药列表
  96. currentList[currentList.length] = {name: data.name, patient_id: data.patient_id};
  97. } else if (data.type == 'JH') {//来自发药叫号的消息
  98. //判断正在取药是否为空,不为空将上一位放入过号队列
  99. if ($('#rightPerson').text() != "") {
  100. pastList[pastList.length] = {name: rightPerson.name, patient_id: rightPerson.patient_id};
  101. }
  102. //设置正在取药,清除过号队列
  103. $('#rightPerson').text(data.name);
  104. rightPerson = data;
  105. dispenseListClear(data);
  106. currentListClear(data);
  107. pastListClear(data);
  108. //生成语音
  109. $.ajax({
  110. type: "GET",
  111. url: 'http://webhis.thyy.cn:8706/voice/textToSpeech?text='+"请"+data.name+"到四号窗口取药",
  112. contentType: "application/json;charset=UTF-8",
  113. dataType: "json",
  114. headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
  115. success: function (res) {
  116. if (res.code == 200) {
  117. //播放后台生成的叫号语音
  118. var mp3 = new Audio(res.data);
  119. mp3.play();
  120. }
  121. }
  122. });
  123. } else if (data.type == 'FY') {//来自发药处理后的消息
  124. //将该用户叫号列表清除
  125. if(rightPerson != '' && rightPerson != null && rightPerson.patient_id == data.patient_id){
  126. rightPerson = null;
  127. $('#rightPerson').text("");
  128. }
  129. dispenseListClear(data);
  130. currentListClear(data);
  131. pastListClear(data);
  132. }
  133. }