diff --git a/src/app/elements/connect/connect.component.ts b/src/app/elements/connect/connect.component.ts index a71ea35f0..4c5badb2e 100644 --- a/src/app/elements/connect/connect.component.ts +++ b/src/app/elements/connect/connect.component.ts @@ -1,5 +1,5 @@ -import {Component, EventEmitter, Input, OnDestroy, OnInit, Output} from '@angular/core'; -import {connectEvt} from '@app/globals'; +import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'; +import { connectEvt } from '@app/globals'; import { AlertService, AppService, @@ -10,18 +10,17 @@ import { SettingService, ViewService } from '@app/services'; -import {Account, Asset, ConnectData, ConnectionToken, View} from '@app/model'; -import {launchLocalApp} from '@app/utils/common'; -import {ElementConnectDialogComponent} from '@app/elements/connect/connect-dialog/connect-dialog.component'; -import {NzModalService} from 'ng-zorro-antd/modal'; -import {ElementDownloadDialogComponent} from './download-dialog/download-dialog.component'; -import {firstValueFrom} from 'rxjs'; - +import { Account, Asset, ConnectData, ConnectionToken, View } from '@app/model'; +import { launchLocalApp } from '@app/utils/common'; +import { ElementConnectDialogComponent } from '@app/elements/connect/connect-dialog/connect-dialog.component'; +import { NzModalService } from 'ng-zorro-antd/modal'; +import { ElementDownloadDialogComponent } from './download-dialog/download-dialog.component'; +import { firstValueFrom } from 'rxjs'; @Component({ standalone: false, selector: 'elements-connect', - templateUrl: 'connect.component.html', + templateUrl: 'connect.component.html' }) export class ElementConnectComponent implements OnInit, OnDestroy { @Output() onNewView: EventEmitter = new EventEmitter(); @@ -32,17 +31,17 @@ export class ElementConnectComponent implements OnInit, OnDestroy { accounts: Array; preConnectData: ConnectData; - constructor(private _appSvc: AppService, - private _alert: AlertService, - private _dialog: NzModalService, - private _logger: LogService, - private _http: HttpService, - private _connectTokenSvc: ConnectTokenService, - private _i18n: I18nService, - private _settingSvc: SettingService, - public viewSrv: ViewService, - ) { - } + constructor( + private _appSvc: AppService, + private _alert: AlertService, + private _dialog: NzModalService, + private _logger: LogService, + private _http: HttpService, + private _connectTokenSvc: ConnectTokenService, + private _i18n: I18nService, + private _settingSvc: SettingService, + public viewSrv: ViewService + ) {} ngOnInit(): void { this.subscribeConnectEvent(); @@ -87,7 +86,7 @@ export class ElementConnectComponent implements OnInit, OnDestroy { const idObject = {}; const idList = id.split('&'); for (const element of idList) { - idObject[element.split('=')[0]] = (element.split('=')[1]); + idObject[element.split('=')[0]] = element.split('=')[1]; } return idObject; } @@ -123,7 +122,9 @@ export class ElementConnectComponent implements OnInit, OnDestroy { */ async connectAsset(asset: Asset, splitConnect = false) { if (!asset) { - const msg = this._i18n.instant('Asset not found or You have no permission to access it, please refresh asset tree'); + const msg = this._i18n.instant( + 'Asset not found or You have no permission to access it, please refresh asset tree' + ); const title = await this._i18n.instant('Permission expired'); await this._alert.error(msg, title); return; @@ -153,13 +154,6 @@ export class ElementConnectComponent implements OnInit, OnDestroy { return; } - // if (connToken.protocol === 'k8s') { - // const url = `${window.location.protocol}//${window.location.host}/luna/k8s/${connToken.id}?asset=${asset.id}`; - // window.open(url); - // return; - // console.log('k8s', this.viewSrv) - // } - // 分屏连接 if (splitConnect) { return this.currentWebSubView(asset, connectInfo, connToken); @@ -177,7 +171,11 @@ export class ElementConnectComponent implements OnInit, OnDestroy { } if (connectInfo.downloadRDP) { - return this._http.downloadRDPFile(connToken, this._settingSvc.setting, connectInfo.connectOption); + return this._http.downloadRDPFile( + connToken, + this._settingSvc.setting, + connectInfo.connectOption + ); } else if (connectMethod.type === 'native') { this.callLocalClient(connToken).then(); } else if (connectMethod.type === 'applet' && appletConnectMethod === 'client') { @@ -192,7 +190,9 @@ export class ElementConnectComponent implements OnInit, OnDestroy { if (connToken.connect_options.token_reusable) { await this._connectTokenSvc.setReusable(connToken, true).toPromise(); } - const response = await firstValueFrom(this._http.getLocalClientUrl(connToken, this._settingSvc.setting)); + const response = await firstValueFrom( + this._http.getLocalClientUrl(connToken, this._settingSvc.setting) + ); const url = response['url']; launchLocalApp(url, () => { const downLoadStatus = localStorage.getItem('hasDownLoadApp'); @@ -200,17 +200,15 @@ export class ElementConnectComponent implements OnInit, OnDestroy { this._dialog.create({ nzTitle: this._i18n.instant('DownloadClient'), nzContent: ElementDownloadDialogComponent, - nzOnOk: (cmp => cmp.onConfirm()), - nzOnCancel: (cmp => cmp.onCancel()), + nzOnOk: cmp => cmp.onConfirm(), + nzOnCancel: cmp => cmp.onCancel() }); } }); } - createWebView(asset: Asset, connectInfo: any, connToken: ConnectionToken) { const view = new View(asset, connectInfo, connToken, 'node'); - console.log('createWebView', view); this.onNewView.emit(view); } @@ -224,6 +222,7 @@ export class ElementConnectComponent implements OnInit, OnDestroy { this._logger.debug('No account or node'); return false; } + if (!preData.autoLogin) { this._logger.debug('Not auto login'); return false; @@ -237,16 +236,20 @@ export class ElementConnectComponent implements OnInit, OnDestroy { const account = accounts.find(item => { return item.alias === preAccount.alias; }); + if (!account) { this._logger.debug('Account may be not valid'); return false; } + // 验证登录信息 const preAuth = preData.manualAuthInfo; + if (!account.has_secret && (!preAuth || !preAuth.secret)) { this._logger.debug('Account no manual auth'); return false; } + // 验证连接方式 const connectMethods = this._appSvc.getProtocolConnectMethods(preData.protocol.name); if (!connectMethods) { @@ -286,7 +289,7 @@ export class ElementConnectComponent implements OnInit, OnDestroy { nzCentered: true, nzClassName: 'connect-dialog', nzWrapClassName: 'connect-dialog-wrap', - nzFooter: null, + nzFooter: null }); return new Promise(resolve => { diff --git a/src/app/services/app.ts b/src/app/services/app.ts index c43f190c1..143c6aa65 100644 --- a/src/app/services/app.ts +++ b/src/app/services/app.ts @@ -1,15 +1,15 @@ -import {Injectable} from '@angular/core'; -import {Router} from '@angular/router'; -import {CookieService} from 'ngx-cookie-service'; -import {environment} from '@src/environments/environment'; -import {DataStore, User} from '@app/globals'; -import {HttpService} from './http'; -import {LocalStorageService, LogService} from './share'; -import {SettingService} from '@app/services/setting'; -import {Account, Asset, AuthInfo, ConnectData, Endpoint, Organization, View} from '@app/model'; +import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; +import { CookieService } from 'ngx-cookie-service'; +import { environment } from '@src/environments/environment'; +import { DataStore, User } from '@app/globals'; +import { HttpService } from './http'; +import { LocalStorageService, LogService } from './share'; +import { SettingService } from '@app/services/setting'; +import { Account, Asset, AuthInfo, ConnectData, Endpoint, Organization, View } from '@app/model'; import * as CryptoJS from 'crypto-js'; -import {OrganizationService} from './organization'; -import {I18nService} from '@app/services/i18n'; +import { OrganizationService } from './organization'; +import { I18nService } from '@app/services/i18n'; declare function unescape(s: string): string; @@ -34,14 +34,16 @@ export class AppService { private newLoginHasOpen = false; // 避免多次打开新登录页 private checkSecond = 120; - constructor(private _http: HttpService, - private _router: Router, - private _cookie: CookieService, - private _i18n: I18nService, - private _logger: LogService, - private _settingSvc: SettingService, - private _localStorage: LocalStorageService, - private _orgSvc: OrganizationService) { + constructor( + private _http: HttpService, + private _router: Router, + private _cookie: CookieService, + private _i18n: I18nService, + private _logger: LogService, + private _settingSvc: SettingService, + private _localStorage: LocalStorageService, + private _orgSvc: OrganizationService + ) { this.setLogLevel(); this.setOrgFromQueryString(); this.checkLogin(); @@ -62,7 +64,7 @@ export class AppService { setOrgFromQueryString() { const oid = this.getQueryString('oid'); if (oid) { - const currentOrg: Organization = {id: oid, name: ''}; + const currentOrg: Organization = { id: oid, name: '' }; this._orgSvc.switchOrg(currentOrg); } } @@ -127,9 +129,9 @@ export class AppService { getErrorMsg(status: string) { const messages = { - 'unauthorized': 'LoginExpireMsg', - 'badrequest': 'Bad request. The server does not understand the syntax of the request', - 'error': 'The server encountered an error while trying to process the request' + unauthorized: 'LoginExpireMsg', + badrequest: 'Bad request. The server does not understand the syntax of the request', + error: 'The server encountered an error while trying to process the request' }; return messages[status]; } @@ -168,7 +170,6 @@ export class AppService { this._http.getUserProfile().then( user => { - console.log('User is: ', user); this._orgSvc.setWorkbenchOrgs(user['workbench_orgs']); Object.assign(User, user); User.logined = true; @@ -182,7 +183,7 @@ export class AppService { gotoLogin(); } // this._router.navigate(['login']); - }, + } ); } @@ -237,17 +238,17 @@ export class AppService { } setPreConnectData(asset: Asset, connectData: ConnectData) { - const {account, protocol, connectMethod, manualAuthInfo, connectOption} = connectData; + const { account, protocol, connectMethod, manualAuthInfo, connectOption } = connectData; const key = `JMS_PRE_${asset.id}`; const saveData = { - account: {alias: account.alias, username: account.username, has_secret: account.has_secret}, - connectMethod: {value: connectMethod.value}, - protocol: {name: protocol.name}, + account: { alias: account.alias, username: account.username, has_secret: account.has_secret }, + connectMethod: { value: connectMethod.value }, + protocol: { name: protocol.name }, downloadRDP: connectData.downloadRDP, autoLogin: connectData.autoLogin, connectOption, - direct: connectData.direct, + direct: connectData.direct }; this.setAccountLocalAuth(asset, account, manualAuthInfo); this._localStorage.set(key, saveData); @@ -330,7 +331,13 @@ export class AppService { setAccountLocalAuth(asset: Asset, account: Account, auth: AuthInfo) { const assetId = asset.id; - const newAuth = Object.assign({alias: account.alias, username: account.username}, auth); + const newAuth = Object.assign({ alias: account.alias, username: account.username }, auth); + + // 如果 auth.alias 是 undefined,保持使用 account.alias + if (auth.alias === undefined && account.alias !== undefined) { + newAuth.alias = account.alias; + } + if (!auth.secret || !auth.rememberAuth) { newAuth.secret = ''; } else { @@ -340,7 +347,7 @@ export class AppService { let auths = this.getAccountLocalAuth(assetId, false); const localKey = `JMS_MA_${assetId}`; - auths = auths.filter((item) => item.username !== newAuth.username); + auths = auths.filter(item => item.username !== newAuth.username); auths.splice(0, 0, newAuth); this._localStorage.set(localKey, auths); } @@ -355,7 +362,7 @@ export class AppService { if (protocol === 'http') { protocol = window.location.protocol.replace(':', ''); } - const data = {'assetId': '', 'sessionId': '', 'token': ''}; + const data = { assetId: '', sessionId: '', token: '' }; if (view.connectToken) { data['token'] = view.connectToken.id; } else { @@ -369,7 +376,7 @@ export class AppService { } } const res = this._http.getSmartEndpoint(data, protocol); - res.catch((err) => { + res.catch(err => { alert(err.error.detail); }); return res;