feat: 增加了登录选项

This commit is contained in:
2025-07-11 16:36:28 +08:00
parent d88c710606
commit b4bb563782
8 changed files with 790 additions and 129 deletions

View File

@@ -377,13 +377,13 @@ export class DataClient {
}
/**
* [TODO:description]
* @param name (optional) [TODO:parameter]
* @param password (optional) [TODO:parameter]
* @return [TODO:return]
* 用户登录,获取 JWT 令牌
* @param name (optional) 用户名
* @param password (optional) 用户密码
* @return JWT 令牌字符串
*/
login(name: string | undefined, password: string | undefined): Promise<FileResponse | null> {
let url_ = this.baseUrl + "/api/Data/login?";
login(name: string | undefined, password: string | undefined): Promise<string> {
let url_ = this.baseUrl + "/api/Data/Login?";
if (name === null)
throw new Error("The parameter 'name' cannot be null.");
else if (name !== undefined)
@@ -397,7 +397,7 @@ export class DataClient {
let options_: RequestInit = {
method: "POST",
headers: {
"Accept": "application/octet-stream"
"Accept": "application/json"
}
};
@@ -406,40 +406,48 @@ export class DataClient {
});
}
protected processLogin(response: Response): Promise<FileResponse | null> {
protected processLogin(response: Response): Promise<string> {
const status = response.status;
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
if (status === 200 || status === 206) {
const contentDisposition = response.headers ? response.headers.get("content-disposition") : undefined;
let fileNameMatch = contentDisposition ? /filename\*=(?:(\\?['"])(.*?)\1|(?:[^\s]+'.*?')?([^;\n]*))/g.exec(contentDisposition) : undefined;
let fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[3] || fileNameMatch[2] : undefined;
if (fileName) {
fileName = decodeURIComponent(fileName);
} else {
fileNameMatch = contentDisposition ? /filename="?([^"]*?)"?(;|$)/g.exec(contentDisposition) : undefined;
fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[1] : undefined;
}
return response.blob().then(blob => { return { fileName: fileName, data: blob, status: status, headers: _headers }; });
if (status === 200) {
return response.text().then((_responseText) => {
let result200: any = null;
let resultData200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
result200 = resultData200 !== undefined ? resultData200 : <any>null;
return result200;
});
} else if (status === 400) {
return response.text().then((_responseText) => {
let result400: any = null;
let resultData400 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
result400 = ProblemDetails.fromJS(resultData400);
return throwException("A server side error occurred.", status, _responseText, _headers, result400);
});
} else if (status === 500) {
return response.text().then((_responseText) => {
return throwException("A server side error occurred.", status, _responseText, _headers);
});
} else if (status !== 200 && status !== 204) {
return response.text().then((_responseText) => {
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
});
}
return Promise.resolve<FileResponse | null>(null as any);
return Promise.resolve<string>(null as any);
}
/**
* [TODO:description]
* @return [TODO:return]
* 测试用户认证,需携带有效 JWT
* @return 认证成功信息
*/
testAuth(): Promise<FileResponse | null> {
testAuth(): Promise<string> {
let url_ = this.baseUrl + "/api/Data/TestAuth";
url_ = url_.replace(/[?&]$/, "");
let options_: RequestInit = {
method: "GET",
headers: {
"Accept": "application/octet-stream"
"Accept": "application/json"
}
};
@@ -448,36 +456,96 @@ export class DataClient {
});
}
protected processTestAuth(response: Response): Promise<FileResponse | null> {
protected processTestAuth(response: Response): Promise<string> {
const status = response.status;
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
if (status === 200 || status === 206) {
const contentDisposition = response.headers ? response.headers.get("content-disposition") : undefined;
let fileNameMatch = contentDisposition ? /filename\*=(?:(\\?['"])(.*?)\1|(?:[^\s]+'.*?')?([^;\n]*))/g.exec(contentDisposition) : undefined;
let fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[3] || fileNameMatch[2] : undefined;
if (fileName) {
fileName = decodeURIComponent(fileName);
} else {
fileNameMatch = contentDisposition ? /filename="?([^"]*?)"?(;|$)/g.exec(contentDisposition) : undefined;
fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[1] : undefined;
}
return response.blob().then(blob => { return { fileName: fileName, data: blob, status: status, headers: _headers }; });
if (status === 200) {
return response.text().then((_responseText) => {
let result200: any = null;
let resultData200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
result200 = resultData200 !== undefined ? resultData200 : <any>null;
return result200;
});
} else if (status === 401) {
return response.text().then((_responseText) => {
let result401: any = null;
let resultData401 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
result401 = ProblemDetails.fromJS(resultData401);
return throwException("A server side error occurred.", status, _responseText, _headers, result401);
});
} else if (status !== 200 && status !== 204) {
return response.text().then((_responseText) => {
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
});
}
return Promise.resolve<FileResponse | null>(null as any);
return Promise.resolve<string>(null as any);
}
/**
* 获取当前用户信息
* @return 用户信息包括ID、用户名、邮箱和板卡ID
*/
getUserInfo(): Promise<GetUserInfoResponse> {
let url_ = this.baseUrl + "/api/Data/GetUserInfo";
url_ = url_.replace(/[?&]$/, "");
let options_: RequestInit = {
method: "GET",
headers: {
"Accept": "application/json"
}
};
return this.http.fetch(url_, options_).then((_response: Response) => {
return this.processGetUserInfo(_response);
});
}
protected processGetUserInfo(response: Response): Promise<GetUserInfoResponse> {
const status = response.status;
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
if (status === 200) {
return response.text().then((_responseText) => {
let result200: any = null;
let resultData200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
result200 = GetUserInfoResponse.fromJS(resultData200);
return result200;
});
} else if (status === 400) {
return response.text().then((_responseText) => {
let result400: any = null;
let resultData400 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
result400 = ProblemDetails.fromJS(resultData400);
return throwException("A server side error occurred.", status, _responseText, _headers, result400);
});
} else if (status === 500) {
return response.text().then((_responseText) => {
return throwException("A server side error occurred.", status, _responseText, _headers);
});
} else if (status === 401) {
return response.text().then((_responseText) => {
let result401: any = null;
let resultData401 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
result401 = ProblemDetails.fromJS(resultData401);
return throwException("A server side error occurred.", status, _responseText, _headers, result401);
});
} else if (status !== 200 && status !== 204) {
return response.text().then((_responseText) => {
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
});
}
return Promise.resolve<GetUserInfoResponse>(null as any);
}
/**
* 注册新用户
* @param name (optional) 用户名
* @param email (optional) [TODO:parameter]
* @param password (optional) [TODO:parameter]
* @return 操作结果
* @param name (optional) 用户名不超过255个字符
* @param email (optional) 邮箱地址
* @param password (optional) 用户密码
* @return 操作结果,成功返回 true失败返回错误信息
*/
signUpUser(name: string | undefined, email: string | undefined, password: string | undefined): Promise<FileResponse | null> {
signUpUser(name: string | undefined, email: string | undefined, password: string | undefined): Promise<boolean> {
let url_ = this.baseUrl + "/api/Data/SignUpUser?";
if (name === null)
throw new Error("The parameter 'name' cannot be null.");
@@ -496,7 +564,7 @@ export class DataClient {
let options_: RequestInit = {
method: "POST",
headers: {
"Accept": "application/octet-stream"
"Accept": "application/json"
}
};
@@ -505,26 +573,34 @@ export class DataClient {
});
}
protected processSignUpUser(response: Response): Promise<FileResponse | null> {
protected processSignUpUser(response: Response): Promise<boolean> {
const status = response.status;
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
if (status === 200 || status === 206) {
const contentDisposition = response.headers ? response.headers.get("content-disposition") : undefined;
let fileNameMatch = contentDisposition ? /filename\*=(?:(\\?['"])(.*?)\1|(?:[^\s]+'.*?')?([^;\n]*))/g.exec(contentDisposition) : undefined;
let fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[3] || fileNameMatch[2] : undefined;
if (fileName) {
fileName = decodeURIComponent(fileName);
} else {
fileNameMatch = contentDisposition ? /filename="?([^"]*?)"?(;|$)/g.exec(contentDisposition) : undefined;
fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[1] : undefined;
}
return response.blob().then(blob => { return { fileName: fileName, data: blob, status: status, headers: _headers }; });
if (status === 200) {
return response.text().then((_responseText) => {
let result200: any = null;
let resultData200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
result200 = resultData200 !== undefined ? resultData200 : <any>null;
return result200;
});
} else if (status === 400) {
return response.text().then((_responseText) => {
let result400: any = null;
let resultData400 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
result400 = ProblemDetails.fromJS(resultData400);
return throwException("A server side error occurred.", status, _responseText, _headers, result400);
});
} else if (status === 500) {
return response.text().then((_responseText) => {
return throwException("A server side error occurred.", status, _responseText, _headers);
});
} else if (status !== 200 && status !== 204) {
return response.text().then((_responseText) => {
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
});
}
return Promise.resolve<FileResponse | null>(null as any);
return Promise.resolve<boolean>(null as any);
}
}
@@ -2286,6 +2362,145 @@ export interface ICameraConfigRequest {
port: number;
}
export class ProblemDetails implements IProblemDetails {
type?: string | undefined;
title?: string | undefined;
status?: number | undefined;
detail?: string | undefined;
instance?: string | undefined;
extensions!: { [key: string]: any; };
[key: string]: any;
constructor(data?: IProblemDetails) {
if (data) {
for (var property in data) {
if (data.hasOwnProperty(property))
(<any>this)[property] = (<any>data)[property];
}
}
if (!data) {
this.extensions = {};
}
}
init(_data?: any) {
if (_data) {
for (var property in _data) {
if (_data.hasOwnProperty(property))
this[property] = _data[property];
}
this.type = _data["type"];
this.title = _data["title"];
this.status = _data["status"];
this.detail = _data["detail"];
this.instance = _data["instance"];
if (_data["extensions"]) {
this.extensions = {} as any;
for (let key in _data["extensions"]) {
if (_data["extensions"].hasOwnProperty(key))
(<any>this.extensions)![key] = _data["extensions"][key];
}
}
}
}
static fromJS(data: any): ProblemDetails {
data = typeof data === 'object' ? data : {};
let result = new ProblemDetails();
result.init(data);
return result;
}
toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
for (var property in this) {
if (this.hasOwnProperty(property))
data[property] = this[property];
}
data["type"] = this.type;
data["title"] = this.title;
data["status"] = this.status;
data["detail"] = this.detail;
data["instance"] = this.instance;
if (this.extensions) {
data["extensions"] = {};
for (let key in this.extensions) {
if (this.extensions.hasOwnProperty(key))
(<any>data["extensions"])[key] = (<any>this.extensions)[key];
}
}
return data;
}
}
export interface IProblemDetails {
type?: string | undefined;
title?: string | undefined;
status?: number | undefined;
detail?: string | undefined;
instance?: string | undefined;
extensions: { [key: string]: any; };
[key: string]: any;
}
export class GetUserInfoResponse implements IGetUserInfoResponse {
/** 用户的唯一标识符 */
id!: string;
/** 用户的名称 */
name!: string;
/** 用户的电子邮箱 */
eMail!: string;
/** 用户关联的板卡ID */
boardID!: string;
constructor(data?: IGetUserInfoResponse) {
if (data) {
for (var property in data) {
if (data.hasOwnProperty(property))
(<any>this)[property] = (<any>data)[property];
}
}
}
init(_data?: any) {
if (_data) {
this.id = _data["id"];
this.name = _data["name"];
this.eMail = _data["eMail"];
this.boardID = _data["boardID"];
}
}
static fromJS(data: any): GetUserInfoResponse {
data = typeof data === 'object' ? data : {};
let result = new GetUserInfoResponse();
result.init(data);
return result;
}
toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
data["id"] = this.id;
data["name"] = this.name;
data["eMail"] = this.eMail;
data["boardID"] = this.boardID;
return data;
}
}
export interface IGetUserInfoResponse {
/** 用户的唯一标识符 */
id: string;
/** 用户的名称 */
name: string;
/** 用户的电子邮箱 */
eMail: string;
/** 用户关联的板卡ID */
boardID: string;
}
export class SystemException extends Exception implements ISystemException {
constructor(data?: ISystemException) {