2022-04-10 01:21:21 +08:00
|
|
|
import Vue from 'vue'
|
|
|
|
import Vuex from 'vuex'
|
2022-05-01 22:39:16 +08:00
|
|
|
import { logout } from '@/api/auth'
|
|
|
|
import { getUserInfo } from '@/api/user'
|
2022-06-02 17:59:57 +08:00
|
|
|
import { passwordLogin, smsLogin, weixinMiniAppLogin } from '@/api/auth'
|
2022-05-02 19:37:17 +08:00
|
|
|
|
2022-06-02 17:59:57 +08:00
|
|
|
const AccessTokenKey = 'ACCESS_TOKEN'
|
|
|
|
const RefreshTokenKey = 'REFRESH_TOKEN'
|
2022-04-10 01:21:21 +08:00
|
|
|
|
|
|
|
Vue.use(Vuex) // vue的插件机制
|
|
|
|
|
|
|
|
// Vuex.Store 构造器选项
|
|
|
|
const store = new Vuex.Store({
|
2022-04-20 03:08:59 +08:00
|
|
|
state: {
|
2022-06-02 17:59:57 +08:00
|
|
|
accessToken: uni.getStorageSync(AccessTokenKey), // 访问令牌
|
|
|
|
refreshToken: uni.getStorageSync(RefreshTokenKey), // 刷新令牌
|
|
|
|
userInfo: {}
|
2022-04-20 03:08:59 +08:00
|
|
|
},
|
|
|
|
getters: {
|
2022-06-02 17:59:57 +08:00
|
|
|
accessToken: state => state.accessToken,
|
|
|
|
refreshToken: state => state.refreshToken,
|
2022-05-02 19:37:17 +08:00
|
|
|
userInfo: state => state.userInfo,
|
2022-06-02 17:59:57 +08:00
|
|
|
hasLogin: state => !!state.accessToken
|
2022-04-20 03:08:59 +08:00
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
// 更新 state 的通用方法
|
2022-05-02 16:53:56 +08:00
|
|
|
SET_STATE_ATTR(state, param) {
|
2022-04-20 03:08:59 +08:00
|
|
|
if (param instanceof Array) {
|
|
|
|
for (let item of param) {
|
|
|
|
state[item.key] = item.val
|
2022-04-10 01:21:21 +08:00
|
|
|
}
|
2022-04-20 03:08:59 +08:00
|
|
|
} else {
|
|
|
|
state[param.key] = param.val
|
|
|
|
}
|
2022-04-10 01:21:21 +08:00
|
|
|
},
|
2022-06-02 17:59:57 +08:00
|
|
|
// 更新令牌
|
2022-05-02 16:53:56 +08:00
|
|
|
SET_TOKEN(state, data) {
|
2022-06-02 17:59:57 +08:00
|
|
|
// 设置令牌
|
|
|
|
const { accessToken, refreshToken } = data
|
|
|
|
state.accessToken = accessToken
|
|
|
|
state.refreshToken = refreshToken
|
|
|
|
uni.setStorageSync(AccessTokenKey, accessToken)
|
|
|
|
uni.setStorageSync(RefreshTokenKey, refreshToken)
|
2022-04-10 01:21:21 +08:00
|
|
|
|
2022-04-20 03:08:59 +08:00
|
|
|
// 加载用户信息
|
2022-05-02 16:53:56 +08:00
|
|
|
this.dispatch('ObtainUserInfo')
|
2022-04-10 01:21:21 +08:00
|
|
|
},
|
2022-04-21 00:01:24 +08:00
|
|
|
// 更新用户信息
|
2022-05-02 16:53:56 +08:00
|
|
|
SET_USER_INFO(state, data) {
|
2022-04-21 00:01:24 +08:00
|
|
|
state.userInfo = data
|
|
|
|
},
|
2022-06-02 17:59:57 +08:00
|
|
|
// 清空令牌 和 用户信息
|
2022-05-02 16:53:56 +08:00
|
|
|
CLEAR_LOGIN_INFO(state) {
|
2022-06-02 17:59:57 +08:00
|
|
|
uni.removeStorageSync(AccessTokenKey)
|
|
|
|
uni.removeStorageSync(RefreshTokenKey)
|
|
|
|
state.accessToken = ''
|
|
|
|
state.refreshToken = ''
|
2022-04-21 00:01:24 +08:00
|
|
|
state.userInfo = {}
|
2022-04-20 03:08:59 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
2022-05-02 16:53:56 +08:00
|
|
|
//账号登录
|
|
|
|
Login({ state, commit }, { type, data }) {
|
|
|
|
if (type === 0) {
|
|
|
|
return passwordLogin(data).then(res => {
|
|
|
|
commit('SET_TOKEN', res.data)
|
|
|
|
})
|
2022-05-05 19:09:04 +08:00
|
|
|
} else if (type === 1) {
|
2022-05-02 16:53:56 +08:00
|
|
|
return smsLogin(data).then(res => {
|
|
|
|
commit('SET_TOKEN', res.data)
|
|
|
|
})
|
2022-05-05 19:09:04 +08:00
|
|
|
} else {
|
2022-06-02 17:59:57 +08:00
|
|
|
return weixinMiniAppLogin(data).then(res => {
|
2022-05-05 19:09:04 +08:00
|
|
|
commit('SET_TOKEN', res.data)
|
|
|
|
})
|
2022-05-02 16:53:56 +08:00
|
|
|
}
|
2022-04-21 00:01:24 +08:00
|
|
|
},
|
|
|
|
// 退出登录
|
2022-05-02 16:53:56 +08:00
|
|
|
async Logout({ state, commit }) {
|
|
|
|
commit('CLEAR_LOGIN_INFO')
|
2022-04-21 00:01:24 +08:00
|
|
|
await logout()
|
2022-05-02 16:53:56 +08:00
|
|
|
},
|
|
|
|
// 获得用户基本信息
|
|
|
|
async ObtainUserInfo({ state, commit }) {
|
|
|
|
const res = await getUserInfo()
|
|
|
|
commit('SET_USER_INFO', res.data)
|
2022-04-10 01:21:21 +08:00
|
|
|
}
|
2022-04-20 03:08:59 +08:00
|
|
|
}
|
2022-04-10 01:21:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
export default store
|