/** * WX开发平台定制前端功能组件 * *【依赖于 jquery 和 dialog ,请大此之前引用】 * * $.msg 消息处理组 * $.msg.show 显示消息 * $.msg.auto 自动处理显示消息 * $.msg.loading 显示加载中的进度 * $.msg.load 提交数据 * $.msg.load_close 关闭框口 * $.msg.close 关闭消息 * * @version 1.0 * @author zoujingli * @link http://github.com/cxphp */ ~(function (jQuery) { 'use strict'; /** * 系统消息处理组件 * * @returns {jquery.wx_L1.msg} */ function _msg() { this.dialog = jQuery.dialog({ id: 'msg', skin: 'black', padding: '5px', cancel: false, title: false, fixed: true, lock: true, beforeunload: function () { this.hidden(); return false; } }).hidden(); this.constructor = this; } /** * 显示标准消息框 * * @param {type} msg 需要显示的消息内容 * @param {type} callback 关闭后的回调函数 * @param {type} time 延时关闭,0 立即关闭、false 永不关闭、大于0 倒计时 * @param {type} type 消息类型,主要用于定义图标 * @returns {jquery.wx_L11.msg.prototype} */ _msg.prototype.show = function (msg, callback, time, type) { var ico = false; var style = 'padding:8px 15px;border-radius:50%;color:#fff;font-size:18px;'; switch (type) { case 'success': ico = '√'; style += 'background:green;background-color:rgba(0,255,0,.8);'; break; case 'error': ico = '×'; style += 'background:red;background-color:rgba(255,0,0,.8);'; break; } (ico) && (msg = '

' + ico + '

' + msg + ''); this.dialog.content('
' + msg + '
').zIndex().visible(); return this.close(time || false, callback); }; /** * 显示加载中并锁屏 * * @param {type} msg 消息内容 * @param {type} time 延时关闭,0 立即关闭、false 永不关闭、大于0 倒计时 * @returns {jquery.wx_L11.msg.prototype} */ _msg.prototype.loading = function (msg, time) { msg = msg ? '

' + msg + '

' : ''; this.dialog.size(200, 40).content('' + msg).title(false).zIndex().visible(); return this.close(time || false); }; /** * 成功时的消息提示 * * @param {type} msg * @param {type} callback 关闭后的回调函数 * @param {type} time 延时关闭,0 立即关闭、false 永不关闭、大于0 倒计时 * @returns {jquery.wx_L11.msg.prototype} */ _msg.prototype.success = function (msg, callback, time) { return this.show(msg, callback, time || 2, 'success'); }; /** * 失败时的消息回复 * * @param {type} msg * @param {type} callback 关闭后的回调函数 * @param {type} time 延时关闭,0 立即关闭、false 永不关闭、大于0 倒计时 * @returns {jquery.wx_L11.msg.prototype} */ _msg.prototype.error = function (msg, callback, time) { return this.show(msg, callback, time || 3, 'error'); }; /** * 消息自动处理与判断显示 * * @param {type} data 消息数据对象 * @param {type} time 延时关闭,0 立即关闭、false 永不关闭、大于0 倒计时 * @returns {jquery.wx_L11.msg.prototype} */ _msg.prototype.auto = function (data, time) { var self = this; if (typeof data === 'object') { if (data.status) { this.success(data.info, function () { self.load_close(); if (jQuery.tab && jQuery.tab.id) { (data.url) ? jQuery.tab.href(data.url) : jQuery.tab.reload(); } else { (data.url) ? window.location.href = data.url : window.location.reload(); } }, time); } else { this.error(data.info, function(){ data.url.indexOf("javascript:") > -1 && eval(data.url); }); } } return this; }; /** * 关闭消息框 * * @param {type} callbck 关闭后的回调函数 * @param {type} time 延时关闭,0 立即关闭、false 永不关闭、大于0 倒计时 * @returns {jquery.wx_L11.msg.prototype} */ _msg.prototype.close = function (time, callback) { var self = this; function _close() { self.dialog.hidden(); (typeof callback === 'function') && callback.call(); } if (time > 0) setTimeout(_close, time * 1000); else if (time !== false) _close(); return this; }; /** * 关闭最顶层Load框 * * @param {type} callback 处理函数,默认是关闭dialog的close操作 * @returns {jquery.wx_L13._msg.prototype} */ _msg.prototype.load_close = function (callback) { var i = (window.loads || []).length - 1; if (i >= 0) { var dialog = window.loads[i]; if (typeof callback === 'function') { callback.call(dialog); window.loads = window.loads.splice(i, 1); } else { if (dialog.submit) { dialog.close(); window.loads = window.loads.splice(i, 1); } } } return this; }; /** * Ajax提交数据至指定URL并自动处理返回的结果 * * @param {type} url 服务处理URL地址 * @param {type} data 需要提交的数据 * @param {type} type 提交的数据类型 get|post * @returns {jquery.wx_L13._msg.prototype} */ _msg.prototype.load = function (url, data, type, msg, time) { var self = this; this.loading(msg); $.ajax({ type: type || 'GET', url: url, data: data || {}, error: function (XMLHttpRequest, textStatus, errorThrown) { /* Ajax请求出现错误时,友好提示用户,并显示错误状态消息 */ $('body').trigger('ajax.submit.error'); self.error('服务器繁忙,请稍候再试!
[ ' + textStatus + ', ' + errorThrown + ' ]'); }, success: function (data) { $('body').trigger('ajax.submit.success'); /* 1、如果访问的页面没有权限或某些执行类操作时会反回标准的JSON数据 */ if (typeof (data) === 'object') { self.auto(data, time); return; } /* 2、如果系统出现致命信息错误时,内容中呆以包涵以下信息 */ if (data.indexOf('系统发生错误') !== -1) { self.error('服务器繁忙, 请稍候再试!'); return; } if (jQuery.tab) { /* HTML数据加载成功后显示到模态框中 */ var dialog = jQuery.dialog({ padding: 0, lock: true, content: data, initialize: function () { var $node = $(this.node); this.title($node.find('title').attr('data-dialog', this.config.id).html() || '窗口'); $node.find('[data-close]').on('click', function () { confirm('确认要关闭窗口吗?') && dialog.close(); }).end().find('form').submit(function () { dialog.submit = true; }).end().find('button:submit').click(function () { dialog.submit = true; }); self.close(); }, beforeunload: function () { self.close(); self.load_close(function () { }); } }); window.loads = window.loads || []; window.loads.push(dialog); } else { $(window.document.body).html(data); } } }); return this; }; /** * 挂载消息对象至jQuery操作 */ jQuery(function () { jQuery.msg = new _msg(); }); })(jQuery);