chore: opt template code
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -12,6 +14,23 @@ import (
|
|||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 需要记录响应内容的 API 路径前缀
|
||||||
|
var logResponsePaths = []string{
|
||||||
|
"/api/v1/message/send",
|
||||||
|
"/api/v2/message/send",
|
||||||
|
}
|
||||||
|
|
||||||
|
// responseBodyWriter 用于捕获响应内容
|
||||||
|
type responseBodyWriter struct {
|
||||||
|
gin.ResponseWriter
|
||||||
|
body *bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w responseBodyWriter) Write(b []byte) (int, error) {
|
||||||
|
w.body.Write(b)
|
||||||
|
return w.ResponseWriter.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
// LogMiddleware Logger is the logrus logger handler
|
// LogMiddleware Logger is the logrus logger handler
|
||||||
func LogMiddleware(notLogged ...string) gin.HandlerFunc {
|
func LogMiddleware(notLogged ...string) gin.HandlerFunc {
|
||||||
//hostname, err := os.Hostname()
|
//hostname, err := os.Hostname()
|
||||||
@@ -33,6 +52,24 @@ func LogMiddleware(notLogged ...string) gin.HandlerFunc {
|
|||||||
raw := c.Request.URL.RawQuery
|
raw := c.Request.URL.RawQuery
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
|
// 判断是否需要捕获响应内容
|
||||||
|
needCaptureResponse := false
|
||||||
|
for _, logPath := range logResponsePaths {
|
||||||
|
if strings.HasPrefix(path, logPath) {
|
||||||
|
needCaptureResponse = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var bodyWriter *responseBodyWriter
|
||||||
|
if needCaptureResponse {
|
||||||
|
bodyWriter = &responseBodyWriter{
|
||||||
|
ResponseWriter: c.Writer,
|
||||||
|
body: bytes.NewBufferString(""),
|
||||||
|
}
|
||||||
|
c.Writer = bodyWriter
|
||||||
|
}
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
stop := time.Since(start)
|
stop := time.Since(start)
|
||||||
@@ -70,6 +107,13 @@ func LogMiddleware(notLogged ...string) gin.HandlerFunc {
|
|||||||
entry.Error(c.Errors.ByType(gin.ErrorTypePrivate).String())
|
entry.Error(c.Errors.ByType(gin.ErrorTypePrivate).String())
|
||||||
} else {
|
} else {
|
||||||
msg := fmt.Sprintf("%s [%s] %s %d %d (%dms)", clientIP, c.Request.Method, path, statusCode, dataLength, latency)
|
msg := fmt.Sprintf("%s [%s] %s %d %d (%dms)", clientIP, c.Request.Method, path, statusCode, dataLength, latency)
|
||||||
|
|
||||||
|
// 如果是发送消息的 API,打印返回内容
|
||||||
|
if needCaptureResponse && bodyWriter != nil {
|
||||||
|
responseBody := bodyWriter.body.String()
|
||||||
|
msg = fmt.Sprintf("%s | Response: %s", msg, responseBody)
|
||||||
|
}
|
||||||
|
|
||||||
if statusCode >= http.StatusInternalServerError {
|
if statusCode >= http.StatusInternalServerError {
|
||||||
entry.Error(msg)
|
entry.Error(msg)
|
||||||
} else if statusCode >= http.StatusBadRequest {
|
} else if statusCode >= http.StatusBadRequest {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ type CronMessages struct {
|
|||||||
|
|
||||||
func GenerateMsgUniqueID() string {
|
func GenerateMsgUniqueID() string {
|
||||||
newUUID := util.GenerateUniqueID()
|
newUUID := util.GenerateUniqueID()
|
||||||
return fmt.Sprintf("C-%s", newUUID)
|
return fmt.Sprintf("CM%s", newUUID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddSendCronMsg(
|
func AddSendCronMsg(
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ type SendTasks struct {
|
|||||||
|
|
||||||
func GenerateTaskUniqueID() string {
|
func GenerateTaskUniqueID() string {
|
||||||
newUUID := util.GenerateUniqueID()
|
newUUID := util.GenerateUniqueID()
|
||||||
return fmt.Sprintf("T-%s", newUUID)
|
return fmt.Sprintf("TK%s", newUUID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddSendTaskWithID 添加实例的时候添加任务
|
// AddSendTaskWithID 添加实例的时候添加任务
|
||||||
|
|||||||
+1
-1
@@ -17,7 +17,7 @@ type SendWays struct {
|
|||||||
|
|
||||||
func GenerateWayUniqueID() string {
|
func GenerateWayUniqueID() string {
|
||||||
newUUID := util.GenerateUniqueID()
|
newUUID := util.GenerateUniqueID()
|
||||||
return fmt.Sprintf("W-%s", newUUID)
|
return fmt.Sprintf("WY%s", newUUID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddSendWay(name string, auth string, wayType string, createdBy string, modifiedBy string) error {
|
func AddSendWay(name string, auth string, wayType string, createdBy string, modifiedBy string) error {
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ const handleSubmit = async () => {
|
|||||||
try {
|
try {
|
||||||
let postData = {
|
let postData = {
|
||||||
"name": formData.name,
|
"name": formData.name,
|
||||||
"id": generateBizUniqueID("C"),
|
"id": generateBizUniqueID("CM"),
|
||||||
"title": formData.title,
|
"title": formData.title,
|
||||||
"content": formData.content,
|
"content": formData.content,
|
||||||
"cron": formData.cron_expression,
|
"cron": formData.cron_expression,
|
||||||
|
|||||||
@@ -153,7 +153,7 @@ export default defineComponent({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 说明 -->
|
<!-- 说明 -->
|
||||||
<div class="border-l-4 border-blue-500 bg-blue-50 dark:bg-blue-950 p-3 rounded text-sm space-y-1">
|
<div class="border-l-4 border-blue-500 bg-blue-50 dark:bg-blue-950 p-3 rounded text-xs space-y-1">
|
||||||
<p class="font-semibold text-blue-900 dark:text-blue-200">💡 使用说明</p>
|
<p class="font-semibold text-blue-900 dark:text-blue-200">💡 使用说明</p>
|
||||||
<ul class="text-blue-800 dark:text-blue-300 space-y-1 ml-4 list-disc">
|
<ul class="text-blue-800 dark:text-blue-300 space-y-1 ml-4 list-disc">
|
||||||
<li><strong>token 参数:</strong>需要使用加密后的 token,不能直接使用明文模板ID(安全考虑)</li>
|
<li><strong>token 参数:</strong>需要使用加密后的 token,不能直接使用明文模板ID(安全考虑)</li>
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ const handleAddSubmit = async () => {
|
|||||||
|
|
||||||
// 组建表单数据
|
// 组建表单数据
|
||||||
let postData = {
|
let postData = {
|
||||||
"id": generateBizUniqueID('I'),
|
"id": generateBizUniqueID('IN'),
|
||||||
"enable": 1,
|
"enable": 1,
|
||||||
"template_id": props.templateData.id,
|
"template_id": props.templateData.id,
|
||||||
"way_id": displayOptions.value[0]?.id,
|
"way_id": displayOptions.value[0]?.id,
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ const handleCancel = () => {
|
|||||||
|
|
||||||
// 添加一条任务
|
// 添加一条任务
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
const taskId = generateBizUniqueID('T');
|
const taskId = generateBizUniqueID('TK');
|
||||||
const postData: Record<string, any> = {
|
const postData: Record<string, any> = {
|
||||||
id: taskId,
|
id: taskId,
|
||||||
name: inputValue.value.trim(),
|
name: inputValue.value.trim(),
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ const handleClose = () => {
|
|||||||
const handleAddSubmit = async () => {
|
const handleAddSubmit = async () => {
|
||||||
// 组建表单数据
|
// 组建表单数据
|
||||||
let postData = {
|
let postData = {
|
||||||
"id": generateBizUniqueID('I'),
|
"id": generateBizUniqueID('IN'),
|
||||||
"enable": 1,
|
"enable": 1,
|
||||||
"task_id": props.editData.id,
|
"task_id": props.editData.id,
|
||||||
"way_id": displayOptions.value[0]?.id,
|
"way_id": displayOptions.value[0]?.id,
|
||||||
|
|||||||
@@ -189,7 +189,7 @@ onMounted(async () => {
|
|||||||
<p class="text-sm text-blue-800 dark:text-blue-200">
|
<p class="text-sm text-blue-800 dark:text-blue-200">
|
||||||
新项目建议使用
|
新项目建议使用
|
||||||
<router-link to="/templates" class="font-medium underline hover:text-blue-600">消息模板</router-link>
|
<router-link to="/templates" class="font-medium underline hover:text-blue-600">消息模板</router-link>
|
||||||
功能,它提供更好的内容管理和维护体验。发送任务主要用于兼容历史数据。
|
功能,它提供更好的内容管理和维护体验。发送任务主要用于兼容早期使用数据。
|
||||||
<a href="https://engigu.github.io/Message-Push-Nest/guide/template.html" target="_blank"
|
<a href="https://engigu.github.io/Message-Push-Nest/guide/template.html" target="_blank"
|
||||||
class="font-medium underline hover:text-blue-600 ml-1">
|
class="font-medium underline hover:text-blue-600 ml-1">
|
||||||
了解更多 →
|
了解更多 →
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ function generateUniqueID() {
|
|||||||
|
|
||||||
function generateBizUniqueID(flag) {
|
function generateBizUniqueID(flag) {
|
||||||
const randomString = generateRandomString(10);
|
const randomString = generateRandomString(10);
|
||||||
return `${flag}-${randomString}`;
|
return `${flag}${randomString}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { generateUniqueID, generateBizUniqueID };
|
export { generateUniqueID, generateBizUniqueID };
|
||||||
|
|||||||
Reference in New Issue
Block a user