index.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { useRoute } from 'vue-router'
  2. import { queryGoodsList } from '@/services/api/goods'
  3. import { queryTaAccounts } from '@/services/api/account'
  4. import { globalState, sessionCache, localCache } from '@/store'
  5. import eventBus from '@/services/bus'
  6. import socket from '@/services/socket'
  7. export const business = new (class {
  8. logoutNotify;
  9. moneyChangedNotify;
  10. constructor() {
  11. // 接收用户登出通知
  12. this.logoutNotify = eventBus.$on('logoutNotify', () => {
  13. socket.closeQuoteServer();
  14. socket.closeTradeServer();
  15. })
  16. // 接收资金变动通知
  17. this.moneyChangedNotify = eventBus.$on('moneyChangedNotify', () => {
  18. getTaAccounts();
  19. })
  20. }
  21. })
  22. /**
  23. * 初始化业务数据
  24. * @param callback
  25. */
  26. export async function initBaseData(callback?: () => void) {
  27. const getGoodsList = queryGoodsList({
  28. success: (res) => {
  29. globalState.setValue('goodsList', res.data);
  30. },
  31. fail: (err) => {
  32. console.error('商品请求失败', err);
  33. }
  34. })
  35. await Promise.all([getGoodsList]);
  36. callback && callback();
  37. }
  38. /**
  39. * 获取资金账户列表
  40. * @returns
  41. */
  42. export function getTaAccounts() {
  43. return queryTaAccounts({
  44. data: {
  45. loginID: getLoginInfo('LoginID')
  46. },
  47. success: (res) => {
  48. globalState.setValue('accountList', res.data);
  49. }
  50. })
  51. }
  52. /**
  53. * 获取登录信息
  54. * @param key
  55. * @returns
  56. */
  57. export function getLoginInfo<Key extends keyof Proto.LoginRsp>(key: Key) {
  58. return localCache.getValue('loginInfo')[key] || sessionCache.getValue('loginInfo')[key];
  59. }
  60. /**
  61. * 获取用户操作权限
  62. * @param filtered 过滤的数据项
  63. * @param reverse 是否反向过滤
  64. * @returns
  65. */
  66. export function getAccountAuth(filtered: string[] = [], reverse = false) {
  67. const route = useRoute();
  68. const result = route.meta.auth as Ermcp.AccountMenu.Auth[];
  69. if (filtered.length) {
  70. if (reverse) {
  71. // 返回除指定的权限代码
  72. return result.filter((e) => !filtered.includes(e.code));
  73. } else {
  74. // 返回指定的权限代码
  75. return result.filter((e) => filtered.includes(e.code));
  76. }
  77. }
  78. return result;
  79. }