var storage = new StorageHelper(); var ClientBase = function () { var clientId = $.cookie("AccessClientId"); this.Config = { appid: "td6ef75dce0add596f", isDebug: true, imgService: "", secret_key: "TDCore0ed384fee930fee6ba77e0632c06afa9", wechatLoginService: "http://zhaocc.m.test.ynsite.com", thisService:"http://zhaocc.m.test.ynsite.com" } var config = this.Config; var clientIP = decodeURIComponent($.cookie("ClientIP")); var token = ""; this.GetAccessToken = function () { if (token == undefined || token == "") { token = storage.getLocalStorage("AccessToken"); } if (token == undefined || token == "") { token = $.cookie("AccessToken"); if (token != undefined && token.length > 0) { this.SetAccessToken(token); } } return token; } this.SetAccessToken = function (value) { token = value; storage.setLocalStorage("AccessToken", value); $.cookie("AccessToken", value, { path: "/", expires: 7 }); } this.UserLogined = function () { token = this.GetAccessToken(); if (token != undefined && token.length > 0) { return true; } return false; } 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.GetClientIP = function () { return clientIP; } this.SetClientIP = function (value) { clientIP = value; $.cookie("ClientIP", value, { path: "/", expires: 7 }); } this.CombinaImg = function (path, width, height, defaultImg) { if (path == undefined || path == "") { if (defaultImg != undefined) { return defaultImg; } return "/res/images/nopic.png"; } if (path.indexOf("http") == 0) { return path; } if (path.indexOf("/upload/") == 0) { if (width != undefined && height != undefined) { if (path.indexOf("/cutthumbs/") > 0) { return this.Config.imgService + path; } if (path.indexOf("/upload/") >= 0) { 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.Config.imgService + path; } else { return this.Config.imgService + path; } } else { return path; } } this.GetSpacePath = function () { var p = ""; var t = this.GetAccessToken(); if (t == undefined || t == "") { return p; } try { var tt = $.base64.decode(t); tt = tt.replace(config.secret_key, ""); var ps = JSON.parse(tt); if (ps != undefined) { p = "user/" + ps.ID; } } catch (e) { } return p } 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.SendApiData = function (apiName, data, successCallBack, failCallBack, errorCallBack, sync) { // 成员函数 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; } addToJson(data, "access_token", this.GetAccessToken()); /*准备请求数据 */ var reqData = new Object(); reqData.in_appid = config.appid; 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); if (config.isDebug) { addToJson(reqData, "params", data); } var url = apiName; var base = this; $.ajax({ url: url, data: JSON.stringify(reqData), type: "post", headers: headers, dataType: 'json', crossDomain: false, async: sync, beforeSend: function () { }, success: function (response, textStatus) { //console.log(response); if (config.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 == 200000) {//请求通过 if (response.out_status == 1 && response.out_refusertoken == 1) { base.SetAccessToken(response.out_usertoken); } if (response.out_status == 1 && successCallBack != undefined) { var obj = JSON.parse(response.out_bodydata); if (config.isDebug) { if (response.execute_time != undefined) { obj.execute_time = response.execute_time; } } if (obj.msg == undefined) { obj.msg = response.out_msg; } successCallBack(obj, reqData); } else if (response.out_status == 1) { tips("tips", "操作成功!", 3); } else if (failCallBack != undefined) { var obj = JSON.parse(response.out_bodydata); if (response.execute_time != undefined) { obj.execute_time = response.execute_time; } if (obj.msg == undefined) { obj.msg = response.out_msg; } failCallBack(obj); } else { var msg = response.out_msg; if (msg == undefined || msg == "") { msg = "无返回数据"; } tips("tips", msg, 3); } } else if (response.out_code == 800000) {//未登录 //底部对话框 funTipLogin(); } else if (response.out_code == 404000) { if (msg == undefined || msg == "") { msg = "数据不存在!"; } tips("tips", msg, 3, function () { goback(); }); } else { if (failCallBack != undefined) { var obj = JSON.parse(response.out_bodydata); if (response.execute_time != undefined) { obj.execute_time = response.execute_time; } if (obj.msg == undefined) { obj.msg = response.out_msg; } failCallBack(obj); } else { var msg = response.out_msg; if (msg == undefined || msg == "") { msg = "无返回数据"; } tips("tips", msg, 3); } } }, error: function (XMLHttpRequest, textStatus, errorThrown) { if (XMLHttpRequest.responseJSON != undefined) { if (config.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) { if (XMLHttpRequest.status == 404) { tips("error", "状态:" + XMLHttpRequest.status + ";出错提示:" + JSON.parse(XMLHttpRequest.responseText).Message, 10); } else if (XMLHttpRequest.status == 500) { tips("error", "状态:" + XMLHttpRequest.status + ";出错提示:" + XMLHttpRequest.responseText.out_msg, 10); } else { var msg = XMLHttpRequest.out_msg; if (msg == undefined || msg == "") { msg = "数据错误"; } tips("error", msg, 10); } } } } }) } } var Users = function () { var token = new ClientBase().GetAccessToken(); this.USER_ID = ""; if (token == undefined || token == "") { this.USER_ID = ""; } } function funTipLogin() { layer.open({ anim: 'up', content: '亲,请先登录哦' , btn: ['确定', '取消'] //, skin: 'footer' , yes: function (index) { window.location.href = "/Account/Login"; layer.closeAll(); }, no: function () { layer.closeAll(); } }); }