Skip to content

Commit 823377f

Browse files
committed
fixed: storage util
add: vue-ls lib
1 parent 2b4427e commit 823377f

File tree

12 files changed

+71
-38
lines changed

12 files changed

+71
-38
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"viser-vue": "^2.3.0",
2121
"vue": "^2.5.17",
2222
"vue-cropper": "^0.4.0",
23+
"vue-ls": "^3.2.0",
2324
"vue-router": "^3.0.1",
2425
"vuex": "^3.0.1"
2526
},

src/components/layout/LayoutMain.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
:menus="menus"
2323
:theme="theme"
2424
:mode="menuMode"
25-
:collapsed="!siderOpen || collapsed"
25+
:collapsed="collapsed"
2626
:collapsible="true"></sider-menu>
2727

2828
<a-layout>
@@ -45,7 +45,7 @@
4545
import SiderMenu from '@/components/menu/SiderMenu'
4646
import LayoutHeader from './LayoutHeader'
4747
import LayoutFooter from './LayoutFooter'
48-
import {mapState} from 'vuex'
48+
import { mapState, mapActions } from 'vuex'
4949
5050
export default {
5151
name: "LayoutView",
@@ -70,19 +70,22 @@
7070
computed: {
7171
...mapState({
7272
mainMenu: state => state.permission.addRouters,
73-
siderOpen: state => state.app.sidebar.opened,
73+
sidebarOpened: state => state.app.sidebar.opened,
7474
theme: state => state.app.theme,
7575
device: state => state.app.device,
7676
})
7777
},
78+
mounted() {
79+
this.collapsed = this.sidebarOpened
80+
},
7881
methods: {
82+
...mapActions(['setSidebar']),
7983
toggle() {
8084
this.collapsed = !this.collapsed;
85+
this.setSidebar(this.collapsed)
8186
},
8287
menuSelect() {
83-
8488
if (this.device !== 'desktop') {
85-
console.log('selected')
8689
this.collapsed = false
8790
}
8891
}

src/components/table/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ export default {
5757
});
5858
},
5959
pageSize(val) {
60+
console.log('pageSize:', val)
6061
Object.assign(this.localPagination, {
6162
pageSize: val
6263
});
6364
},
6465
showSizeChanger(val) {
66+
console.log('showSizeChanger', val)
6567
Object.assign(this.localPagination, {
6668
showSizeChanger: val
6769
});
@@ -111,7 +113,6 @@ export default {
111113
});
112114

113115
!r.totalCount && ['auto', false].includes(this.showPagination) && (this.localPagination = false)
114-
console.log(this.localPagination);
115116
this.localDataSource = r.data; // 返回结果中的数组数据
116117
this.localLoading = false
117118
});
@@ -216,6 +217,7 @@ export default {
216217
return props[k] = _vm[k];
217218
})
218219

220+
219221
// 显示信息提示
220222
if (this.showAlertInfo) {
221223

@@ -248,7 +250,7 @@ export default {
248250
]);
249251

250252
}
251-
253+
/*
252254
return h("a-table", {
253255
tag: "component",
254256
attrs: props,
@@ -257,5 +259,7 @@ export default {
257259
},
258260
scopedSlots: this.$scopedSlots
259261
}, this.$slots.default);
262+
*/
263+
260264
}
261265
};

src/main.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Vue from 'vue'
22
import App from './App.vue'
3+
import Storage from 'vue-ls'
34
import router from './router/'
45
import store from './store/'
56

@@ -13,18 +14,33 @@ import * as dayjs from 'dayjs' // 日期时间支持库
1314

1415
import '@/permission' // permission control
1516

17+
import { ACCESS_TOKEN, DEFAULT_THEME, SIDEBAR_TYPE } from "@/store/mutation-types"
18+
1619
Vue.filter('dayjs', function(dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
1720
return dayjs(dataStr).format(pattern)
1821
})
1922

23+
const options = {
24+
namespace: 'ant__', // key prefix
25+
name: 'ls', // name variable Vue.[ls] or this.[$ls],
26+
storage: 'local', // storage name session, local, memory
27+
}
28+
29+
Vue.config.productionTip = false
30+
31+
Vue.use(Storage, options)
2032
Vue.use(Antd)
2133
Vue.use(VueAxios, router)
2234
Vue.use(Viser)
2335

24-
Vue.config.productionTip = false
25-
2636
new Vue({
2737
router,
2838
store,
39+
mounted () {
40+
store.commit('SET_SIDEBAR_TYPE', Vue.ls.get(SIDEBAR_TYPE, true))
41+
store.commit('TOGGLE_THEME', Vue.ls.get(DEFAULT_THEME, 'dark'))
42+
store.commit('SET_TOKEN', Vue.ls.get(ACCESS_TOKEN))
43+
44+
},
2945
render: h => h(App)
3046
}).$mount('#app')

src/permission.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import Vue from 'vue'
12
import router from './router'
23
import store from './store'
34

45
import NProgress from 'nprogress' // progress bar
56
import 'nprogress/nprogress.css' // progress bar style
6-
7-
import { getToken } from "./utils/auth"
7+
import { ACCESS_TOKEN } from "@/store/mutation-types"
88

