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) +}