Define tx templates in user dir

This commit is contained in:
Wancat
2022-11-17 17:03:40 +08:00
parent 3ef4dfaa48
commit fc4c269ddc
3 changed files with 16 additions and 11 deletions

View File

@@ -10,6 +10,7 @@ import (
"os/exec" "os/exec"
"path" "path"
"strings" "strings"
"text/template"
"time" "time"
) )
@@ -140,9 +141,14 @@ type TxData struct {
Source string `form:"src"` Source string `form:"src"`
} }
func newTx(data TxData) (result string, err error) { func (u *User) newTx(data TxData) (result string, err error) {
data.Date = time.Now().Format("2006/01/02") data.Date = time.Now().Format("2006/01/02")
var buf bytes.Buffer var buf bytes.Buffer
err = ledgerTpl.ExecuteTemplate(&buf, data.Action, data) tpl, err := template.ParseFiles(u.FilePath(data.Action))
return buf.String(), nil if err != nil {
return
}
err = tpl.ExecuteTemplate(&buf, data.Action, data)
result = buf.String()
return
} }

View File

@@ -51,15 +51,16 @@ func router() *gin.Engine {
c.AbortWithError(400, err) c.AbortWithError(400, err)
return return
} }
tx, err := newTx(data) user := getUser(c)
tx, err := user.newTx(data)
if err != nil { if err != nil {
c.AbortWithError(400, err) c.AbortWithError(400, err)
log.Println(err, c.Request.Form) log.Println(err, c.Request.Form)
return return
} }
HTML(c, 200, "new.html", struct { HTML(c, 200, "new.html", gin.H{
Tx string "Tx": tx,
}{tx}) })
}) })
authZone.POST("/submit", func(c *gin.Context) { authZone.POST("/submit", func(c *gin.Context) {
user := getUser(c) user := getUser(c)
@@ -69,9 +70,7 @@ func router() *gin.Engine {
return return
} }
HTML(c, 200, "success.html", struct { c.Redirect(303, "/dashboard")
Tx string
}{tx})
}) })
authZone.GET("/edit", func(c *gin.Context) { authZone.GET("/edit", func(c *gin.Context) {

View File

@@ -1,6 +1,6 @@
{{ define "title" }}Success{{ end }} {{ define "title" }}Success{{ end }}
{{ define "main" }} {{ define "main" }}
<p><strong>Success</strong></p> <p><strong>Success</strong></p>
<p><a href="/edit?filename={{ .FileName }}">繼續編輯</a></p> {{ with .FileName }}<p><a href="/edit?filename={{ . }}">繼續編輯</a></p>{{ end }}
<pre><code>{{ .Tx }}</code></pre> <pre><code>{{ .Tx }}</code></pre>
{{ end }} {{ end }}