UNPKG

@ithinkdt/core

Version:

iThinkDT Core

114 lines (103 loc) 3.47 kB
import { message } from '@ithinkdt/common' import { initStore } from './store' import { initAuth, auth as _auth } from './auth' import { initDict } from './dict' import { initI18n } from './i18n' import { initHttp } from './http' import { $msg, initFeedback } from './feedback' import { initTheme, theme as _theme, usePageTab } from './theme' import { initPage } from './page' export function ithinkdt(ops) { console.debug('[frame] initing...') const store = initStore({ ...ops.store, getUsername: () => _auth.user?.username ?? '' }) const i18n = initI18n(ops.i18n) const dict = initDict(ops.dict) const feedback = initFeedback(ops.feedback) if (ops.http.baseURL.startsWith('//')) { ops.http.baseURL = location.protocol + ops.http.baseURL } const url = ops.http.baseURL.startsWith('/') ? { pathname: ops.http.baseURL, } : new URL(ops.http.baseURL) const { install: auth, jwt, router, } = initAuth({ router: { base: ops.base, routes: ops.routes, index: ops.index, type: ops.type, Lowcode: ops.Lowcode, ...ops.views, }, locale: i18n.global.locale, ...ops.auth, jwt: { cookieDomain: ops.auth.domain || url.hostname || undefined, cookiePath: url.pathname, ...ops.auth.jwt, }, }) const http = initHttp({ ...ops.http, interceptors: [ ...(ops.http.interceptors ?? []), { name: 'message', interceptor: message((err, req) => { console.debug(req.url, err.message ?? err.msg, err) $msg.error(err.message ?? err.msg) }), order: Number.NEGATIVE_INFINITY, }, { name: 'transition', interceptor: (req, next) => { return _theme?.transition ? next(req).then(async (res) => { if (_theme.transition) { await _theme.transition } return res }) : next(req) }, order: -Number.NEGATIVE_INFINITY, }, jwt, i18n.lang, ].filter((it) => !!it), }) const theme = initTheme({ ...ops.theme, Error: ops.views.Error, router }) const page = initPage({ ...ops.page, getUsername: () => _auth.user?.username, getAppCode: () => _auth.app?.appCode, waitTransition: () => _theme.transition, useTableKey: () => { try { const { tab } = usePageTab() return tab?.tabKey || router.currentRoute.value?.path } catch { return router.currentRoute.value?.path } }, }) console.debug(`[frame] inited!`) return { router, store, i18n, page, install(app) { app.directive('tooltip', ops.directives.tooltip) app.directive('spin', ops.directives.spin) app.use(store).use(http).use(auth).use(i18n).use(dict).use(feedback).use(theme).use(page) console.debug(`[frame] installed in app!`) }, } }