@etsoo/smarterp-core
Version:
TypeScript APIs for SmartERP Core
179 lines (178 loc) • 4.74 kB
JavaScript
import { MemberApi } from "./MemberApi";
import { OrgApi } from "./OrgApi";
import { AppApi } from "./AppApi";
import { PublicApi } from "./PublicApi";
import { UserApi } from "./UserApi";
import { AuthApi, IdentityType, IdentityTypeFlags, UserIdentifierType } from "@etsoo/appscript";
import { AuthCodeApi } from "./AuthCodeApi";
import { DataTypes } from "@etsoo/shared";
import { CoreApiService } from "./dto/org/CoreApiService";
/**
* Core application
* 核心应用程序
*/
export class CoreApp {
app;
api;
_appApi;
/**
* Application API
* 应用程序接口
*/
get appApi() {
return (this._appApi ??= new AppApi(this.app, this.api));
}
_authApi;
/**
* Authentication API
* 认证接口
*/
get authApi() {
return (this._authApi ??= new AuthApi(this.app, this.api));
}
_authCodeApi;
/**
* Authentication API
* 认证接口
*/
get authCodeApi() {
return (this._authCodeApi ??= new AuthCodeApi(this.app, this.api));
}
_memberApi;
/**
* Member API
* 会员接口
*/
get memberApi() {
return (this._memberApi ??= new MemberApi(this.app, this.api));
}
_orgApi;
/**
* Organization API
* 机构接口
*/
get orgApi() {
return (this._orgApi ??= new OrgApi(this.app, this.api));
}
_publicApi;
/**
* Public API
* 公共接口
*/
get publicApi() {
return (this._publicApi ??= new PublicApi(this.app, this.api));
}
_userApi;
/**
* User API
* 用户接口
*/
get userApi() {
return (this._userApi ??= new UserApi(this.app, this.api));
}
/**
* Constructor
* 构造函数
* @param app Base application
* @param api API
*/
constructor(app, api) {
this.app = app;
this.api = api;
}
/**
* Get API service label
* 获取API服务标签
* @param service API service
* @returns Result
*/
getApiService(service) {
if (service == null)
return "";
const key = DataTypes.getEnumKey(CoreApiService, service) ?? `${service}`;
return this.app.get(`apiService${key}`) ?? key;
}
/**
* Get API services
* 获取API服务列表
* @returns List of API services
*/
getApiServices() {
return this.app.getEnumList(CoreApiService, "apiService");
}
/**
* Get app name
* 获取应用名称
* @param data App data
* @returns Name
*/
getAppName(data) {
return (data.localName ?? this.app.get(`app${data.appId ?? data.id}`) ?? data.name);
}
/**
* Get user identifier type label
* 获取用户标识类型标签
* @param type Type
* @returns Label
*/
getIdentifierTypeLabel(type) {
const key = DataTypes.getEnumKey(UserIdentifierType, type) ?? `${type}`;
return this.app.get(`uiType${key}`) ?? key;
}
/**
* Get identity label
* 获取身份标签
* @param identity Identity value
* @param joinChar Join character
* @returns Label(s)
*/
getIdentityLabel(identity, joinChar) {
if (identity == null)
return "";
joinChar ??= ", ";
const identities = this.getIdentities(identity);
return identities.map((r) => r.label).join(joinChar);
}
/**
* Get identity flags label
* 获取身份组合标签
* @param identity Identity value
* @param joinChar Join character
* @returns Label(s)
*/
getIdentityFlagsLabel(identity, joinChar) {
if (identity == null)
return "";
joinChar ??= ", ";
const identities = this.getIdentityFlags(identity);
return identities.map((r) => r.label).join(joinChar);
}
/**
* Get identities
* 获取身份列表
* @param identity Identity value combined
* @returns List
*/
getIdentities(identity) {
if (identity == null)
return this.app.getEnumList(IdentityType, "id");
return this.app.getEnumList(IdentityType, "id", (id, _key) => {
if ((id & identity) > 0)
return id;
});
}
/**
* Get identity flags
* 获取身份标志组合
* @param identity Identity value combined
* @returns List
*/
getIdentityFlags(identity) {
if (identity == null)
return this.app.getEnumList(IdentityTypeFlags, "id", (id) => id > 0 ? id : undefined);
return this.app.getEnumList(IdentityTypeFlags, "id", (id, _key) => {
if ((id & identity) > 0)
return id;
});
}
}