| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- import {DbStorage} from "./DbStorage";
- import {Event} from '../../utils/Event'
- import { AppEvent } from '../../event/AppEvent'
- import { app } from 'electron'
- import {logger} from "ee-core/log";
- /**
- * 应用配置服务
- */
- class AppConfigService {
- private static tableName: String = 'app_config'
- public _init() {
- const $this = this
- Event.on(AppEvent.APP_START, () => {
- $this.createTable()
- $this.setAppAutoStart()
- })
- }
- public createTable() {
- if (!DbStorage.checkTableExists(AppConfigService.tableName)) {
- const sql = `CREATE TABLE IF NOT EXISTS ${AppConfigService.tableName} (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- socketPort VARCHAR(128),
- httpProto VARCHAR(32),
- apiUrl VARCHAR(200),
- defaultPrintName VARCHAR(225),
- autoStart INTEGER(2),
- createdAt TEXT DEFAULT (datetime('now', 'localtime'))
- )`
- DbStorage.getDb().exec(sql)
- }
- }
- /**
- * 获取配置
- */
- public async getConfig() {
- return new Promise(async (resolve, _) => {
- const sql = `SELECT * FROM ${AppConfigService.tableName} ORDER BY id DESC LIMIT 1`
- let stmt = DbStorage.getDb().prepare(sql)
- const config = stmt.get();
- if (!config) {
- await this.insertConfig({
- socketPort: '1725',
- httpProto: 'http',
- apiUrl: 'localhost:8080',
- defaultPrintName: '',
- autoStart: 1
- })
- stmt = DbStorage.getDb().prepare(sql)
- resolve(stmt.get())
- }else {
- resolve(config)
- }
- })
- }
- /**
- * 插入配置
- * @param config
- */
- public async insertConfig(config: any) {
- const sql = `INSERT INTO ${AppConfigService.tableName} (socketPort, httpProto, apiUrl, defaultPrintName, autoStart) VALUES (?, ?, ?, ?, ?)`
- const stmt = DbStorage.getDb().prepare(sql)
- return stmt.run(config.socketPort, config.httpProto, config.apiUrl, config.defaultPrintName, config.autoStart)
- }
- /**
- * 更新配置
- * @param config
- */
- public async updateConfig(config: any) {
- if (!config.id) {
- const getConfig = await this.getConfig();
- // @ts-ignore
- config.id = getConfig.id
- }
- const sql = `UPDATE ${AppConfigService.tableName} SET socketPort = ?, httpProto = ?, apiUrl = ?, autoStart = ? WHERE id = ?`
- const stmt = DbStorage.getDb().prepare(sql)
- // @ts-ignore
- return stmt.run(config.socketPort, config.httpProto, config.apiUrl, config.autoStart ? 1 : 0, config.id)
- }
- public async updateDefaultPrintName(printName: string) {
- const config = await this.getConfig();
- const sql = `UPDATE ${AppConfigService.tableName} SET defaultPrintName = ? WHERE id = ?`
- const stmt = DbStorage.getDb().prepare(sql)
- // @ts-ignore
- return stmt.run(printName, config.id)
- }
- public setAppAutoStart() {
- this.getConfig().then(config => {
- if (!config) {
- return
- }
- // @ts-ignore
- const execPath = process.execPath;
- // @ts-ignore
- const enable = config.autoStart == 1
- app.setLoginItemSettings({
- openAtLogin: enable,
- openAsHidden: true,
- path: enable ? execPath : undefined,
- args: enable ? ['--hidden'] : undefined
- })
- }).catch((e: any) => {
- logger.error("App set start at auto ex ---> ", e)
- });
- }
- }
- AppConfigService.toString = () => '[class AppConfigService]'
- const appConfigService = new AppConfigService()
- export {
- AppConfigService,
- appConfigService
- }
|