myMessageBox.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. ;!function (e) {
  2. "use strict";
  3. var _ws, n = function () {
  4. this.v = "1.0.0";
  5. this.busiMessageUrl = "http://192.168.3.115:9201/api-busi/messageBusi/readMessage/";
  6. }, _url, _status, _lock = false, _id, _message = new Array(), _nowMessage;
  7. var _onopen = function (event) {
  8. _status = "open";
  9. console.log("连接成功");
  10. }, _onerror = function (event) {
  11. _status = "error";
  12. console.log("连接出错");
  13. }, _onmessage = function (event) {
  14. var obj = eval("(" + event.data + ")");
  15. if(obj !=undefined && obj !=null){
  16. if(obj[0].code == 1){
  17. // 第一次历史消息查询失败的情况
  18. return;
  19. }if(obj[0].code == 0){
  20. // 第一次历史消息查询成功的情况
  21. if(obj[0].data.size == 1){
  22. _message.push(obj[0].data.messageBusi);
  23. _showMessage();
  24. }else if(obj[0].data.size > 1){
  25. // 提醒未读消息条数
  26. _showHistoryMessage(obj[0].data.size);
  27. }
  28. return;
  29. }
  30. // 实时消息
  31. for (var i = 0; i < obj.length; i++) {
  32. _message.push(obj[i]);
  33. }
  34. if (_message.length > 0) {
  35. _showMessage();
  36. }
  37. }
  38. console.log("收到消息:" + event.data);
  39. }, _onclose = function (event) {
  40. _status = "close";
  41. console.log("关闭连接");
  42. }, _reconnection = function () {
  43. if (!'WebSocket' in window) {
  44. console.error("浏览器不支持推送消息");
  45. return;
  46. }
  47. _ws = new WebSocket(_url,"message_box");
  48. _ws.onopen = _onopen;
  49. _ws.onerror = _onerror;
  50. _ws.onmessage = _onmessage;
  51. _ws.onclose = _onclose;
  52. }, _closeHear = function () {
  53. if (_id) {
  54. clearInterval(_id);
  55. }
  56. }, _heartCheck = function () {
  57. if (_lock) {
  58. return;
  59. }
  60. _lock = true;
  61. _closeHear();
  62. _id = setInterval(function () {
  63. if (_url && _status && _status == "close") {
  64. console.log("尝试重连")
  65. _reconnection();
  66. } else if (_url && _status && _status == "open") {
  67. console.log("心跳检测");
  68. // _ws.send(new ArrayBuffer(4));
  69. _ws.send("ping");
  70. }
  71. _lock = false;
  72. }, 1000 * 60);
  73. }, _readMsg = function () {
  74. try {
  75. var xmlHttp = new XMLHttpRequest();
  76. xmlHttp.onreadystatechange = function () {
  77. //当状态变化时处理的事情
  78. if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
  79. //5.接收响应信息
  80. var data = eval("(" + xmlHttp.responseText + ")");
  81. if (data.code == 0) {
  82. document.getElementById("messageDiv").remove();
  83. _showMessage();
  84. } else {
  85. // 失败后添加至头部
  86. _message.unshift(_nowMessage);
  87. document.getElementById("messageDiv").remove();
  88. _showMessage();
  89. }
  90. }
  91. }
  92. xmlHttp.open("POST", myWebSocket.busiMessageUrl + _nowMessage.id, false);
  93. //4.发送请求
  94. xmlHttp.send();
  95. } catch (e) {
  96. // 失败后添加至头部
  97. _message.unshift(_nowMessage);
  98. document.getElementById("messageDiv").remove();
  99. _showMessage();
  100. }
  101. }, _readHistoryMsg = function () {
  102. document.getElementById("messageDiv").remove();
  103. }, _showHistoryMsg = function () {
  104. document.getElementById("messageDiv").remove();
  105. document.getElementsByClassName("myMessageBox")[0].click();
  106. },_doCopyOrderIdInfo = function (orderId){
  107. //复制订单号id
  108. var inputEle = document.createElement("input");
  109. document.designMode = "on";
  110. inputEle.value = orderId;
  111. document.body.appendChild(inputEle);
  112. inputEle.select();
  113. document.execCommand('copy');
  114. inputEle.blur();
  115. inputEle.style.display = 'none';
  116. document.body.removeChild(inputEle);
  117. document.designMode = "off";
  118. _readMsg();
  119. }, _showMessage = function () {
  120. setTimeout(function () {
  121. if (document.getElementById("messageDiv") != null || _message.length == 0) {
  122. return;
  123. }
  124. _nowMessage = _message.shift();
  125. var property = null;
  126. if(_nowMessage.property !=undefined){
  127. property = JSON.parse(_nowMessage.property);
  128. }
  129. var messageBody = _nowMessage.messageBody;
  130. var orderId = _nowMessage.orderId;
  131. var dateStr = _formatDate(new Date(parseInt(_nowMessage.sendTime)));
  132. var htmls = `
  133. <div id="messageDiv" style="width: 500px;border: 1px solid #cecdcd;background-color: #f2f2f2;color: #fff;position: fixed;right: 15px;bottom: 0px;border-radius: 10px;z-index: 999999;">
  134. <div style="width: 500px;height: 55px;text-align: center;font-size: 24px;line-height: 50px;border-radius: 5px 5px 0 0;background-color:red;margin-left: -1px;">
  135. 消息提醒
  136. </div>
  137. <div style="border: 1px solid #000;border-radius: 11px;margin: 10px;text-align: center;background-color: #fff;color: #000;font-size: 20px;padding:20px;">
  138. ${messageBody}
  139. </div>
  140. <div style="display: flex;justify-content: center;">
  141. <span style="color: black;font-size: 16px;margin: 15px;float: left;">提醒发送人:<span>${_nowMessage.producerOperateName ? _nowMessage.producerOperateName : "系统"}</span></span>
  142. <span style="color: black;font-size: 16px;margin: 15px;float: right;margin-left: 40px;">消息时间:${dateStr}</span>
  143. </div>
  144. <div style="display: flex;justify-content: center;">
  145. <div onclick="myWebSocket.readHistoryMsg()" style="width: 120px;height: 40px;border-radius: 5px;background-color:#009966;text-align: center;line-height: 40px;color: #fff;float: left;margin: 10px;cursor:pointer;">知道了</div>
  146. <div onclick="myWebSocket.doCopyOrderIdInfo(${orderId});" style="width: 120px;height: 40px;border-radius: 5px;background-color:#199ed8;text-align: center;line-height: 40px;color: #fff;float: right;margin: 10px;cursor:pointer;margin-left:50px;">点击复制单号</div>
  147. </div>
  148. </div>`;
  149. document.body.insertAdjacentHTML("beforeend", htmls);
  150. }, 2000)
  151. }, _showHistoryMessage = function (size) {
  152. var dateStr = _formatDate(new Date());
  153. var htmls = `
  154. <div id="messageDiv" style="width: 500px;border: 1px solid #cecdcd;background-color: #f2f2f2;color: #fff;position: fixed;right: 15px;bottom: 0px;border-radius: 10px;z-index: 999999;">
  155. <div style="width: 500px;height: 55px;text-align: center;font-size: 24px;line-height: 50px;border-radius: 5px 5px 0 0;background-color:red;margin-left: -1px;">
  156. 消息提醒
  157. </div>
  158. <div style="border: 1px solid #000;border-radius: 11px;margin: 10px;text-align: center;background-color: #fff;color: #000;font-size: 20px;padding:20px;">
  159. 您有${size}条消息未读
  160. </div>
  161. <div style="display: flex;justify-content: center;">
  162. <span style="color: black;font-size: 16px;margin: 15px;float: left;">提醒发送人:<span>系统</span></span>
  163. <span style="color: black;font-size: 16px;margin: 15px;float: right;margin-left: 40px;">消息时间:${dateStr}</span>
  164. </div>
  165. <div style="display: flex;justify-content: center;">
  166. <div onclick="myWebSocket.readHistoryMsg()" style="width: 120px;height: 40px;border-radius: 5px;background-color:#009966;text-align: center;line-height: 40px;color: #fff;float: left;margin: 10px;cursor:pointer;">知道了</div>
  167. <div onclick="myWebSocket.showHistoryMsg();" style="width: 120px;height: 40px;border-radius: 5px;background-color:#169BD5;text-align: center;line-height: 40px;color: #fff;float: right;margin: 10px;cursor:pointer;margin-left:50px;">查看消息</div>
  168. </div>
  169. </div>`;
  170. document.body.insertAdjacentHTML("beforeend", htmls);
  171. }, _utf8ByteToUnicodeStr = function (utf8Bytes) {
  172. var unicodeStr = "";
  173. for (var pos = 0; pos < utf8Bytes.length;) {
  174. var flag = utf8Bytes[pos];
  175. var unicode = 0;
  176. if ((flag >>> 7) === 0) {
  177. unicodeStr += String.fromCharCode(utf8Bytes[pos]);
  178. pos += 1;
  179. } else if ((flag & 0xFC) === 0xFC) {
  180. unicode = (utf8Bytes[pos] & 0x3) << 30;
  181. unicode |= (utf8Bytes[pos + 1] & 0x3F) << 24;
  182. unicode |= (utf8Bytes[pos + 2] & 0x3F) << 18;
  183. unicode |= (utf8Bytes[pos + 3] & 0x3F) << 12;
  184. unicode |= (utf8Bytes[pos + 4] & 0x3F) << 6;
  185. unicode |= (utf8Bytes[pos + 5] & 0x3F);
  186. unicodeStr += String.fromCharCode(unicode);
  187. pos += 6;
  188. } else if ((flag & 0xF8) === 0xF8) {
  189. unicode = (utf8Bytes[pos] & 0x7) << 24;
  190. unicode |= (utf8Bytes[pos + 1] & 0x3F) << 18;
  191. unicode |= (utf8Bytes[pos + 2] & 0x3F) << 12;
  192. unicode |= (utf8Bytes[pos + 3] & 0x3F) << 6;
  193. unicode |= (utf8Bytes[pos + 4] & 0x3F);
  194. unicodeStr += String.fromCharCode(unicode);
  195. pos += 5;
  196. } else if ((flag & 0xF0) === 0xF0) {
  197. unicode = (utf8Bytes[pos] & 0xF) << 18;
  198. unicode |= (utf8Bytes[pos + 1] & 0x3F) << 12;
  199. unicode |= (utf8Bytes[pos + 2] & 0x3F) << 6;
  200. unicode |= (utf8Bytes[pos + 3] & 0x3F);
  201. unicodeStr += String.fromCharCode(unicode);
  202. pos += 4;
  203. } else if ((flag & 0xE0) === 0xE0) {
  204. unicode = (utf8Bytes[pos] & 0x1F) << 12;
  205. ;
  206. unicode |= (utf8Bytes[pos + 1] & 0x3F) << 6;
  207. unicode |= (utf8Bytes[pos + 2] & 0x3F);
  208. unicodeStr += String.fromCharCode(unicode);
  209. pos += 3;
  210. } else if ((flag & 0xC0) === 0xC0) { //110
  211. unicode = (utf8Bytes[pos] & 0x3F) << 6;
  212. unicode |= (utf8Bytes[pos + 1] & 0x3F);
  213. unicodeStr += String.fromCharCode(unicode);
  214. pos += 2;
  215. } else {
  216. unicodeStr += String.fromCharCode(utf8Bytes[pos]);
  217. pos += 1;
  218. }
  219. }
  220. return unicodeStr;
  221. }, _formatDate = function (now) {
  222. var year = now.getFullYear(); //取得4位数的年份
  223. var month = now.getMonth() + 1; //取得日期中的月份,其中0表示1月,11表示12月
  224. var date = now.getDate(); //返回日期月份中的天数(1到31)
  225. var hour = now.getHours(); //返回日期中的小时数(0到23)
  226. var minute = now.getMinutes(); //返回日期中的分钟数(0到59)
  227. var second = now.getSeconds(); //返回日期中的秒数(0到59)
  228. if (month < 10) {
  229. month = "0" + month;
  230. }
  231. if (date < 10) {
  232. date = "0" + date;
  233. }
  234. if (hour < 10) {
  235. hour = "0" + hour;
  236. }
  237. if (minute < 10) {
  238. minute = "0" + minute;
  239. }
  240. if (second < 10) {
  241. second = "0" + second;
  242. }
  243. return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
  244. };
  245. n.prototype.init = function (url) {
  246. if (!'WebSocket' in window) {
  247. console.error("浏览器不支持推送消息");
  248. return;
  249. }
  250. _url = url;
  251. _ws = new WebSocket(_url,"message_box");
  252. _ws.onopen = _onopen;
  253. _ws.onerror = _onerror;
  254. _ws.onmessage = _onmessage;
  255. _ws.onclose = _onclose;
  256. window.onbeforeunload = function (event) {
  257. _ws.close();
  258. };
  259. _heartCheck();
  260. }, n.prototype.close = function () {
  261. _closeHear();
  262. if(_ws){
  263. _ws.close();
  264. }
  265. }, n.prototype.reconnection = _reconnection, n.prototype.readMsg = _readMsg, n.prototype.doCopyOrderIdInfo = _doCopyOrderIdInfo, n.prototype.readHistoryMsg = _readHistoryMsg,n.prototype.showHistoryMsg = _showHistoryMsg;
  266. e.myWebSocket = new n;
  267. }(window);