socket-com.js 5.0 KB

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