to-medicine-test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. //@ sourceURL=to-medicine-test.js
  2. //待取药队列
  3. var currentList = new Array();
  4. //过号名单队列
  5. var pastList = new Array();
  6. //正在配药队列
  7. var dispenseList = new Array();
  8. //正在取药
  9. var rightPerson = null;
  10. //临时待取药队列
  11. var tempCurrentList = new Array();
  12. //临时过号名单
  13. var tempPastList = new Array();
  14. //临时正在配药队列
  15. var tempDispenseList = new Array();
  16. //待取药队列是否已经加载完成
  17. var currentListHadDone = true;
  18. //过号名单队列是否已经加载完成
  19. var pastListHadDone = true;
  20. //正在配药名单队列是否已经加载完成
  21. var dispenseListHadDone = true;
  22. //药房编号
  23. var groupNo = null;
  24. $(function () {
  25. $(".selectpicker").selectpicker({
  26. dropuAuto: false
  27. });
  28. var yesNo = '<option value="71">门诊西药房</option><option value="81">门诊中药房</option><option value="82">颗粒剂药房</option>';
  29. $('#fyClass').html(yesNo);
  30. $('#fyClass').selectpicker('refresh');
  31. $("#editModal").modal("show");
  32. //进入页面就加载一次
  33. CurentTime();
  34. intClock();
  35. //每秒获取当前时间
  36. setInterval(CurentTime, 1000);
  37. //三十秒从后台拉一次数据
  38. //setInterval(getTableDate, 30000);
  39. //getTableDate();
  40. //设置页面宽度
  41. setHeightForTable();
  42. $(window).resize(function () {
  43. //设置页面宽度
  44. setHeightForTable();
  45. });
  46. $(document).keyup(function (event) {
  47. if (event.keyCode == 13) {
  48. initTable();
  49. }
  50. });
  51. });
  52. /**
  53. * 设置页面宽度
  54. */
  55. function setHeightForTable() {
  56. var height = $(window).height() - 320;
  57. var realHeigt = height / 7;
  58. if (realHeigt < 88) {
  59. realHeigt = 88;
  60. }
  61. $("#tableList tr").css("height", realHeigt + "px");
  62. }
  63. /**
  64. * 取当前时间 格式 yyyy年mm月dd日 hh:mm:ss weekday
  65. * @constructor
  66. */
  67. function CurentTime() {
  68. var now = new Date();
  69. var year = now.getFullYear(); //年
  70. var month = now.getMonth() + 1; //月
  71. var day = now.getDate(); //日
  72. var hh = now.getHours(); //时
  73. var mm = now.getMinutes(); //分
  74. // var ss=now.getSeconds(); //秒
  75. var weekday = new Array(7) //周
  76. weekday[0] = "星期日"
  77. weekday[1] = "星期一"
  78. weekday[2] = "星期二"
  79. weekday[3] = "星期三"
  80. weekday[4] = "星期四"
  81. weekday[5] = "星期五"
  82. weekday[6] = "星期六"
  83. var date = year + "年";
  84. if (month < 10) date += "0";
  85. date += month + "月";
  86. if (day < 10) date += "0";
  87. date += day + "日 ";
  88. date += " " + weekday[now.getDay()];
  89. $("#dateNow").html(date);
  90. var time = "";
  91. if (hh < 10) time += "0";
  92. time += hh + ":";
  93. if (mm < 10) time += '0';
  94. time += mm;
  95. $("#timeNow").html(time)
  96. }
  97. /**
  98. * 初始化时钟
  99. */
  100. function intClock() {
  101. var audioElement = new Audio("");
  102. //clock plugin constructor
  103. $('#myclock').thooClock({
  104. //size:$(document).height()/1.4,
  105. size: 80,
  106. onAlarm: function () {
  107. //all that happens onAlarm
  108. //$('#alarm1').show();
  109. // alarmBackground(0);
  110. //audio element just for alarm sound
  111. document.body.appendChild(audioElement);
  112. var canPlayType = audioElement.canPlayType("audio/ogg");
  113. if (canPlayType.match(/maybe|probably/i)) {
  114. audioElement.src = 'alarm.ogg';
  115. } else {
  116. audioElement.src = 'alarm.mp3';
  117. }
  118. // erst abspielen wenn genug vom mp3 geladen wurde
  119. audioElement.addEventListener('canplay', function () {
  120. audioElement.loop = true;
  121. audioElement.play();
  122. }, false);
  123. },
  124. showNumerals: true,
  125. brandText: 'THOOYORK',
  126. brandText2: 'Germany',
  127. onEverySecond: function () {
  128. //callback that should be fired every second
  129. },
  130. //alarmTime:'15:10',
  131. offAlarm: function () {
  132. //$('#alarm1').hide();
  133. audioElement.pause();
  134. clearTimeout(intVal);
  135. $('body').css('background-color', '#FCFCFC');
  136. }
  137. });
  138. }
  139. /**
  140. * 实例化列表
  141. */
  142. function initTable() {
  143. var winNo = $("#winNo").val();
  144. groupNo = $("#fyClass").val();
  145. if (groupNo == null || groupNo == "") {
  146. alert("未选择药房!");
  147. return;
  148. }
  149. if (winNo == null || winNo == "") {
  150. alert("窗口号未录入!");
  151. return;
  152. }
  153. var yfName = "门诊西药房";
  154. if (groupNo == 81) {
  155. yfName = "门诊中药房";
  156. } else if (groupNo == 82) {
  157. yfName = "颗粒剂药房";
  158. }
  159. $("#yfName").html(yfName);
  160. $("#winNum").text(winNo);
  161. $("#editModal").modal("hide");
  162. getTableDate();
  163. }
  164. /**
  165. * 获取后台最新数据
  166. */
  167. function getTableDate() {
  168. $.ajax({
  169. type: "GET",
  170. contentType: "application/json;charset=UTF-8",
  171. url: "/thmz/getToMedicineUser?groupNo=" + groupNo,
  172. headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
  173. dataType: 'json',
  174. success: function (result) {
  175. if (result == '401' || result == 401) {
  176. window.location.href = '/thmz/login/view'
  177. return;
  178. }
  179. if (result.code == 0) {
  180. var list = result.currentList;
  181. for (let i = 0; i < list.length; i++) {
  182. var current = list[i];
  183. if (current.confirm_flag == 5) {//已配药待发药
  184. currentList[currentList.length] = current;
  185. tempCurrentList[tempCurrentList.length] = current;
  186. //重置标志
  187. currentListHadDone = false;
  188. } else if (current.confirm_flag == 0 || current.confirm_flag == 2) {
  189. dispenseList[dispenseList.length] = current;
  190. tempDispenseList[tempDispenseList.length] = current; //重置标志
  191. dispenseListHadDone = false;
  192. }
  193. }
  194. //10秒刷新一次队列里面的数据,应对数据很多,一页展示不完的情况
  195. setInterval(refeshTable, 10000);
  196. } else {
  197. errorMesage(result);
  198. }
  199. }
  200. });
  201. }
  202. /**
  203. * 刷新列表
  204. */
  205. function refeshTable() {
  206. console.log(new Date().getSeconds());
  207. for (var i = 1; i < 7; i++) {
  208. if (tempPastList == null || tempPastList.length == 0) {
  209. $("#past" + i).text("");
  210. if (i == 6) {
  211. currentListHadDone = true;
  212. //如果过号列表也遍历完成,将待取药列表置空,否责重新遍历待取药列表
  213. //深度拷贝数组
  214. tempPastList = $.extend(true, [], pastList);
  215. }
  216. } else {
  217. //删除并返回数组的第一个元素
  218. var name = tempPastList.shift().name;
  219. $("#past" + i).text(name);
  220. }
  221. }
  222. for (var i = 1; i < 13; i++) {
  223. if (tempCurrentList == null || tempCurrentList.length == 0) {
  224. $("#current" + i).text("");
  225. if (i == 12) {
  226. currentListHadDone = true;
  227. //如果过号列表也遍历完成,将待取药列表置空,否责重新遍历待取药列表
  228. //深度拷贝数组
  229. tempCurrentList = $.extend(true, [], currentList);
  230. }
  231. } else {
  232. //删除并返回数组的第一个元素
  233. var name = tempCurrentList.shift().name;
  234. $("#current" + i).text(name);
  235. }
  236. }
  237. for (var i = 1; i < 13; i++) {
  238. if (tempDispenseList == null || tempDispenseList.length == 0) {
  239. $("#dispense" + i).text("");
  240. if (i == 12) {
  241. dispenseListHadDone = true;
  242. //如果过号列表也遍历完成,将待取药列表置空,否责重新遍历待取药列表
  243. //深度拷贝数组
  244. tempDispenseList = $.extend(true, [], dispenseList);
  245. }
  246. } else {
  247. //删除并返回数组的第一个元素
  248. var name = tempDispenseList.shift().name;
  249. $("#dispense" + i).text(name);
  250. }
  251. }
  252. }
  253. /**
  254. * 清空表格
  255. */
  256. function dispenseClear() {
  257. for (var i = 1; i < 13; i++) {
  258. $("#dispense" + i).text("");
  259. }
  260. }
  261. /**
  262. * 清空表格
  263. */
  264. function currentClear() {
  265. for (var i = 1; i < 13; i++) {
  266. $("#current" + i).text("");
  267. }
  268. }
  269. /**
  270. * 清空表格
  271. */
  272. function pastClear() {
  273. for (var i = 1; i < 7; i++) {
  274. $("#past" + i).text("");
  275. }
  276. }
  277. var websocket = null;
  278. //判断当前浏览器是否支持WebSocket, 主要此处要更换为自己的地址
  279. if ('WebSocket' in window) {
  280. websocket = new WebSocket("ws://" + window.location.host + "/thmz/toMedicineSocket/acceptor");
  281. } else {
  282. alert('Not support websocket')
  283. }
  284. //连接发生错误的回调方法
  285. websocket.onerror = function () {
  286. console.log("发药提醒toMedicineSocket发生错误")
  287. };
  288. //连接成功建立的回调方法
  289. websocket.onopen = function (event) {
  290. console.log("发药提醒toMedicineSocket连接成功");
  291. }
  292. //接收到消息的回调方法
  293. websocket.onmessage = function (event) {
  294. var data = JSON.parse(event.data);
  295. if(data.type == 'sf'){
  296. //添加正在配药列表
  297. dispenseList[dispenseList.length] = {name: data.name, patient_id: data.patient_id};
  298. }else if (data.type == 'py') {//来自配药处理成功后的消息
  299. //删除正在配药列表
  300. for (let i = 0; i < dispenseList.length; i++) {
  301. if (dispenseList[i].patient_id == data.patient_id) {
  302. dispenseList.splice(i, 1);
  303. }
  304. }
  305. //添加等待取药列表
  306. currentList[currentList.length] = {name: data.name, patient_id: data.patient_id};
  307. } else if (data.type == 'jh') {
  308. //判断正在取药是否为空,不为空将上一位放入过号队列
  309. if ($('#rightPerson').text() != "") {
  310. pastList[pastList.length] = {name: rightPerson.name, patient_id: rightPerson.patient_id};
  311. }
  312. //设置正在取药,清除过号队列
  313. $('#rightPerson').text(data.name);
  314. rightPerson = data;
  315. for (let i = 0; i < pastList.length; i++) {
  316. if (pastList[i].patient_id == data.patient_id) {
  317. pastList.splice(i, 1);
  318. break;
  319. }
  320. }
  321. //清除等待取药
  322. for (let i = 0; i < currentList.length; i++) {
  323. if (currentList[i].patient_id == data.patient_id) {
  324. currentList.splice(i, 1);
  325. break;
  326. }
  327. }
  328. //清除正在配药
  329. for (let i = 0; i < dispenseList.length; i++) {
  330. if (dispenseList[i].patient_id == data.patient_id) {
  331. dispenseList.splice(i, 1);
  332. }
  333. }
  334. } else if (data.type == 'fy') {
  335. //将该用户叫号列表清除
  336. if(rightPerson != '' && rightPerson != null && rightPerson.patient_id == data.patient_id){
  337. rightPerson = null;
  338. $('#rightPerson').text("");
  339. }
  340. for (let i = 0; i < currentList.length; i++) {
  341. if (currentList[i].patient_id == data.patient_id) {
  342. currentList.splice(i, 1);
  343. break;
  344. }
  345. }
  346. for (let i = 0; i < dispenseList.length; i++) {
  347. if (dispenseList[i].patient_id == data.patient_id) {
  348. dispenseList.splice(i, 1);
  349. }
  350. }
  351. for (let i = 0; i < pastList.length; i++) {
  352. if (pastList[i].patient_id == data.patient_id) {
  353. pastList.splice(i, 1);
  354. break;
  355. }
  356. }
  357. }
  358. }
  359. //连接关闭的回调方法
  360. websocket.onclose = function () {
  361. console.log("发药提醒toMedicineSocket连接关闭");
  362. }
  363. //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
  364. window.onbeforeunload = function () {
  365. websocket.close();
  366. }
  367. //关闭连接
  368. function closeWebSocket() {
  369. websocket.close();
  370. }