-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathsetting.ts
More file actions
188 lines (168 loc) · 5.64 KB
/
Copy pathsetting.ts
File metadata and controls
188 lines (168 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { Injectable } from "@angular/core";
import { GlobalSetting, Setting } from "@app/model";
import { LocalStorageService } from "./share";
import { I18nService } from "@app/services/i18n";
import { HttpService } from "@app/services/http";
import { canvasWaterMark, getQueryParamFromURL } from "@app/utils/common";
import { BehaviorSubject } from "rxjs";
@Injectable()
export class SettingService {
setting: Setting = new Setting();
public globalSetting: GlobalSetting = new GlobalSetting();
settingKey = "LunaSetting";
public initialized$ = new BehaviorSubject<boolean>(false);
public isLoadTreeAsync$ = new BehaviorSubject<boolean>(true);
public isOpenNewWindow$ = new BehaviorSubject<boolean>(false);
public appletConnectMethod$ = new BehaviorSubject<string>("");
public keyboardLayout$ = new BehaviorSubject<string>("");
public isDirectNavigation$ = new BehaviorSubject<boolean>(false);
public globalSetting$ = new BehaviorSubject<GlobalSetting>(new GlobalSetting());
constructor(
private _localStorage: LocalStorageService,
private _http: HttpService,
private _i18n: I18nService
) {
this.init().then();
}
async getPublicSettings() {
let url = "/api/v1/settings/public/";
const connectionToken = getQueryParamFromURL("token");
if (connectionToken) {
// 解决 /luna/connect?connectToken= 直接方式权限认证问题
url += `?token=${connectionToken}`;
}
this.globalSetting = await this._http.get<any>(url).toPromise();
this.globalSetting$.next(this.globalSetting);
this.setting.commandExecution =
this.globalSetting.SECURITY_COMMAND_EXECUTION;
this.setLogo();
this.setTitle();
this.setFavicon();
this.setRDPResolution();
return new Promise((resolve) => {
resolve(true);
});
}
getSystemSetting() {
return new Promise<void>(async (resolve) => {
const url = "/api/v1/users/preference/?category=luna";
const serverSetting = await this._http.get<any>(url).toPromise();
const localSetting = this._localStorage.get(this.settingKey);
this.setting = Object.assign(this.setting, localSetting, serverSetting);
this._localStorage.set(this.settingKey, this.setting);
this.setAppletConnectMethod();
this.setKeyboardLayout();
resolve();
});
}
setTitle() {
document.title =
this._i18n.instant("Web Terminal") +
` - ${this.globalSetting.INTERFACE.login_title}`;
}
setLogo() {
const logoRef: any = document.getElementById("left-logo");
const logoLogout = this.globalSetting.INTERFACE.logo_logout;
if (logoLogout && logoRef) {
logoRef.src = logoLogout;
}
}
setFavicon() {
// 更改favicon
const link: any =
document.querySelector("link[rel*='icon']") ||
document.createElement("link");
document.getElementsByTagName("head")[0].appendChild(link);
link.type = "image/x-icon";
link.rel = "shortcut icon";
link.href = this.globalSetting.INTERFACE.favicon;
}
setRDPResolution() {
const value = this._localStorage.get(this.settingKey);
if (!value || (value.graphics && !value.graphics.rdp_resolution)) {
this.setting.graphics.rdp_resolution =
this.globalSetting.TERMINAL_GRAPHICAL_RESOLUTION;
}
}
setPrimaryColor() {
const primaryColor = this.globalSetting.INTERFACE["primary_color"];
if (primaryColor) {
document.body.style.setProperty("--primary-color", primaryColor);
}
}
async init() {
if (this.initialized$.value) {
return;
}
await this.getSystemSetting();
await this.getPublicSettings();
this.initialized$.next(true);
}
afterInited() {
if (this.initialized$.value) {
return Promise.resolve(true);
}
return new Promise((resolve) => {
this.initialized$.subscribe((inited) => {
if (inited) {
resolve(true);
}
});
});
}
save() {
const url = "/api/v1/users/preference/?category=luna";
this._http.patch(url, this.setting).toPromise();
this._localStorage.set(this.settingKey, this.setting);
this.setAppletConnectMethod();
this.setKeyboardLayout();
}
isLoadTreeAsync() {
return this.setting.basic.is_async_asset_tree;
}
isOpenNewWindow() {
return this.setting.basic.connect_default_open_method === "new";
}
setAppletConnectMethod() {
this.appletConnectMethod$.next(
this.setting.graphics.applet_connection_method
);
}
setKeyboardLayout() {
this.keyboardLayout$.next(this.setting.graphics.keyboard_layout);
}
// 全局跳过手动输入windows账号密码
globalSkipAllManualPassword(): boolean {
return this.globalSetting.WINDOWS_SKIP_ALL_MANUAL_PASSWORD;
}
isSkipAllManualPassword(): boolean {
if (this.globalSkipAllManualPassword()) {
return true;
}
return this.setting.isSkipAllManualPassword === "1";
}
createWaterMarkIfNeed(element, content) {
this.init().then(() => {
if (this.globalSetting.SECURITY_WATERMARK_ENABLED) {
canvasWaterMark({
container: element,
content: content,
settings: {
width: this.globalSetting.SECURITY_WATERMARK_WIDTH,
height: this.globalSetting.SECURITY_WATERMARK_HEIGHT,
font:
this.globalSetting.SECURITY_WATERMARK_FONT_SIZE +
"px monaco, microsoft yahei",
fillStyle: this.globalSetting.SECURITY_WATERMARK_COLOR,
rotate: this.globalSetting.SECURITY_WATERMARK_ROTATE,
lineHeight: this.globalSetting.SECURITY_WATERMARK_FONT_SIZE,
},
});
}
});
}
hasXPack() {
const hasXpack = this.globalSetting.XPACK_LICENSE_IS_VALID;
return hasXpack;
}
}