diff --git a/app/package.json b/app/package.json index cd65c0e..9501aaf 100644 --- a/app/package.json +++ b/app/package.json @@ -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" diff --git a/app/src/utils/localStorageSet.js b/app/src/utils/localStorageSet.js new file mode 100644 index 0000000..76bb593 --- /dev/null +++ b/app/src/utils/localStorageSet.js @@ -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 + } +} \ No newline at end of file diff --git a/app/src/vuex/modules/fileList.js b/app/src/vuex/modules/fileList.js index 965071b..a0ede86 100644 --- a/app/src/vuex/modules/fileList.js +++ b/app/src/vuex/modules/fileList.js @@ -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'], @@ -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 @@ -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 @@ -53,6 +66,3 @@ export default { mutations } -function isBoolean(val) { - return val === true || val === false -} \ No newline at end of file diff --git a/app/src/vuex/modules/filterList.js b/app/src/vuex/modules/filterList.js index 4fb7a9b..cb93a72 100644 --- a/app/src/vuex/modules/filterList.js +++ b/app/src/vuex/modules/filterList.js @@ -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: {}, // 筛选条件列表 @@ -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 @@ -177,7 +179,4 @@ export default { mutations } -function isBoolean(val) { - return val === true || val === false -}