提交
提交
This commit is contained in:
@@ -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) {
|
||||||
|
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('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) => {
|
success: (res) => {
|
||||||
const openid = res.result.openid
|
const result = res.data
|
||||||
const userInfo = res.result.userInfo || {}
|
if (result && result.code === 0 && result.data) {
|
||||||
userInfo.openid = openid
|
const openid = result.data.openid
|
||||||
|
const userInfo = {
|
||||||
|
openid,
|
||||||
|
sessionKey: result.data.session_key,
|
||||||
|
unionid: result.data.unionid || ''
|
||||||
|
}
|
||||||
|
|
||||||
this.globalData.userInfo = userInfo
|
this.globalData.userInfo = userInfo
|
||||||
this.globalData.isLoggedIn = true
|
this.globalData.isLoggedIn = true
|
||||||
wx.setStorageSync('userInfo', userInfo)
|
wx.setStorageSync('userInfo', userInfo)
|
||||||
|
|
||||||
// 通知当前页面刷新
|
|
||||||
if (this.loginReadyCallback) {
|
if (this.loginReadyCallback) {
|
||||||
this.loginReadyCallback(userInfo)
|
this.loginReadyCallback(userInfo)
|
||||||
}
|
}
|
||||||
},
|
} else {
|
||||||
fail: (err) => {
|
console.error('后端登录失败', result)
|
||||||
console.error('静默登录失败', err)
|
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.isLoggedIn = false
|
||||||
this.globalData.loginFailed = true
|
this.globalData.loginFailed = true
|
||||||
|
|
||||||
// 通知页面登录失败
|
|
||||||
if (this.loginReadyCallback) {
|
if (this.loginReadyCallback) {
|
||||||
this.loginReadyCallback(null)
|
this.loginReadyCallback(null)
|
||||||
}
|
}
|
||||||
|
|||||||
+44
-10
@@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user