f
This commit is contained in:
@@ -161,7 +161,7 @@ func (s *WeChatWorkService) sendNewApplicationNotification(ctx context.Context,
|
||||
applicantName := data["applicant_name"].(string)
|
||||
applicationID := data["application_id"].(string)
|
||||
|
||||
content := fmt.Sprintf(`## 🆕 新的企业认证申请
|
||||
content := fmt.Sprintf(`## 【天远API】🆕 新的企业认证申请
|
||||
|
||||
**企业名称**: %s
|
||||
**申请人**: %s
|
||||
@@ -183,7 +183,7 @@ func (s *WeChatWorkService) sendOCRSuccessNotification(ctx context.Context, data
|
||||
confidence := data["confidence"].(float64)
|
||||
applicationID := data["application_id"].(string)
|
||||
|
||||
content := fmt.Sprintf(`## ✅ OCR识别成功
|
||||
content := fmt.Sprintf(`## 【天远API】✅ OCR识别成功
|
||||
|
||||
**企业名称**: %s
|
||||
**识别置信度**: %.2f%%
|
||||
@@ -204,7 +204,7 @@ func (s *WeChatWorkService) sendOCRFailedNotification(ctx context.Context, data
|
||||
applicationID := data["application_id"].(string)
|
||||
errorMsg := data["error_message"].(string)
|
||||
|
||||
content := fmt.Sprintf(`## ❌ OCR识别失败
|
||||
content := fmt.Sprintf(`## 【天远API】❌ OCR识别失败
|
||||
|
||||
**申请ID**: %s
|
||||
**错误信息**: %s
|
||||
@@ -224,7 +224,7 @@ func (s *WeChatWorkService) sendFaceVerifySuccessNotification(ctx context.Contex
|
||||
applicationID := data["application_id"].(string)
|
||||
confidence := data["confidence"].(float64)
|
||||
|
||||
content := fmt.Sprintf(`## ✅ 人脸识别成功
|
||||
content := fmt.Sprintf(`## 【天远API】✅ 人脸识别成功
|
||||
|
||||
**申请人**: %s
|
||||
**申请ID**: %s
|
||||
@@ -246,7 +246,7 @@ func (s *WeChatWorkService) sendFaceVerifyFailedNotification(ctx context.Context
|
||||
applicationID := data["application_id"].(string)
|
||||
errorMsg := data["error_message"].(string)
|
||||
|
||||
content := fmt.Sprintf(`## ❌ 人脸识别失败
|
||||
content := fmt.Sprintf(`## 【天远API】❌ 人脸识别失败
|
||||
|
||||
**申请人**: %s
|
||||
**申请ID**: %s
|
||||
@@ -269,7 +269,7 @@ func (s *WeChatWorkService) sendAdminApprovedNotification(ctx context.Context, d
|
||||
adminName := data["admin_name"].(string)
|
||||
comment := data["comment"].(string)
|
||||
|
||||
content := fmt.Sprintf(`## ✅ 管理员审核通过
|
||||
content := fmt.Sprintf(`## 【天远API】✅ 管理员审核通过
|
||||
|
||||
**企业名称**: %s
|
||||
**申请ID**: %s
|
||||
@@ -294,7 +294,7 @@ func (s *WeChatWorkService) sendAdminRejectedNotification(ctx context.Context, d
|
||||
adminName := data["admin_name"].(string)
|
||||
reason := data["reason"].(string)
|
||||
|
||||
content := fmt.Sprintf(`## ❌ 管理员审核拒绝
|
||||
content := fmt.Sprintf(`## 【天远API】❌ 管理员审核拒绝
|
||||
|
||||
**企业名称**: %s
|
||||
**申请ID**: %s
|
||||
@@ -318,7 +318,7 @@ func (s *WeChatWorkService) sendContractSignedNotification(ctx context.Context,
|
||||
applicationID := data["application_id"].(string)
|
||||
signerName := data["signer_name"].(string)
|
||||
|
||||
content := fmt.Sprintf(`## 📝 电子合同已签署
|
||||
content := fmt.Sprintf(`## 【天远API】📝 电子合同已签署
|
||||
|
||||
**企业名称**: %s
|
||||
**申请ID**: %s
|
||||
@@ -340,7 +340,7 @@ func (s *WeChatWorkService) sendCertificationCompletedNotification(ctx context.C
|
||||
applicationID := data["application_id"].(string)
|
||||
walletAddress := data["wallet_address"].(string)
|
||||
|
||||
content := fmt.Sprintf(`## 🎉 企业认证完成
|
||||
content := fmt.Sprintf(`## 【天远API】🎉 企业认证完成
|
||||
|
||||
**企业名称**: %s
|
||||
**申请ID**: %s
|
||||
@@ -475,7 +475,7 @@ func (s *WeChatWorkService) SendSystemAlert(ctx context.Context, level, title, m
|
||||
icon = "📢"
|
||||
}
|
||||
|
||||
content := fmt.Sprintf(`## %s 系统告警
|
||||
content := fmt.Sprintf(`## 【天远API】%s 系统告警
|
||||
|
||||
**级别**: %s
|
||||
**标题**: %s
|
||||
@@ -496,7 +496,7 @@ func (s *WeChatWorkService) SendSystemAlert(ctx context.Context, level, title, m
|
||||
func (s *WeChatWorkService) SendDailyReport(ctx context.Context, reportData map[string]interface{}) error {
|
||||
s.logger.Info("发送每日报告")
|
||||
|
||||
content := fmt.Sprintf(`## 📊 企业认证系统每日报告
|
||||
content := fmt.Sprintf(`## 【天远API】📊 企业认证系统每日报告
|
||||
|
||||
**报告日期**: %s
|
||||
|
||||
|
||||
148
internal/infrastructure/external/notification/wechat_work_service_test.go
vendored
Normal file
148
internal/infrastructure/external/notification/wechat_work_service_test.go
vendored
Normal file
@@ -0,0 +1,148 @@
|
||||
package notification_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"tyapi-server/internal/infrastructure/external/notification"
|
||||
)
|
||||
|
||||
// newTestWeChatWorkService 创建用于测试的企业微信服务实例
|
||||
// 默认使用环境变量 WECOM_WEBHOOK,若未设置则使用项目配置中的 webhook。
|
||||
func newTestWeChatWorkService(t *testing.T) *notification.WeChatWorkService {
|
||||
t.Helper()
|
||||
|
||||
webhook := os.Getenv("WECOM_WEBHOOK")
|
||||
if webhook == "" {
|
||||
// 使用你提供的 webhook 地址
|
||||
webhook = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=649bf737-28ca-4f30-ad5f-cfb65b2af113"
|
||||
}
|
||||
|
||||
logger, _ := zap.NewDevelopment()
|
||||
return notification.NewWeChatWorkService(webhook, "", logger)
|
||||
}
|
||||
|
||||
// TestWeChatWork_SendAllBusinessNotifications
|
||||
// 手动运行该用例,将依次向企业微信群推送 5 种业务场景的通知:
|
||||
// 1. 用户充值成功
|
||||
// 2. 用户申请开发票
|
||||
// 3. 用户企业认证成功
|
||||
// 4. 用户余额低于阈值
|
||||
// 5. 用户余额欠费
|
||||
//
|
||||
// 注意:
|
||||
// - 通知中只使用企业名称和手机号码,不展示用户ID
|
||||
// - 默认使用示例企业名称和手机号,实际使用时请根据需要修改
|
||||
func TestWeChatWork_SendAllBusinessNotifications(t *testing.T) {
|
||||
svc := newTestWeChatWorkService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// 示例企业信息(实际可按需修改)
|
||||
enterpriseName := "测试企业有限公司"
|
||||
phone := "13800000000"
|
||||
|
||||
now := time.Now().Format("2006-01-02 15:04:05")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
content string
|
||||
}{
|
||||
{
|
||||
name: "recharge_success",
|
||||
content: fmt.Sprintf(
|
||||
"### 【天远API】用户充值成功通知\n"+
|
||||
"> 企业名称:%s\n"+
|
||||
"> 联系手机:%s\n"+
|
||||
"> 充值金额:%s 元\n"+
|
||||
"> 入账总额:%s 元(含赠送)\n"+
|
||||
"> 时间:%s\n",
|
||||
enterpriseName,
|
||||
phone,
|
||||
"1000.00",
|
||||
"1050.00",
|
||||
now,
|
||||
),
|
||||
},
|
||||
{
|
||||
name: "invoice_applied",
|
||||
content: fmt.Sprintf(
|
||||
"### 【天远API】用户申请开发票\n"+
|
||||
"> 企业名称:%s\n"+
|
||||
"> 联系手机:%s\n"+
|
||||
"> 申请开票金额:%s 元\n"+
|
||||
"> 发票类型:%s\n"+
|
||||
"> 申请时间:%s\n"+
|
||||
"\n请财务尽快审核并开具发票。",
|
||||
enterpriseName,
|
||||
phone,
|
||||
"500.00",
|
||||
"增值税专用发票",
|
||||
now,
|
||||
),
|
||||
},
|
||||
{
|
||||
name: "certification_completed",
|
||||
content: fmt.Sprintf(
|
||||
"### 【天远API】企业认证成功\n"+
|
||||
"> 企业名称:%s\n"+
|
||||
"> 联系手机:%s\n"+
|
||||
"> 完成时间:%s\n"+
|
||||
"\n该企业已完成认证,请相关同事同步更新内部系统并关注后续接入情况。",
|
||||
enterpriseName,
|
||||
phone,
|
||||
now,
|
||||
),
|
||||
},
|
||||
{
|
||||
name: "low_balance_alert",
|
||||
content: fmt.Sprintf(
|
||||
"### 【天远API】用户余额预警\n"+
|
||||
"<font color=\"warning\">用户余额已低于预警阈值,请及时跟进。</font>\n"+
|
||||
"> 企业名称:%s\n"+
|
||||
"> 联系手机:%s\n"+
|
||||
"> 当前余额:%s 元\n"+
|
||||
"> 预警阈值:%s 元\n"+
|
||||
"> 时间:%s\n",
|
||||
enterpriseName,
|
||||
phone,
|
||||
"180.00",
|
||||
"200.00",
|
||||
now,
|
||||
),
|
||||
},
|
||||
{
|
||||
name: "arrears_alert",
|
||||
content: fmt.Sprintf(
|
||||
"### 【天远API】用户余额欠费告警\n"+
|
||||
"<font color=\"warning\">该企业已发生欠费,请及时联系并处理。</font>\n"+
|
||||
"> 企业名称:%s\n"+
|
||||
"> 联系手机:%s\n"+
|
||||
"> 当前余额:%s 元\n"+
|
||||
"> 欠费金额:%s 元\n"+
|
||||
"> 时间:%s\n",
|
||||
enterpriseName,
|
||||
phone,
|
||||
"-50.00",
|
||||
"50.00",
|
||||
now,
|
||||
),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if err := svc.SendMarkdownMessage(ctx, tc.content); err != nil {
|
||||
t.Fatalf("发送场景[%s]通知失败: %v", tc.name, err)
|
||||
}
|
||||
// 简单间隔,避免瞬时发送过多消息
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user