(function (){
	//入口js
	'use strict';
	/**
	 * URL参数转换为对象
	 */
	Location.prototype.searchObj = function(url){
		/**
		 * 传入的是对象 - 解析为参数字符串 不带？
		 * 出入是字符串不传入 - 解析为对象
		 */
		if (url instanceof Object) {
			let str = [];
			for (let i in url) {
				str.push(`${i}=${url[i]}`);
			}
			return str.join('&');
		} else {
			var obj = {};
			var arr = ((url || this.search)+"").match(/[?&][^?&]+=[^?&]+/g);
			if (arr) {
				arr.forEach(function(item) {
					var tempArr = item.substring(1).split('=');
					obj[decodeURIComponent(tempArr[0])] = decodeURIComponent(tempArr[1]);
					length++;
				});
			}
			return obj;
		}
	}
	//对象
	window.U = {
		versions: "", //版本控制
		LOAD: [], //加载完成全局数组
		script: document.querySelector('script[door]'), //获取当前目录级别
		dire: '', //目录
		_dire: '', //相对目录
		//加载公共函数
		require: function(_url, _type, callback) {
			var _this = this;
			var url = Array.isArray(_url) ? _url.shift() : _url;
			if (!url) {	return this;}
			if(typeof _type === "function"){
				callback = _type;
				_type = false;
			}
			var type = /\.[^\.]+$/.exec(url.split('?')[0]); //保存请求类型 js或css
			//添加版本号
			url = url.split('?')[0] + '?' + location.searchObj(Object.assign(location.searchObj('?v_=' + this.versions),location.searchObj(url)));
			//拼接地址
			var href = this.dire + url;
			//_type为true 不进行url转换
			_type && (href = url);
			//全路劲直接访问
			(url.substr(0, 2) == '//' || /^http(s*):\/\//.test(url)) && (href = url);
			var elem = null;
			if (callback || this.___switch) {
				switch (type && type.toString()) {
					case '.js':
						elem = document.createElement("script");
						//elem.defer="defer";//所有加载完成后按顺序执行
						elem.type = "text/javascript";
						//拼接连接地址
						elem.src = href;
						break;
					case '.css':
						elem = document.createElement("link");
						elem.type = "text/css";
						elem.rel = 'stylesheet';
						elem.media = 'all';
						//拼接连接地址
						elem.href = href;
						break;
					default:
						elem = {};
						break;
				}
				let eveFun = function(){
					if(typeof callback === 'function'){
						let fun = 'vbz'+new Date().valueOf();
						_this[fun] = callback;
						_this[fun](true);
						delete _this[fun];
					}
				}
				if (elem.readyState) {
					//加载响应,成功
					elem.onreadystatechange = function() {
						if (elem.readyState == "loaded" || elem.readyState == "complete") {
							elem.onreadystatechange = null;
							eveFun();
						}
					};
				} else {
					//加载完成
					elem.onload = function() {
						eveFun();
					};
				}
				//加载失败
				elem.onerror = function() {
					eveFun();
				};
			} else {
				switch (type && type.toString()) {
					case '.js':
						document.write("<script type='text/javascript' src='" + href + "'></script>");
						break;
					case '.css':
						document.write("<link rel='stylesheet' type='text/css' media='all' href='" + href + "'>");
						break;
					default:
						console.error('无法加载文件', href);
						break;
				}
			}
			elem && document.querySelector('html').appendChild(elem);
			Array.isArray(_url) && _url.length && this.require(_url, _type, callback);
		},
		//加载完毕执行
		ready: function(callback) {
			var _this = this;
			if (typeof callback == 'function') {
				let funName = "tempReady" + _this.LOAD.length;
				this[funName] = callback;
				_this.LOAD.push(funName);
			}
			window.onload = function() {
				_this.___switch = true;
				for (var i = 0; i < _this.LOAD.length; i++) {
					_this[_this.LOAD[i]]();
					delete _this[_this.LOAD[i]];
				}
				_this.LOAD = [];
			}
		},
		//初始化
		init: function() {
			if (this.script) {
				//this.versions = this.script.getAttribute('door');
				var jHtml = this.script.outerHTML,
					srcS = 'src="',
					strE = '"';
				srcS = jHtml.indexOf(srcS) + srcS.length; //路径开始位置
				strE = jHtml.indexOf(strE, srcS); //路径结束位置
				jHtml = jHtml.substring(srcS, strE); //完整路径带文件名
				strE = jHtml.lastIndexOf('/'); //查找倒数第一斜杠位置
				this.dire = jHtml.substring(0, strE + 1); //完整相对路径
				/**
				 * (项目跟目录) 预留本地加载文件名，实现预先本地配置 貌似无法完成
				 * 通过配置项目名（项目目录）来获取项目路劲进行全项目页面相对路径配置
				 * 完成通过全域名引用admin框架
				 * 目前未实现
				 */
				this.dire.lastIndexOf("../") > -1 && (this._dire = this.dire.substr(0, this.dire.lastIndexOf("../") + 3));
			} else {
				console.error("入口js标签没有关键属性 door");
			}
		}
	};
	//初始化
	U.init();
	/**
	 * ajax代理拦截器
	 */
	!function(t) {
		function r(i) {
			if (n[i]) return n[i].exports;
			var o = n[i] = {
				exports: {},
				id: i,
				loaded: !1
			};
			return t[i].call(o.exports, o, o.exports, r), o.loaded = !0, o.exports
		}
		var n = {};
		return r.m = t, r.c = n, r.p = "", r(0)
	}([function(t, r, n) {
		n(1)(U)
	}, function(t, r) {
		t.exports = function(t) {
			t.hookAjax = function(t) {
				var xhrObj = t;
				function r(t) {
					return function() {
						return this.hasOwnProperty(t + "_") ? this[t + "_"] : this.xhr[t]
					}
				}
	
				function n(r) {
					return function(n) {
						var i = this.xhr,
							o = this;
						return 0 != r.indexOf("on") ? void(this[r + "_"] = n) : void(t[r] ? i[r] = function() {
							t[r](o) || n.apply(i, arguments)
						} : i[r] = n)
					}
				}
	
				function i(r) {
					return function() {
						var n = [].slice.call(arguments);
						if (!t[r] || !t[r].call(this, n, this.xhr)) return this.xhr[r].apply(this.xhr, n)
					}
				}
				return window._ahrealxhr = window._ahrealxhr || XMLHttpRequest, XMLHttpRequest = function() {
					let xhr = this.xhr = new window._ahrealxhr;
					for (var t in this.xhr) {
						var o = "";
						try {o = typeof this.xhr[t]} catch (t) {}
						"function" === o ? this[t] = i(t) : Object.defineProperty(this, t, {
							get: r(t),
							set: n(t)
						})
					}
				}, window._ahrealxhr
			}, t.unHookAjax = function() {
				window._ahrealxhr && (XMLHttpRequest = window._ahrealxhr), window._ahrealxhr = void 0
			}, t.default = t
		}
	}]);
	//加载公共方法
	U.require('js/U-public.js');
	//实时动态加载配置文件
	U.require("js/U-config.js?v_= " + new Date().getTime());
	//加载全局拦截
	U.require("js/U-hookAjax.js?v_= " + new Date().getTime());
})();
