This commit is contained in:
parent
19cfb2c9c3
commit
566188f405
4
package-lock.json
generated
4
package-lock.json
generated
@ -12,7 +12,7 @@
|
||||
"packages/*"
|
||||
],
|
||||
"dependencies": {
|
||||
"lodash": "4.17.21",
|
||||
"lodash": "^4.17.21",
|
||||
"winston": "3.14.2",
|
||||
"winston-daily-rotate-file": "5.0.0"
|
||||
},
|
||||
@ -21,7 +21,7 @@
|
||||
"@commitlint/config-conventional": "19.2.2",
|
||||
"@eslint/js": "9.9.0",
|
||||
"@lerna/conventional-commits": "6.4.1",
|
||||
"@types/lodash": "4.17.7",
|
||||
"@types/lodash": "^4.17.7",
|
||||
"@types/node": "22.4.0",
|
||||
"eslint": "9.9.0",
|
||||
"eslint-plugin-simple-import-sort": "12.1.1",
|
||||
|
@ -26,7 +26,7 @@
|
||||
"@commitlint/config-conventional": "19.2.2",
|
||||
"@eslint/js": "9.9.0",
|
||||
"@lerna/conventional-commits": "6.4.1",
|
||||
"@types/lodash": "4.17.7",
|
||||
"@types/lodash": "^4.17.7",
|
||||
"@types/node": "22.4.0",
|
||||
"eslint": "9.9.0",
|
||||
"eslint-plugin-simple-import-sort": "12.1.1",
|
||||
@ -39,8 +39,8 @@
|
||||
"typescript-eslint": "8.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"lodash": "4.17.21",
|
||||
"lodash": "^4.17.21",
|
||||
"winston": "3.14.2",
|
||||
"winston-daily-rotate-file": "5.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,5 +16,11 @@
|
||||
"msg"
|
||||
],
|
||||
"author": "RainSun <zhaoyingbo@live.cn>",
|
||||
"license": "ISC"
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/lodash": "*"
|
||||
},
|
||||
"dependencies": {
|
||||
"lodash": "*"
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import _ from "lodash"
|
||||
|
||||
import { LarkAction, LarkEvent } from "./types"
|
||||
|
||||
/**
|
||||
@ -119,4 +121,57 @@ export const getMessageId = (body: LarkEvent.Data | LarkAction.Data) => {
|
||||
return ""
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成卡片消息
|
||||
* @param {any} card - 卡片消息
|
||||
* @param {object} variables - 变量
|
||||
* @param {boolean} [stringify=true] - 是否返回 JSON 字符串
|
||||
*/
|
||||
export const genCardMsg = (
|
||||
card: any,
|
||||
variables: { [key: string]: any },
|
||||
stringify: boolean = true
|
||||
): any => {
|
||||
const replaceVariables = (str: string): string => {
|
||||
const variablePattern = /\$\{(\w+)\}/g
|
||||
const matches = str.match(variablePattern)
|
||||
|
||||
if (matches && matches.length === 1 && matches[0] === str) {
|
||||
// 如果字符串中只有一个变量,且整个字符串就是这个变量
|
||||
const variableName = matches[0].slice(2, -1) // 去掉 ${ 和 }
|
||||
return variables[variableName] || ""
|
||||
} else {
|
||||
// 否则只替换字符串中的变量部分
|
||||
return str.replace(variablePattern, (_, v) => variables[v] || "")
|
||||
}
|
||||
}
|
||||
|
||||
const traverseAndReplace = (obj: any): any => {
|
||||
if (_.isString(obj)) {
|
||||
return replaceVariables(obj)
|
||||
} else if (_.isArray(obj)) {
|
||||
return obj.map(traverseAndReplace)
|
||||
} else if (_.isObject(obj)) {
|
||||
return _.mapValues(obj, traverseAndReplace)
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
const updatedCard = traverseAndReplace(card)
|
||||
return stringify ? JSON.stringify(updatedCard) : updatedCard
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成消息卡片的Options
|
||||
*/
|
||||
export const genCardOptions = (options: Record<string, string>) => {
|
||||
return Object.entries(options).map(([text, value]) => ({
|
||||
text: {
|
||||
tag: "plain_text",
|
||||
content: text,
|
||||
},
|
||||
value,
|
||||
}))
|
||||
}
|
||||
|
||||
export { LarkAction, LarkEvent }
|
||||
|
Loading…
x
Reference in New Issue
Block a user