1107
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* 添加操作日志
|
||||
*/
|
||||
|
||||
// 需要添加日志的云函数列表
|
||||
const urlList = [
|
||||
{ url: "admin/system/user/sys/add", title: "用户添加" },
|
||||
{ url: "admin/system/user/sys/update", title: "用户修改" },
|
||||
{ url: "admin/system/user/sys/delete", title: "用户删除" },
|
||||
{ url: "admin/system/user/sys/batchUpdateStatus", title: "批量修改用户冻结状态" }
|
||||
];
|
||||
|
||||
// 生成regExp规则
|
||||
let regExp = urlList.map((item, index) => {
|
||||
return `^${item.url}$`;
|
||||
});
|
||||
|
||||
module.exports = [{
|
||||
id: "addAdminLog",
|
||||
// 需要添加操作日志的云函数列表
|
||||
regExp,
|
||||
description: "添加admin操作日志",
|
||||
index: 999, // 确保最后执行即可
|
||||
mode: "onActionExecuted", // 可选 onActionExecuting onActionExecuted
|
||||
enable: true, // 通过设置enable=false可以关闭该中间件
|
||||
main: async function(event, serviceRes) {
|
||||
let { url, data = {}, uniIdToken, util, filterResponse, originalParam } = event;
|
||||
let { uniID, config, vk, db, _ } = util;
|
||||
let { userInfo = {} } = filterResponse;
|
||||
let res = serviceRes;
|
||||
try {
|
||||
// 此处加 try catch 保证即使添加日志环节出错,不影响业务运行。
|
||||
if (res.code === 0) {
|
||||
// 只有请求成功的请求才添加日志
|
||||
let item = vk.pubfn.getListItem(urlList, "url", url);
|
||||
let title = item && item.title ? item.title : "未分类";
|
||||
delete data.uid;
|
||||
// 日志写入数据库
|
||||
// 获取请求id(只有云端云函数才有请求id,本地运行的云函数无请求id)
|
||||
let request_id;
|
||||
if (typeof vk.pubfn.getUniCloudRequestId === "function") {
|
||||
request_id = vk.pubfn.getUniCloudRequestId();
|
||||
}
|
||||
await vk.baseDao.add({
|
||||
dbName: "opendb-admin-log",
|
||||
dataJson: {
|
||||
user_id: userInfo._id,
|
||||
user_name: userInfo.nickname,
|
||||
title,
|
||||
ip: originalParam.context.CLIENTIP,
|
||||
url: url,
|
||||
request_param: data,
|
||||
response: serviceRes,
|
||||
request_id
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("添加日志异常", err);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}]
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* 自定义过滤器 - 前置
|
||||
*/
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
id: "xxxx1",
|
||||
regExp: "^xxx1/kh", // 正则匹配规则,这个是以^xxx1/kh/开头的云函数会被拦截
|
||||
description: "这里是你过滤器1号的描述",
|
||||
index: 310,// 此处必须>300 因为检测用户是否登录的过滤器的index是200(sys是300,因此为了能通用,建议填大于300的值)(越小越先执行)
|
||||
mode:"onActionExecuting", // 可选 onActionExecuting onActionExecuted
|
||||
main: async function(event) {
|
||||
// 这里是拦截规则,可以查数据库,最终code:0 代表通过,其他均为未通过,msg是被拦截的原因
|
||||
let { data = {}, util } = event;
|
||||
let { vk } = util;
|
||||
return {
|
||||
code: -1,
|
||||
msg: "被自定义过滤器1号拦截了"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* 自定义过滤器 - 后置
|
||||
*/
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
id: "xxxx2",
|
||||
regExp: "^xxx2/kh", // 正则匹配规则,这个是以^xxx2/kh/开头的云函数会被拦截
|
||||
description: "这里是你过滤器2号的描述",
|
||||
index: 310,// 此处必须>300 因为检测用户是否登录的过滤器的index是200(sys是300,因此为了能通用,建议填大于300的值)(越小越先执行)
|
||||
mode:"onActionExecuted", // 可选 onActionExecuting onActionExecuted
|
||||
main: async function(event, serviceRes) {
|
||||
let { data = {}, util } = event;
|
||||
let { vk } = util;
|
||||
serviceRes.msg = "被过滤器修改后的值";
|
||||
return serviceRes;
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 注册成功后,统一给用户初始化字段值
|
||||
*/
|
||||
|
||||
module.exports = [{
|
||||
id: "registerInit",
|
||||
// 符合下方正则条件的云函数才会进入该中间件
|
||||
regExp: [
|
||||
"^user/pub/register(.*)",
|
||||
"^user/pub/login(.*)",
|
||||
"^admin/system/user/sys/add"
|
||||
],
|
||||
description: "注册成功后,统一给用户初始化字段值",
|
||||
index: 301,
|
||||
mode: "onActionExecuted", // 可选 onActionExecuting onActionExecuted
|
||||
enable: true, // 通过设置enable=false可以关闭该中间件
|
||||
main: async function(event, serviceRes) {
|
||||
let { data = {}, util, filterResponse, originalParam } = event;
|
||||
let { vk, config, db, _ } = util;
|
||||
let res = serviceRes;
|
||||
|
||||
// 初始化用户信息(只在用户注册成功时才执行下面的代码)
|
||||
if (res.code === 0 && res.type === "register" && res.uid) {
|
||||
let uniIdConfig = vk.pubfn.getUniIdConfig(config); // 获取uniID的配置
|
||||
// 你要初始化的字段数据
|
||||
let initData = {
|
||||
// 这里可以加你想要初始化的其他字段,如 status字段初始化为 0
|
||||
status: 0, // 用户状态:0 正常 1 禁用 2 审核中 3 审核拒绝
|
||||
|
||||
};
|
||||
// 如果开启了删除dcloud_appid功能,则初始化数据带上 dcloud_appid: _.remove()
|
||||
if (uniIdConfig.removeDcloudAppid) {
|
||||
// remove功能是删除该字段(代表关闭隔离不同端用户功能),该用户在其他项目中登录时就不会产生新的账号 详见:https://uniapp.dcloud.io/uniCloud/uni-id.html#isolate-user
|
||||
initData["dcloud_appid"] = _.remove();
|
||||
}
|
||||
// 修改用户信息,并返回修改后的用户信息
|
||||
res.userInfo = await vk.daoCenter.userDao.updateAndReturn({
|
||||
whereJson: {
|
||||
_id: res.uid,
|
||||
},
|
||||
dataJson: initData
|
||||
});
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}]
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* 返回用户信息
|
||||
* 由于通常前端需要对用户信息、登录token进行缓存
|
||||
* 故云函数对用户进行修改后,需要返回给前端最新的用户信息,以便前端始终能够缓存最新用户信息
|
||||
*/
|
||||
|
||||
module.exports = [{
|
||||
id: "returnUserInfo",
|
||||
regExp: [
|
||||
"^user/pub/login(.*)",
|
||||
"^user/pub/register(.*)",
|
||||
"^user/kh/bind(.*)",
|
||||
"^user/kh/unbind(.*)",
|
||||
"^user/kh/update(.*)",
|
||||
"^user/kh/set(.*)",
|
||||
"^user/kh/acceptInvite$",
|
||||
"^user/kh/getMyUserInfo$",
|
||||
"^client/user/pub/register(.*)",
|
||||
"^client/user/pub/login(.*)"
|
||||
],
|
||||
description: "用于指定哪些云函数请求后需要通知前端更新userInfo、token、tokenExpired",
|
||||
index: 310,
|
||||
mode: "onActionExecuted", // 可选 onActionExecuting onActionExecuted
|
||||
enable: true, // 通过设置enable=false可以关闭该中间件
|
||||
main: async function(event, serviceRes) {
|
||||
let { data = {}, uniIdToken, util, filterResponse, originalParam } = event;
|
||||
let { uniID, config = {}, vk, db, _ } = util;
|
||||
let { userInfo = {} } = filterResponse;
|
||||
let res = serviceRes;
|
||||
if (res.code === 0) {
|
||||
if (vk.pubfn.isNull(res.userInfo)) {
|
||||
if (vk.pubfn.isNotNull(res.uid)) {
|
||||
res.userInfo = await vk.daoCenter.userDao.findById(res.uid);
|
||||
} else if (vk.pubfn.isNotNull(uniIdToken)) {
|
||||
let tokenRes = await uniID.checkToken(uniIdToken);
|
||||
if (tokenRes.code === 0) {
|
||||
res.userInfo = tokenRes.userInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (vk.pubfn.isNotNull(res.userInfo)) {
|
||||
if (res.type === "login") {
|
||||
// 如果是登录
|
||||
// 判断用户当前 token数量 是否超过了最大限制 若 tokenMaxLimit 设置为0,代表不限制
|
||||
let tokenMaxLimit = config.uni && config.uni.tokenMaxLimit;
|
||||
if (tokenMaxLimit > 0) {
|
||||
let newUserInfo = await vk.baseDao.findById({
|
||||
dbName: "uni-id-users",
|
||||
id: res.userInfo._id,
|
||||
fieldJson: { token: true }
|
||||
});
|
||||
let token = newUserInfo.token || [];
|
||||
if (token.length > tokenMaxLimit) {
|
||||
// 淘汰旧的 token
|
||||
token = token.splice(tokenMaxLimit * -1);
|
||||
await vk.baseDao.updateById({
|
||||
dbName: "uni-id-users",
|
||||
id: res.userInfo._id,
|
||||
dataJson: {
|
||||
token: _.set(token)
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
res.needUpdateUserInfo = true;
|
||||
// 此处可以继续去除你不想返回给前端的字段
|
||||
let deleteKeys = ["token", "password"];
|
||||
res.userInfo = vk.pubfn.deleteObjectKeys(res.userInfo, deleteKeys);
|
||||
}
|
||||
if (vk.pubfn.isNotNullAll(res.token, res.tokenExpired)) {
|
||||
// 当返回vk_uni_token对象后,框架会让token自动被前端缓存,前端不需要手动再保存token至本地缓存了。
|
||||
res.vk_uni_token = {
|
||||
token: res.token,
|
||||
tokenExpired: res.tokenExpired
|
||||
};
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}]
|
||||
Reference in New Issue
Block a user