封装基础数据操作,错误返回
This commit is contained in:
parent
a5c7896d0a
commit
34a7a78894
|
@ -2,7 +2,6 @@ package backend
|
|||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"qteam/app/constants/errorcode"
|
||||
"qteam/app/http/controllers"
|
||||
"qteam/app/http/entities/backend"
|
||||
"qteam/app/services"
|
||||
|
@ -13,18 +12,13 @@ func Login(c *gin.Context) {
|
|||
}
|
||||
|
||||
func UserList(c *gin.Context) {
|
||||
request := new(backend.UserListRequest)
|
||||
err := controllers.GenRequest(c, request)
|
||||
if err != nil {
|
||||
controllers.Error(c, errorcode.ParamError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
request := controllers.GetRequest(c).(backend.UserListRequest)
|
||||
where := map[string]interface{}{
|
||||
"Mobile": request.Mobile,
|
||||
}
|
||||
count, list, err := services.GetListByWhere(where, request.Page, request.Limit)
|
||||
|
||||
controllers.Success(c, gin.H{"data": list, "count": count}, "成功")
|
||||
controllers.HandRes(c, gin.H{"data": list, "count": count}, err)
|
||||
return
|
||||
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/go-playground/locales/zh"
|
||||
ut "github.com/go-playground/universal-translator"
|
||||
"github.com/qit-team/snow-core/redis"
|
||||
|
@ -98,12 +99,13 @@ type HTTPError struct {
|
|||
* @param c
|
||||
* @param request 传入request数据结构的指针 如 new(TestRequest)
|
||||
*/
|
||||
func GenRequest(c *gin.Context, request interface{}) (err error) {
|
||||
body, err := ReadBody(c)
|
||||
if err != nil {
|
||||
return
|
||||
func GenRequest(c *gin.Context, request interface{}) (msgs []string, err error) {
|
||||
if c.Request.Method == "GET" {
|
||||
err = c.ShouldBindQuery(request)
|
||||
} else {
|
||||
err = c.ShouldBindJSON(request)
|
||||
}
|
||||
err = json.Unmarshal(body, request)
|
||||
|
||||
if err == nil {
|
||||
validate := validator.New()
|
||||
zh_ch := zh.New()
|
||||
|
@ -113,12 +115,16 @@ func GenRequest(c *gin.Context, request interface{}) (err error) {
|
|||
//验证器注册翻译器
|
||||
zh_translations.RegisterDefaultTranslations(validate, trans)
|
||||
errValidate := validate.Struct(request)
|
||||
|
||||
if errValidate != nil {
|
||||
utils.Log(c, "param_validator_exception:"+c.Request.URL.Path, errValidate)
|
||||
return errValidate
|
||||
for _, v := range errValidate.(validator.ValidationErrors) {
|
||||
msgs = append(msgs, v.Translate(trans))
|
||||
}
|
||||
err = errors.New(errorcode.GetMsg(errorcode.ParamError, ""))
|
||||
return
|
||||
}
|
||||
}
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
// 重复读取body
|
||||
|
@ -131,6 +137,11 @@ func ReadBody(c *gin.Context) (body []byte, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func GetRequest(c *gin.Context) interface{} {
|
||||
request, _ := c.Get("request")
|
||||
return request
|
||||
}
|
||||
|
||||
func HandRes(c *gin.Context, data interface{}, err error) {
|
||||
if err == nil {
|
||||
Success(c, data, "")
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
package front
|
||||
|
||||
type LoginRequest struct {
|
||||
UserName string `json:"user_name"`
|
||||
UserName string `json:"user_name" validate:"required"`
|
||||
}
|
||||
|
|
|
@ -68,10 +68,10 @@ func ValidateRequest() gin.HandlerFunc {
|
|||
utils.Log(c, "path", path)
|
||||
controllers.HandCodeRes(c, nil, errorcode.NotFound)
|
||||
} else {
|
||||
err := controllers.GenRequest(c, handler())
|
||||
msg, err := controllers.GenRequest(c, handler())
|
||||
if err != nil {
|
||||
utils.Log(c, "path", path)
|
||||
controllers.HandCodeRes(c, nil, errorcode.ParamError)
|
||||
controllers.Error(c, errorcode.ParamError, msg...)
|
||||
} else {
|
||||
c.Set("request", handler())
|
||||
c.Next()
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type CommonFields struct {
|
||||
Creator string `xorm:"creator"`
|
||||
CreateTime time.Time `xorm:"CreateTime created"`
|
||||
UpdateTime time.Time `xorm:"UpdateTime updated"`
|
||||
Deleted time.Time `xorm:"'deleted'"`
|
||||
}
|
||||
|
||||
func GetListByPage(page int, pageSize int, where []map[string]interface{}, db xorm.EngineGroup) (count int64, data []interface{}, err error) {
|
||||
db.ShowSQL(true)
|
||||
var rs []interface{}
|
||||
var session = db.Where("1 = 1")
|
||||
for k, v := range where {
|
||||
session = session.Where(k, v)
|
||||
}
|
||||
|
||||
count, err = session.Limit(pageSize, (page-1)*page).FindAndCount(&rs)
|
||||
return
|
||||
}
|
||||
|
||||
func GetOne(where []map[string]interface{}, db xorm.EngineGroup) (rs interface{}, err error) {
|
||||
var session = db.Where("1 = 1")
|
||||
for k, v := range where {
|
||||
session = session.Where(k, v)
|
||||
}
|
||||
err = session.Find(&rs)
|
||||
return
|
||||
}
|
||||
|
||||
func Insert(pb interface{}, db xorm.EngineGroup) (int64, error) {
|
||||
return db.Insert(pb)
|
||||
}
|
||||
|
||||
func UpdateById(id int64, pb interface{}, db xorm.EngineGroup) error {
|
||||
_, err := db.Where("id = ?", id).Update(pb)
|
||||
return err
|
||||
}
|
||||
|
||||
func DetleById(id int64, db xorm.EngineGroup) (err error) {
|
||||
_, err = db.Where("id = ?", id).Delete()
|
||||
return
|
||||
}
|
|
@ -13,7 +13,6 @@ func (p *Products) FromDomain(do domains.Product) {
|
|||
p.Issuperposition = do.IsSuperposition
|
||||
p.Status = do.Status
|
||||
p.Isneedbill = do.TemplateId
|
||||
p.CreateTime = do.CreateTime
|
||||
p.Supplierproductname = do.SupplierProductName
|
||||
p.Supplierproductid = do.SupplierProductId
|
||||
}
|
||||
|
@ -29,7 +28,6 @@ func (p *Products) ToDomain() (do domains.Product) {
|
|||
do.IsSuperposition = p.Issuperposition
|
||||
do.Status = p.Status
|
||||
do.TemplateId = p.Isneedbill
|
||||
do.CreateTime = p.CreateTime
|
||||
do.SupplierProductName = p.Supplierproductname
|
||||
do.SupplierProductId = p.Supplierproductid
|
||||
return
|
||||
|
|
|
@ -2,7 +2,6 @@ package productsmodel
|
|||
|
||||
import (
|
||||
"github.com/qit-team/snow-core/db"
|
||||
"qteam/app/models"
|
||||
"sync"
|
||||
)
|
||||
|
||||
|
@ -27,7 +26,6 @@ type Products struct {
|
|||
//Createtime time.Time `xorm:"'CreateTime' datetime"`
|
||||
Supplierproductid int64 `xorm:"'SupplierProductId' bigint(0)"`
|
||||
Supplierproductname string `xorm:"'SupplierProductName' varchar(255)"`
|
||||
models.CommonFields `xorm:"extends"`
|
||||
}
|
||||
|
||||
// 表名
|
||||
|
|
4
go.mod
4
go.mod
|
@ -4,6 +4,7 @@ go 1.21
|
|||
|
||||
require (
|
||||
github.com/BurntSushi/toml v0.4.1
|
||||
github.com/ahmetb/go-linq/v3 v3.2.0
|
||||
github.com/forgoer/openssl v1.6.0
|
||||
github.com/gin-gonic/gin v1.7.7
|
||||
github.com/go-playground/locales v0.14.0
|
||||
|
@ -21,13 +22,13 @@ require (
|
|||
google.golang.org/grpc v1.56.3
|
||||
google.golang.org/protobuf v1.30.0
|
||||
gopkg.in/go-playground/validator.v9 v9.31.0
|
||||
xorm.io/builder v0.3.9
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/KyleBanks/depth v1.2.1 // indirect
|
||||
github.com/PuerkitoBio/purell v1.1.1 // indirect
|
||||
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
|
||||
github.com/ahmetb/go-linq/v3 v3.2.0 // indirect
|
||||
github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68 // indirect
|
||||
github.com/alibabacloud-go/tea v1.1.17 // indirect
|
||||
github.com/alibabacloud-go/tea-utils v1.4.4 // indirect
|
||||
|
@ -95,7 +96,6 @@ require (
|
|||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
stathat.com/c/consistent v1.0.0 // indirect
|
||||
xorm.io/builder v0.3.9 // indirect
|
||||
xorm.io/core v0.7.3 // indirect
|
||||
xorm.io/xorm v1.2.5 // indirect
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue