15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const Storage = require('../storage');
const ConfigCache = require('./cache');
var SystemDb = undefined;
 
const Cfg = {
 
  /**
   * 获取 coredb
   */
  _getCoreDB() {
    // [todo] 要么每次new对象,要么所有地方都用同一个实例,否则会出现数据无法刷新的情况
    SystemDb = Storage.connection('system');
    return SystemDb;
  },
 
  /**
   * all
   */
  all(fromCache = true) {
    if (fromCache === true) {
      // 如果子进程
      const cacheValue = ConfigCache.all();
      return cacheValue;
    }
    const cdb = this._getCoreDB();
    const config = cdb.getItem('config');
  
    return config;
  },
 
  /**
   * setAll
   */
  setAll(value) {
    const cdb = this._getCoreDB();
    cdb.setItem('config', value);
  
    return;
  },
 
  /**
   * setValue
   */
  setValue(key, value) {
    const cdb = this._getCoreDB();
    cdb.setConfigItem(key, value);
  
    return;
  },
 
  /**
   * getValue
   */
  getValue(key, fromCache = true) {
    if (fromCache === true) {
      const cacheValue = ConfigCache.getValue(key);
      return cacheValue;
    }
 
    const cdb = this._getCoreDB();
    let v = cdb.getConfigItem(key);
  
    return v;
  },  
 
  /**
   * isFileProtocol
   */
  isFileProtocol(config) {
    if (config.protocol == 'file://') {
      return true;
    }
    return false;
  },
 
  /**
   * isWebProtocol
   */
  isWebProtocol(config) {
    if (['http://', 'https://'].includes(config.protocol)) {
      return true;
    }
    return false;
  },   
};
 
module.exports = Cfg;