Commit 5efd621f210ef87b1d94f6c6cbb5c58a83e8091e

Authored by sfmind
1 parent a54298bf

vuex统一命名风格

yudao-ui-app/pages/login/login.vue
@@ -142,20 +142,12 @@ export default { @@ -142,20 +142,12 @@ export default {
142 }, 142 },
143 handleSubmit() { 143 handleSubmit() {
144 this.$refs.form.validate().then(res => { 144 this.$refs.form.validate().then(res => {
145 - if (this.currentModeIndex === 0) {  
146 - this.handleLoginPromise(passwordLogin({ mobile: this.formData.mobile, password: this.formData.password }))  
147 - } else if (this.currentModeIndex === 1) {  
148 - this.handleLoginPromise(smsLogin({ mobile: this.formData.mobile, code: this.formData.code }))  
149 - }  
150 - })  
151 - },  
152 - handleLoginPromise(promise) {  
153 - promise.then(res => {  
154 - this.$store.commit('setToken', res.data)  
155 - uni.$u.toast('登录成功')  
156 - setTimeout(() => {  
157 - this.navigateBack()  
158 - }, 1000) 145 + this.$store.dispatch('Login', { type: this.currentModeIndex, data: this.formData }).then(res => {
  146 + uni.$u.toast('登录成功')
  147 + setTimeout(() => {
  148 + this.navigateBack()
  149 + }, 1000)
  150 + })
159 }) 151 })
160 }, 152 },
161 navigateBack() { 153 navigateBack() {
yudao-ui-app/pages/profile/profile.vue
@@ -81,7 +81,7 @@ export default { @@ -81,7 +81,7 @@ export default {
81 const tempFilePaths = chooseImageRes.tempFilePaths 81 const tempFilePaths = chooseImageRes.tempFilePaths
82 updateAvatar(tempFilePaths[0]).then(res => { 82 updateAvatar(tempFilePaths[0]).then(res => {
83 this.userInfo.avatar = res.data 83 this.userInfo.avatar = res.data
84 - this.$store.commit('setUserInfo', this.userInfo) 84 + this.$store.commit('SET_USER_INFO', this.userInfo)
85 }) 85 })
86 } 86 }
87 }) 87 })
@@ -90,7 +90,7 @@ export default { @@ -90,7 +90,7 @@ export default {
90 updateNickname({ nickname: this.tempName }).then(res => { 90 updateNickname({ nickname: this.tempName }).then(res => {
91 this.nameEditOn = false; 91 this.nameEditOn = false;
92 this.userInfo.nickname = this.tempName 92 this.userInfo.nickname = this.tempName
93 - this.$store.commit('setUserInfo', this.userInfo) 93 + this.$store.commit('SET_USER_INFO', this.userInfo)
94 }) 94 })
95 } 95 }
96 } 96 }
yudao-ui-app/pages/user/user.vue
@@ -73,7 +73,7 @@ export default { @@ -73,7 +73,7 @@ export default {
73 }, 73 },
74 onLoad() { 74 onLoad() {
75 if (this.hasLogin){ 75 if (this.hasLogin){
76 - this.$store.dispatch('obtainUserInfo') 76 + this.$store.dispatch('ObtainUserInfo')
77 } 77 }
78 }, 78 },
79 methods: { 79 methods: {
@@ -90,7 +90,7 @@ export default { @@ -90,7 +90,7 @@ export default {
90 content: '您确定要退出登录吗', 90 content: '您确定要退出登录吗',
91 success: res => { 91 success: res => {
92 if (res.confirm) { 92 if (res.confirm) {
93 - this.$store.dispatch('logout') 93 + this.$store.dispatch('Logout')
94 } else if (res.cancel) { 94 } else if (res.cancel) {
95 //console.log('用户点击取消') 95 //console.log('用户点击取消')
96 } 96 }
@@ -100,7 +100,7 @@ export default { @@ -100,7 +100,7 @@ export default {
100 }, 100 },
101 computed: { 101 computed: {
102 userInfo() { 102 userInfo() {
103 - return this.$store.state.userInfo 103 + return this.$store.getters.userInfo
104 }, 104 },
105 hasLogin() { 105 hasLogin() {
106 return this.$store.getters.hasLogin 106 return this.$store.getters.hasLogin
yudao-ui-app/store/index.js
@@ -2,6 +2,7 @@ import Vue from 'vue' @@ -2,6 +2,7 @@ import Vue from 'vue'
2 import Vuex from 'vuex' 2 import Vuex from 'vuex'
3 import { logout } from '@/api/auth' 3 import { logout } from '@/api/auth'
4 import { getUserInfo } from '@/api/user' 4 import { getUserInfo } from '@/api/user'
  5 +import { passwordLogin, smsLogin } from '../api/auth'
5 6
6 Vue.use(Vuex) // vue的插件机制 7 Vue.use(Vuex) // vue的插件机制
7 8
@@ -14,13 +15,16 @@ const store = new Vuex.Store({ @@ -14,13 +15,16 @@ const store = new Vuex.Store({
14 timerIdent: false // 全局 1s 定时器,只在全局开启一个,所有需要定时执行的任务监听该值即可,无需额外开启 TODO 芋艿:需要看看 15 timerIdent: false // 全局 1s 定时器,只在全局开启一个,所有需要定时执行的任务监听该值即可,无需额外开启 TODO 芋艿:需要看看
15 }, 16 },
16 getters: { 17 getters: {
  18 + userInfo(state) {
  19 + return state.userInfo
  20 + },
17 hasLogin(state) { 21 hasLogin(state) {
18 return !!state.token 22 return !!state.token
19 } 23 }
20 }, 24 },
21 mutations: { 25 mutations: {
22 // 更新 state 的通用方法 26 // 更新 state 的通用方法
23 - setStateAttr(state, param) { 27 + SET_STATE_ATTR(state, param) {
24 if (param instanceof Array) { 28 if (param instanceof Array) {
25 for (let item of param) { 29 for (let item of param) {
26 state[item.key] = item.val 30 state[item.key] = item.val
@@ -30,22 +34,22 @@ const store = new Vuex.Store({ @@ -30,22 +34,22 @@ const store = new Vuex.Store({
30 } 34 }
31 }, 35 },
32 // 更新token 36 // 更新token
33 - setToken(state, data) { 37 + SET_TOKEN(state, data) {
34 // 设置 Token 38 // 设置 Token
35 const { token } = data 39 const { token } = data
36 state.token = token 40 state.token = token
37 uni.setStorageSync('token', token) 41 uni.setStorageSync('token', token)
38 42
39 // 加载用户信息 43 // 加载用户信息
40 - this.dispatch('obtainUserInfo') 44 + this.dispatch('ObtainUserInfo')
41 }, 45 },
42 // 更新用户信息 46 // 更新用户信息
43 - setUserInfo(state, data) { 47 + SET_USER_INFO(state, data) {
44 state.userInfo = data 48 state.userInfo = data
45 uni.setStorageSync('userInfo', data) 49 uni.setStorageSync('userInfo', data)
46 }, 50 },
47 // 清空 Token 和 用户信息 51 // 清空 Token 和 用户信息
48 - clearLoginInfo(state) { 52 + CLEAR_LOGIN_INFO(state) {
49 uni.removeStorageSync('token') 53 uni.removeStorageSync('token')
50 state.token = '' 54 state.token = ''
51 uni.removeStorageSync('userInfo') 55 uni.removeStorageSync('userInfo')
@@ -53,15 +57,28 @@ const store = new Vuex.Store({ @@ -53,15 +57,28 @@ const store = new Vuex.Store({
53 } 57 }
54 }, 58 },
55 actions: { 59 actions: {
56 - // 获得用户基本信息  
57 - async obtainUserInfo({ state, commit }) {  
58 - const res = await getUserInfo()  
59 - commit('setUserInfo', res.data) 60 + //账号登录
  61 + Login({ state, commit }, { type, data }) {
  62 + console.log(type, data)
  63 + if (type === 0) {
  64 + return passwordLogin(data).then(res => {
  65 + commit('SET_TOKEN', res.data)
  66 + })
  67 + } else {
  68 + return smsLogin(data).then(res => {
  69 + commit('SET_TOKEN', res.data)
  70 + })
  71 + }
60 }, 72 },
61 // 退出登录 73 // 退出登录
62 - async logout({ state, commit }) {  
63 - commit('clearLoginInfo') 74 + async Logout({ state, commit }) {
  75 + commit('CLEAR_LOGIN_INFO')
64 await logout() 76 await logout()
  77 + },
  78 + // 获得用户基本信息
  79 + async ObtainUserInfo({ state, commit }) {
  80 + const res = await getUserInfo()
  81 + commit('SET_USER_INFO', res.data)
65 } 82 }
66 } 83 }
67 }) 84 })