139 lines
3.3 KiB
JavaScript
139 lines
3.3 KiB
JavaScript
// 云数据库操作工具库
|
|
|
|
const db = wx.cloud.database()
|
|
const _ = db.command
|
|
|
|
/**
|
|
* 用户相关操作
|
|
*/
|
|
const userDB = {
|
|
/**
|
|
* 通过 openId 查找用户,不存在则创建
|
|
* @param {string} openid
|
|
* @param {object} userInfo - { avatarUrl, nickName }
|
|
* @returns {Promise<object>} 用户记录
|
|
*/
|
|
async loginOrCreate(openid, userInfo) {
|
|
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 }
|
|
} 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 获取用户信息
|
|
* @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
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 预约相关操作
|
|
*/
|
|
const appointmentDB = {
|
|
/**
|
|
* 创建预约
|
|
* @param {object} data - 预约表单数据
|
|
* @returns {Promise<string>} 新记录 _id
|
|
*/
|
|
async create(data) {
|
|
const res = await db.collection('appointments').add({
|
|
data: {
|
|
...data,
|
|
status: 'pending',
|
|
statusText: '待审核',
|
|
createTime: db.serverDate()
|
|
}
|
|
})
|
|
return res._id
|
|
},
|
|
|
|
/**
|
|
* 获取当前用户的预约列表(按创建时间倒序)
|
|
* @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
|
|
},
|
|
|
|
/**
|
|
* 获取当前用户最新一条预约
|
|
* @param {string} openid
|
|
* @returns {Promise<object|null>}
|
|
*/
|
|
async getLatest(openid) {
|
|
const res = await db.collection('appointments')
|
|
.where({ _openid: openid })
|
|
.orderBy('createTime', 'desc')
|
|
.limit(1)
|
|
.get()
|
|
return res.data.length > 0 ? res.data[0] : null
|
|
},
|
|
|
|
/**
|
|
* 取消预约
|
|
* @param {string} id - 预约记录 _id
|
|
* @param {string} openid - 当前用户 openid,用于权限校验
|
|
* @returns {Promise<boolean>} 是否成功
|
|
*/
|
|
async cancel(id, openid) {
|
|
// 先校验该预约属于当前用户
|
|
const res = await db.collection('appointments').doc(id).get()
|
|
if (res.data._openid !== openid) {
|
|
return false
|
|
}
|
|
if (res.data.status !== 'pending') {
|
|
return false
|
|
}
|
|
await db.collection('appointments').doc(id).update({
|
|
data: {
|
|
status: 'cancelled',
|
|
statusText: '已取消'
|
|
}
|
|
})
|
|
return true
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
db,
|
|
_,
|
|
userDB,
|
|
appointmentDB
|
|
}
|