|
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
"reflect"
|
|
|
|
|
"server/call"
|
|
|
|
|
"server/common"
|
|
|
|
|
"server/db"
|
|
|
|
|
"server/modules/web/values"
|
|
|
|
|
"server/util"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
|
"github.com/liangdas/mqant/log"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Gin struct {
|
|
|
|
|
R
|
|
|
|
|
RetData interface{} // 定制返回内容
|
|
|
|
|
TmpData interface{}
|
|
|
|
|
TmpData2 interface{}
|
|
|
|
|
Context *gin.Context
|
|
|
|
|
UID int
|
|
|
|
|
Token string
|
|
|
|
|
Lang string
|
|
|
|
|
Referrer string
|
|
|
|
|
// Share int
|
|
|
|
|
Channel int
|
|
|
|
|
UUID string
|
|
|
|
|
WhiteIps []string
|
|
|
|
|
Prefix string
|
|
|
|
|
DeviceType int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type R struct {
|
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
|
Msg string `json:"msg"`
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewApp(c *gin.Context) *Gin {
|
|
|
|
|
g := &Gin{Context: c, R: R{Code: values.CodeOK}}
|
|
|
|
|
_uid, _ := c.Get("uid")
|
|
|
|
|
if _uid != nil {
|
|
|
|
|
g.UID = _uid.(int)
|
|
|
|
|
}
|
|
|
|
|
g.Token = c.GetHeader("token")
|
|
|
|
|
g.Lang = common.CheckLang(c.GetHeader("lang"))
|
|
|
|
|
g.Referrer = c.GetHeader("referrer")
|
|
|
|
|
channel := c.GetHeader("channel")
|
|
|
|
|
if len(channel) > 0 {
|
|
|
|
|
g.Channel, _ = strconv.Atoi(channel)
|
|
|
|
|
}
|
|
|
|
|
if channel == "" || g.Channel == 0 {
|
|
|
|
|
// todo 先写死
|
|
|
|
|
g.Channel = 1000
|
|
|
|
|
}
|
|
|
|
|
if g.Channel == 0 {
|
|
|
|
|
u, _ := url.Parse(c.Request.Referer())
|
|
|
|
|
if u != nil && len(u.Host) > 0 {
|
|
|
|
|
index := strings.Index(u.Host, ".")
|
|
|
|
|
if index > 0 {
|
|
|
|
|
g.Prefix = u.Host[:index]
|
|
|
|
|
}
|
|
|
|
|
channel := call.GetChannelByURL(u.Host[index+1:])
|
|
|
|
|
if channel != nil {
|
|
|
|
|
g.Channel = channel.ChannelID
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g.UUID = c.GetHeader("uuid")
|
|
|
|
|
deviceType := c.GetHeader("platform")
|
|
|
|
|
g.DeviceType, _ = strconv.Atoi(deviceType)
|
|
|
|
|
return g
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Response setting gin.JSON(不加密)
|
|
|
|
|
func (g *Gin) ResponseB() {
|
|
|
|
|
ret := g.RetData
|
|
|
|
|
if ret == nil {
|
|
|
|
|
ret = g.R
|
|
|
|
|
}
|
|
|
|
|
g.Context.JSON(http.StatusOK, ret)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Response setting gin.JSON
|
|
|
|
|
func (g *Gin) Response() {
|
|
|
|
|
if g.R.Code == values.CodeRetry {
|
|
|
|
|
} else if g.R.Code == values.CodeToken {
|
|
|
|
|
g.R.Msg = "login expired"
|
|
|
|
|
}
|
|
|
|
|
jsonData, _ := json.Marshal(g.R)
|
|
|
|
|
g.Context.Data(http.StatusOK, "text", util.AesEncrypt(jsonData))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// S 解析请求(非加密)
|
|
|
|
|
func (g *Gin) SB(one interface{}) (pass bool) {
|
|
|
|
|
if err := g.Context.ShouldBind(one); err != nil {
|
|
|
|
|
log.Error("err:%v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
pass = true
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// S 解析请求(加密)
|
|
|
|
|
func (g *Gin) S(one interface{}) (pass bool) {
|
|
|
|
|
data, err := ioutil.ReadAll(g.Context.Request.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.Code = values.CodeRetry
|
|
|
|
|
log.Error("err:%v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if data == nil {
|
|
|
|
|
log.Debug("is nil")
|
|
|
|
|
}
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
|
log.Debug("")
|
|
|
|
|
}
|
|
|
|
|
jsonData, err := util.AesDecrypt(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.Code = values.CodeRetry
|
|
|
|
|
log.Error("err:%v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = binding.JSON.BindBody(jsonData, one)
|
|
|
|
|
// err = json.Unmarshal(jsonData, one)
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.Code = values.CodeRetry
|
|
|
|
|
log.Error("err:%v", 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从数据库中读取活动数据(不存在时创建)
|
|
|
|
|
func (g *Gin) MGetActivity(tar interface{}) (pass bool) {
|
|
|
|
|
err := db.Mysql().Get(tar)
|
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
|
db.Mysql().Create(tar)
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
log.Error("err:%v", err)
|
|
|
|
|
g.R.Code = values.CodeRetry
|
|
|
|
|
g.R.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
pass = true
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从数据库中读取数据
|
|
|
|
|
func (g *Gin) MGet(tar interface{}) (pass bool) {
|
|
|
|
|
err := db.Mysql().Get(tar)
|
|
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
|
|
|
log.Error("err:%v", 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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetUID 可过滤的url带token时获取uid
|
|
|
|
|
func (g *Gin) GetUID() {
|
|
|
|
|
if g.UID > 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
token := g.Context.GetHeader("token")
|
|
|
|
|
if token == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
uid, _ := db.Redis().GetInt(common.GetRedisKeyToken(token))
|
|
|
|
|
if uid > 0 {
|
|
|
|
|
g.UID = uid
|
|
|
|
|
}
|
|
|
|
|
}
|