vuex统一命名风格

pull/2/head
sfmind 2022-05-02 16:53:56 +08:00
parent a54298bf25
commit 5efd621f21
4 changed files with 39 additions and 30 deletions

View File

@ -142,20 +142,12 @@ export default {
}, },
handleSubmit() { handleSubmit() {
this.$refs.form.validate().then(res => { this.$refs.form.validate().then(res => {
if (this.currentModeIndex === 0) { this.$store.dispatch('Login', { type: this.currentModeIndex, data: this.formData }).then(res => {
this.handleLoginPromise(passwordLogin({ mobile: this.formData.mobile, password: this.formData.password })) uni.$u.toast('登录成功')
} else if (this.currentModeIndex === 1) { setTimeout(() => {
this.handleLoginPromise(smsLogin({ mobile: this.formData.mobile, code: this.formData.code })) this.navigateBack()
} }, 1000)
}) })
},
handleLoginPromise(promise) {
promise.then(res => {
this.$store.commit('setToken', res.data)
uni.$u.toast('登录成功')
setTimeout(() => {
this.navigateBack()
}, 1000)
}) })
}, },
navigateBack() { navigateBack() {

View File

@ -81,7 +81,7 @@ export default {
const tempFilePaths = chooseImageRes.tempFilePaths const tempFilePaths = chooseImageRes.tempFilePaths
updateAvatar(tempFilePaths[0]).then(res => { updateAvatar(tempFilePaths[0]).then(res => {
this.userInfo.avatar = res.data this.userInfo.avatar = res.data
this.$store.commit('setUserInfo', this.userInfo) this.$store.commit('SET_USER_INFO', this.userInfo)
}) })
} }
}) })
@ -90,7 +90,7 @@ export default {
updateNickname({ nickname: this.tempName }).then(res => { updateNickname({ nickname: this.tempName }).then(res => {
this.nameEditOn = false; this.nameEditOn = false;
this.userInfo.nickname = this.tempName this.userInfo.nickname = this.tempName
this.$store.commit('setUserInfo', this.userInfo) this.$store.commit('SET_USER_INFO', this.userInfo)
}) })
} }
} }

View File

@ -73,7 +73,7 @@ export default {
}, },
onLoad() { onLoad() {
if (this.hasLogin){ if (this.hasLogin){
this.$store.dispatch('obtainUserInfo') this.$store.dispatch('ObtainUserInfo')
} }
}, },
methods: { methods: {
@ -90,7 +90,7 @@ export default {
content: '您确定要退出登录吗', content: '您确定要退出登录吗',
success: res => { success: res => {
if (res.confirm) { if (res.confirm) {
this.$store.dispatch('logout') this.$store.dispatch('Logout')
} else if (res.cancel) { } else if (res.cancel) {
//console.log('') //console.log('')
} }
@ -100,7 +100,7 @@ export default {
}, },
computed: { computed: {
userInfo() { userInfo() {
return this.$store.state.userInfo return this.$store.getters.userInfo
}, },
hasLogin() { hasLogin() {
return this.$store.getters.hasLogin return this.$store.getters.hasLogin

View File

@ -2,6 +2,7 @@ import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import { logout } from '@/api/auth' import { logout } from '@/api/auth'
import { getUserInfo } from '@/api/user' import { getUserInfo } from '@/api/user'
import { passwordLogin, smsLogin } from '../api/auth'
Vue.use(Vuex) // vue的插件机制 Vue.use(Vuex) // vue的插件机制
@ -14,13 +15,16 @@ const store = new Vuex.Store({
timerIdent: false // 全局 1s 定时器,只在全局开启一个,所有需要定时执行的任务监听该值即可,无需额外开启 TODO 芋艿:需要看看 timerIdent: false // 全局 1s 定时器,只在全局开启一个,所有需要定时执行的任务监听该值即可,无需额外开启 TODO 芋艿:需要看看
}, },
getters: { getters: {
userInfo(state) {
return state.userInfo
},
hasLogin(state) { hasLogin(state) {
return !!state.token return !!state.token
} }
}, },
mutations: { mutations: {
// 更新 state 的通用方法 // 更新 state 的通用方法
setStateAttr(state, param) { SET_STATE_ATTR(state, param) {
if (param instanceof Array) { if (param instanceof Array) {
for (let item of param) { for (let item of param) {
state[item.key] = item.val state[item.key] = item.val
@ -30,22 +34,22 @@ const store = new Vuex.Store({
} }
}, },
// 更新token // 更新token
setToken(state, data) { SET_TOKEN(state, data) {
// 设置 Token // 设置 Token
const { token } = data const { token } = data
state.token = token state.token = token
uni.setStorageSync('token', token) uni.setStorageSync('token', token)
// 加载用户信息 // 加载用户信息
this.dispatch('obtainUserInfo') this.dispatch('ObtainUserInfo')
}, },
// 更新用户信息 // 更新用户信息
setUserInfo(state, data) { SET_USER_INFO(state, data) {
state.userInfo = data state.userInfo = data
uni.setStorageSync('userInfo', data) uni.setStorageSync('userInfo', data)
}, },
// 清空 Token 和 用户信息 // 清空 Token 和 用户信息
clearLoginInfo(state) { CLEAR_LOGIN_INFO(state) {
uni.removeStorageSync('token') uni.removeStorageSync('token')
state.token = '' state.token = ''
uni.removeStorageSync('userInfo') uni.removeStorageSync('userInfo')
@ -53,15 +57,28 @@ const store = new Vuex.Store({
} }
}, },
actions: { actions: {
// 获得用户基本信息 //账号登录
async obtainUserInfo({ state, commit }) { Login({ state, commit }, { type, data }) {
const res = await getUserInfo() console.log(type, data)
commit('setUserInfo', res.data) if (type === 0) {
return passwordLogin(data).then(res => {
commit('SET_TOKEN', res.data)
})
} else {
return smsLogin(data).then(res => {
commit('SET_TOKEN', res.data)
})
}
}, },
// 退出登录 // 退出登录
async logout({ state, commit }) { async Logout({ state, commit }) {
commit('clearLoginInfo') commit('CLEAR_LOGIN_INFO')
await logout() await logout()
},
// 获得用户基本信息
async ObtainUserInfo({ state, commit }) {
const res = await getUserInfo()
commit('SET_USER_INFO', res.data)
} }
} }
}) })