Skip to content

Commit c7988bf

Browse files
committed
更改localstorage
1 parent a70d3ad commit c7988bf

File tree

5 files changed

+128
-3
lines changed

5 files changed

+128
-3
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "easycode-javascript",
3-
"version": "1.0.0",
3+
"version": "1.0.4",
44
"private": true,
55
"scripts": {
66
"serve": "vue-cli-service serve",
@@ -15,7 +15,6 @@
1515
"dependencies": {
1616
"core-js": "^3.6.5",
1717
"ejs": "^3.1.5",
18-
"electron-localstorage": "^1.0.5",
1918
"element-ui": "^2.13.2",
2019
"github-markdown-css": "^4.0.0",
2120
"lodash": "^4.17.20",

src/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import mysql from 'mysql2'
44
import ejs from 'ejs'
55
import { v4 as uuidv4 } from 'uuid'
66
import { exec } from 'child_process'
7-
import localstorage from 'electron-localstorage'
7+
import localstorage from './localstorage'
88
import path from 'path'
99

1010
function queryAllTables (connection, db) {

src/background.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { app, protocol, BrowserWindow, Menu } from 'electron'
44
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
55
import api from './api'
6+
67
// import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
78
const isDevelopment = process.env.NODE_ENV !== 'production'
89

src/localstorage.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
'use strict'
2+
3+
/**
4+
* 读取本地配置
5+
*/
6+
const fs = require('fs')
7+
const path = require('path')
8+
/**
9+
* 判断配置文件是否存在
10+
*/
11+
function isExit () {
12+
let success = true
13+
if (!(localConfig.config && typeof localConfig.config === 'object')) {
14+
success = initConfig()
15+
}
16+
return success
17+
}
18+
/**
19+
* 初始化config
20+
*/
21+
function initConfig () {
22+
try {
23+
const config = readConfig()
24+
if (config) {
25+
localConfig.config = JSON.parse(config)
26+
return true
27+
}
28+
const defalutConfig = {}
29+
const content = JSON.stringify(defalutConfig)
30+
fs.writeFileSync(localConfig.configUrl, content)
31+
localConfig.config = defalutConfig
32+
return true
33+
} catch (e) {
34+
return false
35+
}
36+
}
37+
/**
38+
* 读取文件
39+
*/
40+
function readConfig () {
41+
try {
42+
const result = fs.readFileSync(localConfig.configUrl)
43+
return result
44+
} catch (error) {
45+
return false
46+
}
47+
}
48+
/**
49+
* 写入文件
50+
*/
51+
function writeConfig (value) {
52+
try {
53+
const content = JSON.stringify(value)
54+
fs.writeFileSync(localConfig.configUrl, content)
55+
return true
56+
} catch (e) {
57+
return false
58+
}
59+
}
60+
61+
const localConfig = {
62+
config: null,
63+
configUrl: path.join(__dirname, '../static/localConfig.json'),
64+
setStoragePath: (path) => {
65+
localConfig.configUrl = path
66+
},
67+
getStoragePath: () => {
68+
return localConfig.configUrl
69+
},
70+
getItem: (key) => {
71+
const success = isExit()
72+
if (success) {
73+
const result = localConfig.config[key]
74+
return result || ''
75+
}
76+
return null
77+
},
78+
setItem: (key, value) => {
79+
const success = isExit()
80+
if (success) {
81+
const config = Object.assign({}, localConfig.config)
82+
config[key] = value
83+
const suc = writeConfig(config)
84+
if (suc) {
85+
localConfig.config = config
86+
return true
87+
}
88+
}
89+
return false
90+
},
91+
getAll: () => {
92+
const success = isExit()
93+
if (success) {
94+
return localConfig.config
95+
}
96+
return null
97+
},
98+
removeItem: (key) => {
99+
const value = localConfig.getItem(key)
100+
if (value) {
101+
const config = Object.assign({}, localConfig.config)
102+
delete config[key]
103+
const suc = writeConfig(config)
104+
if (suc) {
105+
localConfig.config = config
106+
return true
107+
}
108+
}
109+
return false
110+
},
111+
clear: () => {
112+
const success = isExit()
113+
if (success) {
114+
const suc = writeConfig({})
115+
if (suc) {
116+
localConfig.config = {}
117+
return true
118+
}
119+
}
120+
return false
121+
}
122+
}
123+
124+
module.exports = localConfig

static/localConfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"setting":{"fileGenerationDirectory":"C:\\Users\\john\\Desktop","templateDirectory":""}}

0 commit comments

Comments
 (0)