index.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { reactive } from 'vue'
  2. import axios from 'axios'
  3. import plus from '@/utils/h5plus'
  4. interface AppConfig {
  5. appId: string;
  6. appName: string; // 应用名称
  7. version: string; // 应用版本
  8. versionCode: string; // 应用版本号
  9. apiUrl: string; // API地址
  10. quoteUrl: string; // WS地址
  11. tradeUrl: string; // WS地址
  12. tokenStore: 'session' | 'local'; // token 存储方式
  13. locales: string[]; // 多语言配置
  14. loginVerifyCodeEnabled: boolean; // 是否启用登录验证码
  15. }
  16. export default new (class {
  17. /** 服务配置信息 */
  18. private appConfig = reactive<AppConfig>({
  19. appId: '',
  20. appName: '',
  21. version: '1.0.0',
  22. versionCode: '100000',
  23. apiUrl: '',
  24. quoteUrl: '',
  25. tradeUrl: '',
  26. tokenStore: 'session',
  27. locales: [],
  28. loginVerifyCodeEnabled: true
  29. })
  30. /** 服务初始化完成状态 */
  31. isReady = false
  32. /**
  33. * 手动初始化
  34. */
  35. private onload = Promise.resolve()
  36. /**
  37. * 初始化服务配置
  38. */
  39. private async init() {
  40. const filePath = './config/appconfig.json'
  41. if (plus.hasPlus()) {
  42. const res = await plus.getLocalFileContent(filePath)
  43. Object.assign(this.appConfig, JSON.parse(res))
  44. } else {
  45. const res = await axios(filePath)
  46. Object.assign(this.appConfig, res.data)
  47. }
  48. this.isReady = true
  49. }
  50. /**
  51. * 服务初始化完成时触发
  52. */
  53. onReady() {
  54. if (!this.isReady) {
  55. this.onload = this.init()
  56. }
  57. // 确保当前只有一个初始化实例
  58. return this.onload
  59. }
  60. /**
  61. * 获取服务配置地址
  62. * @param key
  63. * @returns
  64. */
  65. getConfig<K extends keyof AppConfig>(key: K) {
  66. return this.appConfig[key]
  67. }
  68. })