diff --git a/app.js b/app.js index 2b9b6ac..dc71f5f 100644 --- a/app.js +++ b/app.js @@ -9,34 +9,72 @@ App({ }) } - // 自动静默登录:调用云函数获取 openid + + + // 自动静默登录:wx.login 获取 code,再请求后端接口换取 openid this.silentLogin() }, silentLogin() { - wx.cloud.callFunction({ - name: 'login', - data: {}, - success: (res) => { - const openid = res.result.openid - const userInfo = res.result.userInfo || {} - userInfo.openid = openid - - this.globalData.userInfo = userInfo - this.globalData.isLoggedIn = true - wx.setStorageSync('userInfo', userInfo) - - // 通知当前页面刷新 - if (this.loginReadyCallback) { - this.loginReadyCallback(userInfo) + wx.login({ + success: (loginRes) => { + if (loginRes.code) { + this.loginWithCode(loginRes.code) + } else { + console.error('wx.login 失败', loginRes.errMsg) + this.globalData.isLoggedIn = false + this.globalData.loginFailed = true + if (this.loginReadyCallback) { + this.loginReadyCallback(null) + } } }, fail: (err) => { - console.error('静默登录失败', err) + console.error('wx.login 调用失败', err) this.globalData.isLoggedIn = false 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) { this.loginReadyCallback(null) } diff --git a/utils/cloud.js b/utils/cloud.js index c9ca208..1e4c040 100644 --- a/utils/cloud.js +++ b/utils/cloud.js @@ -17,11 +17,17 @@ function getCmd() { * @returns {object} 格式化后的记录 */ function formatRecord(record) { + if (!record) return record const date = record.createTime let createTimeStr = '' 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') + } else if (typeof date === 'number') { + createTimeStr = new Date(date).toLocaleString('zh-CN') } else { 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 * @returns {Promise} */ - async getLatest(openid) { - const db = getDb() - const res = await db.collection('appointments') - .where({ _openid: openid }) - .orderBy('createTime', 'desc') - .limit(1) - .get() - return res.data.length > 0 ? res.data[0] : null + getLatest(openid) { + return new Promise((resolve, reject) => { + wx.request({ + url: 'https://xcx.yun.588580.xyz/api/wx-mini/appointment/latest', + method: 'GET', + data: { openid }, + success: (res) => { + 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) + } + }) + }) }, /**