提交
This commit is contained in:
chenglijuan
2026-04-21 13:05:37 +08:00
parent d5478429c0
commit 404730238b
2 changed files with 100 additions and 28 deletions
+56 -18
View File
@@ -9,34 +9,72 @@ App({
}) })
} }
// 自动静默登录:调用云函数获取 openid
// 自动静默登录:wx.login 获取 code,再请求后端接口换取 openid
this.silentLogin() this.silentLogin()
}, },
silentLogin() { silentLogin() {
wx.cloud.callFunction({ wx.login({
name: 'login', success: (loginRes) => {
data: {}, if (loginRes.code) {
success: (res) => { this.loginWithCode(loginRes.code)
const openid = res.result.openid } else {
const userInfo = res.result.userInfo || {} console.error('wx.login 失败', loginRes.errMsg)
userInfo.openid = openid this.globalData.isLoggedIn = false
this.globalData.loginFailed = true
this.globalData.userInfo = userInfo if (this.loginReadyCallback) {
this.globalData.isLoggedIn = true this.loginReadyCallback(null)
wx.setStorageSync('userInfo', userInfo) }
// 通知当前页面刷新
if (this.loginReadyCallback) {
this.loginReadyCallback(userInfo)
} }
}, },
fail: (err) => { fail: (err) => {
console.error('静默登录失败', err) console.error('wx.login 调用失败', err)
this.globalData.isLoggedIn = false this.globalData.isLoggedIn = false
this.globalData.loginFailed = true this.globalData.loginFailed = true
if (this.loginReadyCallback) {
this.loginReadyCallback(null)
}
}
})
},
// 通知页面登录失败 loginWithCode(code) {
wx.request({
url: 'https://xcx.yun.588580.xyz/api/wx-mini/login',
method: 'GET',
data: { code },
success: (res) => {
const result = res.data
if (result && result.code === 0 && result.data) {
const openid = result.data.openid
const userInfo = {
openid,
sessionKey: result.data.session_key,
unionid: result.data.unionid || ''
}
this.globalData.userInfo = userInfo
this.globalData.isLoggedIn = true
wx.setStorageSync('userInfo', userInfo)
if (this.loginReadyCallback) {
this.loginReadyCallback(userInfo)
}
} else {
console.error('后端登录失败', result)
this.globalData.isLoggedIn = false
this.globalData.loginFailed = true
if (this.loginReadyCallback) {
this.loginReadyCallback(null)
}
}
},
fail: (err) => {
console.error('请求后端登录接口失败', err)
this.globalData.isLoggedIn = false
this.globalData.loginFailed = true
if (this.loginReadyCallback) { if (this.loginReadyCallback) {
this.loginReadyCallback(null) this.loginReadyCallback(null)
} }
+44 -10
View File
@@ -17,11 +17,17 @@ function getCmd() {
* @returns {object} 格式化后的记录 * @returns {object} 格式化后的记录
*/ */
function formatRecord(record) { function formatRecord(record) {
if (!record) return record
const date = record.createTime const date = record.createTime
let createTimeStr = '' let createTimeStr = ''
if (date) { if (date) {
if (typeof date === 'object' && date.$date) { if (typeof date === 'string') {
// 后端API返回的时间字符串,直接格式化
createTimeStr = new Date(date).toLocaleString('zh-CN')
} else if (typeof date === 'object' && date.$date) {
createTimeStr = new Date(date.$date).toLocaleString('zh-CN') createTimeStr = new Date(date.$date).toLocaleString('zh-CN')
} else if (typeof date === 'number') {
createTimeStr = new Date(date).toLocaleString('zh-CN')
} else { } else {
createTimeStr = new Date(date).toLocaleString('zh-CN') createTimeStr = new Date(date).toLocaleString('zh-CN')
} }
@@ -114,18 +120,46 @@ const appointmentDB = {
}, },
/** /**
* 获取当前用户最新一条预约 * 将后端API返回的预约记录映射为前端模板兼容的字段
* 后端字段: id, visitDate, visitTime, createTime, ...
* 前端字段: _id, date, time, createTime(格式化后), ...
*/
_mapApiRecord(record) {
if (!record) return null
return {
...record,
_id: record.id,
date: record.visitDate,
time: record.visitTime
}
},
/**
* 获取当前用户最新一条预约(通过后端API)
* @param {string} openid * @param {string} openid
* @returns {Promise<object|null>} * @returns {Promise<object|null>}
*/ */
async getLatest(openid) { getLatest(openid) {
const db = getDb() return new Promise((resolve, reject) => {
const res = await db.collection('appointments') wx.request({
.where({ _openid: openid }) url: 'https://xcx.yun.588580.xyz/api/wx-mini/appointment/latest',
.orderBy('createTime', 'desc') method: 'GET',
.limit(1) data: { openid },
.get() success: (res) => {
return res.data.length > 0 ? res.data[0] : null const result = res.data
if (result && result.code === 0) {
resolve(this._mapApiRecord(result.data) || null)
} else {
console.error('获取最新预约失败', result)
reject(new Error(result ? result.message : '请求失败'))
}
},
fail: (err) => {
console.error('请求后端获取最新预约失败', err)
reject(err)
}
})
})
}, },
/** /**