优化下登录逻辑
This commit is contained in:
@ -10,7 +10,7 @@
|
||||
<template>
|
||||
<view class="">
|
||||
<view class="">
|
||||
<navbar title="个人信息" custom-class='!bg-[#F6F7F8]'></navbar>
|
||||
<navbar title="个人信息" custom-class='!bg-[#F6F7F8]' :leftArrow="false"></navbar>
|
||||
</view>
|
||||
|
||||
<view class="bg-white rounded-16rpx mt-20rpx mx-30rpx px-30rpx py-32rpx">
|
||||
@ -122,6 +122,8 @@
|
||||
|
||||
<script lang="ts" setup>
|
||||
import {toast} from '@/utils/toast'
|
||||
import { useUserStore } from '@/store'
|
||||
import { router } from '@/utils/tools'
|
||||
|
||||
const OSS = inject('OSS')
|
||||
const showLogoutPopup = ref<boolean>(false) // 是否显示退出登录弹出框
|
||||
@ -159,7 +161,12 @@
|
||||
|
||||
// 退出
|
||||
handleLogout: () => {
|
||||
|
||||
const userStore = useUserStore()
|
||||
userStore.logout()
|
||||
if (!userStore.isLoggedIn) {
|
||||
toast.info('退出成功')
|
||||
router.reLaunch('/pages/my/my')
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
@ -27,10 +27,12 @@ export const getUrlCode = (): { [key: string]: string | undefined } => {
|
||||
*/
|
||||
export async function snsapiBaseAuthorize() {
|
||||
// TODO 测试代码
|
||||
wxSnsapiBaseLogin({code: '031dX13w3vRqT539Mo0w30juM84dX13s'}).then((res: IUserInfoVo) => {
|
||||
wxSnsapiBaseLogin({code: '081kkz0w3wXuX53Hf83w3l4oy30kkz0z'}).then((res: IUserInfoVo) => {
|
||||
console.log("登录成功 ~ snsapiBaseAuthorize ~ res:", res)
|
||||
// 映射 IUserLogin 到 IUserInfoVo
|
||||
useUserStore().setUserInfo(res)
|
||||
uni.$emit('loginSuccess')
|
||||
|
||||
}).catch(err => {
|
||||
// 失败就重新授权
|
||||
uni.setStorageSync('wechatCode', 0)
|
||||
@ -59,6 +61,7 @@ export async function snsapiBaseAuthorize() {
|
||||
// 保存最新code
|
||||
uni.setStorageSync('wechatCode',code)
|
||||
// 使用code换取用户信息(同步写法)
|
||||
uni.showLoading({title: '登录中...'})
|
||||
try {
|
||||
const res: IUserInfoVo = await wxSnsapiBaseLogin({code})
|
||||
console.log("登录成功 ~ snsapiBaseAuthorize ~ res:", res)
|
||||
@ -70,8 +73,10 @@ export async function snsapiBaseAuthorize() {
|
||||
// } else {
|
||||
uni.$emit('loginSuccess')
|
||||
// }
|
||||
uni.hideLoading()
|
||||
} catch (err) {
|
||||
uni.setStorageSync('wechatCode', 0)
|
||||
uni.hideLoading()
|
||||
console.log('请求失败', err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
<wd-navbar safeAreaInsetTop :bordered="false" custom-style="background-color: transparent !important;">
|
||||
<template #right>
|
||||
<view class="mr-16rpx flex items-center right-slot">
|
||||
<a :href="'tel:' + userInfo.mobile">
|
||||
<a :href="'tel:' + userInfo?.mobile">
|
||||
<wd-img width="36rpx" height="36rpx" :src="`${OSS}icon/icon_service.png`"></wd-img>
|
||||
</a>
|
||||
</view>
|
||||
@ -27,8 +27,8 @@
|
||||
<view>
|
||||
<wd-img width="120rpx" height="120rpx" :src="`${OSS}icon/icon_avatar.png`" mode="aspectFill" round />
|
||||
</view>
|
||||
<!-- <view class="flex-1 ml-22rpx flex justify-between items-center" @click="My.handleToProfile"> -->
|
||||
<view class="flex-1 ml-22rpx flex justify-between items-center">
|
||||
<view class="flex-1 ml-22rpx flex justify-between items-center" @click="My.handleToProfile">
|
||||
<!-- <view class="flex-1 ml-22rpx flex justify-between items-center"> -->
|
||||
<view>
|
||||
<view class="text-[#303133] text-36rpx leading-50rpx ml-8rpx">{{ isLogin ? userInfo.nickname : '立即登录' }}</view>
|
||||
<view v-if="isLogin" class="flex justify-center items-center vip-bg mt-10rpx" >
|
||||
@ -56,11 +56,11 @@
|
||||
<view class="mt-16rpx mx-30rpx flex justify-between">
|
||||
<view class="flex items-center">
|
||||
<view class="w-160rpx text-[#303133] text-center" @click="My.handleToCoupon">
|
||||
<view class="font-bold text-36rpx leading-50rpx"> {{ isLogin ? user.coupon_count : '- -' }}</view>
|
||||
<view class="font-bold text-36rpx leading-50rpx"> {{ isLogin ? user?.coupon_count : '- -' }}</view>
|
||||
<view class="text-24rpx leading-34rpx">优惠券</view>
|
||||
</view>
|
||||
<view class="w-160rpx text-[#303133] text-center" @click="My.handleToCollect">
|
||||
<view class="font-bold text-36rpx leading-50rpx"> {{ isLogin ? user.collect_count : '- -' }}</view>
|
||||
<view class="font-bold text-36rpx leading-50rpx"> {{ isLogin ? user?.collect_count : '- -' }}</view>
|
||||
<view class="text-24rpx leading-34rpx">收藏</view>
|
||||
</view>
|
||||
</view>
|
||||
@ -86,7 +86,7 @@
|
||||
<wd-img width="100%" height="100%" :src="`${OSS}icon/icon_vip.png`" mode="aspectFill"></wd-img>
|
||||
</view>
|
||||
<view class="flex items-center leading-34rpx" @click="My.handleToVipBenefits">
|
||||
<view class="font-400 text-24rpx ml-12rpx mr-20rpx text-[#EECC99]">2026.03.06到期</view>
|
||||
<view class="font-400 text-24rpx ml-12rpx mr-20rpx text-[#EECC99]">{{ isLogin ? '会员到期时间' : '- -' }}</view>
|
||||
<view class="flex items-center mt-4rpx">
|
||||
<wd-icon name="arrow-right" size="24rpx" color="#EECC99"></wd-icon>
|
||||
</view>
|
||||
@ -101,7 +101,7 @@
|
||||
<view class="flex items-center justify-between mx-40rpx">
|
||||
<view class="flex item-center leading-34rpx text-[#EECC99]">
|
||||
<view class="font-400 text-24rpx mr-18rpx">上月消费</view>
|
||||
<view class="font-400 text-28rpx mr-18rpx">¥23014.02</view>
|
||||
<view class="font-400 text-28rpx mr-18rpx">¥{{ isLogin ? '上月消费金额显示' : '- -' }}</view>
|
||||
</view>
|
||||
<view class="font-400 text-24rpx text-[#D2D0D0] leading-34rpx">请尽快领取会员权益</view>
|
||||
</view>
|
||||
@ -235,33 +235,25 @@
|
||||
|
||||
onShow(() => {
|
||||
const code = getUrlCode().code // 截取code
|
||||
const userStore = useUserStore()
|
||||
console.log("🚀 ~ userStore:", userStore.userInfo)
|
||||
if (userStore.userInfo) {
|
||||
userInfo.value = userStore.userInfo
|
||||
isLogin.value = true
|
||||
My.handleInitUserInfo(code)
|
||||
// const userStore = useUserStore()
|
||||
// if (userStore.isLoggedIn) {
|
||||
// userInfo.value = userStore.userInfo
|
||||
// isLogin.value = userStore.isLoggedIn
|
||||
|
||||
// 获取用户详情信息接口
|
||||
getUserInfo().then(res => {
|
||||
user.value = res
|
||||
})
|
||||
|
||||
} else if (code && !userStore.userInfo.token) {
|
||||
// 这里是微信授权之后跳转回到本页面获取到code但是没有登录状态的话需要进行登录操作
|
||||
snsapiBaseAuthorize()
|
||||
}
|
||||
// // 获取用户详情信息接口
|
||||
// getUserInfo().then(res => {
|
||||
// user.value = res
|
||||
// })
|
||||
// } else if (code && !userStore.userInfo.token) {
|
||||
// // 这里是微信授权之后跳转回到本页面获取到code但是没有登录状态的话需要进行登录操作
|
||||
// snsapiBaseAuthorize()
|
||||
// }
|
||||
})
|
||||
|
||||
onLoad(() => {
|
||||
uni.$on('loginSuccess', () => {
|
||||
// 刷新用户信息
|
||||
console.log("🚀 ~ 刷新用户信息:")
|
||||
const userStore = useUserStore()
|
||||
if (userStore.userInfo) {
|
||||
userInfo.value = userStore.userInfo
|
||||
isLogin.value = true
|
||||
}
|
||||
// 你可以在这里做跳转或弹窗提示
|
||||
My.handleInitUserInfo()
|
||||
})
|
||||
|
||||
My.handleInit()
|
||||
@ -272,12 +264,29 @@
|
||||
})
|
||||
|
||||
const My = {
|
||||
// 初始化
|
||||
handleInit: () => {
|
||||
getMyCoupon().then(res => {
|
||||
couponList.value = Array.isArray(res) ? res : []
|
||||
})
|
||||
},
|
||||
|
||||
handleInitUserInfo: (code: string = '') => {
|
||||
const userStore = useUserStore()
|
||||
if (code && !userStore.userInfo.token) {
|
||||
// 这里是微信授权之后跳转回到本页面获取到code但是没有登录状态的话需要进行登录操作
|
||||
snsapiBaseAuthorize()
|
||||
} else if (userStore.isLoggedIn) {
|
||||
userInfo.value = userStore.userInfo
|
||||
isLogin.value = userStore.isLoggedIn
|
||||
|
||||
// 获取用户详情信息接口
|
||||
getUserInfo().then(res => {
|
||||
user.value = res
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 领取优惠券
|
||||
handleClaimCoupon: async (id: number) => {
|
||||
const res = await claimMyCoupon({id})
|
||||
|
||||
@ -25,6 +25,9 @@ const userInfoState: IUserInfoVo = {
|
||||
mobile: ''
|
||||
}
|
||||
|
||||
// 默认未登录
|
||||
const isLoggedIn = ref<boolean>(false)
|
||||
|
||||
export const useUserStore = defineStore(
|
||||
'user',
|
||||
() => {
|
||||
@ -41,6 +44,7 @@ export const useUserStore = defineStore(
|
||||
val.avatar = defaultAvatar
|
||||
}
|
||||
userInfo.value = val
|
||||
isLoggedIn.value = true
|
||||
}
|
||||
const setUserAvatar = (avatar: string) => {
|
||||
userInfo.value.avatar = avatar
|
||||
@ -52,6 +56,7 @@ export const useUserStore = defineStore(
|
||||
userInfo.value = { ...userInfoState }
|
||||
uni.removeStorageSync('userInfo')
|
||||
uni.removeStorageSync('token')
|
||||
isLoggedIn.value = false
|
||||
}
|
||||
/**
|
||||
* 获取用户信息
|
||||
@ -111,7 +116,8 @@ export const useUserStore = defineStore(
|
||||
setUserAvatar,
|
||||
logout,
|
||||
setUserInfo,
|
||||
removeUserInfo
|
||||
removeUserInfo,
|
||||
isLoggedIn
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
@ -34,7 +34,7 @@ export const router = {
|
||||
}, time);
|
||||
},
|
||||
|
||||
//关闭当前所有页面跳转至非table页面
|
||||
//关闭当前所有页面跳转至页面
|
||||
reLaunch: (url: string, time = 0) => {
|
||||
setTimeout(function() {
|
||||
uni.reLaunch({
|
||||
|
||||
Reference in New Issue
Block a user