From 68f2e322a44a87a6461b772ece0e6640366db121 Mon Sep 17 00:00:00 2001 From: zhaoyingbo Date: Fri, 17 Jan 2025 12:51:25 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=89=A9=E5=B1=95=E6=8E=A5=E6=94=B6?= =?UTF-8?q?=E7=BB=84=E6=A8=A1=E5=9E=8B=EF=BC=8C=E6=94=AF=E6=8C=81=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E4=BF=A1=E6=81=AF=EF=BC=9B=E4=BC=98=E5=8C=96=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E8=AF=B7=E6=B1=82=E9=AA=8C=E8=AF=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/receiveGroup/index.ts | 16 +++++++++---- routes/message/index.ts | 49 +++++++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/db/receiveGroup/index.ts b/db/receiveGroup/index.ts index 7b0e638..0d03a03 100644 --- a/db/receiveGroup/index.ts +++ b/db/receiveGroup/index.ts @@ -1,5 +1,6 @@ import { RecordModel } from "pocketbase" +import { AppInfoModel } from "../../constant/config" import { managePbError } from "../../utils/pbTools" import pbClient from "../pbClient" @@ -7,23 +8,28 @@ const DB_NAME = "message_group" export interface ReceiveGroup { name: string - email?: string[] chatId?: string[] - openId?: string[] - unionId?: string[] userId?: string[] } export type ReceiveGroupModel = ReceiveGroup & RecordModel +export interface ReceiveGroupModelWithApp extends ReceiveGroupModel { + expand: { + app: AppInfoModel + } +} + /** * 根据ID获取指定消息组 * @param id * @returns */ const getOne = (id: string) => - managePbError(() => - pbClient.collection(DB_NAME).getOne(id) + managePbError(() => + pbClient.collection(DB_NAME).getOne(id, { + expand: "app", + }) ) const receiveGroup = { diff --git a/routes/message/index.ts b/routes/message/index.ts index 7c94b50..6c73fea 100644 --- a/routes/message/index.ts +++ b/routes/message/index.ts @@ -1,4 +1,5 @@ import { stringifyJson } from "@egg/hooks" +import { LarkService } from "@egg/net-tool" import db from "../../db" import { Log } from "../../db/log" @@ -19,8 +20,8 @@ const ID_TYPE_MAP = { * @returns {false | Response} 如果校验失败,返回响应对象;否则返回 false */ const validateMessageReq = ({ body, genResp }: Context): false | Response => { - if (!body.api_key) { - return genResp.badRequest("api_key is required") + if (!body.api_key && !body.group_id) { + return genResp.badRequest("api_key or group_id is required") } if (!body.group_id && !body.receive_id) { return genResp.badRequest("group_id or receive_id is required") @@ -79,16 +80,20 @@ export const manageMessageReq = async (ctx: Context): Promise => { finalContent, } - // 校验 api_key - const apiKeyInfo = await db.apiKey.getOne(body.api_key) - if (!apiKeyInfo) { - const error = "api key not found" - await db.log.create({ ...baseLog, error }) - return genResp.notFound(error) - } + let larkService: LarkService | null = null - // 生成 Lark 服务 - const larkService = genLarkService(apiKeyInfo.expand.app.name, requestId) + if (body.api_key) { + // 校验 api_key + const apiKeyInfo = await db.apiKey.getOne(body.api_key) + if (!apiKeyInfo) { + const error = "api key not found" + await db.log.create({ ...baseLog, error }) + return genResp.notFound(error) + } + + // 生成 Lark 服务 + larkService = genLarkService(apiKeyInfo.expand.app.name, requestId) + } // 如果有 group_id,则发送给所有 group_id 中的人 if (body.group_id) { @@ -100,13 +105,22 @@ export const manageMessageReq = async (ctx: Context): Promise => { return genResp.notFound(error) } - const { chatId, openId, unionId, userId, email } = group + if (!larkService) { + if (!group.expand?.app?.name) { + const error = "api key not found" + await db.log.create({ ...baseLog, error }) + return genResp.notFound(error) + } + larkService = genLarkService(group.expand.app.name, requestId) + } + + const { chatId, userId } = group // 构造发送消息函数 const makeSendFunc = (receive_id_type: LarkServer.ReceiveIDType) => { return (receive_id: string) => { sendList.push( - larkService.message + larkService!.message .send(receive_id_type, receive_id, body.msg_type, finalContent) .then((res) => { sendResult[ID_TYPE_MAP[receive_id_type]][receive_id] = res @@ -117,10 +131,13 @@ export const manageMessageReq = async (ctx: Context): Promise => { // 创建消息列表 if (chatId) chatId.map(makeSendFunc("chat_id")) - if (openId) openId.map(makeSendFunc("open_id")) - if (unionId) unionId.map(makeSendFunc("union_id")) if (userId) userId.map(makeSendFunc("user_id")) - if (email) email.map(makeSendFunc("email")) + } + + if (!larkService) { + const error = "api key not found" + await db.log.create({ ...baseLog, error }) + return genResp.notFound(error) } // 如果有 receive_id,则发送给所有 receive_id 中的人