2021-09-28 18:23:34 +08:00
|
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"SciencesServer/config"
|
|
|
|
|
"SciencesServer/serve/logger"
|
|
|
|
|
"SciencesServer/utils"
|
|
|
|
|
"errors"
|
2022-02-23 15:18:55 +08:00
|
|
|
|
"net/http"
|
|
|
|
|
|
2021-09-28 18:23:34 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
SuccessCode = 200 //成功的状态码
|
|
|
|
|
FailureCode = 999 //失败的状态码
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ApiHandle func(c *gin.Context) interface{}
|
|
|
|
|
|
|
|
|
|
func GetSession() ApiHandle {
|
|
|
|
|
return func(c *gin.Context) interface{} {
|
|
|
|
|
value, _ := c.Get(config.TokenForSession)
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 11:29:27 +08:00
|
|
|
|
func GetTenantID() ApiHandle {
|
2021-10-12 17:27:41 +08:00
|
|
|
|
return func(c *gin.Context) interface{} {
|
2022-01-11 11:10:59 +08:00
|
|
|
|
value := c.GetHeader(config.ContentForTenantID)
|
2022-01-11 10:41:46 +08:00
|
|
|
|
|
2022-01-11 11:10:59 +08:00
|
|
|
|
if value == "" {
|
2022-03-08 14:33:32 +08:00
|
|
|
|
// 登录身份
|
2022-02-15 17:19:23 +08:00
|
|
|
|
return uint64(1)
|
2022-01-05 18:40:08 +08:00
|
|
|
|
}
|
2022-01-11 11:10:59 +08:00
|
|
|
|
return utils.StringToUnit64(value)
|
2021-10-12 17:27:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 18:23:34 +08:00
|
|
|
|
func Bind(req interface{}) ApiHandle {
|
|
|
|
|
return func(c *gin.Context) interface{} {
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
if c.Request.Method == "GET" {
|
|
|
|
|
err = c.ShouldBindQuery(req)
|
|
|
|
|
} else {
|
|
|
|
|
err = c.ShouldBindBodyWith(req, binding.JSON)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.ErrorF("Request URL【%s】Params Bind Error:%v", c.Request.URL, err)
|
2022-01-12 15:05:14 +08:00
|
|
|
|
return errors.New("参数异常/缺失【" + err.Error() + "】")
|
2021-09-28 18:23:34 +08:00
|
|
|
|
}
|
|
|
|
|
c.Set("params", utils.AnyToJSON(req))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func APISuccess(data ...interface{}) ApiHandle {
|
|
|
|
|
return func(c *gin.Context) interface{} {
|
|
|
|
|
resp := &response{
|
|
|
|
|
Code: SuccessCode,
|
|
|
|
|
Message: "ok",
|
|
|
|
|
}
|
|
|
|
|
if len(data) > 0 {
|
|
|
|
|
resp.Data = data[0]
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func APIFailure(err error) ApiHandle {
|
|
|
|
|
return func(c *gin.Context) interface{} {
|
|
|
|
|
resp := &response{
|
|
|
|
|
Code: FailureCode,
|
|
|
|
|
Message: "failure",
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
resp.Message = err.Error()
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
c.Abort()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func APIResponse(err error, data ...interface{}) ApiHandle {
|
|
|
|
|
return func(c *gin.Context) interface{} {
|
|
|
|
|
resp := &response{
|
|
|
|
|
Code: SuccessCode,
|
|
|
|
|
Message: "ok",
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
resp.Code = FailureCode
|
|
|
|
|
resp.Message = err.Error()
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
c.Abort()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if len(data) > 0 {
|
|
|
|
|
resp.Data = data[0]
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|