parent
5c6c5e8ea4
commit
7d741c9436
5 changed files with 420 additions and 449 deletions
@ -1,137 +1,128 @@ |
||||
import Setting from "@/setting"; |
||||
import util from "@/libs/util"; |
||||
import { post, get, del, put } from "@/plugins/requests/index.js"; |
||||
import { post } from "@/plugins/requests/index.js"; |
||||
import api from "@/api"; |
||||
import addRoutes from "@/libs/route/addRoutes"; |
||||
|
||||
/** |
||||
* 用户信息 |
||||
* */ |
||||
export default { |
||||
namespaced: true, |
||||
state: { |
||||
customer: false, // true:为客户,没有个人中心
|
||||
customerName: "", // 客户名称
|
||||
title: "", |
||||
logoUrl: "", |
||||
avatar: "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png", |
||||
userId: '', |
||||
userName: "", |
||||
roleId: "", |
||||
dataTime: "", |
||||
roleName: '', |
||||
schoolId: '', |
||||
fromClient: false, // 是否从学生端登录
|
||||
logView: false // 是否查询过日志状态,如果有,则不再调接口
|
||||
}, |
||||
mutations: { |
||||
SET_INFO: (state, info) => { |
||||
if (info.avatar) state.avatar = info.userAvatars; |
||||
state.userId = info.userId; |
||||
state.userName = info.userName; |
||||
state.roleId = info.roleId; |
||||
state.dataTime = info.dataTime; |
||||
}, |
||||
SET_CUSTOMER: (state, customer) => { |
||||
state.customer = customer; |
||||
}, |
||||
SET_CUSTOMER_NAME: (state, customerName) => { |
||||
state.customerName = customerName; |
||||
}, |
||||
SET_TITLE: (state, title) => { |
||||
state.title = title; |
||||
}, |
||||
SET_LOGO_URL: (state, logoUrl) => { |
||||
state.logoUrl = logoUrl; |
||||
}, |
||||
SET_AVATAR: (state, avatar) => { |
||||
state.avatar = avatar; |
||||
}, |
||||
SET_USERNAME: (state, userName) => { |
||||
state.userName = userName; |
||||
}, |
||||
SET_ROLENAME: (state, roleName) => { |
||||
state.roleName = roleName |
||||
}, |
||||
SET_FROM: (state, fromClient) => { |
||||
state.fromClient = fromClient |
||||
}, |
||||
SET_SCHOOLID: (state, schoolId) => { |
||||
state.schoolId = schoolId |
||||
}, |
||||
SET_LOG: (state) => { |
||||
state.logView = true |
||||
}, |
||||
setUserId: (state, userId) => { |
||||
state.userId = userId |
||||
}, |
||||
}, |
||||
actions: { |
||||
login({ state, commit }, userInfo) { |
||||
return new Promise((resolve, reject) => { |
||||
post(api.logins, userInfo).then(res => { |
||||
// console.log(JSON.stringify(res));
|
||||
if (res.status == 200) { |
||||
let { data } = res; |
||||
// 生成路由权限
|
||||
// 每个系统都是用这套角色权限代码的,后端也是基本一样,除了个别字段可能会不一样
|
||||
// 总体来说就两个步骤: 1是生成能够访问的路由的数组集合,2是生成能看到的按钮的数组集合
|
||||
// res.message.listValue && Setting.dynamicRoute && addRoutes(res.message.listValue)
|
||||
// 2021-10-13重做权限,因权限系统封装
|
||||
util.local.set(Setting.tokenKey, data.token, Setting.tokenExpires); |
||||
commit("SET_CUSTOMER", data.customer); |
||||
commit("SET_CUSTOMER_NAME", data.customerName); |
||||
util.successMsg("登录成功"); |
||||
commit("SET_INFO", data); |
||||
resolve(); |
||||
} else { |
||||
console.log(res.message) |
||||
util.errorMsg(res.message); |
||||
namespaced: true, |
||||
state: { |
||||
customerName: "", // 客户名称
|
||||
title: "", |
||||
logoUrl: "", |
||||
avatar: "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png", |
||||
userId: '', |
||||
userName: "", |
||||
roleId: "", |
||||
dataTime: "", |
||||
roleName: '', |
||||
schoolId: '', |
||||
fromClient: false, // 是否从学生端登录
|
||||
logView: false // 是否查询过日志状态,如果有,则不再调接口
|
||||
}, |
||||
mutations: { |
||||
SET_INFO: (state, info) => { |
||||
if (info.avatar) state.avatar = info.userAvatars; |
||||
state.userId = info.userId; |
||||
state.userName = info.userName; |
||||
state.roleId = info.roleId; |
||||
state.dataTime = info.dataTime; |
||||
}, |
||||
SET_CUSTOMER_NAME: (state, customerName) => { |
||||
state.customerName = customerName; |
||||
}, |
||||
SET_TITLE: (state, title) => { |
||||
state.title = title; |
||||
}, |
||||
SET_LOGO_URL: (state, logoUrl) => { |
||||
state.logoUrl = logoUrl; |
||||
}, |
||||
SET_AVATAR: (state, avatar) => { |
||||
state.avatar = avatar; |
||||
}, |
||||
SET_USERNAME: (state, userName) => { |
||||
state.userName = userName; |
||||
}, |
||||
SET_ROLENAME: (state, roleName) => { |
||||
state.roleName = roleName |
||||
}, |
||||
SET_FROM: (state, fromClient) => { |
||||
state.fromClient = fromClient |
||||
}, |
||||
SET_SCHOOLID: (state, schoolId) => { |
||||
state.schoolId = schoolId |
||||
}, |
||||
SET_LOG: (state) => { |
||||
state.logView = true |
||||
}, |
||||
setUserId: (state, userId) => { |
||||
state.userId = userId |
||||
}, |
||||
}, |
||||
actions: { |
||||
login ({ state, commit }, userInfo) { |
||||
return new Promise((resolve, reject) => { |
||||
post(api.logins, userInfo).then(res => { |
||||
// console.log(JSON.stringify(res));
|
||||
if (res.status == 200) { |
||||
let { data } = res |
||||
util.local.set(Setting.tokenKey, data.token, Setting.tokenExpires); |
||||
commit("SET_CUSTOMER", data.customer); |
||||
commit("SET_CUSTOMER_NAME", data.customerName); |
||||
util.successMsg("登录成功"); |
||||
commit("SET_INFO", data); |
||||
resolve(); |
||||
} else { |
||||
console.log(res.message) |
||||
util.errorMsg(res.message); |
||||
|
||||
reject(res); |
||||
} |
||||
}).catch(error => { |
||||
reject(error); |
||||
}); |
||||
}); |
||||
}, |
||||
logout({ commit, state, dispatch }) { |
||||
return new Promise((resolve, reject) => { |
||||
util.local.remove(Setting.storeKey); |
||||
util.local.remove(Setting.tokenKey); |
||||
if (state.fromClient) { |
||||
util.cookies.remove('serverLogin') |
||||
location.href = Setting.isDev |
||||
? `http://${location.hostname}:8082/#/` |
||||
: Setting.isTest |
||||
? `${location.origin}/student/#/login` |
||||
: `${location.origin}/#/login` |
||||
} else { |
||||
location.reload() |
||||
} |
||||
resolve(); |
||||
}); |
||||
}, |
||||
setInfo({ state, commit }, info) { |
||||
commit("SET_INFO", info); |
||||
}, |
||||
setTitle({ state, commit }, title) { |
||||
commit("SET_TITLE", title); |
||||
}, |
||||
setLogoUrl({ state, commit }, logoUrl) { |
||||
commit("SET_LOGO_URL", logoUrl); |
||||
}, |
||||
setCustomer({ state, commit }, customer) { |
||||
commit("SET_CUSTOMER", customer); |
||||
}, |
||||
setCustomerName({ state, commit }, customerName) { |
||||
commit("SET_CUSTOMER_NAME", customerName); |
||||
}, |
||||
setAvatar({ state, commit }, avatar) { |
||||
commit("SET_AVATAR", avatar); |
||||
}, |
||||
setUserName({ state, commit }, userName) { |
||||
commit("SET_USERNAME", userName); |
||||
} |
||||
reject(res); |
||||
} |
||||
}).catch(error => { |
||||
reject(error); |
||||
}); |
||||
}); |
||||
}, |
||||
logout ({ commit, state, dispatch }) { |
||||
return new Promise((resolve, reject) => { |
||||
post(api.logout).then(() => { |
||||
util.local.remove(Setting.storeKey) |
||||
util.local.remove(Setting.tokenKey) |
||||
if (state.fromClient) { |
||||
util.cookies.remove('serverLogin') |
||||
location.href = Setting.isDev |
||||
? `http://${location.hostname}:8082/#/` |
||||
: Setting.isTest |
||||
? `${location.origin}/student/#/login` |
||||
: `${location.origin}/#/login` |
||||
} else { |
||||
location.reload() |
||||
} |
||||
resolve() |
||||
}).catch(error => { |
||||
reject(error) |
||||
}) |
||||
}) |
||||
}, |
||||
setInfo ({ state, commit }, info) { |
||||
commit("SET_INFO", info); |
||||
}, |
||||
setTitle ({ state, commit }, title) { |
||||
commit("SET_TITLE", title); |
||||
}, |
||||
setLogoUrl ({ state, commit }, logoUrl) { |
||||
commit("SET_LOGO_URL", logoUrl); |
||||
}, |
||||
setCustomerName ({ state, commit }, customerName) { |
||||
commit("SET_CUSTOMER_NAME", customerName); |
||||
}, |
||||
setAvatar ({ state, commit }, avatar) { |
||||
commit("SET_AVATAR", avatar); |
||||
}, |
||||
setUserName ({ state, commit }, userName) { |
||||
commit("SET_USERNAME", userName); |
||||
} |
||||
} |
||||
}; |
Loading…
Reference in new issue