190 lines
5.3 KiB
JavaScript
190 lines
5.3 KiB
JavaScript
;(function ($) {
|
||
"use strict";
|
||
|
||
// 默认api配置
|
||
var defaults = {
|
||
api_url: '',
|
||
data: {
|
||
method: '',
|
||
format: 'json',
|
||
charset: 'utf-8',
|
||
version: '1.0',
|
||
user_auth_token: '',
|
||
biz_content: null
|
||
},
|
||
cacheMethods: []
|
||
};
|
||
|
||
// 缓存
|
||
var Cache = {};
|
||
|
||
// 初始化函数
|
||
var init = function () {
|
||
|
||
// 通过域名判断环境
|
||
switch (window.location.hostname) {
|
||
case 'localhost':
|
||
defaults.api_url = 'http://localhost/proxy/trunk/api/Index.php';
|
||
break;
|
||
case 'proxytest.tscce.cn':
|
||
defaults.api_url = 'https://proxytest.tscce.cn/api/Index.php';
|
||
break;
|
||
default:
|
||
defaults.api_url = 'https://dlapiv3.tscce.cn/Index.php';
|
||
}
|
||
|
||
if($.getPageName() === 'welcome.html')
|
||
{
|
||
var adminInfo = sessionStorage.getItem('adminInfo');
|
||
if (adminInfo) Cache.adminInfo = JSON.parse(adminInfo);
|
||
}
|
||
|
||
|
||
|
||
};
|
||
|
||
$.extend(
|
||
{
|
||
api: function (obj) {
|
||
// 判断是否有缓存
|
||
var tmp = obj.data.method;
|
||
if(Cache[tmp]) {
|
||
console.log(tmp, 'from cache');
|
||
obj.success && obj.success(Cache[obj.data.method]);
|
||
return;
|
||
}
|
||
|
||
var data = $.extend({}, defaults.data, obj.data);
|
||
//console.log('merge data', data);
|
||
var token = sessionStorage.getItem("token");
|
||
data.user_auth_token = token;
|
||
|
||
if(Cache.adminInfo) data.biz_content = $.extend({}, data.biz_content, Cache.adminInfo);
|
||
|
||
if(obj.isLoading) $.loading();
|
||
$.ajax(
|
||
{
|
||
url: defaults.api_url,
|
||
type: 'POST',
|
||
//dataType: 'json',
|
||
//async: !!obj.async,
|
||
data: data,
|
||
timeout: 10000,
|
||
success: function (r) {
|
||
try {
|
||
r = (typeof r === 'string') ? JSON.parse(decodeURIComponent(r)) : r;
|
||
if (typeof r !== 'object') throw {code: 1, msg: '数据异常'};
|
||
|
||
console.log(data.method, r);
|
||
|
||
if (r.user_auth_token) {
|
||
try {
|
||
sessionStorage.setItem('token', r.user_auth_token);
|
||
} catch (e) {
|
||
alert("如果此页面无法正常显示,请关闭浏览器的隐身模式,再打开此页面。");
|
||
}
|
||
}
|
||
|
||
if (r.retcode !== 0) throw {code: r.retcode, msg: r.retinfo};
|
||
|
||
if (r.biz_content.state && parseInt(r.biz_content.state) !== 0) throw {
|
||
code: r.biz_content.state,
|
||
msg: r.biz_content.error
|
||
};
|
||
|
||
// 判断是否要缓存数据
|
||
if($.inArray(data.method, defaults.cacheMethods) !== -1) {
|
||
Cache[data.method] = r.biz_content;
|
||
}
|
||
|
||
obj.success && obj.success(r.biz_content);
|
||
} catch (e) {
|
||
if(obj.error)
|
||
obj.error(e);
|
||
else
|
||
$.alert(e.msg);
|
||
}
|
||
},
|
||
complete: function (r) {
|
||
if(obj.isLoading && typeof Cache.loadingLayerIndex === 'number') layer.close(Cache.loadingLayerIndex);
|
||
obj.complete && obj.complete(r);
|
||
},
|
||
error: function (xhr, textStatus) {
|
||
alert('网络较慢!');
|
||
}
|
||
}
|
||
)
|
||
|
||
},
|
||
alert: function (msg) {
|
||
if(typeof layer === 'object')
|
||
layer.open({content: msg, btn: '我知道了'});
|
||
else
|
||
alert(msg);
|
||
//layer ? layer.open({content: msg, btn: '我知道了'}) : alert(msg);
|
||
},
|
||
confirm: function (func) {
|
||
var $obj = {
|
||
content: "确认操作?",
|
||
btn: ['确认', '取消'],
|
||
yes: function (i) {
|
||
layer.close(i);
|
||
func();
|
||
}
|
||
};
|
||
layer.open($obj);
|
||
},
|
||
loading: function (func) {
|
||
Cache.loadingLayerIndex = layer.open(
|
||
{
|
||
type: 2,
|
||
content: '加载中',
|
||
shadeClose: false,
|
||
success: function (e) {
|
||
func && func(e);
|
||
}
|
||
});
|
||
},
|
||
store: function (name, obj) {
|
||
sessionStorage.setItem(name, JSON.stringify(obj));
|
||
},
|
||
isRightId: function (playerId) {
|
||
return /^\d{6,10}$/.test(playerId);
|
||
},
|
||
isWeiXin: function () {
|
||
if(typeof Cache.isWeiXin === 'boolean') return Cache.isWeiXin;
|
||
var ua = navigator.userAgent.toLowerCase().match("micromessenger");
|
||
if (ua && ua[0] === "micromessenger") {
|
||
Cache.isWeiXin = true;
|
||
return true;
|
||
}
|
||
|
||
Cache.isWeiXin = false;
|
||
return false;
|
||
},
|
||
getQueryObj: function () {
|
||
if(Cache.get) return Cache.get;
|
||
var tmp = window.location.search.substr(1).split('&');
|
||
var res = {};
|
||
for (var i = 0; i < tmp.length; i++) {
|
||
var arr = tmp[i].split('=');
|
||
res[arr[0]] = arr[1];
|
||
}
|
||
|
||
if(!res.agentid || !res.channelid) throw {code: 1, msg: '链接中没有agentid或channelid参数,请确认链接是否正确'};
|
||
|
||
|
||
Cache.get = res;
|
||
return res;
|
||
},
|
||
getPageName: function () {
|
||
if(Cache.pageName) return Cache.pageName;
|
||
var name = window.location.pathname.split('/').pop();
|
||
Cache.pageName = name;
|
||
return name;
|
||
}
|
||
}
|
||
);
|
||
|
||
init();
|
||
})(jQuery); |