abp.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. var abp = abp || {};
  2. (function () {
  3. /* Application paths *****************************************/
  4. // Current application root path (including virtual directory if exists).
  5. abp.appPath = abp.appPath || '/';
  6. /* AUTHORIZATION **********************************************/
  7. // Implements Authorization API that simplifies usage of authorization scripts generated by Abp.
  8. abp.auth = abp.auth || {};
  9. abp.auth.tokenCookieName = 'Abp.AuthToken';
  10. abp.auth.tokenHeaderName = 'Authorization';
  11. abp.auth.setToken = function (authToken, expireDate) {
  12. abp.utils.setCookieValue(abp.auth.tokenCookieName, authToken, expireDate, abp.appPath);
  13. };
  14. abp.auth.getToken = function () {
  15. return abp.utils.getCookieValue(abp.auth.tokenCookieName);
  16. }
  17. abp.auth.clearToken = function () {
  18. abp.auth.setToken();
  19. }
  20. /* UTILS ***************************************************/
  21. abp.utils = abp.utils || {};
  22. /**
  23. * Sets a cookie value for given key.
  24. * This is a simple implementation created to be used by ABP.
  25. * Please use a complete cookie library if you need.
  26. * @param {string} key
  27. * @param {string} value
  28. * @param {Date} expireDate (optional). If not specified the cookie will expire at the end of session.
  29. * @param {string} path (optional)
  30. */
  31. abp.utils.setCookieValue = function (key, value, expireDate, path) {
  32. var cookieValue = encodeURIComponent(key) + '=';
  33. if (value) {
  34. cookieValue = cookieValue + encodeURIComponent(value);
  35. }
  36. if (expireDate) {
  37. cookieValue = cookieValue + "; expires=" + expireDate.toUTCString();
  38. }
  39. if (path) {
  40. cookieValue = cookieValue + "; path=" + path;
  41. }
  42. document.cookie = cookieValue;
  43. };
  44. /**
  45. * Gets a cookie with given key.
  46. * This is a simple implementation created to be used by ABP.
  47. * Please use a complete cookie library if you need.
  48. * @param {string} key
  49. * @returns {string} Cookie value or null
  50. */
  51. abp.utils.getCookieValue = function (key) {
  52. var equalities = document.cookie.split('; ');
  53. for (var i = 0; i < equalities.length; i++) {
  54. if (!equalities[i]) {
  55. continue;
  56. }
  57. var splitted = equalities[i].split('=');
  58. if (splitted.length != 2) {
  59. continue;
  60. }
  61. if (decodeURIComponent(splitted[0]) === key) {
  62. return decodeURIComponent(splitted[1] || '');
  63. }
  64. }
  65. return null;
  66. };
  67. /**
  68. * Deletes cookie for given key.
  69. * This is a simple implementation created to be used by ABP.
  70. * Please use a complete cookie library if you need.
  71. * @param {string} key
  72. * @param {string} path (optional)
  73. */
  74. abp.utils.deleteCookie = function (key, path) {
  75. var cookieValue = encodeURIComponent(key) + '=';
  76. cookieValue = cookieValue + "; expires=" + (new Date(new Date().getTime() - 86400000)).toUTCString();
  77. if (path) {
  78. cookieValue = cookieValue + "; path=" + path;
  79. }
  80. document.cookie = cookieValue;
  81. }
  82. /* SECURITY ***************************************/
  83. abp.security = abp.security || {};
  84. abp.security.antiForgery = abp.security.antiForgery || {};
  85. abp.security.antiForgery.tokenCookieName = 'XSRF-TOKEN-VberAdmin';
  86. abp.security.antiForgery.tokenHeaderName = 'X-XSRF-TOKEN';
  87. abp.security.antiForgery.getToken = function () {
  88. return abp.utils.getCookieValue(abp.security.antiForgery.tokenCookieName);
  89. };
  90. /* Swagger */
  91. abp.swagger = abp.swagger || {};
  92. abp.swagger.addAuthToken = function () {
  93. var authToken = abp.auth.getToken();
  94. if (!authToken) {
  95. return false;
  96. }
  97. var cookieAuth = new SwaggerClient.ApiKeyAuthorization(abp.auth.tokenHeaderName, 'Bearer ' + authToken, 'header');
  98. swaggerUi.api.clientAuthorizations.add('bearerAuth', cookieAuth);
  99. return true;
  100. }
  101. abp.swagger.addCsrfToken = function () {
  102. var csrfToken = abp.security.antiForgery.getToken();
  103. if (!csrfToken) {
  104. return false;
  105. }
  106. var csrfCookieAuth = new SwaggerClient.ApiKeyAuthorization(abp.security.antiForgery.tokenHeaderName, csrfToken, 'header');
  107. swaggerUi.api.clientAuthorizations.add(abp.security.antiForgery.tokenHeaderName, csrfCookieAuth);
  108. return true;
  109. }
  110. function addAntiForgeryTokenToXhr(xhr) {
  111. var antiForgeryToken = abp.security.antiForgery.getToken();
  112. if (antiForgeryToken) {
  113. xhr.setRequestHeader(abp.security.antiForgery.tokenHeaderName, antiForgeryToken);
  114. }
  115. }
  116. function loginUserInternal(callback) {
  117. var usernameOrEmailAddress = document.getElementById('usernameOrEmailOrPhone').value;
  118. if (!usernameOrEmailAddress) {
  119. alert('需要用户名或电子邮件地址,请尝试使用有效值!');
  120. return false;
  121. }
  122. var password = document.getElementById('password').value;
  123. if (!password) {
  124. alert('需要密码,请尝试输入有效值!');
  125. return false;
  126. }
  127. var xhr = new XMLHttpRequest();
  128. xhr.onreadystatechange = function () {
  129. if (xhr.readyState === XMLHttpRequest.DONE) {
  130. if (xhr.status === 200) {
  131. var responseJSON = JSON.parse(xhr.responseText);
  132. var result = responseJSON.result;
  133. var expireDate = new Date(Date.now() + (result.expireInSeconds * 1000));
  134. abp.auth.setToken(result.accessToken, expireDate);
  135. callback();
  136. } else {
  137. alert('登录失败!');
  138. }
  139. }
  140. };
  141. xhr.open('POST', '/api/TokenAuth/Authenticate', true);
  142. //xhr.setRequestHeader('Abp.TenantId', tenantId);
  143. //xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  144. xhr.setRequestHeader('Content-type', 'application/json');
  145. addAntiForgeryTokenToXhr(xhr);
  146. xhr.send(
  147. JSON.stringify(
  148. { usernameOrEmailOrPhone: usernameOrEmailAddress, password: password }
  149. )
  150. );
  151. }
  152. abp.swagger.login = function (callback) {
  153. ////Get TenantId first
  154. //var tenancyName = document.getElementById('tenancyName').value;
  155. //if (tenancyName) {
  156. // var xhrTenancyName = new XMLHttpRequest();
  157. // xhrTenancyName.onreadystatechange = function () {
  158. // if (xhrTenancyName.readyState === XMLHttpRequest.DONE && xhrTenancyName.status === 200) {
  159. // var responseJSON = JSON.parse(xhrTenancyName.responseText);
  160. // var result = responseJSON.result;
  161. // if (result.state === 1) { // Tenant exists and active.
  162. // loginUserInternal(result.tenantId, callback); // Login for tenant
  163. // } else {
  164. // alert('There is no such tenant or tenant is not active !');
  165. // }
  166. // }
  167. // };
  168. // xhrTenancyName.open('POST', '/api/services/app/Account/IsTenantAvailable', true);
  169. // xhrTenancyName.setRequestHeader('Content-type', 'application/json');
  170. // addAntiForgeryTokenToXhr(xhrTenancyName);
  171. // xhrTenancyName.send(
  172. // JSON.stringify({ tenancyName: tenancyName })
  173. // );
  174. //} else {
  175. // loginUserInternal(null, callback); // Login for host
  176. //}
  177. loginUserInternal(callback);
  178. };
  179. abp.swagger.logout = function () {
  180. abp.auth.clearToken();
  181. }
  182. abp.swagger.closeAuthDialog = function () {
  183. if (document.getElementById('abp-auth-dialog')) {
  184. document.getElementsByClassName("swagger-ui")[1].removeChild(document.getElementById('abp-auth-dialog'));
  185. }
  186. }
  187. abp.swagger.openAuthDialog = function (loginCallback) {
  188. abp.swagger.closeAuthDialog();
  189. var abpAuthDialog = document.createElement('div');
  190. abpAuthDialog.className = 'dialog-ux';
  191. abpAuthDialog.id = 'abp-auth-dialog';
  192. document.getElementsByClassName("swagger-ui")[1].appendChild(abpAuthDialog);
  193. // -- backdrop-ux
  194. var backdropUx = document.createElement('div');
  195. backdropUx.className = 'backdrop-ux';
  196. abpAuthDialog.appendChild(backdropUx);
  197. // -- modal-ux
  198. var modalUx = document.createElement('div');
  199. modalUx.className = 'modal-ux';
  200. abpAuthDialog.appendChild(modalUx);
  201. // -- -- modal-dialog-ux
  202. var modalDialogUx = document.createElement('div');
  203. modalDialogUx.className = 'modal-dialog-ux';
  204. modalUx.appendChild(modalDialogUx);
  205. // -- -- -- modal-ux-inner
  206. var modalUxInner = document.createElement('div');
  207. modalUxInner.className = 'modal-ux-inner';
  208. modalDialogUx.appendChild(modalUxInner);
  209. // -- -- -- -- modal-ux-header
  210. var modalUxHeader = document.createElement('div');
  211. modalUxHeader.className = 'modal-ux-header';
  212. modalUxInner.appendChild(modalUxHeader);
  213. var modalHeader = document.createElement('h3');
  214. modalHeader.innerText = 'Authorize';
  215. modalUxHeader.appendChild(modalHeader);
  216. // -- -- -- -- modal-ux-content
  217. var modalUxContent = document.createElement('div');
  218. modalUxContent.className = 'modal-ux-content';
  219. modalUxInner.appendChild(modalUxContent);
  220. modalUxContent.onkeydown = function (e) {
  221. if (e.keyCode === 13) {
  222. //try to login when user presses enter on authorize modal
  223. abp.swagger.login(loginCallback);
  224. }
  225. };
  226. //Inputs
  227. //createInput(modalUxContent, 'tenancyName', 'Tenancy Name (Leave empty for Host)');
  228. createInput(modalUxContent, 'usernameOrEmailOrPhone', '用户名');
  229. createInput(modalUxContent, 'password', '密码', 'password');
  230. //Buttons
  231. var authBtnWrapper = document.createElement('div');
  232. authBtnWrapper.className = 'auth-btn-wrapper';
  233. modalUxContent.appendChild(authBtnWrapper);
  234. //Close button
  235. var closeButton = document.createElement('button');
  236. closeButton.className = 'btn modal-btn auth btn-done button';
  237. closeButton.innerText = '关闭';
  238. closeButton.style.marginRight = '5px';
  239. closeButton.onclick = abp.swagger.closeAuthDialog;
  240. authBtnWrapper.appendChild(closeButton);
  241. //Authorize button
  242. var authorizeButton = document.createElement('button');
  243. authorizeButton.className = 'btn modal-btn auth authorize button';
  244. authorizeButton.innerText = '登录';
  245. authorizeButton.onclick = function () {
  246. abp.swagger.login(loginCallback);
  247. };
  248. authBtnWrapper.appendChild(authorizeButton);
  249. }
  250. function createInput(container, id, title, type) {
  251. var wrapper = document.createElement('div');
  252. wrapper.className = 'wrapper';
  253. container.appendChild(wrapper);
  254. var label = document.createElement('label');
  255. label.innerText = title;
  256. wrapper.appendChild(label);
  257. var section = document.createElement('section');
  258. section.className = 'block-tablet col-10-tablet block-desktop col-10-desktop';
  259. wrapper.appendChild(section);
  260. var input = document.createElement('input');
  261. input.id = id;
  262. input.type = type ? type : 'text';
  263. input.style.width = '100%';
  264. section.appendChild(input);
  265. }
  266. })();