u
This commit is contained in:
1 parent
92fbbf7c6d
commit
b03e2c1e69
149 files changed
+925
-14628
No files matched your search
@@ -0,0 +1,16 @@
|
||||
import { createPinia } from 'pinia'
|
||||
import { createPersistedState } from 'pinia-plugin-persistedstate' // 数据持久化
|
||||
|
||||
const store = createPinia()
|
||||
store.use(
|
||||
createPersistedState({
|
||||
storage: {
|
||||
getItem: uni.getStorageSync,
|
||||
setItem: uni.setStorageSync,
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
export default store
|
||||
|
||||
export * from './user'
|
||||
@@ -0,0 +1,58 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { computed, ref } from 'vue'
|
||||
|
||||
const initState = { token: '', orgid: '', userid: '' }
|
||||
|
||||
export const useUserStore = defineStore(
|
||||
'user',
|
||||
() => {
|
||||
const userInfo = ref<IUserInfo>({ ...initState })
|
||||
|
||||
const setUserInfo = (val: IUserInfo) => {
|
||||
userInfo.value = val
|
||||
}
|
||||
|
||||
const reset = () => {
|
||||
userInfo.value = { orgid: userInfo.value.orgid, userid: userInfo.value.userid, token: '' }
|
||||
}
|
||||
|
||||
const isLogined = computed(
|
||||
() => !!(userInfo.value.token && userInfo.value.orgid && userInfo.value.userid),
|
||||
)
|
||||
|
||||
function requireLogin(): boolean {
|
||||
if (!isLogined.value) {
|
||||
uni.showToast({
|
||||
title: '请先登录',
|
||||
icon: 'none',
|
||||
})
|
||||
|
||||
const pages = getCurrentPages()
|
||||
const currentPage = pages[pages.length - 1]
|
||||
const path = currentPage?.route ? `/${currentPage.route}` : ''
|
||||
|
||||
if (!path || path !== '/pages-sub/login/index') {
|
||||
// 等 toast 显示完再跳转
|
||||
uni.navigateTo({
|
||||
url: '/pages-sub/login/index',
|
||||
})
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return {
|
||||
userInfo,
|
||||
setUserInfo,
|
||||
reset,
|
||||
isLogined,
|
||||
requireLogin,
|
||||
}
|
||||
},
|
||||
{
|
||||
persist: true,
|
||||
},
|
||||
)
|
||||
Reference in new issue
Block a user