This commit is contained in:
那些花儿
2025-04-25 08:48:11 +08:00
parent 94656c9449
commit 89c2b07ca9
19 changed files with 1702 additions and 1604 deletions
+88 -13
View File
@@ -51,19 +51,25 @@
</template>
<script setup>
import {ref, reactive, computed, onMounted} from 'vue'
import {ref, reactive, computed, onMounted, onBeforeUnmount} from 'vue'
import {Toast} from 'tdesign-mobile-vue';
import {useRequest} from "@/util/request";
import {useRequest} from "@/utils/request";
const request = useRequest()
const loginForm = reactive({
phone: '',
phone: '18571562330',
smsCode: ''
})
// 倒计时相关
const countdown = ref(0)
const timer = ref(null)
// 本地存储键名
const SMS_COUNTDOWN_KEY = '__SMS_COUNTDOWN__'
const SMS_PHONE_KEY = '__SMS_PHONE__'
// 是否在倒计时中
const isCountingDown = computed(() => countdown.value > 0)
// 验证码按钮文字
@@ -71,18 +77,66 @@ const countDownText = computed(() => {
return isCountingDown.value ? `${countdown.value}秒后重试` : '获取验证码'
})
// 开始倒计时
const startCountDown = () => {
countdown.value = 60
// 保存倒计时状态到本地存储
const saveCountdownState = (seconds, phone) => {
if (seconds <= 0) {
uni.removeStorageSync(SMS_COUNTDOWN_KEY)
uni.removeStorageSync(SMS_PHONE_KEY)
return
}
// 保存结束时间戳和手机号
const endTime = Date.now() + seconds * 1000
uni.setStorageSync(SMS_COUNTDOWN_KEY, endTime)
uni.setStorageSync(SMS_PHONE_KEY, phone)
}
// 从本地存储恢复倒计时状态
const restoreCountdownState = () => {
const endTime = uni.getStorageSync(SMS_COUNTDOWN_KEY)
const savedPhone = uni.getStorageSync(SMS_PHONE_KEY)
if (endTime && savedPhone && savedPhone === loginForm.phone) {
const now = Date.now()
const remainingSeconds = Math.floor((endTime - now) / 1000)
if (remainingSeconds > 0) {
countdown.value = remainingSeconds
startCountDownTimer() // 重新启动定时器
} else {
// 倒计时已结束,清除存储
uni.removeStorageSync(SMS_COUNTDOWN_KEY)
uni.removeStorageSync(SMS_PHONE_KEY)
}
}
}
// 开始倒计时定时器
const startCountDownTimer = () => {
if (timer.value) {
clearInterval(timer.value)
}
timer.value = setInterval(() => {
countdown.value--
if (countdown.value <= 0) {
clearInterval(timer.value)
timer.value = null
// 清除存储
uni.removeStorageSync(SMS_COUNTDOWN_KEY)
uni.removeStorageSync(SMS_PHONE_KEY)
}
}, 1000)
}
// 开始倒计时
const startCountDown = () => {
countdown.value = 60
saveCountdownState(60, loginForm.phone)
startCountDownTimer()
}
// 获取验证码
async function getSmsCode() {
@@ -103,8 +157,6 @@ async function getSmsCode() {
}
} catch (e) {
Toast.error(e.message)
} finally {
}
}
@@ -119,12 +171,30 @@ async function handleLogin() {
}
try {
const {data, code, msg} = await useRequest().post('/client/auth/sms-login', loginForm)
const {data, code, msg} = await request.post('/client/auth/sms-login', loginForm)
if (code === 0) {
Toast.success('登录成功')
uni.switchTab({
url: '/pages/index/index'
})
// 保存token
uni.setStorageSync('token', data)
// 获取之前保存的重定向URL
const redirectUrl = uni.getStorageSync('REDIRECT_URL') || '/pages/index/index'
uni.removeStorageSync('REDIRECT_URL') // 使用后删除
// 延迟跳转,让用户看到成功提示
setTimeout(() => {
if (redirectUrl.startsWith('/pages/index/') || redirectUrl.startsWith('/pages/profile/')) {
// 主页和个人中心,使用switchTab
uni.switchTab({
url: redirectUrl
})
} else {
// 其他页面使用reLaunch
uni.reLaunch({
url: redirectUrl
})
}
}, 1000)
} else {
Toast.error(msg)
}
@@ -134,13 +204,18 @@ async function handleLogin() {
}
}
// 页面加载时恢复倒计时状态
onMounted(() => {
restoreCountdownState()
})
// 页面卸载前清理定时器
onBeforeUnmount(() => {
if (timer.value) {
clearInterval(timer.value)
timer.value = null
}
})
</script>
<style lang="less" scoped>