AppConfigService.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {DbStorage} from "./DbStorage";
  2. import {Event} from '../../utils/Event'
  3. import { AppEvent } from '../../event/AppEvent'
  4. import { app } from 'electron'
  5. import {logger} from "ee-core/log";
  6. /**
  7. * 应用配置服务
  8. */
  9. class AppConfigService {
  10. private static tableName: String = 'app_config'
  11. public _init() {
  12. const $this = this
  13. Event.on(AppEvent.APP_START, () => {
  14. $this.createTable()
  15. $this.setAppAutoStart()
  16. })
  17. }
  18. public createTable() {
  19. if (!DbStorage.checkTableExists(AppConfigService.tableName)) {
  20. const sql = `CREATE TABLE IF NOT EXISTS ${AppConfigService.tableName} (
  21. id INTEGER PRIMARY KEY AUTOINCREMENT,
  22. socketPort VARCHAR(128),
  23. httpProto VARCHAR(32),
  24. apiUrl VARCHAR(200),
  25. defaultPrintName VARCHAR(225),
  26. autoStart INTEGER(2),
  27. createdAt TEXT DEFAULT (datetime('now', 'localtime'))
  28. )`
  29. DbStorage.getDb().exec(sql)
  30. }
  31. }
  32. /**
  33. * 获取配置
  34. */
  35. public async getConfig() {
  36. return new Promise(async (resolve, _) => {
  37. const sql = `SELECT * FROM ${AppConfigService.tableName} ORDER BY id DESC LIMIT 1`
  38. let stmt = DbStorage.getDb().prepare(sql)
  39. const config = stmt.get();
  40. if (!config) {
  41. await this.insertConfig({
  42. socketPort: '1725',
  43. httpProto: 'http',
  44. apiUrl: 'localhost:8080',
  45. defaultPrintName: '',
  46. autoStart: 1
  47. })
  48. stmt = DbStorage.getDb().prepare(sql)
  49. resolve(stmt.get())
  50. }else {
  51. resolve(config)
  52. }
  53. })
  54. }
  55. /**
  56. * 插入配置
  57. * @param config
  58. */
  59. public async insertConfig(config: any) {
  60. const sql = `INSERT INTO ${AppConfigService.tableName} (socketPort, httpProto, apiUrl, defaultPrintName, autoStart) VALUES (?, ?, ?, ?, ?)`
  61. const stmt = DbStorage.getDb().prepare(sql)
  62. return stmt.run(config.socketPort, config.httpProto, config.apiUrl, config.defaultPrintName, config.autoStart)
  63. }
  64. /**
  65. * 更新配置
  66. * @param config
  67. */
  68. public async updateConfig(config: any) {
  69. if (!config.id) {
  70. const getConfig = await this.getConfig();
  71. // @ts-ignore
  72. config.id = getConfig.id
  73. }
  74. const sql = `UPDATE ${AppConfigService.tableName} SET socketPort = ?, httpProto = ?, apiUrl = ?, autoStart = ? WHERE id = ?`
  75. const stmt = DbStorage.getDb().prepare(sql)
  76. // @ts-ignore
  77. return stmt.run(config.socketPort, config.httpProto, config.apiUrl, config.autoStart ? 1 : 0, config.id)
  78. }
  79. public async updateDefaultPrintName(printName: string) {
  80. const config = await this.getConfig();
  81. const sql = `UPDATE ${AppConfigService.tableName} SET defaultPrintName = ? WHERE id = ?`
  82. const stmt = DbStorage.getDb().prepare(sql)
  83. // @ts-ignore
  84. return stmt.run(printName, config.id)
  85. }
  86. public setAppAutoStart() {
  87. this.getConfig().then(config => {
  88. if (!config) {
  89. return
  90. }
  91. // @ts-ignore
  92. const execPath = process.execPath;
  93. // @ts-ignore
  94. const enable = config.autoStart == 1
  95. app.setLoginItemSettings({
  96. openAtLogin: enable,
  97. openAsHidden: true,
  98. path: enable ? execPath : undefined,
  99. args: enable ? ['--hidden'] : undefined
  100. })
  101. }).catch((e: any) => {
  102. logger.error("App set start at auto ex ---> ", e)
  103. });
  104. }
  105. }
  106. AppConfigService.toString = () => '[class AppConfigService]'
  107. const appConfigService = new AppConfigService()
  108. export {
  109. AppConfigService,
  110. appConfigService
  111. }