Files
crawlab/backend/services/context/context.go
yaziming 81f6cf021f Backend:
improve
     - AuthMiddleware 注入当前用户的信息
     - 增加Context服务支持快捷获取当前登录者信息
     - 重构Login/GetMe接口逻辑避免重复的数据库查询
     - 规范化error信息声明(向下兼容,旧代码可逐渐迁移规范化)
     - 修正部分不符合规范的代码
2019-08-31 21:26:56 +08:00

74 lines
1.3 KiB
Go

package context
import (
"crawlab/constants"
"crawlab/errors"
"crawlab/model"
"fmt"
"github.com/apex/log"
"github.com/gin-gonic/gin"
errors2 "github.com/pkg/errors"
"net/http"
"runtime/debug"
)
type Context struct {
*gin.Context
}
func (c *Context) User() *model.User {
userIfe, exists := c.Get(constants.ContextUser)
if !exists {
return nil
}
user, ok := userIfe.(*model.User)
if !ok {
return nil
}
return user
}
func (c *Context) Success(data interface{}, meta interface{}) {
if meta == nil {
meta = gin.H{}
}
if data == nil {
data = gin.H{}
}
c.JSON(http.StatusOK, gin.H{
"status": "ok",
"message": "success",
"data": data,
"error": "",
})
}
func (c *Context) FailedWithError(err error, httpCode ...int) {
var code = 200
if len(httpCode) > 0 {
code = httpCode[0]
}
log.Errorf("handle error:" + err.Error())
debug.PrintStack()
switch errors2.Cause(err).(type) {
case errors.OPError:
c.AbortWithStatusJSON(code, gin.H{
"status": "ok",
"message": "error",
"error": err.Error(),
})
break
default:
fmt.Println("deprecated....")
c.AbortWithStatusJSON(code, gin.H{
"status": "ok",
"message": "error",
"error": err.Error(),
})
}
}
func WithGinContext(context *gin.Context) *Context {
return &Context{Context: context}
}