var Package = function(url, options) { this.defaults = { //默认配置,可以通过option覆盖 URL: url || localStorage.getItem("url"), token: "", //存储token }; this.settings = Object.assign({}, this.defaults, options); //覆盖默认配置操作 }; Package.prototype = { firstLogin: function(loginName) { let _t = this; return new Promise((resolve, reject) => { $.ajax({ type: "POST", url: this.settings.URL + "/videoService/accounts/authorize", contentType: "application/json", //如果提交的是json数据类型,则必须有此参数,表示提交的数据类型 dateType: "json", data: JSON.stringify({ userName: this.settings.username, clientType: "winpc", ipAddress: "", //可不传 pid: 2548, //可不传 }), beforeSend: function(xhr) { xhr.setRequestHeader("X-Subject-Token", ""); xhr.setRequestHeader( "Content-Type", "application/json;charset=UTF-8" ); }, success: function(jsonResult) { resolve(jsonResult); }, error: function(data1) { reject(JSON.parse(data1.responseText)); }, }); }); }, keepalive: function() { let _t = this; if (localStorage.getItem("token")) { $.ajax({ type: 'PUT', url: this.settings.URL + '/videoService/accounts/token/keepalive', contentType: 'application/json', //如果提交的是json数据类型,则必须有此参数,表示提交的数据类型 dateType: 'json', data: JSON.stringify({ token: localStorage.getItem('token'), duration: 86400, }), beforeSend: function(xhr) { xhr.setRequestHeader( 'X-Subject-Token', localStorage.getItem('token'), ); xhr.setRequestHeader( 'Content-Type', 'application/json;charset=UTF-8', ); }, success: function(jsonResult) { // setTimeout(function() { // _t.keepalive(); // }, 20000); }, error: function(data) { localStorage.removeItem('token'); _t.login(); }, }); } else { setTimeout(() => { _t.login(); }, 1000 * (_t.logincount++)); } }, logincount: 0, login: async function() { let _t = this; var loginName = this.settings.username; var password = this.settings.password; localStorage.setItem("password", password); localStorage.setItem('url', _t.settings.URL); try { if (window.dologin) { window.dologin(); } } catch (error) {} console.log("_-------") _t.keepalive(); // _t.firstLogin(loginName).catch((firstLoginRes) => { // //console.log("firstLoginRes: ", firstLoginRes); // var md1 = hex_md5(password); //第1次加密 // var md2 = hex_md5(loginName + md1); //第2次加密 // var md3 = hex_md5(md2); //第3次加密 // var md4 = hex_md5(loginName + ":" + firstLoginRes.realm + ":" + md3); //第4次加密 // var signature = hex_md5(md4 + ":" + firstLoginRes.randomKey); //第5次加密 // $.ajax({ // type: "POST", // url: this.settings.URL + "/videoService/accounts/authorize", // contentType: "application/json", //如果提交的是json数据类型,则必须有此参数,表示提交的数据类型 // dateType: "json", // async: false, // data: JSON.stringify({ // userName: loginName, // signature: signature, // randomKey: firstLoginRes.randomKey, // encryptType: "MD5", // clientType: "winpc", // pid: 2548, // expiredTime:86400, // }), // beforeSend: function(xhr) { // xhr.setRequestHeader("X-Subject-Token", ""); // xhr.setRequestHeader( // "Content-Type", // "application/json;charset=UTF-8" // ); // }, // success: function(jsonResult) { // if (typeof jsonResult === "string") { // if (jsonResult && JSON.parse(jsonResult).token) { // localStorage.setItem("token", JSON.parse(jsonResult).token); // localStorage.setItem("userId", JSON.parse(jsonResult).userId); // localStorage.setItem("url", _t.settings.URL); // localStorage.setItem("userName", loginName); // // _t.settings.token = JSON.parse(jsonResult).token; // console.log("localStorage-->", localStorage); // _t.keepalive(); // // window.location.href = "./pages/菜单.html"; // } // } else { // if (jsonResult && jsonResult.token) { // localStorage.setItem("token", jsonResult.token); // localStorage.setItem("userId", jsonResult.userId); // localStorage.setItem("url", _t.settings.URL); // localStorage.setItem("userName", loginName); // // _t.settings.token = JSON.parse(jsonResult).token; // console.log("localStorage-->", localStorage); // _t.keepalive(); // // window.location.href = "./pages/菜单.html"; // } // } // }, // error: function(data) { // let msg = JSON.parse(data.responseText).message; // $(".loginTip .error-wrap .error").html(msg); // $(".loginTip").show(); // }, // }); // }); }, signOut() { let _t = this; $.ajax({ type: "POST", url: _t.settings.URL + "/videoService/accounts/unauthorize", contentType: "application/json", //如果提交的是json数据类型,则必须有此参数,表示提交的数据类型 async: false, dateType: "json", data: JSON.stringify({ userName: localStorage.getItem("userName"), token: localStorage.getItem("token"), }), beforeSend: function(xhr) { xhr.setRequestHeader("X-Subject-Token", localStorage.getItem("token")); xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); //xhr.setRequestHeader("Origin",null); }, success: function(jsonResult) { localStorage.clear(); window.location.href = "../login.html"; }, error: function(data) { //obj if (JSON.parse(data.responseText).message === "invalid token") { window.location.href = "../login.html"; } }, }); }, };