admin.js

admin.js

自己收藏的js、无实际用途

(function($){
	$.extend({
		//管理系统系统层通用方法
		admin:{
			/**
			 * encodeURL(unescape):用于对URL的组成部分进行个别编码。因此对";/?:@&=+$,#"不会被编码。网页提交表单时,如果有空格,会被转化为+字符
			 * encodeURLComponent(decodeURIComponent):对整个URL进行编码。
			 * 获取地址栏参数
			 */
			getUrlParam:function(param){
				var reg=new RegExp("(^|&)"+param+"=([^&]*)(&|$)");
				var r=windw.location.search.substr(1).match(reg);
				if(r!=null){
					return decodeURIComponent(r[2]);
				}else{
					return null;
				}
			},
			/**
			 * 完整的URL的组成部分
			 * scheme://host:port/path?query#fragment
			 * schema:通信协议,常用http,ftp,maito等。
			 * host:主机,服务器(计算机)域名系统DNS主机名或IP地址
			 * port:端口、整数,可选。省略时使用方案的默认的端口,如http的端口为80
			 * path:路径,由零个或多个"/"符号隔开的字符串、一般用来表示主机上的一个目录或文件地址
			 * query:查询,可选,用于给动态网页传递参数,可由多个参数用"&"符号隔开,每个参数的名称和值用"="符号隔开
			 * fragment:信息片段,用于指定网络资源中的片段,也称瞄点
			 * 如http://www.baidu.com/index.html?ver=1.0&id=12#imhere
			 */
			getUrl:function(){
				return window.location.href;
			}
			/**
			 * 返回Url的协议部分,如上述实例的http
			 */
			getSchema:function(){
				return window.location.protocol;
			},
			/**
			 * 返回Url的主机部分。如上面实例的www.baidu.com
			 */
			getHost:function(){
				return window.location.host;
			},
			/**
			 * 返回Url的端口 
			 * 如果采用协议默认的端口,如上面的http的默认端口为80,则不管地址栏是否存在端口值,都返回空字符串
			 */
			getPort:function(){
				return window.location.port;
			},
			/**
			 * 返回URL的路径部分
			 */
			getUrlPath:function(){
				return window.location.pathname;
			},
			/**
			 * 返回查询参数部分,如上面实例的?ver=1.0&id=12
			 */
			getSearch():function(){
				return window.location.search;
			},
			/**
			 * 返回瞄点,如上面实例:#imhere
			 */
			getHash:function(){
				return window.location.hash;
			},
			exit:function(callback){
				$.common.data($.setter["tableName"],{key:$.setter.request.toKenName,remove:true});
				callback&&callback(); //有回调则执行回调函数
				window.location.href=$.setter.ctx()+'login.html';
			},
			error:function(content,options){
				$.modal.alertError(content);
			},
			isOk:function(res){ //用于判断是否成功的情况
				var response=$.setter.response;
				var statusCode=response.statusCodel;
				if(res[response.statusName]==statusCode.ok){
					return ture;
				}else{
					return false;
				}
			},
			msgName:function(){
				var response=$.setter.response;
				return response.msgName;
			}
			/**
			 * ajax请求
			 */
			req:function(options){
				var that=this,
				success=options.success,
				error=options.error,
				request=$.setter.request,
				response=$.setter.response,
				debug=function(){
					return $.setter.debug?'<br><cite>URL:</cite>'+options.url:'';
				};
				options.data=options.data||{};
				var isJsonStr=options["isJsonStr"];
				if($.common.isEmpty(isJsonStr)){
					isJsonStr=true;
				}
				if(typeof options.data!=='string' &&isJsonStr){
					options.data=JSON.stringify(options.data);
					options.contentType="application/json;charset=utf-8";
				}
				options.heads=options.headers||{};
				if(request.tokenName){
					var sendData=typeof options.data==='string'?JSON.parse(options.data):options.data;
					var tokenValue=request.tokenName in sendData?options.data[request.tokenName]
						:($.common.data($.setter.tableName)[request.tokenName||'']);
					options.data[request.tokenName]=tokenValue;
					options.heads[request.tokenName]=tokenValue;
				}
				var url=options.url;
				if($.common.isNotEmpty(url)){ //自动过滤测试文件路径
					var param="";
					var ind=url.indexOf("?");
					if(ind>0){
						param=url.substr(ind);
						url=url.substring(0,ind);
					}
					if($.setter.active){
						if($.common.endWith(url,".json")){
							url=url.substring(0,url.length-5);
						}
					}else{
						if(!$.common.endWith(url,".json")){
							url+=".json";
						}
					}
					if($.common.isNotEmpty(param)){
						url+=param;
					}
					option.url=url;
				}
				delete options.success;
				delete options.error;
				options=$.extend({
					type:'post',
					dataType:'json',
					xhrFields:{
						withCredentials:true  //在跨域情况下可以携带cookie
					},
					success:function(res){
						var statusCode=response.statusCode;
						if(res[response.statusName]==statusCode.ok){
							if(typeof options.done==='function'){
								var ret=option.done(res);
								if(ret){
									$.when.apply(this,ret).then(function(){ //异步返回后关闭
										$.modal.closeLoading();
										$.modal.enable();
									});
								}
							}
							$.modal.closeLoading();
							$.modal.enable();
						}else if(res[response.statusName]==statusCode.logout){
							 $.admin.exit();
						}else{
							var errorInfo=['<cite>Error:</cite>'+(res[response.msgName]||'返回状态码异常').debug()].join('');
							$.admin.error(errorInfo);
							$.modal.closeLoading();
							$.modal.enable();
						}
						typeof success==='function' && success(res);
					},
					error :function(e,code){
						//修改了
						e=JSON.parse(e.responseText);
						if(e.retCode==='40100'){
							//需要登录错误直接跳转登录页面
							$.util.redirect($.setter.rootPath()+'login/login.html',top);
						}else{
							//其他错误弹框提示
							var errorInfo=['请求异常,请重试<br><cite>错误码:</cite>'+e.retCode+'<br><cite>错误信息:</cite>'+e.retMsg,debug()].join('')];
						   $.admin.error(errorInfo);
						}
						$.modal.closeLoading();
						$.modal.enable();
						
					}
				},options);
				if($.setter.debug){
					log.log("提交请求");
					log.log(options);
				}
				return $.ajax(options);
			},
			postForm:function(url,data,callback){
				var config={
					url:url,
					type:"post",
					dataType:"json",
					data:data,
					isJsonStr:false,
					beforeSend:function(){
						$.modal.loading("正在处理中,请稍后....");
					},
					done:function(result){
						if(typeof callback==='function'){
							return callback(result);
						}
					}
				};
				return $.admin.req(config);
			},
			postJson:function(url,data,callback){
				return post(url,data,callback);
			},
			//post请求传输
			post:function(url,data,callback){
				var config={
					url:url,
					type:"post",
					dataType:"json",
					data:data,
					isJsonStr:true,
					beforeSend:function(){
						$.modal.loading("正在处理中,请稍后...");
					},
					done:function(result){
						if(typeof callback==='function'){
							return callback(result);
						}
					}
				};
				return $.admin.req(config);
			},
			//get请求传输
			get:function(url,callback){
				var config={
					url:url,
					type:"get",
					dataType:"json",
					"isJsonStr":false,
					beforeSend:function(){
						$.modal.loading("正在处理,请稍后...");
					},
					done:function(result){
						if(typeof callback==='function'){
							return callback(result);
						}
					}
				};
				return $.admin.req(config);
			}
		}
  });
})(jQuery);

