init
This commit is contained in:
13
src/store/errLog.js
Normal file
13
src/store/errLog.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const errLog = {
|
||||
state: {
|
||||
errLog: []
|
||||
},
|
||||
pushLog(log) {
|
||||
this.state.errLog.unshift(log)
|
||||
},
|
||||
clearLog() {
|
||||
this.state.errLog = [];
|
||||
}
|
||||
};
|
||||
|
||||
export default errLog;
|
15
src/store/getters.js
Normal file
15
src/store/getters.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const getters = {
|
||||
sidebar: state => state.app.sidebar,
|
||||
livenewsChannels: state => state.app.livenewsChannels,
|
||||
token: state => state.user.token,
|
||||
avatar: state => state.user.avatar,
|
||||
name: state => state.user.name,
|
||||
uid: state => state.user.uid,
|
||||
email: state => state.user.email,
|
||||
introduction: state => state.user.introduction,
|
||||
auth_type: state => state.user.auth_type,
|
||||
status: state => state.user.status,
|
||||
roles: state => state.user.roles,
|
||||
setting: state => state.user.setting
|
||||
};
|
||||
export default getters
|
17
src/store/index.js
Normal file
17
src/store/index.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import Vue from 'vue';
|
||||
import Vuex from 'vuex';
|
||||
import app from './modules/app';
|
||||
import user from './modules/user';
|
||||
import getters from './getters';
|
||||
|
||||
Vue.use(Vuex);
|
||||
|
||||
const store = new Vuex.Store({
|
||||
modules: {
|
||||
app,
|
||||
user
|
||||
},
|
||||
getters
|
||||
});
|
||||
|
||||
export default store
|
38
src/store/modules/app.js
Normal file
38
src/store/modules/app.js
Normal file
@@ -0,0 +1,38 @@
|
||||
import Cookies from 'js-cookie';
|
||||
|
||||
const app = {
|
||||
state: {
|
||||
sidebar: {
|
||||
opened: !+Cookies.get('sidebarStatus')
|
||||
},
|
||||
theme: 'default',
|
||||
livenewsChannels: Cookies.get('livenewsChannels') || '[]'
|
||||
},
|
||||
mutations: {
|
||||
TOGGLE_SIDEBAR: state => {
|
||||
if (state.sidebar.opened) {
|
||||
Cookies.set('sidebarStatus', 1);
|
||||
} else {
|
||||
Cookies.set('sidebarStatus', 0);
|
||||
}
|
||||
state.sidebar.opened = !state.sidebar.opened;
|
||||
},
|
||||
SET_LIVENEWS_CHANNELS: (status, channels) => {
|
||||
status.livenewsChannels = JSON.stringify(channels);
|
||||
Cookies.set('livenewsChannels', JSON.stringify(channels));
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
ToggleSideBar: ({ commit }) => {
|
||||
commit('TOGGLE_SIDEBAR')
|
||||
},
|
||||
setTheme: ({ commit }, theme) => {
|
||||
commit('SET_THEME', theme)
|
||||
},
|
||||
setlivenewsChannels: ({ commit }, channels) => {
|
||||
commit('SET_LIVENEWS_CHANNELS', channels)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default app;
|
129
src/store/modules/user.js
Normal file
129
src/store/modules/user.js
Normal file
@@ -0,0 +1,129 @@
|
||||
// import { loginByEmail, loginByThirdparty } from 'api/login';
|
||||
// import { userInfo, userLogout } from 'api/adminUser';
|
||||
import Cookies from 'js-cookie';
|
||||
import userMap from 'mock/login';
|
||||
|
||||
const user = {
|
||||
state: {
|
||||
user: '',
|
||||
status: '',
|
||||
email: '',
|
||||
code: '',
|
||||
uid: undefined,
|
||||
auth_type: '',
|
||||
token: Cookies.get('X-Ivanka-Token'),
|
||||
name: '',
|
||||
avatar: '',
|
||||
introduction: '',
|
||||
roles: [],
|
||||
setting: {
|
||||
articlePlatform: []
|
||||
}
|
||||
},
|
||||
|
||||
mutations: {
|
||||
SET_AUTH_TYPE: (state, type) => {
|
||||
state.auth_type = type;
|
||||
},
|
||||
SET_CODE: (state, code) => {
|
||||
state.code = code;
|
||||
},
|
||||
SET_TOKEN: (state, token) => {
|
||||
state.token = token;
|
||||
},
|
||||
SET_UID: (state, uid) => {
|
||||
state.uid = uid;
|
||||
},
|
||||
SET_EMAIL: (state, email) => {
|
||||
state.email = email;
|
||||
},
|
||||
SET_INTRODUCTION: (state, introduction) => {
|
||||
state.introduction = introduction;
|
||||
},
|
||||
SET_SETTING: (state, setting) => {
|
||||
state.setting = setting;
|
||||
},
|
||||
SET_STATUS: (state, status) => {
|
||||
state.status = status;
|
||||
},
|
||||
SET_NAME: (state, name) => {
|
||||
state.name = name;
|
||||
},
|
||||
SET_AVATAR: (state, avatar) => {
|
||||
state.avatar = avatar;
|
||||
},
|
||||
SET_ROLES: (state, roles) => {
|
||||
state.roles = roles;
|
||||
},
|
||||
LOGIN_SUCCESS: () => {
|
||||
console.log('login success')
|
||||
},
|
||||
LOGOUT_USER: state => {
|
||||
state.user = '';
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
// 邮箱登录
|
||||
LoginByEmail({ commit }, userInfo) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const email = userInfo.email.split('@')[0];
|
||||
if (userMap[email]) {
|
||||
commit('SET_ROLES', userMap[email].role);
|
||||
commit('SET_TOKEN', userMap[email].token);
|
||||
Cookies.set('X-Ivanka-Token', userMap[email].token);
|
||||
resolve();
|
||||
} else {
|
||||
reject('账号不正确');
|
||||
}
|
||||
});
|
||||
},
|
||||
// 第三方验证登录
|
||||
LoginByThirdparty({ commit, state }, code) {
|
||||
return new Promise((resolve, reject) => {
|
||||
commit('SET_CODE', code);
|
||||
loginByThirdparty(state.status, state.email, state.code, state.auth_type).then(response => {
|
||||
commit('SET_TOKEN', response.data.token);
|
||||
Cookies.set('X-Ivanka-Token', response.data.token);
|
||||
resolve();
|
||||
}).catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
// 获取用户信息
|
||||
GetInfo({ commit, state }) {
|
||||
return new Promise(resolve => {
|
||||
const token = state.token;
|
||||
commit('SET_ROLES', userMap[token].role);
|
||||
commit('SET_NAME', userMap[token].name);
|
||||
commit('SET_AVATAR', userMap[token].avatar);
|
||||
commit('SET_INTRODUCTION', userMap[token].introduction);
|
||||
resolve();
|
||||
});
|
||||
},
|
||||
// 登出
|
||||
LogOut({ commit, state }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
userLogout(state.token).then(() => {
|
||||
commit('SET_TOKEN', '');
|
||||
Cookies.remove('X-Ivanka-Token');
|
||||
resolve();
|
||||
}).catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
// 前端 登出
|
||||
FedLogOut({ commit }) {
|
||||
return new Promise(resolve => {
|
||||
commit('SET_TOKEN', '');
|
||||
Cookies.remove('X-Ivanka-Token');
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default user;
|
39
src/store/permission.js
Normal file
39
src/store/permission.js
Normal file
@@ -0,0 +1,39 @@
|
||||
const permission = {
|
||||
state: {
|
||||
permissionRoutes: []
|
||||
},
|
||||
init(data) {
|
||||
const roles = data.roles;
|
||||
const router = data.router;
|
||||
const permissionRoutes = router.filter(v => {
|
||||
if (roles.indexOf('admin') >= 0) return true;
|
||||
if (this.hasPermission(roles, v)) {
|
||||
if (v.children && v.children.length > 0) {
|
||||
v.children = v.children.filter(child => {
|
||||
if (this.hasPermission(roles, child)) {
|
||||
return child
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return v
|
||||
} else {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
this.permissionRoutes = permissionRoutes;
|
||||
},
|
||||
get() {
|
||||
return this.permissionRoutes
|
||||
},
|
||||
hasPermission(roles, route) {
|
||||
if (route.meta && route.meta.role) {
|
||||
return roles.some(role => route.meta.role.indexOf(role) >= 0)
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default permission;
|
Reference in New Issue
Block a user