-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_api_context.go
59 lines (51 loc) · 1.42 KB
/
web_api_context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package ngin
import (
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/nilorg/sdk/errors"
)
// WebAPIContext Web上下文
type WebAPIContext struct {
*gin.Context
opts Options
}
// SetCurrentAccount 设置当前账户
func (ctx *WebAPIContext) SetCurrentAccount(data interface{}) error {
session := sessions.Default(ctx.Context)
session.Set(ctx.opts.SessionCurrentAccountKey, data)
return session.Save()
}
// GetCurrentAccount 设置当前账户
func (ctx *WebAPIContext) GetCurrentAccount() interface{} {
session := sessions.Default(ctx.Context)
return session.Get(ctx.opts.SessionCurrentAccountKey)
}
// DelCurrentAccount 删除当前账户
func (ctx *WebAPIContext) DelCurrentAccount() error {
session := sessions.Default(ctx.Context)
session.Delete(ctx.opts.SessionCurrentAccountKey)
return session.Save()
}
// ResultError 返回错误
func (ctx *WebAPIContext) ResultError(err error) {
if berr, ok := err.(*errors.BusinessError); ok {
ctx.JSON(http.StatusBadRequest, gin.H{
"error": berr,
})
} else {
ctx.JSON(http.StatusBadRequest, gin.H{
"error": errors.New(0, err.Error()),
})
}
}
// NewWebAPIControllerFunc WebAPI控制器函数
func NewWebAPIControllerFunc(ctlFunc func(ctx *WebAPIContext), opts ...Option) gin.HandlerFunc {
return func(ctx *gin.Context) {
tmplCtx := &WebAPIContext{
Context: ctx,
opts: newOptions(opts...),
}
ctlFunc(tmplCtx)
}
}