diff --git a/yudao-ui-app/store/getters.js b/yudao-ui-app/store/getters.js new file mode 100644 index 000000000..720a48620 --- /dev/null +++ b/yudao-ui-app/store/getters.js @@ -0,0 +1,6 @@ +const getters = { + accessToken: state => state.user.accessToken, + userInfo: state => state.user.userInfo, + hasLogin: state => !!state.user.accessToken +} +export default getters diff --git a/yudao-ui-app/store/index.js b/yudao-ui-app/store/index.js index 9eed31c90..e46138835 100644 --- a/yudao-ui-app/store/index.js +++ b/yudao-ui-app/store/index.js @@ -1,91 +1,15 @@ import Vue from 'vue' import Vuex from 'vuex' -import { logout } from '@/api/auth' -import { getUserInfo } from '@/api/user' -import { passwordLogin, smsLogin, weixinMiniAppLogin } from '@/api/auth' - -const AccessTokenKey = 'ACCESS_TOKEN' -const RefreshTokenKey = 'REFRESH_TOKEN' +import user from './mudules/user' +import getters from './getters' Vue.use(Vuex) // vue的插件机制 // Vuex.Store 构造器选项 const store = new Vuex.Store({ - state: { - accessToken: uni.getStorageSync(AccessTokenKey), // 访问令牌 - refreshToken: uni.getStorageSync(RefreshTokenKey), // 刷新令牌 - userInfo: {} + modules: { + user }, - getters: { - accessToken: state => state.accessToken, - refreshToken: state => state.refreshToken, - userInfo: state => state.userInfo, - hasLogin: state => !!state.accessToken - }, - mutations: { - // 更新 state 的通用方法 - SET_STATE_ATTR(state, param) { - if (param instanceof Array) { - for (let item of param) { - state[item.key] = item.val - } - } else { - state[param.key] = param.val - } - }, - // 更新令牌 - SET_TOKEN(state, data) { - // 设置令牌 - const { accessToken, refreshToken } = data - state.accessToken = accessToken - state.refreshToken = refreshToken - uni.setStorageSync(AccessTokenKey, accessToken) - uni.setStorageSync(RefreshTokenKey, refreshToken) - - // 加载用户信息 - this.dispatch('ObtainUserInfo') - }, - // 更新用户信息 - SET_USER_INFO(state, data) { - state.userInfo = data - }, - // 清空令牌 和 用户信息 - CLEAR_LOGIN_INFO(state) { - uni.removeStorageSync(AccessTokenKey) - uni.removeStorageSync(RefreshTokenKey) - state.accessToken = '' - state.refreshToken = '' - state.userInfo = {} - } - }, - actions: { - //账号登录 - Login({ state, commit }, { type, data }) { - if (type === 0) { - return passwordLogin(data).then(res => { - commit('SET_TOKEN', res.data) - }) - } else if (type === 1) { - return smsLogin(data).then(res => { - commit('SET_TOKEN', res.data) - }) - } else { - return weixinMiniAppLogin(data).then(res => { - commit('SET_TOKEN', res.data) - }) - } - }, - // 退出登录 - async Logout({ state, commit }) { - commit('CLEAR_LOGIN_INFO') - await logout() - }, - // 获得用户基本信息 - async ObtainUserInfo({ state, commit }) { - const res = await getUserInfo() - commit('SET_USER_INFO', res.data) - } - } + getters }) - export default store diff --git a/yudao-ui-app/store/mudules/user.js b/yudao-ui-app/store/mudules/user.js new file mode 100644 index 000000000..5784dfda7 --- /dev/null +++ b/yudao-ui-app/store/mudules/user.js @@ -0,0 +1,101 @@ +import { getUserInfo } from '@/api/user' +import { passwordLogin, smsLogin, weixinMiniAppLogin, logout } from '@/api/auth' + +const AccessTokenKey = 'ACCESS_TOKEN' +const RefreshTokenKey = 'REFRESH_TOKEN' + +const user = { + state: { + accessToken: uni.getStorageSync(AccessTokenKey), // 访问令牌 + refreshToken: uni.getStorageSync(RefreshTokenKey), // 刷新令牌 + userInfo: {} + }, + mutations: { + // 更新 state 的通用方法 + SET_STATE_ATTR(state, param) { + if (param instanceof Array) { + for (let item of param) { + state[item.key] = item.val + } + } else { + state[param.key] = param.val + } + }, + // 更新令牌 + SET_TOKEN(state, data) { + // 设置令牌 + const { accessToken, refreshToken } = data + state.accessToken = accessToken + state.refreshToken = refreshToken + uni.setStorageSync(AccessTokenKey, accessToken) + uni.setStorageSync(RefreshTokenKey, refreshToken) + + // 加载用户信息 + this.dispatch('ObtainUserInfo') + }, + // 更新用户信息 + SET_USER_INFO(state, data) { + state.userInfo = data + }, + // 清空令牌 和 用户信息 + CLEAR_LOGIN_INFO(state) { + uni.removeStorageSync(AccessTokenKey) + uni.removeStorageSync(RefreshTokenKey) + state.accessToken = '' + state.refreshToken = '' + state.userInfo = {} + } + }, + actions: { + //账号登录 + Login({ state, commit }, { type, data }) { + if (type === 0) { + return passwordLogin(data) + .then(res => { + commit('SET_TOKEN', res.data) + return Promise.resolve(res) + }) + .catch(err => { + return Promise.reject(err) + }) + } else if (type === 1) { + return smsLogin(data) + .then(res => { + commit('SET_TOKEN', res.data) + return Promise.resolve(res) + }) + .catch(err => { + return Promise.reject(err) + }) + } else { + return weixinMiniAppLogin(data) + .then(res => { + commit('SET_TOKEN', res.data) + return Promise.resolve(res) + }) + .catch(err => { + return Promise.reject(err) + }) + } + }, + // 退出登录 + Logout({ state, commit }) { + return logout() + .then(res => { + return Promise.resolve(res) + }) + .catch(err => { + return Promise.reject(err) + }) + .finally(() => { + commit('CLEAR_LOGIN_INFO') + }) + }, + // 获得用户基本信息 + async ObtainUserInfo({ state, commit }) { + const res = await getUserInfo() + commit('SET_USER_INFO', res.data) + } + } +} +export default user