登录按钮添加loading

This commit is contained in:
kron
2025-08-09 12:32:06 +08:00
parent 1f79280301
commit ae245872f4

View File

@@ -1,5 +1,6 @@
<script setup> <script setup>
import { ref } from "vue"; import { ref } from "vue";
import { onShow } from "@dcloudio/uni-app";
import Avatar from "@/components/Avatar.vue"; import Avatar from "@/components/Avatar.vue";
import SButton from "@/components/SButton.vue"; import SButton from "@/components/SButton.vue";
import { getMyDevicesAPI, loginAPI, getHomeData } from "@/apis"; import { getMyDevicesAPI, loginAPI, getHomeData } from "@/apis";
@@ -15,6 +16,7 @@ const props = defineProps({
const agree = ref(false); const agree = ref(false);
const avatarUrl = ref(""); const avatarUrl = ref("");
const nickName = ref(""); const nickName = ref("");
const loading = ref(false);
const handleAgree = () => { const handleAgree = () => {
agree.value = !agree.value; agree.value = !agree.value;
}; };
@@ -28,6 +30,7 @@ function onNicknameChange(e) {
} }
const handleLogin = () => { const handleLogin = () => {
if (loading.value) return;
if (!avatarUrl.value) { if (!avatarUrl.value) {
return uni.showToast({ return uni.showToast({
title: "请选择头像", title: "请选择头像",
@@ -46,6 +49,7 @@ const handleLogin = () => {
icon: "none", icon: "none",
}); });
} }
loading.value = true;
uni.login({ uni.login({
provider: "weixin", provider: "weixin",
success: async (loginRes) => { success: async (loginRes) => {
@@ -66,6 +70,7 @@ const handleLogin = () => {
props.onClose(); props.onClose();
}, },
fail: (err) => { fail: (err) => {
loading.value = false;
uni.showToast({ uni.showToast({
title: "登录失败", title: "登录失败",
icon: "none", icon: "none",
@@ -74,6 +79,9 @@ const handleLogin = () => {
}, },
}); });
}; };
onShow(() => {
loading.value = false;
});
</script> </script>
<template> <template>
@@ -101,12 +109,21 @@ const handleLogin = () => {
/> />
</view> </view>
<SButton :rounded="20" width="80vw" :onClick="handleLogin"> <SButton :rounded="20" width="80vw" :onClick="handleLogin">
<block v-if="!loading">
<image <image
src="../static/wechat-icon.png" src="../static/wechat-icon.png"
mode="widthFix" mode="widthFix"
class="wechat-icon" class="wechat-icon"
/> />
<text :style="{ color: '#000' }">登录/注册</text> <text :style="{ color: '#000' }">登录/注册</text>
</block>
<block v-else>
<image
src="../static/btn-loading.png"
mode="widthFix"
class="loading"
/>
</block>
</SButton> </SButton>
<view class="protocol" @click="handleAgree"> <view class="protocol" @click="handleAgree">
<view v-if="!agree" /> <view v-if="!agree" />
@@ -188,4 +205,10 @@ const handleLogin = () => {
width: 80%; width: 80%;
display: flex; display: flex;
} }
.loading {
width: 25px;
height: 25px;
background-blend-mode: darken;
animation: rotate 1s linear infinite;
}
</style> </style>