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 变量控制)
这个提交包含在:
@@ -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: '未知操作' }
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户