From 2e85177a6c48e7763030bdf0ada8ac9f84e7ebe1 Mon Sep 17 00:00:00 2001 From: BaoXuebin Date: Tue, 14 Dec 2021 21:24:39 +0800 Subject: [PATCH] add: wxpay import --- server.go | 1 + service/import.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/server.go b/server.go index 1ee11b1..f050500 100644 --- a/server.go +++ b/server.go @@ -86,6 +86,7 @@ func RegisterRouter(router *gin.Engine) { authorized.GET("/file/content", service.QueryLedgerSourceFileContent) authorized.POST("/file", service.UpdateLedgerSourceFileContent) authorized.POST("/import/alipay", service.ImportAliPayCSV) + authorized.POST("/import/wx", service.ImportWxPayCSV) } } diff --git a/service/import.go b/service/import.go index 5f61714..559b9cd 100644 --- a/service/import.go +++ b/service/import.go @@ -58,3 +58,52 @@ func ImportAliPayCSV(c *gin.Context) { OK(c, result) } + +func ImportWxPayCSV(c *gin.Context) { + ledgerConfig := script.GetLedgerConfigFromContext(c) + + file, _ := c.FormFile("file") + f, _ := file.Open() + reader := csv.NewReader(bufio.NewReader(f)) + + result := make([]Transaction, 0) + + currency := "CNY" + currencySymbol := script.GetCommoditySymbol(currency) + + for { + lines, err := reader.Read() + if err == io.EOF { + break + } else if err != nil { + script.LogError(ledgerConfig.Mail, err.Error()) + } + if len(lines) > 8 { + fields := strings.Fields(lines[0]) + status := strings.Trim(lines[4], " ") + account := "" + if status == "收入" { + account = "Income:" + } else if status == "支出" { + account = "Expenses:" + } else { + continue + } + + if len(fields) >= 2 { + result = append(result, Transaction{ + Id: strings.Trim(lines[8], " "), + Date: strings.Trim(fields[0], " "), + Payee: strings.Trim(lines[2], " "), + Narration: strings.Trim(lines[3], " "), + Number: strings.Trim(lines[5], "¥"), + Account: account, + Currency: currency, + CurrencySymbol: currencySymbol, + }) + } + } + } + + OK(c, result) +}