refactor: migrate to WeChat cloud development

- 新增 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 变量控制)
这个提交包含在:
Guoguo
2026-04-24 22:03:16 +08:00
父节点 5064cf119d
当前提交 36e80d503a
修改 16 个文件,包含 425 行新增26 行删除
@@ -0,0 +1,23 @@
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
exports.main = async (event, context) => {
const { action } = event
if (action === 'dashboard') {
const deviceCount = await db.collection('bindings').where({ status: 'active' }).count()
const userCount = await db.collection('users').count()
const treatmentCount = await db.collection('treatment_records').count()
return {
code: 0,
data: {
device_count: deviceCount.total,
user_count: userCount.total,
treatment_count: treatmentCount.total
}
}
}
return { code: -1, message: '未知操作' }
}
@@ -0,0 +1,9 @@
{
"name": "admin",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"wx-server-sdk": "~2.6.3"
}
}
+51
查看文件
@@ -0,0 +1,51 @@
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
const openid = wxContext.OPENID
if (event.action === 'login') {
let userRecord = await db.collection('users').where({ openid }).get()
if (userRecord.data.length === 0) {
const newUser = {
openid,
nickname: '',
avatar_url: '',
phone: '',
gender: 0,
created_at: db.serverDate()
}
await db.collection('users').add({ data: newUser })
userRecord = await db.collection('users').where({ openid }).get()
}
const user = userRecord.data[0]
return {
code: 0,
data: {
token: openid + '_' + Date.now(),
user_id: user._id,
user_info: {
user_id: user._id,
nickname: user.nickname || '用户' + openid.slice(-4),
avatar_url: user.avatar_url || '',
phone: user.phone || '',
gender: user.gender || 0
}
}
}
}
if (event.action === 'refresh') {
const userRecord = await db.collection('users').where({ openid }).get()
if (userRecord.data.length === 0) {
return { code: 1001, message: '用户不存在' }
}
return { code: 0, data: { token: openid + '_' + Date.now() } }
}
return { code: -1, message: '未知操作' }
}
@@ -0,0 +1,9 @@
{
"name": "auth",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"wx-server-sdk": "~2.6.3"
}
}
@@ -0,0 +1,89 @@
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 === 'bind') {
const { device_id } = event.data
if (!device_id) return { code: -1, message: '缺少设备ID' }
const existBind = await db.collection('bindings').where({ openid, status: 'active' }).count()
if (existBind.total > 0) {
const exist = await db.collection('bindings').where({ openid, status: 'active' }).get()
return { code: 2001, message: '已绑定设备', data: { device_id: exist.data[0].device_id } }
}
await db.collection('bindings').add({
data: {
openid,
device_id,
status: 'active',
bind_time: db.serverDate()
}
})
const subExist = await db.collection('subscriptions').where({ openid, status: 'active' }).count()
if (subExist.total === 0) {
await db.collection('subscriptions').add({
data: {
openid,
plan_type: 'trial',
status: 'active',
start_time: db.serverDate(),
end_time: new Date(Date.now() + 7 * 24 * 3600 * 1000),
remaining_days: 7
}
})
}
return {
code: 0,
data: {
device_id,
bind_token: 'bt_' + Date.now(),
subscription: { plan_type: 'trial', remaining_days: 7 }
}
}
}
if (action === 'unbind') {
await db.collection('bindings').where({ openid, status: 'active' }).update({ data: { status: 'inactive' } })
return { code: 0, data: {} }
}
if (action === 'list') {
const bindings = await db.collection('bindings').where({ openid, status: 'active' }).get()
const devices = bindings.data.map(b => ({
device_id: b.device_id,
name: '我的光面膜',
status: 'online',
bind_time: b.bind_time
}))
return { code: 0, data: { devices, total: devices.length } }
}
if (action === 'detail') {
const { device_id } = event.data
const bindings = await db.collection('bindings').where({ openid, device_id, status: 'active' }).get()
if (bindings.data.length === 0) return { code: -1, message: '设备未绑定' }
const b = bindings.data[0]
return {
code: 0,
data: {
device_id: b.device_id,
name: '我的光面膜',
firmware_version: '1.0.0',
battery_level: 85,
status: 'online',
bind_time: b.bind_time
}
}
}
return { code: -1, message: '未知操作' }
}
@@ -0,0 +1,9 @@
{
"name": "device",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"wx-server-sdk": "~2.6.3"
}
}
@@ -0,0 +1,58 @@
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
const openid = wxContext.OPENID
const { action } = event
if (action === 'status') {
const subs = await db.collection('subscriptions').where({ openid, status: 'active' }).get()
if (subs.data.length === 0) {
return { code: 0, data: { plan_type: 'none', status: 'inactive', remaining_days: 0 } }
}
const sub = subs.data[0]
const now = new Date()
const end = new Date(sub.end_time)
const remain = Math.max(0, Math.ceil((end - now) / (24 * 3600 * 1000)))
return {
code: 0,
data: {
plan_type: sub.plan_type,
status: remain > 0 ? 'active' : 'expired',
remaining_days: remain,
start_time: sub.start_time,
end_time: sub.end_time
}
}
}
if (action === 'purchase') {
const { plan_type } = event.data
const price = plan_type === 'monthly' ? 99 : 899
const days = plan_type === 'monthly' ? 30 : 365
await db.collection('subscriptions').where({ openid, status: 'active' }).update({ data: { status: 'expired' } })
await db.collection('subscriptions').add({
data: {
openid,
plan_type,
status: 'active',
start_time: db.serverDate(),
end_time: new Date(Date.now() + days * 24 * 3600 * 1000),
remaining_days: days,
price
}
})
return { code: 0, data: { order_id: 'ORD' + Date.now(), plan_type, status: 'paid' } }
}
if (action === 'verify') {
return { code: 0, data: { valid: true } }
}
return { code: -1, message: '未知操作' }
}
@@ -0,0 +1,9 @@
{
"name": "subscription",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"wx-server-sdk": "~2.6.3"
}
}
@@ -0,0 +1,42 @@
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: '未知操作' }
}
@@ -0,0 +1,9 @@
{
"name": "treatment",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"wx-server-sdk": "~2.6.3"
}
}
+39
查看文件
@@ -0,0 +1,39 @@
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
const openid = wxContext.OPENID
const { action } = event
if (action === 'profile') {
const users = await db.collection('users').where({ openid }).get()
if (users.data.length === 0) return { code: 1001, message: '用户不存在' }
const user = users.data[0]
const binds = await db.collection('bindings').where({ openid, status: 'active' }).count()
return {
code: 0,
data: {
user_id: user._id,
nickname: user.nickname || '用户' + openid.slice(-4),
avatar_url: user.avatar_url || '',
phone: user.phone || '',
gender: user.gender || 0,
device_count: binds.total,
created_at: user.created_at
}
}
}
if (action === 'update') {
const updates = {}
if (event.data.nickname) updates.nickname = event.data.nickname
if (event.data.avatar_url) updates.avatar_url = event.data.avatar_url
if (event.data.gender !== undefined) updates.gender = event.data.gender
await db.collection('users').where({ openid }).update({ data: updates })
return { code: 0, data: {} }
}
return { code: -1, message: '未知操作' }
}
@@ -0,0 +1,9 @@
{
"name": "user",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"wx-server-sdk": "~2.6.3"
}
}