/* * main.ts * Copyright (C) 2024 veypi * 2024-07-04 14:34 * Distributed under terms of the MIT license. */ import './assets/css/oaer.scss' import bus from './bus' import logic from './logic' import ui from './components' import * as typs from './typs' import api from './api' export default new class { private ui?: ui constructor() { } init(host?: string, code?: string) { if (host) { logic.host = host } if (code) { logic.token.refresh.set(code) } return new Promise((resolve, reject) => { logic.init().then((e) => { api.user.Get(e.uid!).then((u) => { resolve(Object.assign(u, { access: e.access! })) }).catch((e) => { reject(e) }) }).catch((e) => { reject(e) }) }) } render_ui(domid = 'voa') { if (!this.ui) { this.ui = new ui() } let root = document.querySelector('#' + domid) if (root) { this.ui.mount(root) } else { console.warn(`not found
`) } } isValid() { return logic.token.refresh.isVaild() } login() { bus.emit('login') } logout() { bus.emit('logout') } on(evt: string, fn: (d?: any) => void) { bus.on(evt, fn) } api() { return { user: { Get: api.user.Get, List: api.user.List, }, app: { Get: api.app.Get, List: api.app.List, }, } } Token() { return logic.token.app.raw() } TokenRefresh() { return new Promise((resolve, reject) => { logic.token.app.update().then((e) => { resolve(e.raw()) }).catch(() => reject) }) } }