@etsoo/smarterp-core
Version:
TypeScript APIs for SmartERP Core
194 lines (193 loc) • 5.29 kB
JavaScript
import { DataTypes } from "@etsoo/shared";
import { BaseApi, ProductUnit, RepeatOption } from "@etsoo/appscript";
const unitPrefix = "unit";
/**
* Public API
* 公共接口
*/
export class PublicApi extends BaseApi {
/**
* Constructor
* @param app Application
* @param api API
*/
constructor(app, api = app.api) {
super(app, api);
}
/**
* Accept invitation
* @param rq Request data
* @param payload Payload
* @returns Result
*/
acceptInvitation(rq, payload) {
return this.api.post("Public/AcceptInvitation", rq, payload);
}
/**
* Create barcode
* @param rq Request data
* @param payload Payload
* @returns Result
*/
createBarcode(rq, payload) {
return this.api.post("Public/CreateBarcode", rq, payload);
}
/**
* Get currency label
* @param currency Currency
* @returns Label
*/
getCurrencyLabel(currency) {
const c = `currency${currency}`;
return this.app.get(c) ?? c;
}
/**
* Get cultures
* @param ids Culture ids to include and order by
* @param payload Payload
* @returns Result
*/
getCultures(ids, payload) {
return this.api.post("Public/GetCultures", ids, {
...payload,
contentType: "application/json"
});
}
/**
* Get currencies
* @param ids Currency ids to include and order by
* @param payload Payload
* @returns Result
*/
getCurrencies(ids, payload) {
return this.api.post("Public/GetCurrencies", ids, {
...payload,
contentType: "application/json"
});
}
/**
* Get custom resources
* @param culture Culture
* @param payload Payload
* @returns Result
*/
getCustomResources(culture, payload) {
return this.api.get(`Public/GetCustomResources/${culture}`, undefined, payload);
}
/**
* Get Pinyin
* @param rq Request data
* @param payload Payload
* @returns Result
*/
getPinyin(rq, payload) {
return this.api.post("Public/GetPinyin", rq, payload);
}
/**
* Get regions
* @param ids Region ids to include and order by
* @param payload Payload
* @returns Result
*/
getRegions(ids, payload) {
return this.api.post("Public/GetRegions", ids, {
...payload,
contentType: "application/json"
});
}
/**
* Get product unit's label
* Please define the label in culture with key 'unitPC' for ProductUnit.PC like that
* @param unit Unit
* @param isJoined Add the join label like 'per Kg' for Kg
* @returns Label
*/
getUnitLabel(unit, isJoined) {
const key = ProductUnit[unit];
const label = this.app.get(unitPrefix + key) ?? key;
const join = this.getUnitJoin(isJoined);
if (join) {
return join.format(label);
}
return label;
}
getUnitJoin(isJoined) {
return typeof isJoined === "string"
? this.app.get(isJoined) ?? isJoined
: isJoined
? this.app.get("unitJoin")
: undefined;
}
/**
* Get mobile base64 QRCode
* @param id User id
* @param host Host URL
* @param payload Payload
*/
mobileQRCode(id, host, payload) {
return this.api.post("Public/MobileQRCode", { id, host }, payload);
}
/**
* Parse China PIN
* @param pin Pin code
* @param payload Payload
* @returns Result
*/
parseChinaPin(pin, payload) {
return this.api.get(`Public/ParseChinaPin/${pin}`, undefined, payload);
}
/**
* Query place
* @param rq Request data
* @param payload Payload
* @returns Result
*/
queryPlace(rq, payload) {
return this.api.post("Public/QueryPlace", rq, payload);
}
/**
* Read member invitation
* @param id Id
* @param payload Payload
* @returns Result
*/
readInvitation(id, payload) {
return this.api.get(`Public/ReadInvitation/${id}`, undefined, payload);
}
/**
*
* Get all repeat options
* @param options Define the order and limit the items
* @param isJoined Add the join label
* @returns Result
*/
repeatOptions(options, isJoined = true) {
options ??= DataTypes.getEnumKeys(RepeatOption);
return this.units(options, isJoined);
}
/**
* Get all supported cultures
* @param payload Payload
* @returns Result
*/
supportedCultures(payload) {
return this.api.get("Public/SupportedCultures", undefined, payload);
}
/**
*
* Get all product units
* @param options Define the order and limit the items
* @param isJoined Add the join label like 'per Kg' for Kg
* @returns Units
*/
units(options, isJoined) {
options ??= DataTypes.getEnumKeys(ProductUnit);
return options.map((key) => {
const id = DataTypes.getEnumByKey(ProductUnit, key);
return {
id,
label: this.getUnitLabel(id, this.getUnitJoin(isJoined)).formatInitial(true)
};
});
}
}