| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- var abp = abp || {};
- (function () {
- /* Application paths *****************************************/
- // Current application root path (including virtual directory if exists).
- abp.appPath = abp.appPath || '/';
- /* AUTHORIZATION **********************************************/
- // Implements Authorization API that simplifies usage of authorization scripts generated by Abp.
- abp.auth = abp.auth || {};
- abp.auth.tokenCookieName = 'Abp.AuthToken';
- abp.auth.tokenHeaderName = 'Authorization';
- abp.auth.setToken = function (authToken, expireDate) {
- abp.utils.setCookieValue(abp.auth.tokenCookieName, authToken, expireDate, abp.appPath);
- };
- abp.auth.getToken = function () {
- return abp.utils.getCookieValue(abp.auth.tokenCookieName);
- }
- abp.auth.clearToken = function () {
- abp.auth.setToken();
- }
- /* UTILS ***************************************************/
- abp.utils = abp.utils || {};
- /**
- * Sets a cookie value for given key.
- * This is a simple implementation created to be used by ABP.
- * Please use a complete cookie library if you need.
- * @param {string} key
- * @param {string} value
- * @param {Date} expireDate (optional). If not specified the cookie will expire at the end of session.
- * @param {string} path (optional)
- */
- abp.utils.setCookieValue = function (key, value, expireDate, path) {
- var cookieValue = encodeURIComponent(key) + '=';
- if (value) {
- cookieValue = cookieValue + encodeURIComponent(value);
- }
- if (expireDate) {
- cookieValue = cookieValue + "; expires=" + expireDate.toUTCString();
- }
- if (path) {
- cookieValue = cookieValue + "; path=" + path;
- }
- document.cookie = cookieValue;
- };
- /**
- * Gets a cookie with given key.
- * This is a simple implementation created to be used by ABP.
- * Please use a complete cookie library if you need.
- * @param {string} key
- * @returns {string} Cookie value or null
- */
- abp.utils.getCookieValue = function (key) {
- var equalities = document.cookie.split('; ');
- for (var i = 0; i < equalities.length; i++) {
- if (!equalities[i]) {
- continue;
- }
- var splitted = equalities[i].split('=');
- if (splitted.length != 2) {
- continue;
- }
- if (decodeURIComponent(splitted[0]) === key) {
- return decodeURIComponent(splitted[1] || '');
- }
- }
- return null;
- };
- /**
- * Deletes cookie for given key.
- * This is a simple implementation created to be used by ABP.
- * Please use a complete cookie library if you need.
- * @param {string} key
- * @param {string} path (optional)
- */
- abp.utils.deleteCookie = function (key, path) {
- var cookieValue = encodeURIComponent(key) + '=';
- cookieValue = cookieValue + "; expires=" + (new Date(new Date().getTime() - 86400000)).toUTCString();
- if (path) {
- cookieValue = cookieValue + "; path=" + path;
- }
- document.cookie = cookieValue;
- }
- /* SECURITY ***************************************/
- abp.security = abp.security || {};
- abp.security.antiForgery = abp.security.antiForgery || {};
- abp.security.antiForgery.tokenCookieName = 'XSRF-TOKEN-VberAdmin';
- abp.security.antiForgery.tokenHeaderName = 'X-XSRF-TOKEN';
- abp.security.antiForgery.getToken = function () {
- return abp.utils.getCookieValue(abp.security.antiForgery.tokenCookieName);
- };
- /* Swagger */
- abp.swagger = abp.swagger || {};
- abp.swagger.addAuthToken = function () {
- var authToken = abp.auth.getToken();
- if (!authToken) {
- return false;
- }
- var cookieAuth = new SwaggerClient.ApiKeyAuthorization(abp.auth.tokenHeaderName, 'Bearer ' + authToken, 'header');
- swaggerUi.api.clientAuthorizations.add('bearerAuth', cookieAuth);
- return true;
- }
- abp.swagger.addCsrfToken = function () {
- var csrfToken = abp.security.antiForgery.getToken();
- if (!csrfToken) {
- return false;
- }
- var csrfCookieAuth = new SwaggerClient.ApiKeyAuthorization(abp.security.antiForgery.tokenHeaderName, csrfToken, 'header');
- swaggerUi.api.clientAuthorizations.add(abp.security.antiForgery.tokenHeaderName, csrfCookieAuth);
- return true;
- }
- function addAntiForgeryTokenToXhr(xhr) {
- var antiForgeryToken = abp.security.antiForgery.getToken();
- if (antiForgeryToken) {
- xhr.setRequestHeader(abp.security.antiForgery.tokenHeaderName, antiForgeryToken);
- }
- }
- function loginUserInternal(callback) {
- var usernameOrEmailAddress = document.getElementById('usernameOrEmailOrPhone').value;
- if (!usernameOrEmailAddress) {
- alert('需要用户名或电子邮件地址,请尝试使用有效值!');
- return false;
- }
- var password = document.getElementById('password').value;
- if (!password) {
- alert('需要密码,请尝试输入有效值!');
- return false;
- }
- var xhr = new XMLHttpRequest();
- xhr.onreadystatechange = function () {
- if (xhr.readyState === XMLHttpRequest.DONE) {
- if (xhr.status === 200) {
- var responseJSON = JSON.parse(xhr.responseText);
- var result = responseJSON.result;
- var expireDate = new Date(Date.now() + (result.expireInSeconds * 1000));
- abp.auth.setToken(result.accessToken, expireDate);
- callback();
- } else {
- alert('登录失败!');
- }
- }
- };
- xhr.open('POST', '/api/TokenAuth/Authenticate', true);
- //xhr.setRequestHeader('Abp.TenantId', tenantId);
- //xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
- xhr.setRequestHeader('Content-type', 'application/json');
- addAntiForgeryTokenToXhr(xhr);
- xhr.send(
- JSON.stringify(
- { usernameOrEmailOrPhone: usernameOrEmailAddress, password: password }
- )
- );
- }
- abp.swagger.login = function (callback) {
- ////Get TenantId first
- //var tenancyName = document.getElementById('tenancyName').value;
- //if (tenancyName) {
- // var xhrTenancyName = new XMLHttpRequest();
- // xhrTenancyName.onreadystatechange = function () {
- // if (xhrTenancyName.readyState === XMLHttpRequest.DONE && xhrTenancyName.status === 200) {
- // var responseJSON = JSON.parse(xhrTenancyName.responseText);
- // var result = responseJSON.result;
- // if (result.state === 1) { // Tenant exists and active.
- // loginUserInternal(result.tenantId, callback); // Login for tenant
- // } else {
- // alert('There is no such tenant or tenant is not active !');
- // }
- // }
- // };
- // xhrTenancyName.open('POST', '/api/services/app/Account/IsTenantAvailable', true);
- // xhrTenancyName.setRequestHeader('Content-type', 'application/json');
- // addAntiForgeryTokenToXhr(xhrTenancyName);
- // xhrTenancyName.send(
- // JSON.stringify({ tenancyName: tenancyName })
- // );
- //} else {
- // loginUserInternal(null, callback); // Login for host
- //}
- loginUserInternal(callback);
- };
- abp.swagger.logout = function () {
- abp.auth.clearToken();
- }
- abp.swagger.closeAuthDialog = function () {
- if (document.getElementById('abp-auth-dialog')) {
- document.getElementsByClassName("swagger-ui")[1].removeChild(document.getElementById('abp-auth-dialog'));
- }
- }
- abp.swagger.openAuthDialog = function (loginCallback) {
- abp.swagger.closeAuthDialog();
- var abpAuthDialog = document.createElement('div');
- abpAuthDialog.className = 'dialog-ux';
- abpAuthDialog.id = 'abp-auth-dialog';
- document.getElementsByClassName("swagger-ui")[1].appendChild(abpAuthDialog);
- // -- backdrop-ux
- var backdropUx = document.createElement('div');
- backdropUx.className = 'backdrop-ux';
- abpAuthDialog.appendChild(backdropUx);
- // -- modal-ux
- var modalUx = document.createElement('div');
- modalUx.className = 'modal-ux';
- abpAuthDialog.appendChild(modalUx);
- // -- -- modal-dialog-ux
- var modalDialogUx = document.createElement('div');
- modalDialogUx.className = 'modal-dialog-ux';
- modalUx.appendChild(modalDialogUx);
- // -- -- -- modal-ux-inner
- var modalUxInner = document.createElement('div');
- modalUxInner.className = 'modal-ux-inner';
- modalDialogUx.appendChild(modalUxInner);
- // -- -- -- -- modal-ux-header
- var modalUxHeader = document.createElement('div');
- modalUxHeader.className = 'modal-ux-header';
- modalUxInner.appendChild(modalUxHeader);
- var modalHeader = document.createElement('h3');
- modalHeader.innerText = 'Authorize';
- modalUxHeader.appendChild(modalHeader);
- // -- -- -- -- modal-ux-content
- var modalUxContent = document.createElement('div');
- modalUxContent.className = 'modal-ux-content';
- modalUxInner.appendChild(modalUxContent);
- modalUxContent.onkeydown = function (e) {
- if (e.keyCode === 13) {
- //try to login when user presses enter on authorize modal
- abp.swagger.login(loginCallback);
- }
- };
- //Inputs
- //createInput(modalUxContent, 'tenancyName', 'Tenancy Name (Leave empty for Host)');
- createInput(modalUxContent, 'usernameOrEmailOrPhone', '用户名');
- createInput(modalUxContent, 'password', '密码', 'password');
- //Buttons
- var authBtnWrapper = document.createElement('div');
- authBtnWrapper.className = 'auth-btn-wrapper';
- modalUxContent.appendChild(authBtnWrapper);
- //Close button
- var closeButton = document.createElement('button');
- closeButton.className = 'btn modal-btn auth btn-done button';
- closeButton.innerText = '关闭';
- closeButton.style.marginRight = '5px';
- closeButton.onclick = abp.swagger.closeAuthDialog;
- authBtnWrapper.appendChild(closeButton);
- //Authorize button
- var authorizeButton = document.createElement('button');
- authorizeButton.className = 'btn modal-btn auth authorize button';
- authorizeButton.innerText = '登录';
- authorizeButton.onclick = function () {
- abp.swagger.login(loginCallback);
- };
- authBtnWrapper.appendChild(authorizeButton);
- }
- function createInput(container, id, title, type) {
- var wrapper = document.createElement('div');
- wrapper.className = 'wrapper';
- container.appendChild(wrapper);
- var label = document.createElement('label');
- label.innerText = title;
- wrapper.appendChild(label);
- var section = document.createElement('section');
- section.className = 'block-tablet col-10-tablet block-desktop col-10-desktop';
- wrapper.appendChild(section);
- var input = document.createElement('input');
- input.id = id;
- input.type = type ? type : 'text';
- input.style.width = '100%';
- section.appendChild(input);
- }
- })();
|