login.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. var Package = function(url, options) {
  2. this.defaults = {
  3. //默认配置,可以通过option覆盖
  4. URL: url || localStorage.getItem("url"),
  5. token: "", //存储token
  6. };
  7. this.settings = Object.assign({}, this.defaults, options); //覆盖默认配置操作
  8. };
  9. Package.prototype = {
  10. firstLogin: function(loginName) {
  11. let _t = this;
  12. return new Promise((resolve, reject) => {
  13. $.ajax({
  14. type: "POST",
  15. url: this.settings.URL + "/videoService/accounts/authorize",
  16. contentType: "application/json", //如果提交的是json数据类型,则必须有此参数,表示提交的数据类型
  17. dateType: "json",
  18. data: JSON.stringify({
  19. userName: this.settings.username,
  20. clientType: "winpc",
  21. ipAddress: "", //可不传
  22. pid: 2548, //可不传
  23. }),
  24. beforeSend: function(xhr) {
  25. xhr.setRequestHeader("X-Subject-Token", "");
  26. xhr.setRequestHeader(
  27. "Content-Type",
  28. "application/json;charset=UTF-8"
  29. );
  30. },
  31. success: function(jsonResult) {
  32. resolve(jsonResult);
  33. },
  34. error: function(data1) {
  35. reject(JSON.parse(data1.responseText));
  36. },
  37. });
  38. });
  39. },
  40. keepalive: function() {
  41. let _t = this;
  42. if (localStorage.getItem("token")) {
  43. $.ajax({
  44. type: "PUT",
  45. url: this.settings.URL + "/videoService/accounts/token/keepalive",
  46. contentType: "application/json", //如果提交的是json数据类型,则必须有此参数,表示提交的数据类型
  47. dateType: "json",
  48. data: JSON.stringify({
  49. token: localStorage.getItem("token"),
  50. duration: 120,
  51. }),
  52. beforeSend: function(xhr) {
  53. xhr.setRequestHeader(
  54. "X-Subject-Token",
  55. localStorage.getItem("token")
  56. );
  57. xhr.setRequestHeader(
  58. "Content-Type",
  59. "application/json;charset=UTF-8"
  60. );
  61. },
  62. success: function(jsonResult) {
  63. // setTimeout(function() {
  64. // _t.keepalive();
  65. // }, 20000);
  66. },
  67. error: function(data) {
  68. _t.login()
  69. },
  70. });
  71. }
  72. },
  73. login: async function() {
  74. let _t = this;
  75. var loginName = this.settings.username;
  76. var password = this.settings.password;
  77. localStorage.setItem("password", password);
  78. _t.firstLogin(loginName).catch((firstLoginRes) => {
  79. //console.log("firstLoginRes: ", firstLoginRes);
  80. var md1 = hex_md5(password); //第1次加密
  81. var md2 = hex_md5(loginName + md1); //第2次加密
  82. var md3 = hex_md5(md2); //第3次加密
  83. var md4 = hex_md5(loginName + ":" + firstLoginRes.realm + ":" + md3); //第4次加密
  84. var signature = hex_md5(md4 + ":" + firstLoginRes.randomKey); //第5次加密
  85. $.ajax({
  86. type: "POST",
  87. url: this.settings.URL + "/videoService/accounts/authorize",
  88. contentType: "application/json", //如果提交的是json数据类型,则必须有此参数,表示提交的数据类型
  89. dateType: "json",
  90. async: false,
  91. data: JSON.stringify({
  92. userName: loginName,
  93. signature: signature,
  94. randomKey: firstLoginRes.randomKey,
  95. encryptType: "MD5",
  96. clientType: "winpc",
  97. pid: 2548,
  98. expiredTime:86400,
  99. }),
  100. beforeSend: function(xhr) {
  101. xhr.setRequestHeader("X-Subject-Token", "");
  102. xhr.setRequestHeader(
  103. "Content-Type",
  104. "application/json;charset=UTF-8"
  105. );
  106. },
  107. success: function(jsonResult) {
  108. if (typeof jsonResult === "string") {
  109. if (jsonResult && JSON.parse(jsonResult).token) {
  110. localStorage.setItem("token", JSON.parse(jsonResult).token);
  111. localStorage.setItem("userId", JSON.parse(jsonResult).userId);
  112. localStorage.setItem("url", _t.settings.URL);
  113. localStorage.setItem("userName", loginName);
  114. // _t.settings.token = JSON.parse(jsonResult).token;
  115. console.log("localStorage-->", localStorage);
  116. _t.keepalive();
  117. // window.location.href = "./pages/菜单.html";
  118. }
  119. } else {
  120. if (jsonResult && jsonResult.token) {
  121. localStorage.setItem("token", jsonResult.token);
  122. localStorage.setItem("userId", jsonResult.userId);
  123. localStorage.setItem("url", _t.settings.URL);
  124. localStorage.setItem("userName", loginName);
  125. // _t.settings.token = JSON.parse(jsonResult).token;
  126. console.log("localStorage-->", localStorage);
  127. _t.keepalive();
  128. // window.location.href = "./pages/菜单.html";
  129. }
  130. }
  131. },
  132. error: function(data) {
  133. let msg = JSON.parse(data.responseText).message;
  134. $(".loginTip .error-wrap .error").html(msg);
  135. $(".loginTip").show();
  136. },
  137. });
  138. });
  139. },
  140. signOut() {
  141. let _t = this;
  142. $.ajax({
  143. type: "POST",
  144. url: _t.settings.URL + "/videoService/accounts/unauthorize",
  145. contentType: "application/json", //如果提交的是json数据类型,则必须有此参数,表示提交的数据类型
  146. async: false,
  147. dateType: "json",
  148. data: JSON.stringify({
  149. userName: localStorage.getItem("userName"),
  150. token: localStorage.getItem("token"),
  151. }),
  152. beforeSend: function(xhr) {
  153. xhr.setRequestHeader("X-Subject-Token", localStorage.getItem("token"));
  154. xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  155. //xhr.setRequestHeader("Origin",null);
  156. },
  157. success: function(jsonResult) {
  158. localStorage.clear();
  159. window.location.href = "../login.html";
  160. },
  161. error: function(data) {
  162. //obj
  163. if (JSON.parse(data.responseText).message === "invalid token") {
  164. window.location.href = "../login.html";
  165. }
  166. },
  167. });
  168. },
  169. };