//IE8 不支持CORS-跨域访问的支持,所需要增加如下方法
jQuery.support.cors=true;
/**设置全局ajax处理*/
$.ajaxSetup({
	/**
	 * 当请求完成之后调用这个函数,无论成功或失败
	 * 传入XMLHttpRequest对象,以及一个包含成功或错误代码的字符串
	 */
	complete: function(XMLHttpRequest,textStatus){
		if(textStatus=='timeout'){
			$.modal.alertWarning("服务器超时,请稍后再试!")
			$.modal.closeLoading();
		}else if(textStatus=='parseerror'){
			$.modal.alertWarning("服务器错误,请联系管理员!")
			$.modal.closeLoading();
		}
	}
});
//全局异常捕获
window.onerror=function(msg,url,lineNo,columnNo,error){
	if(!(typeof msg==='string')){
		msg="出错了";
	}
	var str=msg.toLowerCase();
	var scr="script error";
	if(str.indexOf(scr)>0){
		$.admin.error("脚本加载出错,请查看浏览器输出明细");
	}else{
		var message=[
		 '脚本出错了,请联系管理员',
		 '<br>出错信息:'+msg,
		 '<br>出错链接:'+url,
		 '<br>出错行号:'+lineNo,
		 '<br>出错位置:'+columnNo
		].join('');
		$.admin.error(message);
		if($.setter.debug){
			error=error||'';
			console.error(error);
		}
	}
	return true;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值