UNPKG

nuijs

Version:

nui框架

271 lines (232 loc) 7.58 kB
Nui.define(function(require){ var util = require('../core/util'); var loading = require('../components/layer/loading'); var ajax = $.ajax; var defaults = { // 公共查询参数 query: {}, //公共参数配置 data:{}, //结果集合 status:{}, //接口扩展名 .do .php ext:'', //响应成功字段名 name:'result', //响应成功字段值 value:'success', //拦截器 intercept: null, success: null, error: null, } var request = function(options, text, under){ if(typeof options === 'string'){ options = { url:options, type:'GET' } } if(!options.url){ return } if( options.contentType && options.contentType.indexOf('application/json') !== -1 && options.data && typeof options.data !== 'string' ){ options.dataType = 'json'; options.data = JSON.stringify(options.data); } var _loading; if(text !== null){ var opts = { content:text||'正在加载数据...' } if(under){ if(under instanceof jQuery){ opts.container = under } else{ opts.under = under } } _loading = loading(opts) } var success = options.success || $.ajaxSettings.success || $.noop; var error = options.error || $.ajaxSettings.error || $.noop; //登录拦截器 var intercept = function(){ var callback = defaults.intercept || defaults.success; if(typeof callback === 'function'){ return callback.apply(this, arguments) } } options.success = function(res, status, xhr){ if(_loading){ _loading.destroy() } if(res && options.intercept !== false && intercept.call(this, res, status, xhr) === false){ return false } success.call(this, res, status, xhr) } options.error = function(){ if(_loading){ _loading.destroy() } if(typeof defaults.error === 'function' && defaults.error.apply(this, arguments) === false){ return false } error.apply(this, arguments) } var paramIndex = options.url.indexOf('?'); var params = '?'; if(paramIndex !== -1){ params = options.url.substr(paramIndex); options.url = options.url.substr(0, paramIndex).replace(/\/+$/, ''); } if(options.url && !/^(https?:)?\/\//.test(options.url) && (defaults.preurl)){ var preurl = defaults.preurl; if(typeof preurl === 'function'){ preurl = preurl(options.url) } options.url = ((preurl || '') + options.url).replace(/^\/+/, '/') } if(options.ext !== false && defaults.ext && !/\.\w+$/.test(options.url)){ options.url += defaults.ext } if(options.cache !== true){ params = util.setParam('_', new Date().getTime(), params); delete options.cache } if (defaults.query) { var query = defaults.query; if (typeof query === 'function') { query = defaults.query(); } if (typeof query === 'object') { params = util.setParam(query, params); } } if(params !== '?'){ options.url += params } return ajax($.extend(true, {}, { dataType:'json', data:(function(data){ if(typeof data === 'function'){ return data() } return data })(defaults.data) }, options)) } request.config = function(){ var args = arguments, len = args.length; var defs = {}; if(len === 1){ if(typeof args[0] === 'object'){ defs = args[0] } else if(typeof args[0] === 'string'){ return defaults[args[0]] } } else if(len > 1){ defs[args[0]] = defs[args[1]] } return $.extend(true, defaults, defs) } var method = function(options, msg){ return function(url, data, callback, text, under, opts){ if(typeof data === 'function'){ under = text; text = callback; callback = data; data = undefined; } else if(typeof data === 'string' || data === null){ under = callback; text = data; data = callback = undefined; } if (!opts) { opts = {}; } if(typeof callback === 'object'){ if(callback === null){ under = text; text = callback; callback = undefined; } else{ var object = callback; var status = defaults.status; callback = function(res, _status, xhr){ if(!res){ res = {} } if(status && typeof status === 'object'){ object = $.extend({}, status, object, opts.status) } Nui.each(object, function(cb, key) { var resStatus = res[defaults.name].toString(); var strKey = key.toString(); if ( (strKey === 'other' && resStatus !== defaults.value) || resStatus === strKey || (strKey.indexOf('^') === 0 && new RegExp(strKey).test(resStatus))) { cb && cb.call(object, res, _status, xhr) return false } }) } } } if(text && typeof text === 'object'){ under = text; text = undefined; } if(msg && !text && text !== null){ text = msg } return request($.extend({ url:url, data:data, success:callback }, options, opts), text, under) } } request.get = method({ type:'GET' }); request.sync = method({ type:'GET', async:false }); request.update = method({ type:'GET' }, '正在保存数据...'); request.jsonp = method({ type:'GET', dataType:'jsonp' }); request.post = method({ type:'POST' }); request.postSync = method({ type:'POST', async:false }); request.postUpdate = method({ type:'POST' }, '正在保存数据...'); request.postJSON = method({ type:'POST', contentType:'application/json;charset=utf-8' }, '正在保存数据...'); return request })