add: wxpay import
This commit is contained in:
parent
d1f3971e91
commit
2e85177a6c
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue