import { useRoute } from 'vue-router' import { queryGoodsList } from '@/services/api/goods' import { queryTaAccounts } from '@/services/api/account' import { globalState, sessionCache, localCache } from '@/store' import eventBus from '@/services/bus' import socket from '@/services/socket' export const business = new (class { logoutNotify; moneyChangedNotify; constructor() { // 接收用户登出通知 this.logoutNotify = eventBus.$on('logoutNotify', () => { socket.closeQuoteServer(); socket.closeTradeServer(); }) // 接收资金变动通知 this.moneyChangedNotify = eventBus.$on('moneyChangedNotify', () => { getTaAccounts(); }) } }) /** * 初始化业务数据 * @param callback */ export async function initBaseData(callback?: () => void) { const getGoodsList = queryGoodsList({ success: (res) => { globalState.setValue('goodsList', res.data); }, fail: (err) => { console.error('商品请求失败', err); } }) await Promise.all([getGoodsList]); callback && callback(); } /** * 获取资金账户列表 * @returns */ export function getTaAccounts() { return queryTaAccounts({ data: { loginID: getLoginInfo('LoginID') }, success: (res) => { globalState.setValue('accountList', res.data); } }) } /** * 获取登录信息 * @param key * @returns */ export function getLoginInfo(key: Key) { return localCache.getValue('loginInfo')[key] || sessionCache.getValue('loginInfo')[key]; } /** * 获取用户操作权限 * @param filtered 过滤的数据项 * @param reverse 是否反向过滤 * @returns */ export function getAccountAuth(filtered: string[] = [], reverse = false) { const route = useRoute(); const result = route.meta.auth as Ermcp.AccountMenu.Auth[]; if (filtered.length) { if (reverse) { // 返回除指定的权限代码 return result.filter((e) => !filtered.includes(e.code)); } else { // 返回指定的权限代码 return result.filter((e) => filtered.includes(e.code)); } } return result; }