feat: 添加登录失败处理及记录格式化功能
增加登录失败状态处理和重试机制 将记录格式化逻辑提取到工具函数 优化云数据库查询性能
This commit is contained in:
+28
-22
@@ -1,19 +1,26 @@
|
||||
// index.js
|
||||
const { appointmentDB } = require('../../utils/cloud')
|
||||
const { appointmentDB, formatRecord } = require('../../utils/cloud')
|
||||
const app = getApp()
|
||||
|
||||
Page({
|
||||
data: {
|
||||
isLoggedIn: false,
|
||||
loginFailed: false,
|
||||
latestRecord: null
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
if (app.globalData.isLoggedIn) {
|
||||
this.onLoginReady()
|
||||
} else if (app.globalData.loginFailed) {
|
||||
this.onLoginFailed()
|
||||
} else {
|
||||
app.loginReadyCallback = () => {
|
||||
this.onLoginReady()
|
||||
app.loginReadyCallback = (userInfo) => {
|
||||
if (userInfo) {
|
||||
this.onLoginReady()
|
||||
} else {
|
||||
this.onLoginFailed()
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -25,20 +32,32 @@ Page({
|
||||
},
|
||||
|
||||
onLoginReady() {
|
||||
this.setData({ isLoggedIn: true })
|
||||
this.setData({ isLoggedIn: true, loginFailed: false })
|
||||
this.loadLatestRecord()
|
||||
},
|
||||
|
||||
async loadLatestRecord() {
|
||||
if (!this.data.isLoggedIn) {
|
||||
this.setData({ latestRecord: null })
|
||||
return
|
||||
onLoginFailed() {
|
||||
this.setData({ isLoggedIn: false, loginFailed: true })
|
||||
},
|
||||
|
||||
onRetry() {
|
||||
this.setData({ loginFailed: false })
|
||||
app.silentLogin()
|
||||
app.loginReadyCallback = (userInfo) => {
|
||||
if (userInfo) {
|
||||
this.onLoginReady()
|
||||
} else {
|
||||
this.onLoginFailed()
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
async loadLatestRecord() {
|
||||
try {
|
||||
const openid = app.globalData.userInfo.openid
|
||||
const record = await appointmentDB.getLatest(openid)
|
||||
if (record) {
|
||||
this.setData({ latestRecord: this.formatRecord(record) })
|
||||
this.setData({ latestRecord: formatRecord(record) })
|
||||
} else {
|
||||
this.setData({ latestRecord: null })
|
||||
}
|
||||
@@ -48,19 +67,6 @@ Page({
|
||||
}
|
||||
},
|
||||
|
||||
formatRecord(record) {
|
||||
const date = record.createTime
|
||||
let createTimeStr = ''
|
||||
if (date) {
|
||||
if (typeof date === 'object' && date.$date) {
|
||||
createTimeStr = new Date(date.$date).toLocaleString('zh-CN')
|
||||
} else {
|
||||
createTimeStr = new Date(date).toLocaleString('zh-CN')
|
||||
}
|
||||
}
|
||||
return { ...record, createTime: createTimeStr }
|
||||
},
|
||||
|
||||
goAppointment() {
|
||||
wx.navigateTo({
|
||||
url: '/pages/appointment/appointment'
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
<!--index.wxml-->
|
||||
<view class="page">
|
||||
<!-- loading 遮罩 -->
|
||||
<view class="loading-mask" wx:if="{{!isLoggedIn}}">
|
||||
<view class="loading-mask" wx:if="{{!isLoggedIn && !loginFailed}}">
|
||||
<view class="loading-spinner"></view>
|
||||
<text class="loading-text">正在获取身份信息...</text>
|
||||
</view>
|
||||
|
||||
<!-- 登录失败 -->
|
||||
<view class="loading-mask" wx:if="{{loginFailed}}">
|
||||
<text class="fail-icon">⚠️</text>
|
||||
<text class="fail-text">网络异常,请重试</text>
|
||||
<view class="retry-btn" bindtap="onRetry">重新加载</view>
|
||||
</view>
|
||||
|
||||
<view class="header">
|
||||
<view class="header-icon">🏢</view>
|
||||
<text class="header-title">访客预约系统</text>
|
||||
|
||||
+27
-2
@@ -56,6 +56,30 @@ page {
|
||||
color: #999;
|
||||
}
|
||||
|
||||
/* 登录失败 */
|
||||
.fail-icon {
|
||||
font-size: 80rpx;
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
|
||||
.fail-text {
|
||||
font-size: 28rpx;
|
||||
color: #999;
|
||||
margin-bottom: 32rpx;
|
||||
}
|
||||
|
||||
.retry-btn {
|
||||
font-size: 28rpx;
|
||||
color: #fff;
|
||||
background: #1890ff;
|
||||
padding: 16rpx 56rpx;
|
||||
border-radius: 36rpx;
|
||||
}
|
||||
|
||||
.retry-btn:active {
|
||||
background: #096dd9;
|
||||
}
|
||||
|
||||
.header-title {
|
||||
font-size: 44rpx;
|
||||
font-weight: 700;
|
||||
@@ -80,11 +104,12 @@ page {
|
||||
border-radius: 20rpx;
|
||||
padding: 36rpx 32rpx;
|
||||
margin-bottom: 24rpx;
|
||||
box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
|
||||
box-shadow: 0 8rpx 32rpx rgba(24, 144, 255, 0.12), 0 2rpx 8rpx rgba(0, 0, 0, 0.06);
|
||||
}
|
||||
|
||||
.action-card:active {
|
||||
background: #f0f0f0;
|
||||
box-shadow: 0 4rpx 16rpx rgba(24, 144, 255, 0.08), 0 1rpx 4rpx rgba(0, 0, 0, 0.04);
|
||||
}
|
||||
|
||||
.action-icon-wrap {
|
||||
@@ -140,7 +165,7 @@ page {
|
||||
border-radius: 20rpx;
|
||||
padding: 28rpx 32rpx;
|
||||
margin-top: 8rpx;
|
||||
box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
|
||||
box-shadow: 0 8rpx 32rpx rgba(82, 196, 26, 0.12), 0 2rpx 8rpx rgba(0, 0, 0, 0.06);
|
||||
}
|
||||
|
||||
.latest-header {
|
||||
|
||||
Reference in New Issue
Block a user