988 lines
23 KiB
JavaScript
988 lines
23 KiB
JavaScript
|
|
const corpRole = require("../corp-role");
|
||
|
|
const common = require("../../common");
|
||
|
|
const utils = require("../utils");
|
||
|
|
const dayjs = require("dayjs");
|
||
|
|
const api = require("../../api");
|
||
|
|
const { ObjectId } = require("mongodb");
|
||
|
|
let db = null;
|
||
|
|
exports.main = async (event, mongodb) => {
|
||
|
|
db = mongodb;
|
||
|
|
switch (event.type) {
|
||
|
|
case "getCorpMember":
|
||
|
|
return await exports.getCorpMember(event);
|
||
|
|
case "getCorpMemberByUserId":
|
||
|
|
return await exports.getCorpMemberByUserId(event);
|
||
|
|
case "getCorpMemberData":
|
||
|
|
return await exports.getCorpMemberData(event);
|
||
|
|
case "getCorpMemberJob":
|
||
|
|
return await exports.getCorpMemberJob(event);
|
||
|
|
case "getCorpMemberAndCustomorCount":
|
||
|
|
return await exports.getCorpMemberAndCustomorCount(event);
|
||
|
|
case "getCorpMainMember":
|
||
|
|
return await exports.getCorpMainMember(event);
|
||
|
|
case "getRolesMemberList":
|
||
|
|
return await exports.getRolesMemberList(event);
|
||
|
|
case "getSuperAdmin":
|
||
|
|
return await exports.getSuperAdmin(event);
|
||
|
|
case "addCorpMember":
|
||
|
|
return await exports.addCorpMember(event);
|
||
|
|
case "updateCorpMember":
|
||
|
|
return await exports.updateCorpMember(event);
|
||
|
|
case "removeCorpMember":
|
||
|
|
return await exports.removeCorpMember(event);
|
||
|
|
case "updateCorpMemberByUserId":
|
||
|
|
return await exports.updateCorpMemberByUserId(event);
|
||
|
|
case "removeAccount":
|
||
|
|
return await exports.removeAccount(event);
|
||
|
|
case "getNotOpenedAccount":
|
||
|
|
return await exports.getNotOpenedAccount(event);
|
||
|
|
case "getOpenedAccount":
|
||
|
|
return await exports.getOpenedAccount(event);
|
||
|
|
case "getCustomMemberInfo":
|
||
|
|
return await exports.getCustomMemberInfo(event);
|
||
|
|
case "getCorpUsers":
|
||
|
|
return await exports.getCorpUsers(event);
|
||
|
|
case "getCorpMemberByTeamsAndJobs":
|
||
|
|
return await exports.getCorpMemberByTeamsAndJobs(event);
|
||
|
|
case "corpMemberExist":
|
||
|
|
return await exports.corpMemberExist(event);
|
||
|
|
case "checkAdminRole":
|
||
|
|
return await exports.checkAdminRole(event);
|
||
|
|
case "getCorpMemberHomepageInfo":
|
||
|
|
return await exports.getCorpMemberHomepageInfo(event);
|
||
|
|
case "addWechatFriend":
|
||
|
|
return await exports.addWechatFriend(event);
|
||
|
|
case "removeWechatFriend":
|
||
|
|
return await exports.removeWechatFriend(event);
|
||
|
|
case "getWechatFriends":
|
||
|
|
return await getWechatFriends(event);
|
||
|
|
case "batchUpdateWechatFriends":
|
||
|
|
return await exports.batchUpdateWechatFriends(event);
|
||
|
|
case "getExternalUserIdByUserId":
|
||
|
|
return await getExternalUserIdByUserId(event);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
exports.getCorpUsers = async (context) => {
|
||
|
|
const { corpId, userIds } = context;
|
||
|
|
if (!corpId || !Array.isArray(userIds))
|
||
|
|
return { success: false, message: "查询失败" };
|
||
|
|
if (userIds.length === 0)
|
||
|
|
return { success: true, message: "查询成功", list: [] };
|
||
|
|
try {
|
||
|
|
const data = await db
|
||
|
|
.collection("corp-member")
|
||
|
|
.find(
|
||
|
|
{ corpId, userid: { $in: userIds } },
|
||
|
|
{
|
||
|
|
projection: {
|
||
|
|
anotherName: 1,
|
||
|
|
avatar: 1,
|
||
|
|
job: 1,
|
||
|
|
userid: 1,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
)
|
||
|
|
.toArray();
|
||
|
|
return { success: true, message: "查询成功", list: data };
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: error.message,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 根据获取机构成员
|
||
|
|
exports.getCorpMember = async (context) => {
|
||
|
|
try {
|
||
|
|
const { page, pageSize, params } = context;
|
||
|
|
let { memberList, ...rest } = params;
|
||
|
|
if (memberList) {
|
||
|
|
rest["userid"] = { $in: memberList };
|
||
|
|
}
|
||
|
|
const total = await db.collection("corp-member").countDocuments(rest);
|
||
|
|
const pages = Math.ceil(total / pageSize);
|
||
|
|
const data = await db
|
||
|
|
.collection("corp-member")
|
||
|
|
.find(rest, {
|
||
|
|
projection: { auth_corp_info: 0 },
|
||
|
|
})
|
||
|
|
.skip((page - 1) * pageSize)
|
||
|
|
.limit(pageSize)
|
||
|
|
.toArray();
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功",
|
||
|
|
data: data,
|
||
|
|
total: total,
|
||
|
|
pages: pages,
|
||
|
|
size: pageSize,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: error.message,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// superAdminName
|
||
|
|
exports.getSuperAdmin = async (context) => {
|
||
|
|
const { corpId, env } = context;
|
||
|
|
try {
|
||
|
|
const data = await db
|
||
|
|
.collection("corp-member")
|
||
|
|
.find({
|
||
|
|
corpId,
|
||
|
|
roleType: "superAdmin",
|
||
|
|
})
|
||
|
|
.toArray();
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
data,
|
||
|
|
message: "获取成功",
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "获取失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 更新机构成员
|
||
|
|
exports.updateCorpMember = async (context) => {
|
||
|
|
let { id, params, corpId } = context;
|
||
|
|
try {
|
||
|
|
params["updateTime"] = new Date().getTime();
|
||
|
|
const res = await db
|
||
|
|
.collection("corp-member")
|
||
|
|
.updateOne(
|
||
|
|
{
|
||
|
|
corpId,
|
||
|
|
$or: [
|
||
|
|
{ _id: id },
|
||
|
|
{ _id: new ObjectId(id) }
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{ $set: params }
|
||
|
|
);
|
||
|
|
if (params.job) {
|
||
|
|
await api.getMemberApi({ corpId, type: "updateSopTaskExecuteUser" });
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "更新成功",
|
||
|
|
data: res,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: error,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 删除机构成员
|
||
|
|
exports.removeCorpMember = async (context) => {
|
||
|
|
let { id, corpId, env } = context;
|
||
|
|
try {
|
||
|
|
const res = await db.collection("corp-member").deleteOne({
|
||
|
|
_id: id,
|
||
|
|
corpId,
|
||
|
|
});
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "删除成功",
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: error,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 新增机构成员
|
||
|
|
exports.addCorpMember = async (context) => {
|
||
|
|
let { params, env } = context;
|
||
|
|
const { corpId, open_userid } = params;
|
||
|
|
try {
|
||
|
|
if (open_userid) {
|
||
|
|
const flag = await corpLimitation(corpId, env);
|
||
|
|
if (!flag) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "机构成员数已达上限!",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
params["createTime"] = new Date().getTime();
|
||
|
|
params._id = common.generateRandomString(24);
|
||
|
|
const res = await db.collection("corp-member").insertOne(params);
|
||
|
|
if (params.job) {
|
||
|
|
await api.getMemberApi({ corpId, type: "updateSopTaskExecuteUser" });
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "新增成功",
|
||
|
|
data: res,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: error,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
async function corpLimitation(corpId, env) {
|
||
|
|
const corpMemberCount = await db.collection("corp-member").countDocuments({
|
||
|
|
corpId,
|
||
|
|
open_userid: { $exists: true },
|
||
|
|
});
|
||
|
|
const corp = await db.collection("corp").findOne({ corpId });
|
||
|
|
const { package } = corp;
|
||
|
|
if (!package) return true;
|
||
|
|
const { accountCount, giveAccountCount } = package;
|
||
|
|
const allAccountCount = accountCount + giveAccountCount;
|
||
|
|
return corpMemberCount <= allAccountCount;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 更新机构成员通过OpenUserID
|
||
|
|
exports.updateCorpMemberByUserId = async (context) => {
|
||
|
|
let { userid, params, env, corpId } = context;
|
||
|
|
try {
|
||
|
|
params["updateTime"] = new Date().getTime();
|
||
|
|
const res = await db.collection("corp-member").updateOne(
|
||
|
|
{
|
||
|
|
userid,
|
||
|
|
corpId,
|
||
|
|
},
|
||
|
|
{ $set: params }
|
||
|
|
);
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "更新成功",
|
||
|
|
data: res,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: error,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
exports.getCorpMainMember = async (context) => {
|
||
|
|
let { corpId, env } = context;
|
||
|
|
try {
|
||
|
|
const mainRole = await db.collection("sys-role").findOne({
|
||
|
|
corpId,
|
||
|
|
roleId: "main",
|
||
|
|
});
|
||
|
|
const member = await db.collection("corp-member").findOne({
|
||
|
|
corpId,
|
||
|
|
roleIds: mainRole._id,
|
||
|
|
});
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功",
|
||
|
|
data: member,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: error,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
exports.getRolesMemberList = async (context) => {
|
||
|
|
let { corpId, env } = context;
|
||
|
|
try {
|
||
|
|
const res = await db
|
||
|
|
.collection("sys-role")
|
||
|
|
.aggregate([
|
||
|
|
{
|
||
|
|
$lookup: {
|
||
|
|
from: "corp-member",
|
||
|
|
localField: "_id",
|
||
|
|
foreignField: "roleIds",
|
||
|
|
as: "roleEmployees",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
$match: {
|
||
|
|
corpId: corpId,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
])
|
||
|
|
.toArray();
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功",
|
||
|
|
data: res,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "获取失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
exports.getCorpMemberByUserId = async (context) => {
|
||
|
|
let { corpId, userId } = context;
|
||
|
|
console.log("获取用户信息", context);
|
||
|
|
try {
|
||
|
|
const data = await db
|
||
|
|
.collection("corp-member")
|
||
|
|
.find({
|
||
|
|
corpId,
|
||
|
|
userid: userId,
|
||
|
|
})
|
||
|
|
.toArray();
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功",
|
||
|
|
data,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "获取失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
exports.getCorpMemberAndCustomorCount = async (item) => {
|
||
|
|
const { corpId, env } = item;
|
||
|
|
try {
|
||
|
|
const customtorCount = await db.collection("member").countDocuments({
|
||
|
|
corpId,
|
||
|
|
});
|
||
|
|
const corpMemberCount = await db.collection("corp-member").countDocuments({
|
||
|
|
corpId,
|
||
|
|
open_userid: { $exists: true },
|
||
|
|
});
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功",
|
||
|
|
customtorCount,
|
||
|
|
corpMemberCount,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "获取失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 删除账号
|
||
|
|
exports.removeAccount = async (item) => {
|
||
|
|
const { corpId, userId, env } = item;
|
||
|
|
try {
|
||
|
|
if (!userId)
|
||
|
|
return {
|
||
|
|
message: "账号不能为空",
|
||
|
|
success: false,
|
||
|
|
};
|
||
|
|
if (!corpId)
|
||
|
|
return {
|
||
|
|
message: "机构ID不能为空",
|
||
|
|
success: false,
|
||
|
|
};
|
||
|
|
// 删除账号
|
||
|
|
await db.collection("corp-member").deleteMany({ corpId, userid: userId });
|
||
|
|
// 删除团队
|
||
|
|
await db.collection("team").updateMany(
|
||
|
|
{ corpId, memberList: userId },
|
||
|
|
{
|
||
|
|
$pull: {
|
||
|
|
memberLeaderList: userId,
|
||
|
|
friendlyMembers: userId,
|
||
|
|
memberList: userId,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
);
|
||
|
|
return {
|
||
|
|
message: "删除成功",
|
||
|
|
success: true,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
message: "删除失败",
|
||
|
|
success: false,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 获取未开通的账户
|
||
|
|
exports.getNotOpenedAccount = async (item) => {
|
||
|
|
const { corpId } = item;
|
||
|
|
try {
|
||
|
|
const fetchData = async (page, pageSize, db) => {
|
||
|
|
const data = await db
|
||
|
|
.collection("corp-member")
|
||
|
|
.find({
|
||
|
|
corpId,
|
||
|
|
open_userid: { $exists: false },
|
||
|
|
})
|
||
|
|
.skip((page - 1) * pageSize)
|
||
|
|
.limit(pageSize)
|
||
|
|
.toArray();
|
||
|
|
return data;
|
||
|
|
};
|
||
|
|
const list = await utils.getAllData(fetchData, db);
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功!",
|
||
|
|
data: list,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "获取失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
// 获取已开通的账户
|
||
|
|
exports.getOpenedAccount = async (item) => {
|
||
|
|
const { corpId } = item;
|
||
|
|
try {
|
||
|
|
const fetchData = async (page, pageSize, db) => {
|
||
|
|
const data = await db
|
||
|
|
.collection("corp-member")
|
||
|
|
.find({ corpId, open_userid: { $exists: true } })
|
||
|
|
.skip((page - 1) * pageSize)
|
||
|
|
.limit(pageSize)
|
||
|
|
.toArray();
|
||
|
|
return data;
|
||
|
|
};
|
||
|
|
const list = await utils.getAllData(fetchData, db);
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功!",
|
||
|
|
data: list,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "获取失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 获取机构成员的岗位信息
|
||
|
|
exports.getCorpMemberJob = async (item) => {
|
||
|
|
const { env } = item;
|
||
|
|
try {
|
||
|
|
const data = await db.collection("sys-corp-member-job").find().toArray();
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功!",
|
||
|
|
data,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "获取失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 获取机构成员数据
|
||
|
|
exports.getCorpMemberData = async (event) => {
|
||
|
|
const { userId, corpId } = event;
|
||
|
|
console.log("获取用户信息", event);
|
||
|
|
try {
|
||
|
|
const member = await db
|
||
|
|
.collection("corp-member")
|
||
|
|
.findOne({ userid: userId, corpId }, { projection: { password: 0 } });
|
||
|
|
if (!member) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "用户不存在",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
// 获取角色信息
|
||
|
|
member["teamId"] = await getTeamListByuserId(event);
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功",
|
||
|
|
data: member,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: error,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 获取用户所在的团队列表
|
||
|
|
async function getTeamListByuserId(event) {
|
||
|
|
const { userId, corpId, env } = event;
|
||
|
|
const data = await db
|
||
|
|
.collection("team")
|
||
|
|
.find({ corpId, memberList: userId })
|
||
|
|
.toArray();
|
||
|
|
return data.map((item) => item.teamId);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取自定义成员信息
|
||
|
|
exports.getCustomMemberInfo = async (context) => {
|
||
|
|
const { id: _id, userid, corpId, fields } = context;
|
||
|
|
if (
|
||
|
|
!_id ||
|
||
|
|
!userid ||
|
||
|
|
!corpId ||
|
||
|
|
!Array.isArray(fields) ||
|
||
|
|
fields.length === 0
|
||
|
|
) {
|
||
|
|
return { success: false, message: "参数错误" };
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
const project = fields.reduce((acc, cur) => ({ ...acc, [cur]: 1 }), {});
|
||
|
|
const query = db
|
||
|
|
.collection("corp-member")
|
||
|
|
.aggregate()
|
||
|
|
.match({ _id, userid, corpId });
|
||
|
|
if (fields.includes("job")) {
|
||
|
|
query.lookup({
|
||
|
|
from: "sys-corp-member-job",
|
||
|
|
localField: "job",
|
||
|
|
foreignField: "value",
|
||
|
|
as: "memberJob",
|
||
|
|
});
|
||
|
|
project.memberJob = 1;
|
||
|
|
}
|
||
|
|
const member = await query.project(project).toArray();
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功!",
|
||
|
|
data: member[0],
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: error.message || "获取失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 根据团队和岗位获取到成员
|
||
|
|
exports.getCorpMemberByTeamsAndJobs = async (event) => {
|
||
|
|
const { teamIds, corpId, jobs } = event;
|
||
|
|
if (!Array.isArray(teamIds) || !corpId || !Array.isArray(jobs)) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "参数错误",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
const teams = await db
|
||
|
|
.collection("team")
|
||
|
|
.find({ teamId: { $in: teamIds }, corpId })
|
||
|
|
.toArray();
|
||
|
|
let userIds = teams.reduce((acc, cur) => acc.concat(cur.memberList), []);
|
||
|
|
if (!jobs.includes("all")) {
|
||
|
|
const res = await db
|
||
|
|
.collection("corp-member")
|
||
|
|
.find({
|
||
|
|
corpId,
|
||
|
|
job: { $in: jobs },
|
||
|
|
accountState: { $ne: "disabled" },
|
||
|
|
open_userid: { $exists: true },
|
||
|
|
userid: { $in: userIds },
|
||
|
|
})
|
||
|
|
.project({ userid: 1, name: 1, job: 1, teamId: 1 })
|
||
|
|
.toArray();
|
||
|
|
userIds = res.map((item) => item.userid);
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功",
|
||
|
|
data: userIds,
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
// 检查机构成员是否存在
|
||
|
|
exports.corpMemberExist = async (event) => {
|
||
|
|
const { userid, corpId } = event;
|
||
|
|
if (!userid || !corpId) {
|
||
|
|
return { success: false, message: "参数错误" };
|
||
|
|
}
|
||
|
|
const total = await db
|
||
|
|
.collection("corp-member")
|
||
|
|
.countDocuments({ userid, corpId });
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
exist: total > 0,
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
// 检查用户是否有管理员角色
|
||
|
|
exports.checkAdminRole = async (ctx) => {
|
||
|
|
const { userid, corpId } = ctx;
|
||
|
|
if (!userid || !corpId) {
|
||
|
|
return { success: false, message: "参数错误" };
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
const user = await db
|
||
|
|
.collection("corp-member")
|
||
|
|
.findOne({ userid, corpId }, { projection: { roleIds: 1 } });
|
||
|
|
if (!user) {
|
||
|
|
return { success: false, message: "用户不存在" };
|
||
|
|
}
|
||
|
|
const ids = Array.isArray(user.roleIds) ? user.roleIds : [];
|
||
|
|
if (ids.length === 0) {
|
||
|
|
return { success: false, message: "用户没有角色" };
|
||
|
|
}
|
||
|
|
const res = await corpRole.main(
|
||
|
|
{
|
||
|
|
type: "checkRoleExistByIds",
|
||
|
|
corpId,
|
||
|
|
ids,
|
||
|
|
roleId: "admin",
|
||
|
|
},
|
||
|
|
db
|
||
|
|
);
|
||
|
|
const { success, message, exist = false } = res;
|
||
|
|
return { success, message, exist };
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: error.message || "获取失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 获取机构成员主页信息
|
||
|
|
exports.getCorpMemberHomepageInfo = async (ctx) => {
|
||
|
|
const { userid, corpId } = ctx;
|
||
|
|
if (!userid || !corpId) {
|
||
|
|
return { success: false, message: "参数错误" };
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
// const user = await db
|
||
|
|
// .collection("corp-member")
|
||
|
|
// .aggregate()
|
||
|
|
// .match({ userid, corpId })
|
||
|
|
// .lookup({
|
||
|
|
// from: "corp",
|
||
|
|
// localField: "corpId",
|
||
|
|
// foreignField: "corpId",
|
||
|
|
// as: "corps",
|
||
|
|
// })
|
||
|
|
// .addFields({
|
||
|
|
// corpNames: {
|
||
|
|
// $map: { input: "$corps", as: "corp", in: "$$corp.corp_name" },
|
||
|
|
// },
|
||
|
|
// })
|
||
|
|
// .lookup({
|
||
|
|
// from: "dept-list",
|
||
|
|
// localField: "deptIds",
|
||
|
|
// foreignField: "_id",
|
||
|
|
// as: "depts",
|
||
|
|
// })
|
||
|
|
// .addFields({
|
||
|
|
// deptNames: {
|
||
|
|
// $map: { input: "$depts", as: "dept", in: "$$dept.deptName" },
|
||
|
|
// },
|
||
|
|
// })
|
||
|
|
// .project({
|
||
|
|
// anotherName: 1,
|
||
|
|
// avatar: 1,
|
||
|
|
// callNumber: 1,
|
||
|
|
// convenienceService: 1,
|
||
|
|
// outpatientTime: 1,
|
||
|
|
// corpNames: 1,
|
||
|
|
// deptNames: 1,
|
||
|
|
// memberTroduce: 1,
|
||
|
|
// job: 1,
|
||
|
|
// })
|
||
|
|
// .limit(1)
|
||
|
|
// .toArray();
|
||
|
|
const user = await db
|
||
|
|
.collection("corp-member")
|
||
|
|
.aggregate([
|
||
|
|
{ $match: { userid, corpId } },
|
||
|
|
{
|
||
|
|
$lookup: {
|
||
|
|
// 第二阶段:与 'corp' 集合进行关联
|
||
|
|
from: "corp",
|
||
|
|
localField: "corpId",
|
||
|
|
foreignField: "corpId",
|
||
|
|
as: "corps",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
$addFields: {
|
||
|
|
// 第三阶段:添加字段,提取 corp_names
|
||
|
|
corpNames: {
|
||
|
|
$map: {
|
||
|
|
input: "$corps", // 遍历 'corps' 数组
|
||
|
|
as: "corp", // 每个元素的别名
|
||
|
|
in: "$$corp.corp_name", // 提取 'corp_name' 字段
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
$lookup: {
|
||
|
|
// 第四阶段:与 'dept-list' 集合进行关联
|
||
|
|
from: "dept-list",
|
||
|
|
localField: "deptIds",
|
||
|
|
foreignField: "_id",
|
||
|
|
as: "depts",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
$addFields: {
|
||
|
|
// 第五阶段:添加字段,提取 dept_names
|
||
|
|
deptNames: {
|
||
|
|
$map: {
|
||
|
|
input: "$depts", // 遍历 'depts' 数组
|
||
|
|
as: "dept", // 每个元素的别名
|
||
|
|
in: "$$dept.deptName", // 提取 'deptName' 字段
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
$project: {
|
||
|
|
// 第六阶段:选择返回的字段
|
||
|
|
anotherName: 1,
|
||
|
|
avatar: 1,
|
||
|
|
callNumber: 1,
|
||
|
|
convenienceService: 1,
|
||
|
|
outpatientTime: 1,
|
||
|
|
corpNames: 1, // 包含 'corpNames' 字段
|
||
|
|
deptNames: 1, // 包含 'deptNames' 字段
|
||
|
|
memberTroduce: 1,
|
||
|
|
job: 1,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
{ $limit: 1 },
|
||
|
|
])
|
||
|
|
.toArray();
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功",
|
||
|
|
data: user[0],
|
||
|
|
};
|
||
|
|
} catch (e) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: e.message || "获取失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
/**
|
||
|
|
*
|
||
|
|
* @param {*} param0
|
||
|
|
*/
|
||
|
|
async function getExternalUserIdByUserId({
|
||
|
|
corpId,
|
||
|
|
userId,
|
||
|
|
externalUserIds,
|
||
|
|
page = 1,
|
||
|
|
pageSize = 500,
|
||
|
|
}) {
|
||
|
|
if (!corpId || !userId || !Array.isArray(externalUserIds)) {
|
||
|
|
return { success: false, message: "参数错误" };
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
const data = await db
|
||
|
|
.collection("wechat-friends")
|
||
|
|
.find({
|
||
|
|
corpId,
|
||
|
|
userid: userId,
|
||
|
|
external_userid: { $in: externalUserIds },
|
||
|
|
})
|
||
|
|
.skip((page - 1) * pageSize)
|
||
|
|
.limit(pageSize)
|
||
|
|
.toArray();
|
||
|
|
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功",
|
||
|
|
data,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: error.message || "获取失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取微信好友
|
||
|
|
async function getWechatFriends(item) {
|
||
|
|
const {
|
||
|
|
corpId,
|
||
|
|
userId,
|
||
|
|
externalUserId,
|
||
|
|
dates,
|
||
|
|
page = 1,
|
||
|
|
pageSize = 10,
|
||
|
|
haveCustomer = false,
|
||
|
|
} = item;
|
||
|
|
let query = { corpId };
|
||
|
|
if (userId) query.userid = userId;
|
||
|
|
if (externalUserId) query.external_userid = externalUserId;
|
||
|
|
if (Array.isArray(dates) && dates.length > 0) {
|
||
|
|
query.createtime = {
|
||
|
|
$gte: dayjs(dates[0]).startOf("day").unix(),
|
||
|
|
$lte: dayjs(dates[1]).endOf("day").unix(),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
const total = await db.collection("wechat-friends").countDocuments(query);
|
||
|
|
const pages = Math.ceil(total / pageSize);
|
||
|
|
// 排序
|
||
|
|
const wechatdb = db
|
||
|
|
.collection("wechat-friends")
|
||
|
|
.aggregate()
|
||
|
|
.match(query)
|
||
|
|
.sort({ createtime: -1 }) // 根据createtime降序排序
|
||
|
|
.skip((page - 1) * pageSize)
|
||
|
|
.limit(pageSize);
|
||
|
|
if (haveCustomer) {
|
||
|
|
wechatdb
|
||
|
|
.lookup({
|
||
|
|
from: "member",
|
||
|
|
localField: "external_userid",
|
||
|
|
foreignField: "externalUserId",
|
||
|
|
as: "customer",
|
||
|
|
})
|
||
|
|
.project({
|
||
|
|
external_userid: 1,
|
||
|
|
addWay: 1,
|
||
|
|
corpId: 1,
|
||
|
|
createtime: 1,
|
||
|
|
gender: 1,
|
||
|
|
name: 1,
|
||
|
|
type: 1,
|
||
|
|
userid: 1,
|
||
|
|
customer: { name: 1, customerStage: 1, _id: 1 },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
const data = await wechatdb.toArray();
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "获取成功",
|
||
|
|
list: data,
|
||
|
|
total,
|
||
|
|
pages,
|
||
|
|
};
|
||
|
|
} catch {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "获取失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 添加机构成员微信好友
|
||
|
|
exports.addWechatFriend = async (item) => {
|
||
|
|
try {
|
||
|
|
if (!item.corpId) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "机构ID不存在",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
const {
|
||
|
|
corpId,
|
||
|
|
external_userid,
|
||
|
|
friendType,
|
||
|
|
gender,
|
||
|
|
name,
|
||
|
|
corp_name,
|
||
|
|
createtime,
|
||
|
|
addWay,
|
||
|
|
userid,
|
||
|
|
} = item;
|
||
|
|
|
||
|
|
const res = await db.collection("wechat-friends").insertOne({
|
||
|
|
corpId,
|
||
|
|
external_userid,
|
||
|
|
friendType,
|
||
|
|
gender,
|
||
|
|
name,
|
||
|
|
corp_name,
|
||
|
|
createtime,
|
||
|
|
addWay,
|
||
|
|
userid,
|
||
|
|
_id: common.generateRandomString(24),
|
||
|
|
});
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "新增成功",
|
||
|
|
data: res,
|
||
|
|
};
|
||
|
|
} catch {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "新增失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 批量更新 wechat-friends 表数据 使用批量插入
|
||
|
|
exports.batchUpdateWechatFriends = async (item) => {
|
||
|
|
const { corpId, data } = item;
|
||
|
|
try {
|
||
|
|
const bulkOps = data.map((item) => ({
|
||
|
|
updateOne: {
|
||
|
|
filter: {
|
||
|
|
corpId,
|
||
|
|
external_userid: item.external_userid,
|
||
|
|
userid: item.userid,
|
||
|
|
},
|
||
|
|
update: { $set: { ...item, _id: common.generateRandomString(24) } },
|
||
|
|
upsert: true,
|
||
|
|
},
|
||
|
|
}));
|
||
|
|
await db.collection("wechat-friends").bulkWrite(bulkOps);
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "更新成功",
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "更新失败",
|
||
|
|
error: error.message,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 删除机构成员微信好友
|
||
|
|
exports.removeWechatFriend = async (item) => {
|
||
|
|
const { userid, external_userid, corpId } = item;
|
||
|
|
try {
|
||
|
|
const res = await db.collection("wechat-friends").deleteMany({
|
||
|
|
userid,
|
||
|
|
external_userid,
|
||
|
|
corpId,
|
||
|
|
});
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
message: "删除成功",
|
||
|
|
};
|
||
|
|
} catch {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
message: "删除失败",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
|