Skip to content

Commit

Permalink
fixed bug: localStorage and json.parse()
Browse files Browse the repository at this point in the history
  • Loading branch information
JChehe committed Nov 17, 2016
1 parent e805140 commit d91ae7a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"main": "electron.js",
"dependencies": {
"font-awesome": "^4.6.3",
"lodash": "^4.17.2",
"moment": "^2.15.0",
"shortid": "^2.2.6",
"vue": "^2.0.0",
"vue-electron": "^1.0.0",
"vue-multiselect": "^1.1.3",
"vue-resource": "^0.7.0",
"vue-resource": "^1.0.3",
"vue-router": "^2.0.0",
"vuex": "^1.0.0",
"xlsx": "^0.8.0"
Expand Down
38 changes: 38 additions & 0 deletions app/src/utils/localStorageSet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export function getLocal(key) {
let localStorage = window.localStorage,
valStr

if(key !== undefined && key !== null) {
valStr = localStorage.getItem(key)
} else {
return false
}

if(valStr !== 'undefined' && valStr !== 'null') {
try {
return JSON.parse(valStr)
} catch(e) {
console.log(`localStorage 的 ${key} 属性解析失败`)
return false
}
} else {
return false
}
}


export function setLocal(key, val) {
let localStorage = window.localStorage

if(key !== undefined && key !== null) {
try {
localStorage.setItem(key, JSON.stringify(val))
} catch (e) {
console.log(`localStorage 的 ${key}:${val}序列化失败`)
return false
}
} else {
console.log(`localStorage 的 ${key} 是非法值 undefined/null`)
return false
}
}
24 changes: 17 additions & 7 deletions app/src/vuex/modules/fileList.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import * as types from '../mutation-types'
import { getLocal, setLocal } from '../../utils/localStorageSet'
import _ from 'lodash'
import { ipcRenderer } from 'electron'

let uploadFiles = JSON.parse(window.localStorage.uploadFiles).length > 0 ? JSON.parse(window.localStorage.uploadFiles) : []


let uploadFiles = (function initUploadFiles() {
let localUploadFiles = getLocal('uploadFiles')
if(_.isArray(localUploadFiles)) {
return localUploadFiles
} else {
return []
}
})();
console.log('uploadFiles', uploadFiles)

const state = {
fileList: uploadFiles, // 最近的excel文件列表(sidebar)
allFileType: ['all', 'xls', 'xlsx'],
Expand All @@ -12,7 +25,7 @@ const state = {

const mutations = {
[types.TOGGLE_SIDEBAR] (state, val) {
if(isBoolean(val) ){
if(_.isBoolean(val) ){
state.isShowSideBar = val
}else{
state.isShowSideBar = !state.isShowSideBar
Expand All @@ -37,11 +50,11 @@ const mutations = {
}else{
state.fileList.unshift(val)
}
window.localStorage.setItem('uploadFiles', JSON.stringify(state.fileList))
setLocal('uploadFiles', state.fileList)
},
[types.DEL_UPLOAD_FILES] (state, index) {
state.fileList.splice(index, 1)
window.localStorage.setItem('uploadFiles', JSON.stringify(state.fileList))
setLocal('uploadFiles', state.fileList)
},
[types.SET_UPLOAD_STATUS] (state, val) {
state.fileStatus = val
Expand All @@ -53,6 +66,3 @@ export default {
mutations
}

function isBoolean(val) {
return val === true || val === false
}
13 changes: 6 additions & 7 deletions app/src/vuex/modules/filterList.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as types from '../mutation-types'
import * as ExcelSet from '../../utils/ExcelSet'
import { getLocal, setLocal } from '../../utils/localStorageSet'
import _ from 'lodash'
import { ipcRenderer } from 'electron'

const SUFFIX_COLKEYS = '_headers'

let filterWay = JSON.parse(window.localStorage.filterWay)
? JSON.parse(window.localStorage.filterWay) : 0
let filterWay = getLocal('filterWay')
? getLocal('filterWay') : 0

const state = {
filterTagList: {}, // 筛选条件列表
Expand Down Expand Up @@ -148,11 +150,11 @@ const mutations = {

[types.SET_FILTER_WAY] (state, val) {
state.filterWay = val
window.localStorage.setItem('filterWay', JSON.stringify(val))
setLocal('filterWay', val)
},

[types.TOGGLE_FILTER_PANEL_STATUS] (state, val) {
if(isBoolean(val)) {
if(_.isBoolean(val)) {
state.isShowFillterPanel = val
}else{
state.isShowFillterPanel = !state.isShowFillterPanel
Expand All @@ -177,7 +179,4 @@ export default {
mutations
}

function isBoolean(val) {
return val === true || val === false
}

0 comments on commit d91ae7a

Please sign in to comment.