feat: 添加登录失败处理及记录格式化功能

增加登录失败状态处理和重试机制
将记录格式化逻辑提取到工具函数
优化云数据库查询性能
This commit is contained in:
chenglijuan
2026-04-18 22:34:35 +08:00
parent 54556374b1
commit d5478429c0
7 changed files with 138 additions and 86 deletions
+57 -33
View File
@@ -1,7 +1,33 @@
// 云数据库操作工具库
const db = wx.cloud.database()
const _ = db.command
/**
* 延迟获取 db 实例,确保 cloud.init 已完成
*/
function getDb() {
return wx.cloud.database()
}
function getCmd() {
return getDb().command
}
/**
* 格式化云数据库时间字段
* @param {object} record - 含 createTime 的记录
* @returns {object} 格式化后的记录
*/
function 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 }
}
/**
* 用户相关操作
@@ -10,50 +36,32 @@ const userDB = {
/**
* 通过 openId 查找用户,不存在则创建
* @param {string} openid
* @param {object} userInfo - { avatarUrl, nickName }
* @returns {Promise<object>} 用户记录
*/
async loginOrCreate(openid, userInfo) {
async loginOrCreate(openid) {
const db = getDb()
const res = await db.collection('users').where({ _openid: openid }).get()
if (res.data.length > 0) {
// 已存在,更新头像昵称
const existing = res.data[0]
await db.collection('users').doc(existing._id).update({
data: {
avatarUrl: userInfo.avatarUrl,
nickName: userInfo.nickName,
lastLoginTime: db.serverDate()
}
})
return { ...existing, avatarUrl: userInfo.avatarUrl, nickName: userInfo.nickName }
return existing
} else {
// 不存在,新建
const addRes = await db.collection('users').add({
data: {
_openid: openid,
avatarUrl: userInfo.avatarUrl,
nickName: userInfo.nickName,
createTime: db.serverDate(),
lastLoginTime: db.serverDate()
}
})
return {
_id: addRes._id,
_openid: openid,
avatarUrl: userInfo.avatarUrl,
nickName: userInfo.nickName
_openid: openid
}
}
},
/**
* 根据 openId 获取用户信息
* @param {string} openid
* @returns {Promise<object|null>}
*/
async getByOpenId(openid) {
const res = await db.collection('users').where({ _openid: openid }).get()
return res.data.length > 0 ? res.data[0] : null
}
}
@@ -67,6 +75,7 @@ const appointmentDB = {
* @returns {Promise<string>} 新记录 _id
*/
async create(data) {
const db = getDb()
const res = await db.collection('appointments').add({
data: {
...data,
@@ -79,16 +88,29 @@ const appointmentDB = {
},
/**
* 获取当前用户的预约列表(按创建时间倒序)
* 获取当前用户的预约列表(按创建时间倒序,自动分页取全部
* @param {string} openid
* @returns {Promise<Array>}
*/
async getList(openid) {
const res = await db.collection('appointments')
.where({ _openid: openid })
.orderBy('createTime', 'desc')
.get()
return res.data
const db = getDb()
const MAX_LIMIT = 100
let allData = []
let countResult = await db.collection('appointments').where({ _openid: openid }).count()
const total = countResult.total
const batchTimes = Math.ceil(total / MAX_LIMIT)
for (let i = 0; i < batchTimes; i++) {
const res = await db.collection('appointments')
.where({ _openid: openid })
.orderBy('createTime', 'desc')
.skip(i * MAX_LIMIT)
.limit(MAX_LIMIT)
.get()
allData = allData.concat(res.data)
}
return allData
},
/**
@@ -97,6 +119,7 @@ const appointmentDB = {
* @returns {Promise<object|null>}
*/
async getLatest(openid) {
const db = getDb()
const res = await db.collection('appointments')
.where({ _openid: openid })
.orderBy('createTime', 'desc')
@@ -112,7 +135,7 @@ const appointmentDB = {
* @returns {Promise<boolean>} 是否成功
*/
async cancel(id, openid) {
// 先校验该预约属于当前用户
const db = getDb()
const res = await db.collection('appointments').doc(id).get()
if (res.data._openid !== openid) {
return false
@@ -131,8 +154,9 @@ const appointmentDB = {
}
module.exports = {
db,
_,
getDb,
getCmd,
formatRecord,
userDB,
appointmentDB
}