123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- require.config({
- paths: {
- 'qrcode': '../addons/notice/js/qrcode',
- 'HackTimer': '../addons/notice/js/HackTimer.min',
- },
- shim: {
- }
- });
- function ajaxInit() {
- if (Config.modulename == 'admin') {
- if (!(Config.controllername == 'index' && Config.actionname == 'index' && Config.notice.admin_real == 1)) {
- return false;
- }
- } else if (Config.modulename == 'index'){
- if (Config.notice.user_real != 1) {
- return false;
- }
- if (!indexUrlCheck()) {
- return false;
- }
- } else {
- return false;
- }
- console.log('ajax_init');
- require(['HackTimer'], function (HackTimer) {
- var url = '';
- if (Config.modulename == 'admin') {
- url = 'notice/admin/statistical';
- }
- if (Config.modulename == 'index') {
- url = '/addons/notice/api/statistical';
- }
- // 获取新消息并提示
- function notice() {
- Fast.api.ajax({
- url: url,
- loading: false
- }, function (data, res) {
- if (data.new) {
- Toastr.info(data.new.content);
- }
- if (Config.modulename == 'admin') {
- Backend.api.sidebar({
- 'notice/admin': data.num,
- });
- }
- setTimeout(function () {
- notice();
- }, 5000);
- return false;
- }, function () {
- return false;
- });
- };
- notice();
- });
- };
- function wsInit() {
- if (Config.modulename == 'admin') {
- if (!(Config.controllername == 'index' && Config.actionname == 'index' && Config.notice.admin_real == 2)) {
- return false;
- }
- } else if (Config.modulename == 'index'){
- if (!indexUrlCheck()) {
- return false;
- }
- if (Config.notice.user_real != 2) {
- return false;
- }
- } else {
- return false;
- }
- console.log('ws_init');
- let NhWs = {
- ws: null,
- timer: null,
- bindurl: '',
- url: '',
- connect: function () {
- var ws = new WebSocket(this.url);
- this.ws = ws;
- ws.onmessage = this.onmessage;
- ws.onclose = this.onclose;
- ws.onerror = this.onerror;
- ws.onopen = this.onopen;
- },
- onmessage: function (e) {
- // json数据转换成js对象
- var data = e.data;
- try {
- JSON.parse(data);
- data = JSON.parse(data) ? JSON.parse(data) : data;
- } catch {
- console.log('ws接收到非对象数据', data);
- return true;
- }
- console.log('ws接收到数据', data, e.data);
- var type = data.type || '';
- var resdata = data.data ? data.data : {};
- switch(type){
- case 'init':
- $.ajax(NhWs.bindurl, {
- data: {
- client_id: data.client_id
- },
- method: 'post'
- })
- break;
- case "new_notice":
- if (Config.modulename == 'admin') {
- Backend.api.sidebar({
- 'notice/admin': resdata.num,
- });
- }
- Toastr.info(resdata.msg);
- // 发送ajax到后台告诉已经看过这条消息
- Fast.api.ajax({
- url: '/addons/notice/api/cache',
- data: {
- time: resdata.time,
- module: Config.modulename
- },
- method: 'post'
- }, function () {
- return false;
- });
- }
- },
- onclose: function () {
- console.log('连接已断开,尝试自动连接');
- setTimeout(function () {
- NhWs.connect();
- }, 5000);
- },
- onopen: function () {
- this.timer = setInterval(function () {
- NhWs.send({"type":"ping"});
- }, 20000);
- },
- onerror: function () {
- console.log('ws连接失败');
- Toastr.error('ws连接失败');
- },
- // 发送数据
- send: function (data) {
- if (typeof data == "object") {
- data = JSON.stringify(data);
- }
- this.ws.send(data);
- },
- };
- if (Config.modulename == 'admin') {
- NhWs.bindurl = Fast.api.fixurl('/addons/notice/ws/bindAdmin');
- // ajax请求获取消息数量等
- Fast.api.ajax({
- url: 'notice/admin/statistical',
- loading: false,
- method: 'post',
- }, function (data, res) {
- if (data.new) {
- Toastr.info(data.new.content);
- }
- Backend.api.sidebar({
- 'notice/admin': data.num,
- });
- return false;
- }, function () {
- return false;
- });
- }
- if (Config.modulename == 'index') {
- NhWs.bindurl = Fast.api.fixurl('/addons/notice/ws/bind');
- // ajax请求最新获取消息数量等
- Fast.api.ajax({
- url: '/addons/notice/api/statistical',
- loading: false,
- method: 'post',
- }, function (data, res) {
- if (data.new) {
- Toastr.info(data.new.content);
- }
- return false;
- }, function () {
- return false;
- });
- }
- NhWs.url = Config.notice.wsurl;
- require(['HackTimer'], function (HackTimer) {
- NhWs.connect();
- });
- };
- function indexUrlCheck() {
- if (Config.modulename == 'index') {
- var url = Config.controllername+'/'+Config.actionname;
- if (Config.notice.user_real_url.indexOf('*') === -1) {
- if (Config.notice.user_real_url.indexOf(url) === -1) {
- return false;
- }
- }
- }
- return true;
- };
- require([], function (undefined) {
- // ajax轮询
- ajaxInit();
- wsInit();
- // 后台绑定按钮
- if (Config.modulename == 'admin' && Config.controllername == 'general.profile' && Config.actionname == 'index') {
- $('[type="submit"]').before('<button style="margin-right: 5px;" type="button" class="btn btn-primary btn-dialog" data-url="notice/admin_mptemplate/bind">模版消息(公众号)</button>');
- }
- });
|