mirror of
https://gitee.com/johng/gf
synced 2026-07-05 05:13:14 +08:00
change Error from struct to interface for package gvalid;error string update for package gdb
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/i18n/gi18n"
|
||||
@ -13,6 +14,6 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(t.Translate(`hello`))
|
||||
fmt.Println(t.Translate(`{#hello}{#world}!`))
|
||||
fmt.Println(t.Translate(context.TODO(), `hello`))
|
||||
fmt.Println(t.Translate(context.TODO(), `{#hello}{#world}!`))
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/i18n/gi18n"
|
||||
@ -13,6 +14,6 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(t.Translate(`hello`))
|
||||
fmt.Println(t.Translate(`{#hello}{#world}!`))
|
||||
fmt.Println(t.Translate(context.TODO(), `hello`))
|
||||
fmt.Println(t.Translate(context.TODO(), `{#hello}{#world}!`))
|
||||
}
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/i18n/gi18n"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -10,6 +12,12 @@ func main() {
|
||||
orderId = 865271654
|
||||
orderAmount = 99.8
|
||||
)
|
||||
fmt.Println(g.I18n().Tfl(`en`, `{#OrderPaid}`, orderId, orderAmount))
|
||||
fmt.Println(g.I18n().Tfl(`zh-CN`, `{#OrderPaid}`, orderId, orderAmount))
|
||||
fmt.Println(g.I18n().Tf(
|
||||
gi18n.WithLanguage(context.TODO(), `en`),
|
||||
`{#OrderPaid}`, orderId, orderAmount,
|
||||
))
|
||||
fmt.Println(g.I18n().Tf(
|
||||
gi18n.WithLanguage(context.TODO(), `zh-CN`),
|
||||
`{#OrderPaid}`, orderId, orderAmount,
|
||||
))
|
||||
}
|
||||
|
||||
@ -2,15 +2,19 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/i18n/gi18n"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g.I18n().SetPath("/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/i18n/gi18n/i18n")
|
||||
s := g.Server()
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.WriteTplContent(`{#hello}{#world}!`, g.Map{
|
||||
"I18nLanguage": r.Get("lang", "zh-CN"),
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(func(r *ghttp.Request) {
|
||||
r.SetCtx(gi18n.WithLanguage(r.Context(), "zh-CN"))
|
||||
r.Middleware.Next()
|
||||
})
|
||||
group.ALL("/", func(r *ghttp.Request) {
|
||||
r.Response.WriteTplContent(`{#hello}{#world}!`)
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
|
||||
64
.example/net/ghttp/client/middleware/client.go
Normal file
64
.example/net/ghttp/client/middleware/client.go
Normal file
@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/crypto/gmd5"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"github.com/gogf/gf/util/guid"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
appId = "123"
|
||||
appSecret = "456"
|
||||
)
|
||||
|
||||
// 注入统一的接口签名参数
|
||||
func injectSignature(jsonContent []byte) []byte {
|
||||
var m map[string]interface{}
|
||||
_ = json.Unmarshal(jsonContent, &m)
|
||||
if len(m) > 0 {
|
||||
m["appid"] = appId
|
||||
m["nonce"] = guid.S()
|
||||
m["timestamp"] = gtime.Timestamp()
|
||||
var (
|
||||
keyArray = garray.NewSortedStrArrayFrom(gutil.Keys(m))
|
||||
sigContent string
|
||||
)
|
||||
keyArray.Iterator(func(k int, v string) bool {
|
||||
sigContent += v
|
||||
sigContent += gconv.String(m[v])
|
||||
return true
|
||||
})
|
||||
m["signature"] = gmd5.MustEncryptString(gmd5.MustEncryptString(sigContent) + appSecret)
|
||||
jsonContent, _ = json.Marshal(m)
|
||||
}
|
||||
return jsonContent
|
||||
}
|
||||
|
||||
func main() {
|
||||
c := g.Client()
|
||||
c.Use(func(c *ghttp.Client, r *http.Request) (resp *ghttp.ClientResponse, err error) {
|
||||
bodyBytes, _ := ioutil.ReadAll(r.Body)
|
||||
if len(bodyBytes) > 0 {
|
||||
// 注入签名相关参数,修改Request原有的提交参数
|
||||
bodyBytes = injectSignature(bodyBytes)
|
||||
r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
|
||||
r.ContentLength = int64(len(bodyBytes))
|
||||
}
|
||||
return c.Next(r)
|
||||
})
|
||||
content := c.ContentJson().PostContent("http://127.0.0.1:8199/", g.Map{
|
||||
"name": "goframe",
|
||||
"site": "https://goframe.org",
|
||||
})
|
||||
fmt.Println(content)
|
||||
}
|
||||
17
.example/net/ghttp/client/middleware/server.go
Normal file
17
.example/net/ghttp/client/middleware/server.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.ALL("/", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetMap())
|
||||
})
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
@ -25,7 +25,7 @@ func main() {
|
||||
//fmt.Println(r.GetBody())
|
||||
if err := r.Parse(&req); err != nil {
|
||||
// Validation error.
|
||||
if v, ok := err.(*gvalid.Error); ok {
|
||||
if v, ok := err.(gvalid.Error); ok {
|
||||
r.Response.WriteJsonExit(RegisterRes{
|
||||
Code: 1,
|
||||
Error: v.FirstString(),
|
||||
|
||||
@ -24,7 +24,7 @@ func main() {
|
||||
var req *RegisterReq
|
||||
if err := r.Parse(&req); err != nil {
|
||||
// Validation error.
|
||||
if v, ok := err.(*gvalid.Error); ok {
|
||||
if v, ok := err.(gvalid.Error); ok {
|
||||
r.Response.WriteJsonExit(RegisterRes{
|
||||
Code: 1,
|
||||
Error: v.FirstString(),
|
||||
|
||||
Reference in New Issue
Block a user