提交 9a2a4af6 编写于 作者: View Design's avatar View Design

401全局登录

上级 33a5804e
...@@ -18,31 +18,25 @@ ...@@ -18,31 +18,25 @@
to="/library" to="/library"
@click="handleClickItem" @click="handleClickItem"
/> />
<ISearchHistory @sign="handleOpenSign" /> <ISearchHistory @sign="$openSign" />
<ClientOnly> <ClientOnly>
<UButton <UButton
v-if="!$isSignIn" v-if="!$isSignIn"
class="flex gap-2 justify-center" class="flex gap-2 justify-center"
size="lg" size="lg"
label="登录" label="登录"
@click="handleOpenSign" @click="$openSign"
/> />
</ClientOnly> </ClientOnly>
</div> </div>
<UModal v-model="isOpenSign"> <UModal v-model="$isOpenSign">
<ISign @close="handleCloseSign" /> <ISign @close="$closeSign" />
</UModal> </UModal>
</template> </template>
<script setup> <script setup>
const { $isSignIn } = storeToRefs(useUserStore()) const { $isSignIn, $isOpenSign } = storeToRefs(useUserStore())
const { $openSign, $closeSign } = useUserStore()
const Layout = inject('Layout') const Layout = inject('Layout')
const isOpenSign = ref(false)
const handleOpenSign = () => {
isOpenSign.value = true
}
const handleCloseSign = () => {
isOpenSign.value = false
}
function handleClickItem () { function handleClickItem () {
Layout.handleCloseAside() Layout.handleCloseAside()
} }
......
...@@ -21,6 +21,7 @@ const request = async (url, options = {}) => { ...@@ -21,6 +21,7 @@ const request = async (url, options = {}) => {
onResponseError({ request, response, options }) { onResponseError({ request, response, options }) {
const status = response.status const status = response.status
if (process.client && (status === 401 || status === 403)) { if (process.client && (status === 401 || status === 403)) {
// 全局弹提示
let title let title
if (status === 401) title = '抱歉,您没有权限操作' if (status === 401) title = '抱歉,您没有权限操作'
else if (status === 403) title = '抱歉,您没有权限访问当前内容' else if (status === 403) title = '抱歉,您没有权限访问当前内容'
...@@ -33,6 +34,12 @@ const request = async (url, options = {}) => { ...@@ -33,6 +34,12 @@ const request = async (url, options = {}) => {
color: 'red' color: 'red'
}) })
}) })
// 全局弹登录
const { $isSignIn } = storeToRefs(useUserStore())
const { $openSign } = useUserStore()
if (status === 401 && !$isSignIn.value) {
$openSign()
}
} }
// 处理响应错误 // 处理响应错误
console.log('[ResponseError]', request) console.log('[ResponseError]', request)
......
...@@ -6,6 +6,7 @@ export const useUserStore = defineStore('user', () => { ...@@ -6,6 +6,7 @@ export const useUserStore = defineStore('user', () => {
const $info = computed(() => userInfo.value ? userInfo.value : {}) const $info = computed(() => userInfo.value ? userInfo.value : {})
const $isSignIn = computed(() => !!token.value) const $isSignIn = computed(() => !!token.value)
const $isOpenSign = ref(false)
function $signOut () { function $signOut () {
token.value = null token.value = null
...@@ -18,5 +19,12 @@ export const useUserStore = defineStore('user', () => { ...@@ -18,5 +19,12 @@ export const useUserStore = defineStore('user', () => {
userInfo.value = data.value.data userInfo.value = data.value.data
} }
return { $info, $isSignIn, $signOut, $updateUserInfo } function $openSign () {
$isOpenSign.value = true
}
function $closeSign () {
$isOpenSign.value = false
}
return { $info, $isSignIn, $isOpenSign, $signOut, $updateUserInfo, $openSign, $closeSign }
}) })
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册