代码优化

This commit is contained in:
kron
2025-06-13 16:36:18 +08:00
parent 5456905873
commit aacfc17a01
4 changed files with 29 additions and 19 deletions

View File

@@ -33,12 +33,13 @@ const handleLogin = () => {
icon: "none",
});
}
// console.log("getUsername", store.getUsername);
uni.getUserProfile({
desc: "用于完善用户资料",
// 先调用wx.getUserProfile获取用户信息
wx.getUserProfile({
desc: "用于完善用户资料", // 声明获取用户个人信息后的用途
success: (res) => {
const { nickName, avatarUrl } = res.userInfo;
// 获取登录凭证
// 获取登录凭证
uni.login({
provider: "weixin",
success: async (loginRes) => {
@@ -59,17 +60,14 @@ const handleLogin = () => {
},
fail: (err) => {
console.log("获取用户信息失败:", err);
uni.showToast({
title: "获取用户信息失败",
icon: "none",
});
},
});
};
const isLogin = () => {
if (!user.value.id) {
showModal.value = true;
}
return !!user.value.id;
};
const toPage = (path) => {
if (!user.value.id) {
showModal.value = true;
@@ -106,7 +104,7 @@ onMounted(async () => {
</block>
<block v-else>
<view
@click="toLoginPage"
@click="() => (showModal = true)"
:style="{ marginTop: '20px', paddingLeft: '10px' }"
>
<text>微信登录</text>

View File

@@ -8,6 +8,7 @@ import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);
const { updateUser } = store;
const toOrderPage = () => {
uni.navigateTo({
@@ -40,6 +41,11 @@ const toRankIntroPage = () => {
url: "/pages/rank-intro",
});
};
const logout = () => {
uni.removeStorageSync("token");
uni.navigateBack();
updateUser();
};
</script>
<template>
@@ -78,6 +84,7 @@ const toRankIntroPage = () => {
<view class="my-grow" @click="toMyGrowthPage">
<image src="../static/my-grow.png" mode="widthFix" />
</view>
<UserItem title="退出登录(仅用于测试)" :onClick="logout" />
</view>
</view>
</template>