You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
227 lines
5.1 KiB
227 lines
5.1 KiB
|
2 months ago
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"net/http"
|
||
|
|
"reflect"
|
||
|
|
"server/db"
|
||
|
|
"server/modules/customer/bdb"
|
||
|
|
"server/modules/customer/values"
|
||
|
|
"server/util"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"github.com/liangdas/mqant/log"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
type Gin struct {
|
||
|
|
C *gin.Context
|
||
|
|
R
|
||
|
|
User *values.User
|
||
|
|
}
|
||
|
|
|
||
|
|
type R struct {
|
||
|
|
Data interface{} `json:"data"`
|
||
|
|
Msg string `json:"msg"`
|
||
|
|
Code int `json:"code"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewApp(c *gin.Context) *Gin {
|
||
|
|
g := &Gin{C: c, R: R{Code: values.CodeOK, Msg: "ok"}}
|
||
|
|
user, ex := c.Get("user")
|
||
|
|
if ex {
|
||
|
|
g.User = user.(*values.User)
|
||
|
|
if len(g.User.Channels) > 0 {
|
||
|
|
json.Unmarshal([]byte(g.User.Channels), &g.User.SChannels)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return g
|
||
|
|
}
|
||
|
|
|
||
|
|
// Response setting gin.JSON
|
||
|
|
func (g *Gin) Response() {
|
||
|
|
if g.Code == values.CodeRetry {
|
||
|
|
g.Msg = "内部错误"
|
||
|
|
} else if g.Code == values.CodePower {
|
||
|
|
g.Msg = "您无权操作此项"
|
||
|
|
}
|
||
|
|
g.C.JSON(http.StatusOK, g.R)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (g *Gin) RecordEdit(t int, detail string) {
|
||
|
|
u, _ := g.C.Get("user")
|
||
|
|
user := u.(*values.User)
|
||
|
|
one := &values.EditHistory{Model: t, Detail: detail, Time: time.Now().Unix(), Operator: user.Name, UID: int(user.ID)}
|
||
|
|
bdb.BackDB.Create(one)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (g *Gin) S(one interface{}) (pass bool) {
|
||
|
|
err := g.C.ShouldBind(one)
|
||
|
|
if err != nil {
|
||
|
|
g.R.Code = values.CodeParam
|
||
|
|
g.R.Msg = err.Error()
|
||
|
|
log.Error("bind %v err:%v", reflect.TypeOf(one), err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
pass = true
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// U 统一json解析方法
|
||
|
|
func (g *Gin) U(src []byte, tar interface{}) (pass bool) {
|
||
|
|
err := json.Unmarshal(src, tar)
|
||
|
|
if err != nil {
|
||
|
|
log.Error("%v err:%v", reflect.TypeOf(tar), err)
|
||
|
|
g.R.Code = values.CodeRetry
|
||
|
|
g.R.Msg = err.Error()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
pass = true
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// MCommit 提交事务
|
||
|
|
func (g *Gin) MCommit(tx *gorm.DB) {
|
||
|
|
if g.Code == values.CodeOK {
|
||
|
|
if err := tx.Commit().Error; err != nil {
|
||
|
|
tx.Rollback()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
tx.Rollback()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// CheckPower 检查权限是否合法
|
||
|
|
func (g *Gin) CheckPower(power string) bool {
|
||
|
|
newMap := map[int][]int{}
|
||
|
|
if err := json.Unmarshal([]byte(power), &newMap); err != nil {
|
||
|
|
log.Error("err:%v", err)
|
||
|
|
g.Code = values.CodeParam
|
||
|
|
g.Msg = err.Error()
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
for i := range newMap {
|
||
|
|
if !values.IsValidPower(i) {
|
||
|
|
log.Error("invalid power:%v", i)
|
||
|
|
g.Code = values.CodeParam
|
||
|
|
g.Msg = fmt.Sprintf("未知权限参数%v", i)
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
|
||
|
|
// 从数据库中读取数据
|
||
|
|
func (g *Gin) MGetSqlAll(model, tar interface{}, condition map[string]interface{}) (pass bool) {
|
||
|
|
sql := ""
|
||
|
|
mod := reflect.TypeOf(model).Elem()
|
||
|
|
for k, v := range condition {
|
||
|
|
tmp, ok := mod.FieldByName(k)
|
||
|
|
if !ok {
|
||
|
|
log.Error("invalid model:%v,condition:%v", model, condition)
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
tag := tmp.Tag.Get("web")
|
||
|
|
if tag == "" || tag == "-" {
|
||
|
|
log.Error("invalid model:%v,condition:%v", model, condition)
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
reft := reflect.TypeOf(v)
|
||
|
|
ref := reflect.ValueOf(v)
|
||
|
|
if reft.Kind() == reflect.Slice {
|
||
|
|
if ref.Len() != 2 {
|
||
|
|
log.Error("invalid model:%v,condition:%v", model, condition)
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if len(sql) > 0 {
|
||
|
|
sql += " and "
|
||
|
|
}
|
||
|
|
sql += fmt.Sprintf("%s >= %v and %s <= %v", tag, ref.Index(0).Interface(), tag, ref.Index(1).Interface())
|
||
|
|
} else {
|
||
|
|
if len(sql) > 0 {
|
||
|
|
sql += " and "
|
||
|
|
}
|
||
|
|
sql += fmt.Sprintf("%s = %v", tag, ref.Interface())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if _, err := db.Mysql().QueryAll(sql, "", model, tar); err != nil {
|
||
|
|
log.Error("err:%v", err)
|
||
|
|
g.Code = values.CodeRetry
|
||
|
|
g.R.Msg = err.Error()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
pass = true
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
func (g *Gin) MUpdateAll(updates []map[string]interface{}, element interface{}) (pass bool) {
|
||
|
|
for _, v := range updates {
|
||
|
|
tmp := reflect.New(reflect.TypeOf(element).Elem()).Interface()
|
||
|
|
// log.Debug("%v", v)
|
||
|
|
tmpbyte, _ := json.Marshal(v)
|
||
|
|
err := json.Unmarshal(tmpbyte, &tmp)
|
||
|
|
if err != nil {
|
||
|
|
log.Error("err%v", err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
ref := reflect.ValueOf(tmp).Elem()
|
||
|
|
reft := reflect.TypeOf(tmp).Elem()
|
||
|
|
id := ref.FieldByName("ID").Int()
|
||
|
|
if id == 0 {
|
||
|
|
if err := db.Mysql().Create(tmp); err != nil {
|
||
|
|
g.Code = values.CodeRetry
|
||
|
|
return
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
update := map[string]interface{}{}
|
||
|
|
for k := range v {
|
||
|
|
field, ok := reft.FieldByName(k)
|
||
|
|
if !ok {
|
||
|
|
field, ok = util.GetStructFieldByJsonTag(tmp, k)
|
||
|
|
if !ok {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
k = field.Name
|
||
|
|
}
|
||
|
|
tag := field.Tag.Get("web")
|
||
|
|
if tag == "" || tag == "-" {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
update[tag] = ref.FieldByName(k).Interface()
|
||
|
|
}
|
||
|
|
// update := util.StructToMap(tmp, "web")
|
||
|
|
model := reflect.New(reflect.TypeOf(element).Elem())
|
||
|
|
model.Elem().FieldByName("ID").SetInt(id)
|
||
|
|
// log.Debug("element:%v", element)
|
||
|
|
log.Debug("update:%v", update)
|
||
|
|
log.Debug("model:%v", model)
|
||
|
|
if err := db.Mysql().Update(model.Interface(), update); err != nil {
|
||
|
|
g.Code = values.CodeRetry
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
pass = true
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
func (g *Gin) MDel(id int, element interface{}) (pass bool) {
|
||
|
|
tmp := reflect.New(reflect.TypeOf(element).Elem())
|
||
|
|
tmp.Elem().FieldByName("ID").SetInt(int64(id))
|
||
|
|
if !db.Mysql().Exist(tmp.Interface()) {
|
||
|
|
g.Code = values.CodeParam
|
||
|
|
g.Msg = "该id不存在"
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := db.Mysql().Del(tmp.Interface()); err != nil {
|
||
|
|
g.Code = values.CodeRetry
|
||
|
|
return
|
||
|
|
}
|
||
|
|
pass = true
|
||
|
|
return
|
||
|
|
}
|