feat: 扩展接收组模型,支持应用信息;优化消息请求验证逻辑
This commit is contained in:
parent
e36c3c8496
commit
68f2e322a4
@ -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<ReceiveGroupModel>(() =>
|
||||
pbClient.collection(DB_NAME).getOne(id)
|
||||
managePbError<ReceiveGroupModelWithApp>(() =>
|
||||
pbClient.collection(DB_NAME).getOne(id, {
|
||||
expand: "app",
|
||||
})
|
||||
)
|
||||
|
||||
const receiveGroup = {
|
||||
|
@ -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<Response> => {
|
||||
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<Response> => {
|
||||
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<Response> => {
|
||||
|
||||
// 创建消息列表
|
||||
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 中的人
|
||||
|
Loading…
x
Reference in New Issue
Block a user