Skip to content

Commit d91ae7a

Browse files
committed
fixed bug: localStorage and json.parse()
1 parent e805140 commit d91ae7a

4 files changed

Lines changed: 63 additions & 16 deletions

File tree

app/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
"main": "electron.js",
88
"dependencies": {
99
"font-awesome": "^4.6.3",
10+
"lodash": "^4.17.2",
1011
"moment": "^2.15.0",
1112
"shortid": "^2.2.6",
1213
"vue": "^2.0.0",
1314
"vue-electron": "^1.0.0",
14-
"vue-multiselect": "^1.1.3",
15-
"vue-resource": "^0.7.0",
15+
"vue-resource": "^1.0.3",
1616
"vue-router": "^2.0.0",
1717
"vuex": "^1.0.0",
1818
"xlsx": "^0.8.0"

app/src/utils/localStorageSet.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export function getLocal(key) {
2+
let localStorage = window.localStorage,
3+
valStr
4+
5+
if(key !== undefined && key !== null) {
6+
valStr = localStorage.getItem(key)
7+
} else {
8+
return false
9+
}
10+
11+
if(valStr !== 'undefined' && valStr !== 'null') {
12+
try {
13+
return JSON.parse(valStr)
14+
} catch(e) {
15+
console.log(`localStorage 的 ${key} 属性解析失败`)
16+
return false
17+
}
18+
} else {
19+
return false
20+
}
21+
}
22+
23+
24+
export function setLocal(key, val) {
25+
let localStorage = window.localStorage
26+
27+
if(key !== undefined && key !== null) {
28+
try {
29+
localStorage.setItem(key, JSON.stringify(val))
30+
} catch (e) {
31+
console.log(`localStorage 的 ${key}:${val}序列化失败`)
32+
return false
33+
}
34+
} else {
35+
console.log(`localStorage 的 ${key} 是非法值 undefined/null`)
36+
return false
37+
}
38+
}

app/src/vuex/modules/fileList.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import * as types from '../mutation-types'
2+
import { getLocal, setLocal } from '../../utils/localStorageSet'
3+
import _ from 'lodash'
24
import { ipcRenderer } from 'electron'
35

4-
let uploadFiles = JSON.parse(window.localStorage.uploadFiles).length > 0 ? JSON.parse(window.localStorage.uploadFiles) : []
6+
7+
8+
let uploadFiles = (function initUploadFiles() {
9+
let localUploadFiles = getLocal('uploadFiles')
10+
if(_.isArray(localUploadFiles)) {
11+
return localUploadFiles
12+
} else {
13+
return []
14+
}
15+
})();
16+
console.log('uploadFiles', uploadFiles)
17+
518
const state = {
619
fileList: uploadFiles, // 最近的excel文件列表(sidebar)
720
allFileType: ['all', 'xls', 'xlsx'],
@@ -12,7 +25,7 @@ const state = {
1225

1326
const mutations = {
1427
[types.TOGGLE_SIDEBAR] (state, val) {
15-
if(isBoolean(val) ){
28+
if(_.isBoolean(val) ){
1629
state.isShowSideBar = val
1730
}else{
1831
state.isShowSideBar = !state.isShowSideBar
@@ -37,11 +50,11 @@ const mutations = {
3750
}else{
3851
state.fileList.unshift(val)
3952
}
40-
window.localStorage.setItem('uploadFiles', JSON.stringify(state.fileList))
53+
setLocal('uploadFiles', state.fileList)
4154
},
4255
[types.DEL_UPLOAD_FILES] (state, index) {
4356
state.fileList.splice(index, 1)
44-
window.localStorage.setItem('uploadFiles', JSON.stringify(state.fileList))
57+
setLocal('uploadFiles', state.fileList)
4558
},
4659
[types.SET_UPLOAD_STATUS] (state, val) {
4760
state.fileStatus = val
@@ -53,6 +66,3 @@ export default {
5366
mutations
5467
}
5568

56-
function isBoolean(val) {
57-
return val === true || val === false
58-
}

app/src/vuex/modules/filterList.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import * as types from '../mutation-types'
22
import * as ExcelSet from '../../utils/ExcelSet'
3+
import { getLocal, setLocal } from '../../utils/localStorageSet'
4+
import _ from 'lodash'
35
import { ipcRenderer } from 'electron'
46

57
const SUFFIX_COLKEYS = '_headers'
68

7-
let filterWay = JSON.parse(window.localStorage.filterWay)
8-
? JSON.parse(window.localStorage.filterWay) : 0
9+
let filterWay = getLocal('filterWay')
10+
? getLocal('filterWay') : 0
911

1012
const state = {
1113
filterTagList: {}, // 筛选条件列表
@@ -148,11 +150,11 @@ const mutations = {
148150

149151
[types.SET_FILTER_WAY] (state, val) {
150152
state.filterWay = val
151-
window.localStorage.setItem('filterWay', JSON.stringify(val))
153+
setLocal('filterWay', val)
152154
},
153155

154156
[types.TOGGLE_FILTER_PANEL_STATUS] (state, val) {
155-
if(isBoolean(val)) {
157+
if(_.isBoolean(val)) {
156158
state.isShowFillterPanel = val
157159
}else{
158160
state.isShowFillterPanel = !state.isShowFillterPanel
@@ -177,7 +179,4 @@ export default {
177179
mutations
178180
}
179181

180-
function isBoolean(val) {
181-
return val === true || val === false
182-
}
183182

0 commit comments

Comments
 (0)