Browse Source

优化登录注册逻辑

master
453530270@qq.com 2 years ago
parent
commit
286e4ff11a
  1. 2
      core/app.js
  2. 6
      pages/login/components/main.vue
  3. 2
      pages/login/index.vue
  4. 1
      store/getters.js
  5. 23
      store/modules/user.js
  6. 1
      store/mutation-types.js

2
core/app.js

@ -158,7 +158,7 @@ export const setCartTabBadge = () => {
* 验证是否已登录 * 验证是否已登录
*/ */
export const checkLogin = () => { export const checkLogin = () => {
return !!store.getters.userId return !!store.getters.accountId
} }
/** /**

6
pages/login/components/main.vue

@ -56,6 +56,9 @@
import * as LoginApi from '@/api/login' import * as LoginApi from '@/api/login'
import * as Verify from '@/utils/verify' import * as Verify from '@/utils/verify'
//
import { checkLogin, showError } from '@/core/app'
export default { export default {
components: { components: {
// nothing // nothing
@ -110,7 +113,6 @@
this.utchk2=true this.utchk2=true
this.utchk1=false; this.utchk1=false;
} }
let bb = this.$md5("123456")
}, },
// //
readpri(e){ readpri(e){
@ -209,7 +211,7 @@
delta: Number(delta || 1) delta: Number(delta || 1)
}) })
} else { } else {
this.$navTo('pages/index/index') this.$navTo('pages/user/index')
} }
}, },

2
pages/login/index.vue

@ -10,6 +10,8 @@
import MpWeixin from './components/mp-weixin' import MpWeixin from './components/mp-weixin'
import SettingKeyEnum from '@/common/enum/setting/Key' import SettingKeyEnum from '@/common/enum/setting/Key'
import SettingModel from '@/common/model/Setting' import SettingModel from '@/common/model/Setting'
export default { export default {
components: { components: {

1
store/getters.js

@ -1,6 +1,7 @@
const getters = { const getters = {
token: state => state.user.token, token: state => state.user.token,
userId: state => state.user.userId, userId: state => state.user.userId,
accountId: state => state.user.accountId,
platform: state => state.app.platform platform: state => state.app.platform
} }

23
store/modules/user.js

@ -1,17 +1,17 @@
import { ACCESS_TOKEN, USER_ID } from '@/store/mutation-types' import { ACCESS_TOKEN, ACCOUNT_ID } from '@/store/mutation-types'
import storage from '@/utils/storage' import storage from '@/utils/storage'
import * as LoginApi from '@/api/login' import * as LoginApi from '@/api/login'
// 登陆成功后执行 // 登陆成功后执行
const loginSuccess = (commit, { token, accountId }) => { const loginSuccess = (commit, { token, user }) => {
// 过期时间30天 // 过期时间30天
const expiryTime = 30 * 86400 const expiryTime = 30 * 86400
// 保存tokne和userId到缓存 // 保存tokne和userId到缓存
storage.set(USER_ID, accountId, expiryTime) storage.set(ACCOUNT_ID, user.accountId, expiryTime)
storage.set(ACCESS_TOKEN, token, expiryTime) storage.set(ACCESS_TOKEN, token, expiryTime)
// 记录到store全局变量 // 记录到store全局变量
commit('SET_TOKEN', token) commit('SET_TOKEN', token)
commit('SET_USER_ID', accountId) commit('SET_ACCOUNT_ID', user.accountId)
} }
const user = { const user = {
@ -26,9 +26,12 @@ const user = {
SET_TOKEN: (state, value) => { SET_TOKEN: (state, value) => {
state.token = value state.token = value
}, },
SET_USER_ID: (state, value) => { // SET_USER_ID: (state, value) => {
state.userId = value // state.userId = value
} // },
SET_ACCOUNT_ID:(state,value)=>{
state.accountId = value
}
}, },
actions: { actions: {
@ -78,13 +81,13 @@ const user = {
Logout({ commit }, data) { Logout({ commit }, data) {
const store = this const store = this
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (store.getters.userId > 0) { if (store.getters.accountId > 0) {
// 删除缓存中的tokne和userId // 删除缓存中的tokne和userId
storage.remove(USER_ID) storage.remove(ACCOUNT_ID)
storage.remove(ACCESS_TOKEN) storage.remove(ACCESS_TOKEN)
// 记录到store全局变量 // 记录到store全局变量
commit('SET_TOKEN', '') commit('SET_TOKEN', '')
commit('SET_USER_ID', null) commit('SET_ACCOUNT_ID', null)
resolve() resolve()
} }
}) })

1
store/mutation-types.js

@ -1,3 +1,4 @@
export const ACCESS_TOKEN = 'AccessToken' export const ACCESS_TOKEN = 'AccessToken'
export const USER_ID = 'userId' export const USER_ID = 'userId'
export const PLATFORM = 'platform' export const PLATFORM = 'platform'
export const ACCOUNT_ID = 'accountId'
Loading…
Cancel
Save