Files
tyapi-server/debug_event_test.go
2025-08-02 02:54:21 +08:00

166 lines
4.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
// 测试发票申请通过的事件系统
func main() {
fmt.Println("🔍 开始测试发票事件系统...")
// 1. 首先获取待处理的发票申请列表
fmt.Println("\n📋 步骤1: 获取待处理的发票申请列表")
applications, err := getPendingApplications()
if err != nil {
fmt.Printf("❌ 获取申请列表失败: %v\n", err)
return
}
if len(applications) == 0 {
fmt.Println("⚠️ 没有待处理的发票申请")
return
}
// 选择第一个申请进行测试
application := applications[0]
fmt.Printf("✅ 找到申请: ID=%s, 公司=%s, 金额=%s\n",
application["id"], application["company_name"], application["amount"])
// 2. 创建一个测试PDF文件
fmt.Println("\n📄 步骤2: 创建测试PDF文件")
testFile, err := createTestPDF()
if err != nil {
fmt.Printf("❌ 创建测试文件失败: %v\n", err)
return
}
defer os.Remove(testFile)
// 3. 通过发票申请(上传文件)
fmt.Println("\n📤 步骤3: 通过发票申请并上传文件")
err = approveInvoiceApplication(application["id"].(string), testFile)
if err != nil {
fmt.Printf("❌ 通过申请失败: %v\n", err)
return
}
fmt.Println("✅ 发票申请通过成功!")
fmt.Println("📧 请检查日志中的邮件发送情况...")
}
// 获取待处理的发票申请列表
func getPendingApplications() ([]map[string]interface{}, error) {
url := "http://localhost:8080/api/v1/admin/invoices/pending"
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
if result["code"] != float64(200) {
return nil, fmt.Errorf("API返回错误: %s", result["message"])
}
data := result["data"].(map[string]interface{})
applications := data["applications"].([]interface{})
applicationsList := make([]map[string]interface{}, len(applications))
for i, app := range applications {
applicationsList[i] = app.(map[string]interface{})
}
return applicationsList, nil
}
// 创建测试PDF文件
func createTestPDF() (string, error) {
// 创建一个简单的PDF内容这里只是示例
pdfContent := []byte("%PDF-1.4\n1 0 obj\n<<\n/Type /Catalog\n/Pages 2 0 R\n>>\nendobj\n2 0 obj\n<<\n/Type /Pages\n/Kids [3 0 R]\n/Count 1\n>>\nendobj\n3 0 obj\n<<\n/Type /Page\n/Parent 2 0 R\n/MediaBox [0 0 612 792]\n/Contents 4 0 R\n>>\nendobj\n4 0 obj\n<<\n/Length 44\n>>\nstream\nBT\n/F1 12 Tf\n72 720 Td\n(Test Invoice) Tj\nET\nendstream\nendobj\nxref\n0 5\n0000000000 65535 f \n0000000009 00000 n \n0000000058 00000 n \n0000000115 00000 n \n0000000204 00000 n \ntrailer\n<<\n/Size 5\n/Root 1 0 R\n>>\nstartxref\n297\n%%EOF\n")
tempFile := filepath.Join(os.TempDir(), "test_invoice.pdf")
err := os.WriteFile(tempFile, pdfContent, 0644)
if err != nil {
return "", err
}
return tempFile, nil
}
// 通过发票申请
func approveInvoiceApplication(applicationID, filePath string) error {
url := fmt.Sprintf("http://localhost:8080/api/v1/admin/invoices/%s/approve", applicationID)
// 创建multipart表单
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
// 添加文件
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
part, err := writer.CreateFormFile("file", "test_invoice.pdf")
if err != nil {
return err
}
_, err = io.Copy(part, file)
if err != nil {
return err
}
// 添加备注
writer.WriteField("admin_notes", "测试通过 - 调试事件系统")
writer.Close()
// 发送请求
req, err := http.NewRequest("POST", url, &buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
return err
}
if result["code"] != float64(200) {
return fmt.Errorf("API返回错误: %s", result["message"])
}
return nil
}