core.min.js 28 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
/*
 * 言语数据加载
 * lang_cn.js 简体中文
 * lang_tw.js 繁体中文
 * lang_ko.js 韩文
 * lang_ja.js 日文
 */
// (function(){
// 	var type ='cn';
// 	var lang = (navigator.language || navigator.browserLanguage).toLowerCase();
// 	if(lang.indexOf('zh') != 1){
// 		//中文
// 		if(lang.indexOf('tw')!=-1 || lang.indexOf('hk') != -1){
// 			//繁体中文
// 			type = 'tw';
// 		}
// 	}else if(lang.indexOf('ko') != -1) {
// 		type = 'ko';	//韩文
// 	} else if(lang.indexOf('ja') != -1){
// 		type = 'ja';	//日文
// 	}
// 	$('<script type="text/javascript" src="/static/js/lagn_' + type + '.js/>').appendTo($('head'));
// })();

/*
 * 定义核心模块
 */
var Core = {};
function initMatching(){
	var userAgent = navigator.userAgent;
	Core.isIos = userAgent.match(/i[^;]+;( U;)? CPU.+Mac OS X/) != null;
	Core.isAndroid = userAgent.indexOf('Android') > -1 || userAgent.indexOf('Linux') > -1;
	Core.isFile = location.href.indexOf('file') == 0;		//判断当前请求是否为file://协议
	Core.isWeixin = userAgent.indexOf('MicroMessenger') != -1;
	var ratio = window.devicePixelRatio;
	Core.ratio = ratio;

	
	var lastServer = localStorage.getItem('last_server');
	if(lastServer && lastServer.indexOf('http:')==0){
		if(Core.isFile){
			Core.server = lastServer;
		}else if(location.href.indexOf(lastServer)==-1){
			location.href = location.href.replace(Core.server,lastServer);
		}
	}
	var ary = null;
	if (location.hash.indexOf('app_id') != -1) {
		ary = location.hash.match(/.*%7C(\d+)_(\d+)&/);
	} else {
		ary = location.search.match(/.*%7C(\d+)_(\d+)&/);
	}
	if (ary && ary.length == 3) {
		//成功匹配屏幕大小, 直接计算屏幕高度
		Core.sdk_w = parseInt(ary[1]);
		Core.sdk_h = parseInt(ary[2]);
		if(Core.isIos && userAgent.indexOf('iPad')!=-1 && Core.sdk_h < 600){
			Core.sdk_w *= 2;
			Core.sdk_h *= 2;
		}
	}
	if(location.href.indexOf('callBackUrl')!=-1){
		try{
			Core.Data.back_url = unescape(location.href.match(/callBackUrl=([^&]+)/)[1]);
		}catch(e){}
		
	}
	
	var _head = $('head');
	if (Core.isAndroid) {
		$('html').attr('data-useragent', 'android');
		if (ratio > 1.5) {
			$('<meta name="viewport" content="target-densitydpi=340;initial-scale=1.0"/>').appendTo(_head);//340
			//Core.ratio = 1.5;
		} else if (ratio < 1) {
			$('<meta name="viewport" content="target-densitydpi=280;initial-scale=1.0"/>').appendTo(_head);//260
			//Core.ratio = 1;
		} else if(ratio == 1){
			$('<meta name="viewport" content="target-densitydpi=300;initial-scale=1.0"/>').appendTo(_head);//300
			//Core.ratio = 1.25;
		}else{
			$('<meta name="viewport" content="target-densitydpi=340;initial-scale=1"/>').appendTo(_head);//340
		}
	}
	var _w = null;
	if (Core.isIos) {
		$('html').attr('data-useragent', 'ios');
		
		if (!Core.sdk_w) Core.sdk_w = 600;
		_scale = 1;
		if(ratio == 2){//5、6、6s(2.4)
			_scale = 2.4 / ratio;
			if(userAgent.indexOf('iPad') != -1){
				_scale = 2 / ratio;
			}
		}else if(ratio >2.5){//6s plus
			_scale = 2.7 / ratio;
		}else if (ratio < 1) {
			_scale = 1.5 / ratio;
		} else {
			_scale = 1;
		}
		if(userAgent.indexOf('iPad') != -1 && Core.sdk_w > 768){
			Core.sdk_w = Core.sdk_w / 2;
			Core.sdk_h = Core.sdk_h / 2 /  _scale;
		}else{
			Core.sdk_h = Core.sdk_h * _scale;
		}
		_w = Core.sdk_w * _scale;
		
		Core.ios_w = _w;
		
		$('meta[name="viewport"]').attr('content','width=' + _w + ',maximum-scale=' + _scale + ',user-scalable=no');
		//$('<meta name="viewport" content="maximum-scale=' + _scale + '"/>').appendTo(_head);
		//$('<meta name="viewport" content="width=' + _w + '"/>').appendTo(_head);
		$(window.document).on('touchmove', function(e) {
			if (e.target.tagName != "IFRAME")
				e.preventDefault();
		});
		$('body').addClass('ios');
		if($("div")[0].getAttribute("class")!="mask"){
			$("div")[0].remove();
		}
	}
};
(function() {
	//兼容localStorage处理
	if(typeof(localStorage) == 'undefined'){
		localStorage = {
			setItem: function(){
				console.log('localStorage not support!');
			},
			getItem: function(){
				console.log('localStorage not support!');
			}
		}
	}
	var userAgent = navigator.userAgent;
	Core.isIos = userAgent.match(/i[^;]+;( U;)? CPU.+Mac OS X/) != null;
	Core.isAndroid = userAgent.indexOf('Android') > -1 || userAgent.indexOf('Linux') > -1;
	Core.isFile = location.href.indexOf('file') == 0;		//判断当前请求是否为file://协议
	Core.isWeixin = userAgent.indexOf('MicroMessenger') != -1;
	var ratio = window.devicePixelRatio;
	Core.ratio = ratio;

	Core.PLATFORM_NAME = '海豹';
    
	if(Core.isIos){
		Core.server = 'https://sdk.uuufish.com';
	}else{
		Core.server = 'http://sdk.uuufish.com';
	}
	

	var lastServer = localStorage.getItem('last_server');
	if(lastServer && lastServer.indexOf('http:')==0){
		if(Core.isFile){
			Core.server = lastServer;
		}else if(location.href.indexOf(lastServer)==-1){
			location.href = location.href.replace(Core.server,lastServer);
		}
	}
	var ary = null;
	if (location.hash.indexOf('app_id') != -1) {
		ary = location.hash.match(/.*%7C(\d+)_(\d+)&/);
	} else {
		ary = location.search.match(/.*%7C(\d+)_(\d+)&/);
	}
	if (ary && ary.length == 3) {
		//成功匹配屏幕大小, 直接计算屏幕高度
		Core.sdk_w = parseInt(ary[1]);
		Core.sdk_h = parseInt(ary[2]);
		if(Core.isIos && userAgent.indexOf('iPad')!=-1 && Core.sdk_h < 600){
			Core.sdk_w *= 2;
			Core.sdk_h *= 2;
		}
	}
	if(location.href.indexOf('callBackUrl')!=-1){
		try{
			Core.Data.back_url = unescape(location.href.match(/callBackUrl=([^&]+)/)[1]);
		}catch(e){}
		
	}
	
	var _head = $('head');
	if (Core.isAndroid) {
		$('html').attr('data-useragent', 'android');
		if (ratio > 1.5) {
			$('<meta name="viewport" content="target-densitydpi=340;initial-scale=1.0"/>').appendTo(_head);//340
			//Core.ratio = 1.5;
		} else if (ratio < 1) {
			$('<meta name="viewport" content="target-densitydpi=280;initial-scale=1.0"/>').appendTo(_head);//260
			//Core.ratio = 1;
		} else if(ratio == 1){
			$('<meta name="viewport" content="target-densitydpi=300;initial-scale=1.0"/>').appendTo(_head);//300
			//Core.ratio = 1.25;
		}else{
			$('<meta name="viewport" content="target-densitydpi=340;initial-scale=1"/>').appendTo(_head);//340
		}
	}
	var _w = null;
	if (Core.isIos) {
		$('html').attr('data-useragent', 'ios');
		
		if (!Core.sdk_w) Core.sdk_w = 600;
		_scale = 1;
		if (ratio > 1.5){//6s plus,
			_scale = 2.8 / ratio;
		} else if (ratio < 1) {
			_scale = 1.5 / ratio;
		} else {
			_scale = 1;
		}
		if(userAgent.indexOf('iPad') != -1 && Core.sdk_w > 768){
			Core.sdk_w = Core.sdk_w / 2;
			Core.sdk_h = Core.sdk_h / 2 / _scale;
		}else{
			Core.sdk_h = Core.sdk_h * _scale;
		}
		_w = Core.sdk_w * _scale;
		
		Core.ios_w = _w;
		
		$('meta[name="viewport"]').attr('content','width=' + _w + ',maximum-scale=' + _scale + ',user-scalable=no');
		//$('<meta name="viewport" content="maximum-scale=' + _scale + '"/>').appendTo(_head);
		//$('<meta name="viewport" content="width=' + _w + '"/>').appendTo(_head);
		$(window.document).on('touchmove', function(e) {
			if (e.target.tagName != "IFRAME")
				e.preventDefault();
		});
		$('body').addClass('ios');
		if($("div")[0].getAttribute("class")!="mask"){
			$("div")[0].remove();
		}
	}
})();
(function(window, core) {

	var modules = {}, tip_timeOut = null;
	// 数据
	core.Data = {};
	core.cur_mod = null;
	core.last_mod = null;
	// 加载Script模板
	core.getTemplate = function(moduleId) {
		var tpl = $("#" + moduleId + "_template").html();
		return tpl;
	};
	core.getMod = function(modId){
		if (!modId in modules) {
			console.error('没有对应的模块id的section:' + modId);
			return;
		}
		return modules[modId];
	};
	// 模块注册
	core.registerMod = function(modId, modCreator) {
		if ($('#' + modId).length == 0)
			console.error('没有对应的模块id的section:' + modId);
		if (modules[modId])
			console.error('模块已经存在:' + modId);

		modules[modId] = new modCreator(modId);
		modules[modId].init();
	};
	//绑定客户端返回按钮
	core.back = function() {
		if (!core.cur_mod || core.cur_mod == '') core.cur_mod = 'home';
		var _$ = $('#' + core.cur_mod);
		var _$btn = _$.find('.op-back');
		if (_$btn.length == 0) {
			location.href = 'sdk://exit?action=login';
		} else {
			var path = _$btn.attr('href');
			if (path.indexOf('#') == 0) {
				//模块跳转
				location.hash = path;
			} else {
				location.href = path;
			}
		}
	};
	//清除iframe内容
	core.clearFrame = function() {
		var _$ = $('#frame iframe');
		_$.remove();
		//if (_$.length > 0) {
		//	setTimeout(function(){
		//		_$.remove();
		//	},3000);
		//}
	}
    core.get_url = function(url){
        var url = url
        if (core.isFile) {
			url = Core.server + url;
		}
        if (core.Data.open_id && core.Data.login_key){
            var connect_str = url.indexOf('?') >=0 ? '&' : '?'
            url = url + connect_str + 'open_id=' + core.Data.open_id + '&login_key=' + core.Data.login_key;
        }
        return url
    }
	//格式化输出字符串
	// core.format = function(str){
	// 	var params = Array.prototype.slice.call(arguments, 1);
	// 	var re = null;
	// 	for(var i=1, l=params.length; i<=l; i++){
	// 		re = new RegExp('\\$' + i, 'g');
	// 		if(params[i-1] == undefined) break;
	// 		str = str.replace(re, params[i-1]);
	// 	}
	// 	return str;
	// }
		
	core.updateData = function(data) {
		var appId = Core.Data.app_id;	//保留app_id
		if (!data) {
			Core.Data.username = null;
			Core.Data.nickname = null;

			return;
		}
		Core.Data = data
		Core.Data.app_id = appId;
		Core.Data.open_id = Core.Data.open_id + '';
		Core.Data.timestamp = Core.Data.timestamp + '';
		if(data.face){
			Core.Data.face = Core.server + data.face;
		}

	}

	/**
	 * 强行更新一个模块,并显示出来
	 */
	core.updateMod = function(modId) {
		if (!modId in modules) {
			console.error('没有对应的模块id的section:' + modId);
            modId = 'home'
			//return;
		}
		location.hash = "#" + modId + '&force';
	}

	/**
	 *  强行重绘所有模块,在用户登出操作后
	 */
	core.clearMods = function() {
		for(var mod in modules){
			modules[mod].isValid = false;
		}
	}

	/**
	 * 显示指定的模块
	 */
	core.showMod = function(modId) {
		if (!modId in modules) {
			console.error('没有对应的模块id的section:' + modId);
			return;
		}
		//location.replace = "#" + modId;
		location.hash = "#" + modId;
	}

	// 显示信息type=1: success, type=0: error, type=2: info, time默认2秒
	core.showTips = function(msg, type, time) {
		if (typeof msg == 'object') msg = msg.msg;
		if (type == undefined)
			type = 1;
		if (time == undefined)
			time = 4000;
		var $alert = $('#alert');
		if (type)
			$alert.removeClass('alert-error').addClass('alert-success');
		else
			$alert.removeClass('alert-success').addClass('alert-error');
		if (tip_timeOut != null) {
			// 上一个消息没消失,替换内容
			$('#alert span').html(msg);
			clearTimeout(tip_timeOut);
			tip_timeOut = null;
			tip_timeOut = setTimeout(function() {
				$('#alert').hide();
				tip_timeOut = null;
			}, time);
			return;
		}
		// 显示信息
		$('#alert span').html(msg);
		$alert.show();
		tip_timeOut = setTimeout(function() {
			$('#alert').hide();
			tip_timeOut = null;
		}, time);
	};

	core.isValid = function($form) {
		var isvalid = true;
		$form.find('input[required]').each(function() {
			if (!isvalid) {
				//已经出错了,直接return;
				return;
			}
			if ($(this).val() == '') {
				$(this).focus();
				isvalid = false;
			}
		});
		if (!isvalid) {
			Core.showTips('请先填写相应信息!', false, 2000);
			return false;
		}
		return true;
	}

	/**
	 * sdk跳转功能,浏览器下打开新页跳转
	 */
	core.goTo = function(url){
		if(!core.Data.game_url){
			url = 'sdk://open?url=' + encodeURIComponent(url);
		}
		location.href = url;
	}
	
	core.getCookie = function(name) {
		var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
		return r ? r[1] : undefined;
	}

	core.GetParameter = function(the_form) {
		var result = "";
		$(the_form).find("input,textarea,select").each(
			function() {
				if ($(this).attr('name') == undefined || $(this).attr('name') == '') {
					return;
				}
				// 过滤不需要的input
				var type = $(this).attr('type');
				if (type == "button" || type == "submit")
					return;

				var chk_box = false;
				if (type == "checkbox" || type == "radio")
					chk_box = true;

				var chk = false;

				if (chk_box) {
					if (!$(this)[0].checked)
						return;
					// 等于 each中的continue;
				}

				result += $(this).attr('name') + "=" + encodeURIComponent($(this).val().trim()) + "&";

			});
		result += 'app_id=' + encodeURIComponent(core.Data.app_id ? core.Data.app_id : core.Data.game_id);
		if (result.indexOf('_xsrf') == -1) {
			result += "&_xsrf=" + core.getCookie("_xsrf");
		}
		return result;
	}
    core.ajax = function(settings){
    //跨域修复https://github.com/imweb/mobile/issues/6
        settings.url = Core.get_url(settings.url)
        settings.beforeSend = function(xhr) {
                try {
                    xhr.withCredentials = true;
                } catch (e) {
                    var nativeOpen = xhr.open;
                    xhr.open = function() {
                        var result = nativeOpen.apply(xhr, arguments);
                        xhr.withCredentials = true;
                        return result;
                    };
                }
            }
            
        return $.ajax(settings);
    },
	core.ajax_do = function($obj, func, context) {
		var param = '';
		var action_methon = null;
		var post_url = '';
		var $sub_btn = $obj.find('[type=submit]');
		var temp = $sub_btn.is('button') ? $sub_btn.html() : $sub_btn
			.attr('value');

		if ($obj[0].tagName == "FORM" || $obj[0].getAttribute('action')) {
			if ($obj[0].disabled) {
				Core.showTips('请求已提交,请稍候!');
				return;
			}
			if (!core.isValid($obj))
				return;
			param = core.GetParameter($obj);
			post_url = $obj.attr('action');
			action_method = "post";
		} else {
			if ($obj[0].disabled) {
				//点击重入
				Core.showTips('请求已提交,请稍候!');
				return;
			}
			post_url = $obj.attr('href');
			param = $obj.attr('params');
			var action_method = $obj.attr('method');
			if (!action_method) {
				action_method = "get";
			}
		}
		if (post_url.indexOf('?') != -1) {
			post_url += '&format=json';
		} else {
			post_url += '?format=json';
		}


		var options = {
			type: action_method,
			url: post_url,
			dataType: 'json',
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			data: param,    
            beforeSend: function(xhr) {
                try {
                    xhr.withCredentials = true;
                } catch (e) {
                    var nativeOpen = xhr.open;
                    xhr.open = function() {
                        var result = nativeOpen.apply(xhr, arguments);
                        xhr.withCredentials = true;
                        return result;
                    };
                }
            },
			success: function(result, textStatus) {
				if (result.code == -10) {
					// 未登录,跳转的登录界面
				} else if (result.code == 0) {

					if (func && typeof func == "function") {
						context ? func.call(context, result) : func(result);
					}
					if (typeof(result.msg) == '$object' && result.msg.msg)
						result.msg = result.msg.msg;

				}
				if ($obj[0].tagName == "FORM") {
					$obj[0].disabled = false;
					$obj.find("input, button").attr('disabled', null);
					var $but_input = $obj.find('[type=submit]');
					if ($but_input.is('button')) {
						$but_input.html(temp);
					} else {
						$but_input.attr('value', temp);
					}
				} else {
					$obj[0].disabled = false;
				}
				if (result.msg != '') {
					if (result.code != 0)
						core.showTips(result.msg, false);
				}

			},
			cache: false,
			timeout: 10000,
			error: function(XMLHttpRequest, textStatus) {
				var response_status = XMLHttpRequest.status;
				var msg = "网络不给力,请重试!" + response_status;

				if (response_status == 403 || response_status == 401) {
					msg = '请先登录!';
				} else if (response_status == 408) {
					msg = '请求超时,请再尝试!';
				} else if (response_status == 411) {
					msg = '提交内容过长。';
				}
				if ($obj[0].tagName == "FORM") {
					core.showTips(msg, false, 3000);
					$obj[0].disabled = false;
					$obj.find("input, button").attr('disabled', null);
					var $but_input = $obj.find('[type=submit]');
					if ($but_input.is('button')) {
						$but_input.html(temp);
					} else {
						$but_input.attr('value', temp);
					}

				} else {
					core.showTips(msg, false, 3000);
					$obj[0].disabled = false;
				}
			}
		}
		if ($obj[0].tagName == "FORM") {
			$obj[0].disabled = true;
			$obj.find("input, button").attr('disabled', true);
			if ($sub_btn.is('button')) {
				$sub_btn.html("提交中...");
			} else {
				$sub_btn.attr('value', "提交中...");
			}
		} else {
			//a标签触发的请求
			$obj[0].disabled = true;
		}
		Core.showTips('加载中...', true, 5000);
		Core.ajax(options);
		return false;
	}

	core.load_json = function($obj, func, context) {
		var path = $obj.data('path');
		if ($obj.data('disabled') == true) {
			Core.showTips('请求已提交,请稍候。');
			return;
		}
	
		var options = {
			type: "get",
			url: path,
			dataType: 'json',
			timeout: 10000,
			success: function(data) {
				if (func && typeof func == "function") {
					context ? func.call(context, data) : func(data);
				}
				$obj.data('disabled', false);
			},
			error: function() {
				//数据加载出错
				Core.showTips('数据加载出错,请检查网络状态。', false, 3000);
				$obj.data('disabled', false);
			}
		}
		$obj.data('disabled', true);
		Core.showTips('加载中...', true, 5000);
		Core.ajax(options);
		return false;
	}

	core.load_html = function($obj, func, context) {
		var data_url = '';
		var isAppend = false;
		var data_tpl_url = $obj.attr('data-params-template');
		if ($obj[0].tagName == "A") {
			data_url = $obj.attr('href');
			isAppend = $obj.attr('data-params-append') == 'true' ? true : false;
		} else {
			data_url = $obj.attr('data-params-url');
		}
		if (data_url == '')
			return;
		if (data_url.indexOf('?') != -1) {
			data_url += '&format=json';
		} else {
			data_url += '?format=json';
		}
		if (data_url.indexOf('game_id') == -1) {
			var _id = core.Data.game_id ? core.Data.game_id : core.Data.app_id;
			data_url += '&game_id=' + _id;
		}
		var dataType = '';
		if (data_tpl_url && data_tpl_url != "") {
			dataType = 'json';
		}
		var data_result = '';

		var options = {
			type: "get",
			url: data_url,
			dataType: dataType,
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			success: function(result) {
				if(result.code && result.code != 0){
					//出错
					Core.showTips(result.msg, false);
					return;
				}
				data_result = result;
				if (data_tpl_url && data_tpl_url != "") {
					// 加载模板
					tpl = Core.getTemplate(data_tpl_url);
					var doTpl = doT.template(tpl);
					result = doTpl(data_result);
					if (isAppend) {
						// 删除最后一项,并将新的内容append进去
						var $wrap = $obj.parent();
						$wrap.is('.more') ? $wrap.replaceWith(result) : $obj
							.find('.more').replaceWith(result);
					} else {
						$obj.html(result);
					}
					if (func && typeof func == "function") {
						context ? func.call(context, result) : func(result);
					}
					Core.showTips('加载完毕!', true, 2000);
				} else {
					$obj.html(result);
					if (func && typeof func == "function") {
						context ? func.call(context, result) : func(result);
					}
					Core.showTips('加载完毕!', true, 2000);
				}

			},
			cache: false,
			timeout: 5000,
			error: function(e) {
				Core.showTips('json format has error');
			}
		}
		Core.showTips('加载中...', true, 5000);
		Core.ajax(options);
	}
	// 判断用户是否登录,使用两个
	core.isAuths = function() {
		if($('#login').length == 1 && !core.Data.username){
			return false;
		}
		return true;
	}
	// 继承
	core.inherit = function(childCtor, parentCtor) {
		/** @constructor */

		function temCtor() {}
		temCtor.prototype = parentCtor.prototype;
		childCtor.superClass_ = parentCtor.prototype;
		childCtor.prototype = new temCtor();
		/** @override */
		childCtor.prototype.constructor = childCtor;
	}
	// 动画显示模块
	core.show_anim = function($target) {
		$target.css({
			zIndex: 2
		});
		$target.show();
	}

	/**
	 * sdk.loadfinished
	 */
	core.loadFinished = function() {
		if (Core.isAndroid) { //If WebKit browser
			if(!Core.Data.game_url){
				try{
					return;
					location.href="sdk://finishLoad?action=login";
				}catch(e){}
			}
		} else {
			console.log('not wbkit browser!');
		}
	}
	core.finishPay = function() {
		var pay_url = "sdk://exit?action=pay";
		core.clearFrame()
		if(Core.Data.game_url && Core.Data.game_url != ''){
			if(Core.Data.back_url){
				pay_url = Core.Data.back_url;
			}else{
				pay_url = Core.Data.game_url;
			}
		}else{
			if(Core.Data.trade_id && Core.Data.trade_id != ''){
				pay_url = "sdk://pay?order_id=" + Core.Data.trade_id;
				pay_url += Core.Data.current_pay_channel ? '&paytype=' + Core.Data.current_pay_channel.name : ''
			}
		}
		try{
			location.href = pay_url;
		}catch(e){}
	}
	core.finishLogin = function() {
		var login_url = 'sdk://exit?action=login';
		var login_params = "open_id=" + Core.Data.open_id + "&token=" + Core.Data.token + "&timestamp=" + Core.Data.timestamp ;
		login_params += Core.Data.login_count ? '&login_count=' + Core.Data.login_count : '' ;
		if(Core.Data.game_url && Core.Data.game_url != ''){
			if(Core.Data.back_url){
				login_url = Core.Data.back_url;
			}else{
				login_url = Core.Data.game_url;
			}
			if(login_url.indexOf("?")!=-1){
				login_url +="&";
			}else{
				login_url +="?";
			}
			login_url += login_params;
		}else{
			login_url = "sdk://login?"+login_params;
			login_url += "&login_key=" + Core.Data.login_key;
		}
		
		try{
			location.href = login_url;
		}catch(e){}
	}

	// 隐藏指定模块
	core.hide = function(modId) {
		if (!modId)
			modId = core.cur_mod;
		if (!(modId in modules)) {
			console.log('没有对应的模块id的section:' + modId);
			return;
		}
		var page_w = $(window).width();
		var $target = $('#' + modId);
		if ($target.find('iframe').length > 0) {
			core.clearFrame();
		}

		$target.css({
			zIndex: 1
		});

		$target.hide();
		core.last_mod = modId;

	}

	// 核心初始化,注册事件
	core.init = function() {
		// $('#loading').hide();
		if(localStorage.getItem('guest_n') || Core.user_names){
			core.isFresh = false;
		} else {
			core.isFresh = true;
		}

		// 注册提示框的关闭按钮
		$('#alert').on('click', '', function(e) {
			if (tip_timeOut) {
				clearTimeout(tip_timeOut);
				tip_timeOut = null;
				$('#alert').hide();
			}
		});
		window.onhashchange = function() {
      
			var hash = location.hash,
				module, params;
			var strs = hash.split('&');
			hash = strs[0];
			params = strs[1];
			if (hash == '') {
              
				if (Core.start_page) {
					hash = Core.start_page;
				} else {
					hash = 'home';
				}
			} else {
				hash = hash.substring(1);
			}
			if (!(hash in modules)) {
				console.log('没有对应的模块id的section:' + hash);
				hash = 'home';
			}
			core.hide();

			module = modules[hash];
			core.cur_mod = hash;
			if (params && params.indexOf('force') != -1) {
				if (module.isDef) {
					module.show(false); // 强制更新面板
				} else { // 列表更新
					//Core.showTips('update list');
					module.$c.find('ol').empty();
					module.show();
				}
			} else {
				module.show();
			}
		}
		// 监听hashChange事件
		var lochash = location.hash.split('&')[0];
		var mod = null;
		if (lochash == '') {
			if (Core.start_page) {
				mod = Core.start_page;
			} else {
				mod = 'home';
			}

			location.hash = "#" + mod;
		} else {
			mod = lochash.substring(1);
           
			onhashchange();
		}

		// if(Core.isIos){
			//ios下,不需要自动登录和加载完成消息
			// return;
		// }
		//新版sdk,支持直接登录、加载后回调
		if (Core.Data.open_id && Core.Data.open_id != '' && $('#login').length != 0 && Core.Data.auto_login == 1 && Core.Data.is_guest == 0) {
			//登录模块,判定可否自动登录
			var d = new Date().getTime();
			var auto = localStorage.getItem('auto');
			var auto_time = parseInt(localStorage.getItem('auto_time'));
			if (auto && auto == 'true') {
				if ((d - auto_time) > 600000) {
					//距离上次登录10分钟,尝试自动登录
					localStorage.setItem('auto_time', d);
					core.finishLogin();
				} else {
					//距离上次登录,小于10分钟,手动登录
					localStorage.setItem('auto_time', d);
					core.loadFinished();
				}
			} else {
				//自动登录
				localStorage.setItem('auto', true);
				localStorage.setItem('auto_time', d);
				Core.finishLogin();
			}
		} else {
			if ($('#login').length != 0) {
				//第一次没有登录,进入手动登录,写入信息
				var f = new Date().getTime();
				localStorage.setItem('auto', true);
				localStorage.setItem('auto_time', f);
			}
			core.loadFinished();
		}
	}
	
	$(function() {
		// 调整页面高度适应
		var page_w, page_h, tag, c_w, c_h;
		var ratio = window.devicePixelRatio;
		if (Core.sdk_h) {
			//成功匹配屏幕大小, 直接计算屏幕高度
			if (ratio != Core.ratio) {
				page_h = Core.sdk_h * (Core.ratio / ratio);
			} else {
				page_h = Core.sdk_h;
			}
			//alert('sdk:' + page_h + ':' + ratio + ':' + Core.ratio +  ':' + Core.ios_w, true, 5000); 
		} else {
			//旧版sdk,只能通过高度取值
			page_w = $(window).width();
			page_h = $(window).height();
			c_w = document.documentElement.clientWidth;
			c_h = document.documentElement.clientHeight;
			if (c_w && c_w > page_w) page_w = c_w;
			if (c_h && c_h > page_h) page_h = c_h;
			//Core.showTips(page_h + ':' + ratio, true, 5000); 
			if (page_h < 450 && page_w == 450) {
				page_h = 450;
			}
		}
		//Core.showTips('page_h:' + page_h + ';sdk_h:' + Core.sdk_h, true, 50000);
		//$('body').height(page_h);
		if(Core.isIos && $(window).width() < $('body').width()){
			$('meta[name="viewport"]').attr('content','width=' + $('body').width() + ',maximum-scale=0.5,user-scalable=no');
		}
		// var x = (page_h - 76) / page_h * 100 + '%';
		// $('<style>.container{height:' + x + ';}</style>').appendTo($('body'));

		// setTimeout(function(){
		// 	Core.showTips('page-w:' + $('body').width() + ';page-h:' + $('body').height() + 'ratio:' + core.ratio);
		// }, 2000);
	});
})(window, Core);


