const cloudbase = require("@cloudbase/node-sdk"); async function checkAuth() { // 获取用户信息 // 在云函数中获取用户身份信息 const app = cloudbase.init(); const { TCB_UUID } = cloudbase.getCloudbaseContext(); let { userInfo } = await app.auth().getEndUserInfo(TCB_UUID); // 未登录用户 if (!userInfo.username && !userInfo.openId) { return { msg: "未登录用户!", result: false } } // 校验用户是否为 CMS 的用户 // 腾讯云 CMS 用户数据集合为 tcb-ext-cms-users // 微信 CMS 用户数据集合为 wx-ext-cms-users const { data: [userRecord] } = await app .database() .collection("tcb-ext-cms-users") .where({ username: userInfo.username }) .get(); // 用户信息不存在 if (!userRecord) { return { msg: "用户不存在,请确认登录信息!", result: false } } // 校验通过 return { msg: "", result: true } } exports.config = { checkAuth }