99
NProgress.configure({ showSpinner: false })// NProgress Configuration
1010

@@ -13,7 +13,7 @@ const whiteList = ['/login']// no redirect whitelist
1313
router.beforeEach((to, from, next) => {
1414
NProgress.start() // start progress bar
1515

16-
if (getToken()) {
16+
if (Vue.ls.get(ACCESS_TOKEN)) {
1717
/* has token */
1818
if (to.path === '/login') {
1919
next({ path: '/dashboard/workplace' })

src/store/modules/app.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,39 @@
1-
import { getStore, setStore } from "@/utils/storage"
2-
3-
let theme = getStore('_DEFAULT_THEME')
1+
import Vue from 'vue'
2+
import { SIDEBAR_TYPE, DEFAULT_THEME } from "@/store/mutation-types"
43

54
const app = {
65
state: {
76
sidebar: {
8-
opened: !+getStore('sidebarStatus'),
7+
opened: true,
98
withoutAnimation: false
109
},
1110
device: 'desktop',
12-
theme: !theme ? 'dark' : theme
11+
theme: ''
1312
},
1413
mutations: {
15-
TOGGLE_SIDEBAR: state => {
16-
if (state.sidebar.opened) {
17-
setStore('sidebarStatus', 1)
18-
} else {
19-
setStore('sidebarStatus', 0)
20-
}
21-
state.sidebar.opened = !state.sidebar.opened
22-
state.sidebar.withoutAnimation = false
14+
SET_SIDEBAR_TYPE: (state, type) => {
15+
state.sidebar.opened = type
16+
Vue.ls.set(SIDEBAR_TYPE, type)
2317
},
2418
CLOSE_SIDEBAR: (state, withoutAnimation) => {
25-
setStore('sidebarStatus', 1)
19+
Vue.ls.set(SIDEBAR_TYPE, true)
2620
state.sidebar.opened = false
2721
state.sidebar.withoutAnimation = withoutAnimation
2822
},
2923
TOGGLE_DEVICE: (state, device) => {
3024
state.device = device
3125
},
3226
TOGGLE_THEME: (state, theme) => {
33-
setStore('_DEFAULT_THEME', theme)
27+
// setStore('_DEFAULT_THEME', theme)
28+
Vue.ls.set(DEFAULT_THEME, theme)
3429
state.theme = theme
3530
}
3631
},
3732
actions: {
38-
ToggleSideBar: ({ commit }) => {
39-
commit('TOGGLE_SIDEBAR')
33+
setSidebar: ({ commit }, type) => {
34+
commit('SET_SIDEBAR_TYPE', type)
4035
},
41-
CloseSideBar({ commit }, { withoutAnimation }) {
36+
CloseSidebar({ commit }, { withoutAnimation }) {
4237
commit('CLOSE_SIDEBAR', withoutAnimation)
4338
},
4439
ToggleDevice({ commit }, device) {

src/store/modules/user.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import Vue from 'vue'
12
import { login, getInfo, logout } from "@/api/login"
2-
import { setToken, getToken, removeToken } from '@/utils/auth'
3+
import { ACCESS_TOKEN } from "@/store/mutation-types"
34
import { welcome } from "@/utils/util"
45

56
const user = {
67
state: {
7-
token: getToken(),
8+
token: '',
89
name: '',
910
welcome: '',
1011
avatar: '',
@@ -37,7 +38,7 @@ const user = {
3738
return new Promise((resolve, reject) => {
3839
login(userInfo).then(response => {
3940
const result = response.result
40-
setToken(result.token)
41+
Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
4142
commit('SET_TOKEN', result.token)
4243
resolve()
4344
}).catch(error => {
@@ -83,7 +84,7 @@ const user = {
8384
return new Promise((resolve) => {
8485
commit('SET_TOKEN', '')
8586
commit('SET_ROLES', [])
86-
removeToken()
87+
Vue.ls.remove(ACCESS_TOKEN)
8788

8889
logout(state.token).then(() => {
8990
resolve()

src/store/mutation-types.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const ACCESS_TOKEN = 'Access-Token'
2+
export const SIDEBAR_TYPE = 'SIDEBAR_TYPE'
3+
export const DEFAULT_THEME = 'DEFAULT_THEME'

src/utils/auth.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { setStore, getStore, clearStore } from "@/utils/storage";
1+
/**
2+
* 弃用
3+
*/
4+
import { setStore, getStore, clearStore } from "@/utils/storage"
25

36
export const TokenKey = 'Access-Token'
47

src/utils/request.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import Vue from 'vue'
12
import axios from 'axios'
23
import store from '../store'
34
import { VueAxios } from './axios'
45
import notification from 'ant-design-vue/es/notification'
5-
import { getToken } from "@/utils/auth"
6-
6+
import { ACCESS_TOKEN } from "@/store/mutation-types"
77

88
// 创建 axios 实例
99
const service = axios.create({
@@ -28,7 +28,7 @@ const err = (error) => {
2828

2929
// request 拦截器
3030
service.interceptors.request.use(config => {
31-
const token = getToken()
31+
const token = Vue.ls.get(ACCESS_TOKEN)
3232
if (token) {
3333
config.headers[ 'Access-Token' ] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
3434
}

0 commit comments

Comments
 (0)