- 新增 cloud-functions/ 目录(6个云函数:auth/device/subscription/treatment/user/admin) - 云函数使用 wx-server-sdk + 云数据库(不再依赖 MySQL) - auth 云函数通过 cloud.getWXContext() 自动获取 openid,无需手动 wx.login - request.js 改为通过 wx.cloud.callFunction() 调用云函数 - 登录流程简化:点击授权 → 直接调 auth 云函数 → 获取用户信息 - 保留 HTTP 模式切换能力(USE_CLOUD 变量控制)
43 行
1.3 KiB
JavaScript
43 行
1.3 KiB
JavaScript
const cloud = require('wx-server-sdk')
|
|
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
|
const db = cloud.database()
|
|
const _ = db.command
|
|
|
|
exports.main = async (event, context) => {
|
|
const wxContext = cloud.getWXContext()
|
|
const openid = wxContext.OPENID
|
|
const { action } = event
|
|
|
|
if (action === 'sync') {
|
|
const d = event.data
|
|
await db.collection('treatment_records').add({
|
|
data: {
|
|
openid,
|
|
session_id: d.session_id || 'S' + Date.now(),
|
|
device_id: d.device_id || '',
|
|
regions: d.regions || 0,
|
|
total_duration_ms: d.total_duration_ms || 0,
|
|
mode: d.mode || 0,
|
|
avg_pd: d.avg_pd || 0,
|
|
created_at: db.serverDate()
|
|
}
|
|
})
|
|
return { code: 0, data: { session_id: d.session_id || 'S' + Date.now() } }
|
|
}
|
|
|
|
if (action === 'history') {
|
|
const { page = 1, page_size = 20 } = event.data || {}
|
|
const totalRes = await db.collection('treatment_records').where({ openid }).count()
|
|
const records = await db.collection('treatment_records')
|
|
.where({ openid })
|
|
.orderBy('created_at', 'desc')
|
|
.skip((page - 1) * page_size)
|
|
.limit(page_size)
|
|
.get()
|
|
|
|
return { code: 0, data: { records: records.data, total: totalRes.total } }
|
|
}
|
|
|
|
return { code: -1, message: '未知操作' }
|
|
}
|