/// var ClientBase = function () { var isDebug = getUrlParameter("debug", "0", $('#jsClientBase').attr('src')); if (isDebug == "1") { isDebug = true; } else { isDebug = false; } var clientId = $.cookie("AccessClientId"); var accessToken = $.cookie("AccessToken"); var accessTokenExpire = $.cookie("AccessTokenExpire"); var apiHost = ""; var clientIP = decodeURIComponent($.cookie("ClientIP")); var storage = new StorageHelper(); var _imgServicePath = getUrlParameter("imgCdn", "0", $('#jsClientBase').attr('src')); this.GetImgService = function () { return _imgServicePath; } this.CombinaImg = function (path, width, height, defaultImg) { if (path == undefined || path == "") { if (defaultImg != undefined) { return defaultImg; } return "/res/nopic_4_3.png"; } if (path.indexOf("http") == 0) { return path; } if (width != undefined && height != undefined) { if (path.indexOf("/cutthumbs/") > 0) { return this.GetImgService() + path; } path = path.replace("/upload/", "/upload/cutthumbs/"); var index = path.lastIndexOf("/"); var front = path.substring(0, index + 1); var back = path.substring(index + 1); path = front + "thumb_" + width + "_" + height + "_" + back; return this.GetImgService() + path; } else { return this.GetImgService() + path; } } this.GetSpacePath = function () { var _path = storage.getLocalStorage("SpacePath"); if (_path == undefined) { _path = "unknow"; } return _path; } this.GetAccessClientId = function () { if (clientId == undefined || clientId == "") { clientId = storage.getLocalStorage("AccessClientId"); } return clientId; } this.SetAccessClientId = function (value) { clientId = value; storage.setLocalStorage("AccessClientId", value); $.cookie("AccessClientId", value, { path: "/", expires: 7 }); } this.GetAccessToken = function () { if (accessToken == undefined || accessToken == "") { accessToken = storage.getLocalStorage("AccessToken"); } return accessToken; } this.SetAccessToken = function (value) { accessToken = value; storage.setLocalStorage("AccessToken", value); $.cookie("AccessToken", value, { path: "/", expires: 7 }); } this.GetAccessTokenExpire = function () { if (accessTokenExpire == undefined || accessTokenExpire == "") { accessTokenExpire = storage.getLocalStorage("AccessTokenExpire"); } return accessTokenExpire; } this.SetAccessTokenExpire = function (value) { accessTokenExpire = value; storage.setLocalStorage("AccessTokenExpire", value); $.cookie("AccessTokenExpire", value, { path: "/", expires: 7 }); } this.GetServerTimeCookie = function () { return $.cookie("ServerTime"); } this.SetServerTimeCookie = function (value) { $.cookie("ServerTime", value, { path: "/", expires: 7 }); } this.GetApiHost = function () { return apiHost; } this.GetClientIP = function () { return clientIP; } this.SetClientIP = function (value) { clientIP = value; $.cookie("ClientIP", value, { path: "/", expires: 7 }); } this.GetRandomCode = function () { var date = new Date(); var expireTime = date.getTime() + 60 * 1000; var array = storage.getLocalStorage("apiRandomCode"); if (array == undefined) { array = new Array(); } //移除过保护器的数据; var arr = new Array(); for (var i = 0; i < array.length; i++) { var item = array[i]; if (item.expireTime > expireTime - 60 * 1000) { arr.push(item); } } var random; var flag = true; while (flag) { random = Math.ceil(Math.random() * 1000000); flag = false; for (var i = 0; i < arr.length; i++) { var value = arr[i]; if (value.key == random) { flag = true; } } } var obj = new Object(); obj.key = random; obj.expireTime = expireTime; arr.push(obj); storage.setLocalStorage("apiRandomCode", arr); return obj.key; } this.getNewAccessClientIdAndAccessToken = function () { // 成员函数 var reqData = new Object(); reqData.in_clientid = ""; reqData.in_random = this.GetRandomCode(); reqData.in_timestamp = new Date().getTime(); var data = new Object(); data.userName = "ynyes3157517"; data.randomCode = this.GetRandomCode(); data.password = md5(data.userName + "ynyes3157517" + data.randomCode); reqData.in_bodydata = JSON.stringify(data); reqData.in_sign = data.password; var base = this; var headers = { "Content-Type": "application/json;charset=utf-8" }; $.ajax({ type: "post", url: "/api/webApi/Init/GetToken", dataType: "json", data: JSON.stringify(reqData), headers: headers, async: false, success: function (response) { if (response.out_code == 200000) { var data = JSON.parse(response.out_bodydata); if (data.status == 1) { base.SetAccessClientId(data.data.AccessClientId); base.SetAccessToken(data.data.AccessToken); base.SetAccessTokenExpire(data.data.AccessTokenExpire); } else { //window.location.reload(); } } } }) } this.refreshAccessToken = function () {// 成员函数 /*准备请求数据 */ var reqData = new Object(); reqData.in_clientid = this.GetAccessClientId(); if (reqData.in_clientid == undefined || reqData.in_clientid == "") { this.getNewAccessClientIdAndAccessToken(); return; } reqData.in_random = this.GetRandomCode(); reqData.in_timestamp = new Date().getTime(); var random = this.GetRandomCode(); var signData = this.GetAccessClientId() + random; var data = new Object(); data.sign = md5(signData); data.randomCode = random; reqData.in_sign = data.sign; reqData.in_bodydata = JSON.stringify(data); var apiName = "/api/webApi/Init/RefreshToken"; var url = this.GetApiHost() + apiName; var base = this; $.ajax({ type: "post", url: "/api/webApi/Init/RefreshToken", dataType: "json", data: JSON.stringify(reqData), async: false, success: function (response) { if (response.out_code == 200000) { var data = JSON.parse(response.out_bodydata); if (data.status == 1) { base.SetAccessClientId(data.data.AccessClientId); base.SetAccessToken(data.data.AccessToken); base.SetAccessTokenExpire(data.data.AccessTokenExpire); debugLog($.cookie("AccessClientId")); debugLog($.cookie("AccessToken")); //var clientId = $.cookie("AccessClientId"); //var accessToken = $.cookie("AccessToken"); } else { //window.location.reload(); } } } }) } this.checkAccessClientIdIsNeedToRefresh = function () { // 成员函数 var tokenExpireTime = this.GetAccessTokenExpire(); var serverTime = this.GetServerTimeCookie(); if (this.GetAccessClientId() == undefined || this.GetAccessClientId() == "" || tokenExpireTime < serverTime) { return true; } else { //将cookie同步到 storage中; this.SetAccessClientId(this.GetAccessClientId()); this.SetAccessToken(this.GetAccessToken()); this.SetAccessTokenExpire(this.GetAccessTokenExpire()); return false; } } this.SendApiData = function (apiName, data, successCallBack, failCallBack, errorCallBack, sync) { // 成员函数 //this.refreshAccessToken(); //if (this.checkAccessClientIdIsNeedToRefresh()) { // //accessToken 失效 // this.refreshAccessToken(); //} if (data == undefined) { data = {}; } if (data.agent == undefined) { addToJson(data, "agent", navigator.userAgent); } if (data.ip == undefined) { addToJson(data, "ip", this.GetClientIP()); } var headers = { "Content-Type": "application/json;charset=utf-8" }; //var headers = { "Content-Type": "application/x-www-form-urlencoded;" }; if (data.headers != undefined) { headers = data.headers; addToJson(data, "headers", undefined); } if (sync == undefined || sync == null) { sync = true; } /*准备请求数据 */ var reqData = new Object(); reqData.in_bodydata = JSON.stringify(data); reqData.in_clientid = this.GetAccessClientId(); reqData.in_random = this.GetRandomCode(); reqData.in_timestamp = new Date().getTime(); //var signData = ""; //for (var i in reqData) { // signData += i + '=' + reqData[i] + "&"; //} //signData = signData.substring(0, signData.length - 1); //signData = this.GetAccessToken() + signData + this.GetAccessToken(); //reqData.in_sign = md5(signData); reqData.lon = "102.709664"; reqData.lat = "25.020279"; if (isDebug) { addToJson(reqData, "params", data); } var url = this.GetApiHost()+ apiName; var base = this; $.ajax({ url: url, data: JSON.stringify(reqData), type: "post", headers: headers, dataType: 'json', crossDomain: false, async: sync, success: function (response, textStatus) { if (isDebug) { var obj = JSON.parse(response.out_bodydata); if (response.execute_time != undefined) { obj.execute_time = response.execute_time; } debugLog(obj); } if (response.out_code == 200001) { //accessClientId 不存在 //此处有坑 需要清除本地存储的用户相关信息 base.getNewAccessClientIdAndAccessToken(); //重新发起请求 data.headers = headers; base.SendApiData(apiName, data, successCallBack); } else if (response.out_code == 200008 || response.out_code == 200200) { //accessToken 失效 base.refreshAccessToken(); //重新发起请求 data.headers = headers; base.SendApiData(apiName, data, successCallBack); } else if (response.out_code == 200000) {//请求通过 if (response.out_status == 1 && successCallBack != undefined) { var obj = JSON.parse(response.out_bodydata); if (isDebug) { if (response.execute_time != undefined) { obj.execute_time = response.execute_time; } } successCallBack(obj, reqData); } else if (response.out_status == 1) { tips("tips", "操作成功!", 3); } else if (failCallBack != undefined) { failCallBack(JSON.parse(response.out_bodydata), response.out_msg); } else { tips("error", response.out_msg, 3); } } else if (response.out_code == 800000) {//未登录 layer.confirm('亲,请先登录哦', { title: "提示", btn: ['确定', '取消'] //按钮 }, function () { window.location.href = "/Account/Login"; layer.closeAll(); }, function () { layer.closeAll(); }); } else { if (failCallBack != undefined) { failCallBack(JSON.parse(response.out_bodydata)); } else { tips("tips", response.out_msg, 3); } } }, error: function (XMLHttpRequest, textStatus, errorThrown) { if (XMLHttpRequest.responseJSON != undefined) { if (isDebug) { if (XMLHttpRequest.status == 404) { tips("error", "状态:" + XMLHttpRequest.status + ";出错提示:" + XMLHttpRequest.responseJSON.MessageDetail, 10); } else if (XMLHttpRequest.status == 500) { tips("error", "状态:" + XMLHttpRequest.status + ";出错提示:" + XMLHttpRequest.responseJSON.out_msg, 10); } } if (errorCallBack != undefined) { errorCallBack(XMLHttpRequest, textStatus, errorThrown); } return; } if (errorCallBack != undefined) { errorCallBack(XMLHttpRequest, textStatus, errorThrown); } else { if (isDebug) { tips("error", XMLHttpRequest.out_msg, 10); } } } }) } }