/**
 * 基础模块
 */
Module = function(modId) {
	this.isDef = null;
	this.isAuth = false;
	this.isReplace = false;	//模块内容部分信息需要在显示前替换,如login模块的app_id替换
	this.moduleId = modId;
	this.$c = $('#' + modId);
	this.isValid = false;
}
Module.prototype.init = function() {
	if (this.$c.attr('data-attr') == 'dot') {
		this.isDef = true;
	} else {
		this.isValid = true;
	}
	if(this.$c.attr('data-replace') == '1'){
		this.isReplace = true;
	}
	if (this.$c.attr('data-auth') == '1') {
		this.isAuth = true;
	}
	if (this.bind && typeof this.bind == 'function')
		this.bind();
}

Module.prototype.show = function(valid) {
	if (valid != undefined)
		this.isValid = valid;
    if (this.isAuth && !Core.isAuths()) {
        if (location.hash.indexOf('login') == -1) {
            Core.isFresh ? Core.showMod('login') : Core.showMod('login');
            return;
        }
    }
	if (!this.isDef) {
		if(this.isReplace){
			//模块第一次显示,需要替换部分未知变量。
			this.$c.find('[data-replace]').each(function(index){
				var $elem = $(this), val;
				var attr = $elem.attr('data-replace');
				var oldValue = $elem.attr(attr);
				var arrays = /\{([\w]+)\}/.exec(oldValue);
				if(arrays.length == 2 && arrays[1] in Core.Data){
					val = Core.Data[arrays[1]];
					var newValue = oldValue.replace(arrays[0], val)
					$elem.attr(attr, newValue);
				} else {
					console.log('can not find variable in:' + oldValue);
				}
			});
			//只需在模块第一次显示时替换变量。
			this.isReplace = false;
		}
		Core.show_anim(this.$c);
	} else {
		if (this.isValid) {
			Core.show_anim(this.$c);
		} else {
			if (!this.tpl)
				this.tpl = doT.template(Core.getTemplate(this.moduleId));
			this.$c.html(this.tpl());
			Core.show_anim(this.$c);
			this.isValid = true;
		}
	}
	if (this.show_after && typeof this.show_after == 'function')
		this.show_after();
}