UNPKG

apass-opensdk-hugong

Version:

飞书Apass低代码平台-飞书开放平台-相关的接口整合和常用的方法整合

153 lines (150 loc) 4.04 kB
const Utils = require('./utils/index') const Object_ = require('./apass/object_') class HG { #logger constructor(logger) { this.setLogger(logger) this.object = new Object_(this) this.utils = new Utils(this) this._time = null } async setLogger(logger) { this.#logger = logger } /** * 运行函数 * @param {*} fn * @returns */ async timeRun(fn) { this.newTime() let result = null if (fn) { result = await fn() } this.printTime() return result } /** * 线程睡眠 * @param {*} time 毫秒 * @returns */ async sleep(time) { return new Promise((r) => setTimeout(() => { this.log4(`sleep ${(time || 1000) / 1000}s`); r(); }, time || 1000)) } /** * 时间计划-记录 */ newTime() { if (this._time) { this.log4('#time reset to before', (Date.now() - this._time) / 1000, 's') } this.log4('#time reset') this._time = Date.now() } /** * 时间计划-打印耗时 */ printTime() { if (!this._time) { return this.#logger.error('#time Error', 'The time has not been initialized ') } this.log4('#time', (Date.now() - this._time) / 1000, 's') } /** * 统一网络请求(使用axios) * @param {*} config * @returns */ async request(config) { this.log4('req =', config) return new Promise((r, s) => { const now = Date.now() axios(config).then(response => { this.log8('resp =', (Date.now() - now) / 1000, 's', response.status, response.statusText, response.data) r(response.data) }).catch(err => { this.#logger.error('resp =', (Date.now() - now) / 1000, 's', err.response.data) s(err.response.data) }) }) } /** * 直接调用axios * @param {*} config * @returns */ axios(config) { return axios(config) } /** * 生成多语言对象(将开放平台返回的多语言对象转换为application.constants.type.Multilingual) * @param {*} zh * @param {*} en * @returns */ toMultilingualByOpenPlatform(zh, en) { if (Array.isArray(zh)) { if (zh[0].hasOwnProperty('lang')) { const _zh = this.toValue(zh.find(it => it.lang == 'zh-CN'), 'value') const _en = this.toValue(zh.find(it => it.lang == 'en-US'), 'value') return new application.constants.type.Multilingual({ zh: _zh || _en, en: _en || _zh }) } if (zh[0].hasOwnProperty('locale')) { const _zh = this.toValue(zh.find(it => it.locale == 'zh_CN' || it.locale == 'zh-CN'), 'value') const _en = this.toValue(zh.find(it => it.locale == 'en_US' || it.locale == 'en-US'), 'value') return new application.constants.type.Multilingual({ zh: _zh || _en, en: _en || _zh }) } } return new application.constants.type.Multilingual({ zh: zh || en, en: en || zh }) } /** * 从多语言对象中获取中文 * @param {*} textArr * @returns */ toTextByMultilingual(textArr) { if (!textArr || !textArr.length) return '--' return textArr.find(it => it.language_code == '2052').text } /** * 从JSON中根据路径获取值 * @param {*} obj * @param {*} path * @param {*} defaultValue * @returns */ toValue(obj, path, defaultValue = undefined) { return path.split('.').reduce((acc, key) => acc?.[key], obj) ?? defaultValue; } listFind(list, key, target, defValue) { return list.find(it => it[key] == target) || defValue } listMap(list, key) { return list.map(it => it[key]) } textToFloat(textAmount, defValue = 0) { return parseFloat((textAmount || '').replace(/,/g, '') || defValue); } log(...arg) { this.#logger.log(...arg) } log4(...arg) { this.logm(4, ...arg) } log8(...arg) { this.logm(8, ...arg) } logm(space, ...arg) { const result = ''.padEnd(space, "-"); this.#logger.log(result, ...arg) } error(...arg) { this.#logger.error(...arg) } warn(...arg) { this.#logger.warn(...arg) } } module.exports = HG