Files
TtxPageAdmin/lib/backend/studio_backend_models.dart
T
2026-05-16 01:10:06 +08:00

336 lines
8.3 KiB
Dart

part of 'studio_backend_client.dart';
class StudioBackendLoginResult {
const StudioBackendLoginResult({
required this.success,
required this.code,
required this.message,
required this.raw,
this.leftDays,
this.session,
this.appConfig,
this.systemConfig,
});
final bool success;
final String code;
final String message;
final int? leftDays;
final StudioBackendSession? session;
final Map<String, dynamic>? appConfig;
final Map<String, dynamic>? systemConfig;
final Map<String, dynamic> raw;
}
class StudioBackendLoginStatusResult {
const StudioBackendLoginStatusResult({
required this.success,
required this.code,
required this.message,
required this.raw,
this.leftDays,
this.session,
this.appConfig,
this.systemConfig,
});
final bool success;
final String code;
final String message;
final int? leftDays;
final StudioBackendSession? session;
final Map<String, dynamic>? appConfig;
final Map<String, dynamic>? systemConfig;
final Map<String, dynamic> raw;
}
class StudioBackendSession {
const StudioBackendSession({
required this.baseUrl,
required this.user,
required this.db,
required this.token,
required this.client,
required this.locale,
required this.timezone,
required this.portal,
required this.secretGen,
required this.sso,
required this.ssoToken,
required this.ssoApp,
required this.ssoClient,
required this.raw,
});
factory StudioBackendSession.initial({
required String baseUrl,
required String workspace,
required String locale,
}) {
return StudioBackendSession(
baseUrl: baseUrl,
user: 'default_user',
db: workspace,
token: 'default_token',
client: '',
locale: locale,
timezone: _localTimezoneCode(),
portal: '',
secretGen: '',
sso: '',
ssoToken: '',
ssoApp: '',
ssoClient: '',
raw: const <String, dynamic>{},
);
}
factory StudioBackendSession.fromJson({
required String baseUrl,
required Map<String, dynamic> raw,
required String fallbackUser,
required String fallbackWorkspace,
required String fallbackLocale,
}) {
return StudioBackendSession(
baseUrl: baseUrl,
user: _pick(raw, ['user', 'username'], fallbackUser),
db: _pick(raw, ['db', 'customer'], fallbackWorkspace),
token: _pick(raw, ['tk', 'token'], ''),
client: _pick(raw, ['client'], ''),
locale: _pick(raw, ['locale', 'language'], fallbackLocale),
timezone: _normalizeTimezone(_pick(raw, ['timezone'], '')),
portal: _pick(raw, ['portal'], ''),
secretGen: _pick(raw, ['secretGen'], ''),
sso: _pick(raw, ['sso'], ''),
ssoToken: _pick(raw, ['ssoToken'], ''),
ssoApp: _pick(raw, ['ssoApp'], ''),
ssoClient: _pick(raw, ['ssoClient'], ''),
raw: raw,
);
}
final String baseUrl;
final String user;
final String db;
final String token;
final String client;
final String locale;
final String timezone;
final String portal;
final String secretGen;
final String sso;
final String ssoToken;
final String ssoApp;
final String ssoClient;
final Map<String, dynamic> raw;
Map<String, String> toHeaders() {
final headers = <String, String>{};
void put(String key, String value) {
if (value.isNotEmpty) {
headers[key] = value;
}
}
put('X-user', user);
put('X-Token', token);
put('X-DB', db);
put('x-client', client);
put('x-locale', locale);
put('X-Portal', portal);
put('X-SSO', sso);
put('X-SSO-TOKEN', ssoToken);
put('X-SSO-APP', ssoApp);
put('X-SSO-CLIENT', ssoClient);
put('x-Timezone', _normalizeTimezone(timezone));
put('x-params', '{}');
if (secretGen.isNotEmpty) {
put('x-sec', secretGen);
}
return headers;
}
StudioBackendSession copyWith({
String? user,
String? db,
String? token,
String? client,
String? locale,
String? timezone,
String? portal,
String? secretGen,
String? sso,
String? ssoToken,
String? ssoApp,
String? ssoClient,
Map<String, dynamic>? raw,
}) {
return StudioBackendSession(
baseUrl: baseUrl,
user: user ?? this.user,
db: db ?? this.db,
token: token ?? this.token,
client: client ?? this.client,
locale: locale ?? this.locale,
timezone: timezone ?? this.timezone,
portal: portal ?? this.portal,
secretGen: secretGen ?? this.secretGen,
sso: sso ?? this.sso,
ssoToken: ssoToken ?? this.ssoToken,
ssoApp: ssoApp ?? this.ssoApp,
ssoClient: ssoClient ?? this.ssoClient,
raw: raw ?? this.raw,
);
}
}
class StudioBackendUserProfile {
const StudioBackendUserProfile({
required this.code,
required this.name,
required this.isDeveloper,
required this.portal,
required this.portraitUrl,
required this.raw,
});
factory StudioBackendUserProfile.fromJson(Map<String, dynamic> raw) {
return StudioBackendUserProfile(
code: _pick(raw, ['code', 'userCode', 'id'], ''),
name: _pick(raw, ['name', 'userName'], ''),
isDeveloper: _pick(raw, ['isDeveloper'], 'false').toLowerCase() == 'true',
portal: _pick(raw, ['portal'], ''),
portraitUrl: _pick(raw, ['portraitUrl'], ''),
raw: raw,
);
}
final String code;
final String name;
final bool isDeveloper;
final String portal;
final String portraitUrl;
final Map<String, dynamic> raw;
}
class StudioBackendOrg {
const StudioBackendOrg({
required this.code,
required this.name,
required this.raw,
});
factory StudioBackendOrg.fromJson(Map<String, dynamic> raw) {
return StudioBackendOrg(
code: _pick(raw, ['code', 'orgCode'], ''),
name: _pick(raw, ['name', 'orgName'], ''),
raw: raw,
);
}
final String code;
final String name;
final Map<String, dynamic> raw;
}
class StudioBackendActionResult {
const StudioBackendActionResult({
required this.success,
required this.code,
required this.message,
required this.data,
required this.raw,
});
final bool success;
final String code;
final String message;
final dynamic data;
final Map<String, dynamic> raw;
}
class StudioBackendDownloadResult {
const StudioBackendDownloadResult({
required this.bytes,
required this.filename,
required this.contentType,
required this.statusCode,
required this.rawHeaders,
});
final Uint8List bytes;
final String filename;
final String contentType;
final int statusCode;
final Map<String, List<String>> rawHeaders;
}
class _BackendJsonResponse {
const _BackendJsonResponse({required this.json, required this.statusCode});
factory _BackendJsonResponse.fromResponse(Response<dynamic> response) {
final data = response.data;
final statusCode = response.statusCode ?? 0;
final location = response.headers.value('location') ?? '';
if (data is String) {
try {
final decoded = jsonDecode(data);
if (decoded is Map<String, dynamic>) {
return _BackendJsonResponse(
json: _withHttpMeta(decoded, statusCode, location),
statusCode: statusCode,
);
}
return _BackendJsonResponse(
json: _withHttpMeta(
<String, dynamic>{'data': decoded},
statusCode,
location,
),
statusCode: statusCode,
);
} catch (_) {
return _BackendJsonResponse(
json: _withHttpMeta(
<String, dynamic>{'raw': data},
statusCode,
location,
),
statusCode: statusCode,
);
}
}
if (data is Map<String, dynamic>) {
return _BackendJsonResponse(
json: _withHttpMeta(data, statusCode, location),
statusCode: statusCode,
);
}
return _BackendJsonResponse(
json: _withHttpMeta(
<String, dynamic>{'data': data},
statusCode,
location,
),
statusCode: statusCode,
);
}
static Map<String, dynamic> _withHttpMeta(
Map<String, dynamic> json,
int statusCode,
String location,
) {
return <String, dynamic>{
...json,
'_httpStatus': statusCode,
if (location.isNotEmpty) '_location': location,
};
}
final Map<String, dynamic> json;
final int statusCode;
}