bootstrap.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. require.config({
  2. paths: {
  3. 'qrcode': '../addons/notice/js/qrcode',
  4. 'HackTimer': '../addons/notice/js/HackTimer.min',
  5. },
  6. shim: {
  7. }
  8. });
  9. function ajaxInit() {
  10. if (Config.modulename == 'admin') {
  11. if (!(Config.controllername == 'index' && Config.actionname == 'index' && Config.notice.admin_real == 1)) {
  12. return false;
  13. }
  14. } else if (Config.modulename == 'index'){
  15. if (Config.notice.user_real != 1) {
  16. return false;
  17. }
  18. if (!indexUrlCheck()) {
  19. return false;
  20. }
  21. } else {
  22. return false;
  23. }
  24. console.log('ajax_init');
  25. require(['HackTimer'], function (HackTimer) {
  26. var url = '';
  27. if (Config.modulename == 'admin') {
  28. url = 'notice/admin/statistical';
  29. }
  30. if (Config.modulename == 'index') {
  31. url = '/addons/notice/api/statistical';
  32. }
  33. // 获取新消息并提示
  34. function notice() {
  35. Fast.api.ajax({
  36. url: url,
  37. loading: false
  38. }, function (data, res) {
  39. if (data.new) {
  40. Toastr.info(data.new.content);
  41. }
  42. if (Config.modulename == 'admin') {
  43. Backend.api.sidebar({
  44. 'notice/admin': data.num,
  45. });
  46. }
  47. setTimeout(function () {
  48. notice();
  49. }, 5000);
  50. return false;
  51. }, function () {
  52. return false;
  53. });
  54. };
  55. notice();
  56. });
  57. };
  58. function wsInit() {
  59. if (Config.modulename == 'admin') {
  60. if (!(Config.controllername == 'index' && Config.actionname == 'index' && Config.notice.admin_real == 2)) {
  61. return false;
  62. }
  63. } else if (Config.modulename == 'index'){
  64. if (!indexUrlCheck()) {
  65. return false;
  66. }
  67. if (Config.notice.user_real != 2) {
  68. return false;
  69. }
  70. } else {
  71. return false;
  72. }
  73. console.log('ws_init');
  74. let NhWs = {
  75. ws: null,
  76. timer: null,
  77. bindurl: '',
  78. url: '',
  79. connect: function () {
  80. var ws = new WebSocket(this.url);
  81. this.ws = ws;
  82. ws.onmessage = this.onmessage;
  83. ws.onclose = this.onclose;
  84. ws.onerror = this.onerror;
  85. ws.onopen = this.onopen;
  86. },
  87. onmessage: function (e) {
  88. // json数据转换成js对象
  89. var data = e.data;
  90. try {
  91. JSON.parse(data);
  92. data = JSON.parse(data) ? JSON.parse(data) : data;
  93. } catch {
  94. console.log('ws接收到非对象数据', data);
  95. return true;
  96. }
  97. console.log('ws接收到数据', data, e.data);
  98. var type = data.type || '';
  99. var resdata = data.data ? data.data : {};
  100. switch(type){
  101. case 'init':
  102. $.ajax(NhWs.bindurl, {
  103. data: {
  104. client_id: data.client_id
  105. },
  106. method: 'post'
  107. })
  108. break;
  109. case "new_notice":
  110. if (Config.modulename == 'admin') {
  111. Backend.api.sidebar({
  112. 'notice/admin': resdata.num,
  113. });
  114. }
  115. Toastr.info(resdata.msg);
  116. // 发送ajax到后台告诉已经看过这条消息
  117. Fast.api.ajax({
  118. url: '/addons/notice/api/cache',
  119. data: {
  120. time: resdata.time,
  121. module: Config.modulename
  122. },
  123. method: 'post'
  124. }, function () {
  125. return false;
  126. });
  127. }
  128. },
  129. onclose: function () {
  130. console.log('连接已断开,尝试自动连接');
  131. setTimeout(function () {
  132. NhWs.connect();
  133. }, 5000);
  134. },
  135. onopen: function () {
  136. this.timer = setInterval(function () {
  137. NhWs.send({"type":"ping"});
  138. }, 20000);
  139. },
  140. onerror: function () {
  141. console.log('ws连接失败');
  142. Toastr.error('ws连接失败');
  143. },
  144. // 发送数据
  145. send: function (data) {
  146. if (typeof data == "object") {
  147. data = JSON.stringify(data);
  148. }
  149. this.ws.send(data);
  150. },
  151. };
  152. if (Config.modulename == 'admin') {
  153. NhWs.bindurl = Fast.api.fixurl('/addons/notice/ws/bindAdmin');
  154. // ajax请求获取消息数量等
  155. Fast.api.ajax({
  156. url: 'notice/admin/statistical',
  157. loading: false,
  158. method: 'post',
  159. }, function (data, res) {
  160. if (data.new) {
  161. Toastr.info(data.new.content);
  162. }
  163. Backend.api.sidebar({
  164. 'notice/admin': data.num,
  165. });
  166. return false;
  167. }, function () {
  168. return false;
  169. });
  170. }
  171. if (Config.modulename == 'index') {
  172. NhWs.bindurl = Fast.api.fixurl('/addons/notice/ws/bind');
  173. // ajax请求最新获取消息数量等
  174. Fast.api.ajax({
  175. url: '/addons/notice/api/statistical',
  176. loading: false,
  177. method: 'post',
  178. }, function (data, res) {
  179. if (data.new) {
  180. Toastr.info(data.new.content);
  181. }
  182. return false;
  183. }, function () {
  184. return false;
  185. });
  186. }
  187. NhWs.url = Config.notice.wsurl;
  188. require(['HackTimer'], function (HackTimer) {
  189. NhWs.connect();
  190. });
  191. };
  192. function indexUrlCheck() {
  193. if (Config.modulename == 'index') {
  194. var url = Config.controllername+'/'+Config.actionname;
  195. if (Config.notice.user_real_url.indexOf('*') === -1) {
  196. if (Config.notice.user_real_url.indexOf(url) === -1) {
  197. return false;
  198. }
  199. }
  200. }
  201. return true;
  202. };
  203. require([], function (undefined) {
  204. // ajax轮询
  205. ajaxInit();
  206. wsInit();
  207. // 后台绑定按钮
  208. if (Config.modulename == 'admin' && Config.controllername == 'general.profile' && Config.actionname == 'index') {
  209. $('[type="submit"]').before('<button style="margin-right: 5px;" type="button" class="btn btn-primary btn-dialog" data-url="notice/admin_mptemplate/bind">模版消息(公众号)</button>');
  210. }
  211. });