feat:"完成页面接口的对接"
This commit is contained in:
@@ -120,6 +120,7 @@
|
||||
|
||||
<script setup lang="uts">
|
||||
import { STORAGE_KEYS } from '@/utils/config.uts'
|
||||
import { wechatLogin } from '@/api/index.uts'
|
||||
|
||||
// 用户信息类型
|
||||
type UserInfo = {
|
||||
@@ -182,41 +183,92 @@
|
||||
})
|
||||
}
|
||||
|
||||
// 获取统计数据
|
||||
const fetchStats = () => {
|
||||
// 获取预约数量(可以从后端API获取)
|
||||
bookingCount.value = 0
|
||||
|
||||
// 获取收藏数量
|
||||
const favorites = uni.getStorageSync(STORAGE_KEYS.FAVORITES) as string[]
|
||||
favoriteCount.value = favorites ? favorites.length : 0
|
||||
}
|
||||
|
||||
// 登录
|
||||
const handleLogin = () => {
|
||||
// #ifdef MP-WEIXIN
|
||||
// 必须在用户点击事件中直接调用 getUserProfile
|
||||
uni.getUserProfile({
|
||||
desc: '用于完善用户资料',
|
||||
success: (res) => {
|
||||
userInfo.value = {
|
||||
id: '',
|
||||
nickName: res.userInfo.nickName,
|
||||
avatar: res.userInfo.avatarUrl,
|
||||
phone: ''
|
||||
} as UserInfo
|
||||
|
||||
// 保存用户信息
|
||||
uni.setStorageSync(STORAGE_KEYS.USER_INFO, {
|
||||
nickName: res.userInfo.nickName,
|
||||
avatar: res.userInfo.avatarUrl
|
||||
} as UTSJSONObject)
|
||||
uni.setStorageSync(STORAGE_KEYS.TOKEN, 'mock_token_' + Date.now().toString())
|
||||
isLoggedIn.value = true
|
||||
|
||||
uni.showToast({
|
||||
title: '登录成功',
|
||||
icon: 'success'
|
||||
success: (profileRes) => {
|
||||
// 在授权成功的回调中再调用 uni.login 获取 code
|
||||
uni.login({
|
||||
provider: 'weixin',
|
||||
success: async (loginRes) => {
|
||||
const code = loginRes.code
|
||||
try {
|
||||
// 调用后端微信登录接口
|
||||
const res = await wechatLogin(code)
|
||||
|
||||
if (res.code === 0 && res.data != null) {
|
||||
const data = res.data as UTSJSONObject
|
||||
const token = data['access_token'] as string
|
||||
const userDataObj = data['user'] as UTSJSONObject
|
||||
|
||||
// 保存 token
|
||||
uni.setStorageSync(STORAGE_KEYS.TOKEN, token)
|
||||
|
||||
// 保存用户信息
|
||||
userInfo.value = {
|
||||
id: String(userDataObj['id']),
|
||||
nickName: profileRes.userInfo.nickName,
|
||||
avatar: profileRes.userInfo.avatarUrl,
|
||||
phone: (userDataObj['phone'] ?? '') as string
|
||||
} as UserInfo
|
||||
|
||||
uni.setStorageSync(STORAGE_KEYS.USER_INFO, {
|
||||
id: userDataObj['id'],
|
||||
nickName: profileRes.userInfo.nickName,
|
||||
avatar: profileRes.userInfo.avatarUrl,
|
||||
phone: userDataObj['phone']
|
||||
} as UTSJSONObject)
|
||||
|
||||
isLoggedIn.value = true
|
||||
|
||||
uni.showToast({
|
||||
title: '登录成功',
|
||||
icon: 'success'
|
||||
})
|
||||
|
||||
// 刷新统计数据
|
||||
fetchStats()
|
||||
} else {
|
||||
throw new Error(res.message)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('登录失败', error)
|
||||
uni.showToast({
|
||||
title: '登录失败,请重试',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
uni.showToast({
|
||||
title: '登录失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
fail: () => {
|
||||
uni.showToast({
|
||||
title: '登录失败',
|
||||
title: '授权失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
})
|
||||
// #endif
|
||||
|
||||
|
||||
// #ifndef MP-WEIXIN
|
||||
uni.showToast({
|
||||
title: '请在微信小程序中登录',
|
||||
@@ -253,17 +305,29 @@
|
||||
|
||||
// 跳转预约列表
|
||||
const goToBookingList = () => {
|
||||
uni.showToast({
|
||||
title: '功能开发中',
|
||||
icon: 'none'
|
||||
if (!isLoggedIn.value) {
|
||||
uni.showToast({
|
||||
title: '请先登录',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
uni.navigateTo({
|
||||
url: '/pages/user/booking-list'
|
||||
})
|
||||
}
|
||||
|
||||
// 跳转收藏列表
|
||||
const goToFavorites = () => {
|
||||
uni.showToast({
|
||||
title: '功能开发中',
|
||||
icon: 'none'
|
||||
if (!isLoggedIn.value) {
|
||||
uni.showToast({
|
||||
title: '请先登录',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
uni.navigateTo({
|
||||
url: '/pages/user/favorites'
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user