设置页面退出登录按钮的封装

This commit is contained in:
WindowBird 2025-08-21 10:15:30 +08:00
parent b34801c6b6
commit b8bc984400
2 changed files with 153 additions and 132 deletions

View File

@ -0,0 +1,135 @@
<template>
<view class="logout-button" @click="handleLogout">
<text class="logout-text">{{ text }}</text>
</view>
</template>
<script setup>
import { userLogout } from '@/api/auth/auth.js'
import { clearToken } from '@/utils/request.js'
//
const props = defineProps({
text: {
type: String,
default: '退出登录'
},
confirmTitle: {
type: String,
default: '确认退出'
},
confirmContent: {
type: String,
default: '确定要退出登录吗?'
},
confirmText: {
type: String,
default: '确定'
},
cancelText: {
type: String,
default: '取消'
},
successMessage: {
type: String,
default: '退出成功'
},
redirectUrl: {
type: String,
default: '/pages/login/login'
}
})
//
const emit = defineEmits(['logout-success', 'logout-error'])
// 退
const handleLogout = async () => {
try {
//
const res = await new Promise((resolve, reject) => {
uni.showModal({
title: props.confirmTitle,
content: props.confirmContent,
confirmText: props.confirmText,
cancelText: props.cancelText,
success: resolve,
fail: reject,
})
})
if (!res.confirm) {
return
}
// 退API
const response = await userLogout()
if (response.code === 200) {
// token
clearToken()
//
uni.removeStorageSync('userInfo')
//
uni.showToast({
title: props.successMessage,
icon: 'success',
duration: 1500,
})
//
emit('logout-success', response)
//
setTimeout(() => {
uni.reLaunch({
url: props.redirectUrl,
})
}, 1500)
} else {
throw new Error(response.msg || '退出失败')
}
} catch (error) {
console.error('退出登录失败:', error)
//
emit('logout-error', error)
uni.showToast({
title: error.message || '退出失败',
icon: 'none',
duration: 2000,
})
}
}
</script>
<style lang="scss" scoped>
.logout-button {
text-align: center;
line-height: 98rpx;
color: #f15a04;
font-size: 16px;
font-weight: 500;
width: 710rpx;
height: 98rpx;
background: #ffffff;
border-radius: 24.5px;
cursor: pointer;
transition: all 0.3s ease;
margin: 0 auto;
&:active {
background: #f5f5f5;
transform: scale(0.98);
}
.logout-text {
color: inherit;
font-size: inherit;
font-weight: inherit;
}
}
</style>

View File

@ -21,16 +21,23 @@
</view>
</view>
</view>
<view class="log-out" @click="handleLogout">退出登录</view>
<view class="logout-container">
<logout-button
@logout-success="onLogoutSuccess"
@logout-error="onLogoutError"
/>
</view>
</view>
</template>
<script>
import { userLogout } from '@/api/auth/auth.js'
import { updateNickName, uploadAvatar } from '@/api/user/user.js'
import { clearToken } from '@/utils/request.js'
import LogoutButton from '@/components/logout-button/logout-button.vue'
export default {
components: {
LogoutButton
},
data() {
return {
userInfoSettings: [
@ -323,59 +330,14 @@ export default {
}
},
// 退
async handleLogout() {
try {
//
const res = await new Promise((resolve, reject) => {
uni.showModal({
title: '确认退出',
content: '确定要退出登录吗?',
confirmText: '确定',
cancelText: '取消',
success: resolve,
fail: reject,
})
})
// 退
onLogoutSuccess(response) {
console.log('退出登录成功:', response)
},
if (!res.confirm) {
return
}
// 退API
const response = await userLogout()
if (response.code === 200) {
// token
clearToken()
//
uni.removeStorageSync('userInfo')
//
uni.showToast({
title: '退出成功',
icon: 'success',
duration: 1500,
})
//
setTimeout(() => {
uni.reLaunch({
url: '/pages/login/login',
})
}, 1500)
} else {
throw new Error(response.msg || '退出失败')
}
} catch (error) {
console.error('退出登录失败:', error)
uni.showToast({
title: error.message || '退出失败',
icon: 'none',
duration: 2000,
})
}
// 退
onLogoutError(error) {
console.error('退出登录失败:', error)
},
},
}
@ -445,84 +407,8 @@ export default {
}
}
.log-out {
.logout-container {
margin-top: 866rpx;
text-align: center;
line-height: 98rpx;
color: #f15a04;
font-size: 16px;
font-weight: 500;
width: 710rpx;
height: 98rpx;
background: #ffffff;
border-radius: 24.5px;
cursor: pointer;
transition: all 0.3s ease;
&:active {
background: #f5f5f5;
transform: scale(0.98);
}
}
.debug-btn {
margin-top: 20rpx;
text-align: center;
line-height: 98rpx;
color: #333;
font-size: 16px;
font-weight: 500;
width: 710rpx;
height: 98rpx;
background: #e0e0e0;
border-radius: 24.5px;
cursor: pointer;
transition: all 0.3s ease;
&:active {
background: #d0d0d0;
transform: scale(0.98);
}
}
.test-btn {
margin-top: 20rpx;
text-align: center;
line-height: 98rpx;
color: #333;
font-size: 16px;
font-weight: 500;
width: 710rpx;
height: 98rpx;
background: #e0e0e0;
border-radius: 24.5px;
cursor: pointer;
transition: all 0.3s ease;
&:active {
background: #d0d0d0;
transform: scale(0.98);
}
}
.validate-btn {
margin-top: 20rpx;
text-align: center;
line-height: 98rpx;
color: #333;
font-size: 16px;
font-weight: 500;
width: 710rpx;
height: 98rpx;
background: #e0e0e0;
border-radius: 24.5px;
cursor: pointer;
transition: all 0.3s ease;
&:active {
background: #d0d0d0;
transform: scale(0.98);
}
}
}
</style>