Initial commit
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package stdLog
|
||||
|
||||
import "fmt"
|
||||
|
||||
type CodeType int
|
||||
|
||||
type CodeMsg struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Tip string `json:"tip,omitempty"`
|
||||
}
|
||||
|
||||
// 错误级别
|
||||
const (
|
||||
ParamLevel = 10000 // 参数级
|
||||
StoreLevel = 20000 // 存储级
|
||||
ServerLevel = 30000 // 服务级
|
||||
BusinessLevel = 40000 // 业务逻辑级
|
||||
)
|
||||
|
||||
// 通用code
|
||||
const (
|
||||
Success CodeType = 200 //Success 通用成功
|
||||
Failure CodeType = 400 //Failure 通用失败
|
||||
)
|
||||
|
||||
const (
|
||||
ParamError CodeType = ParamLevel + iota // 10000 参数错误
|
||||
NoToken // 10001 没有token
|
||||
ParamDeductionRatioError // 10002 无效的扣量值
|
||||
InvalidPhoneNumber // 10003 无效的手机号
|
||||
QueryError // 10004 查询失败
|
||||
)
|
||||
|
||||
const (
|
||||
AdminAccountNotExists CodeType = ServerLevel + iota // 30000 管理员账号不存在
|
||||
AdminPasswordError // 30001 管理员密码错误
|
||||
AdminGetAuthorityFailed // 30002 获取权限失败
|
||||
ChannelNotExists // 30003 渠道不存在
|
||||
AgentAccountNotExists // 30004 代理商账号不存在
|
||||
AgentPasswordError // 30005 代理商密码错误
|
||||
AuthorityRoleExists // 30006 该权限角色已存在
|
||||
AgentHasLocked // 30007 禁止登陆
|
||||
VerifySmsCodeFailed // 30008 验证码错误
|
||||
DisableWithdraw // 30009 禁止提现
|
||||
WithdrawBalanceLess // 30010 提现金额不足
|
||||
WithdrawFailed // 30011 提现失败
|
||||
WithdrawHasDone // 30012 提现已经处理
|
||||
WithdrawRequestFailed // 30013 提现第三方请求失败
|
||||
WithdrawMobileError // 30014 手机号不一致
|
||||
NotSupportWithdraw // 30015 不支持提现
|
||||
AccountExists // 30016 账号已存在
|
||||
AgentAccountBanned // 30017 账号已禁用
|
||||
GoogleAuthFailed // 30018 谷歌验证失败
|
||||
AdvertiseLocExists // 30019 广告位置已存在
|
||||
AdvertiseInvalid // 30020 无效的广告
|
||||
AppInfoError // 30021 应用信息错误
|
||||
InvalidPrice // 30022 无效的金额
|
||||
ChannelExists // 30023 渠道已存在
|
||||
ActivityNoExistErr // 30024 活动不存在
|
||||
LotteryNumReachedTheLimit // 30025 今日抽奖次数达到限制
|
||||
LotteryCodeIsInvalid // 30026 兑换码无效
|
||||
LotteryCodeIsUsed // 30026 兑换码已使用
|
||||
SettlementDouble // 30028 重复结算
|
||||
DownloadM3u8File // 30029 文件服务器访问失败
|
||||
DownloadFileError // 30030 文件服务器访问失败
|
||||
ErrApplicationNotExist // 应用不存在
|
||||
RedisSet // 设置redis错误
|
||||
AppDbError // app数据库错误
|
||||
AppCategoryDbError // appcategory数据库错误
|
||||
AppExistErr // app不能重复
|
||||
ApplicationLocExists // 30036 广告位置已存在
|
||||
)
|
||||
|
||||
const (
|
||||
QueryDataError CodeType = BusinessLevel + iota // 数据查询失败
|
||||
InsertDataError // 添加数据失败
|
||||
UpdateDataError // 更新数据失败
|
||||
DelDataError // 删除数据失败
|
||||
DataNotExist // 数据不存在
|
||||
DataEmpty
|
||||
)
|
||||
|
||||
func (c CodeType) Error() string {
|
||||
return fmt.Sprintf("code=%d,msg=%v,tip=%v", c, c.Msg(), c.Tip())
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package stdLog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// SrvErr 错误定义
|
||||
type SrvErr struct {
|
||||
Code CodeType `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Cause string `json:"-"`
|
||||
}
|
||||
|
||||
// New create an error
|
||||
func New(code CodeType, msg string, cause string) *SrvErr {
|
||||
return &SrvErr{
|
||||
Code: code,
|
||||
Msg: msg,
|
||||
Cause: cause,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SrvErr) Error() string {
|
||||
if s == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("code = %d, msg = %s ,case = %v", s.Code, s.Msg, s.Cause)
|
||||
}
|
||||
|
||||
func (c CodeType) Msg() string {
|
||||
msg, ok := codeAndMsg[c]
|
||||
if ok {
|
||||
return msg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c CodeType) Tip() string {
|
||||
tip, ok := codeAndTip[c]
|
||||
if ok {
|
||||
return tip
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package stdLog
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"reflect"
|
||||
"stat/common/log"
|
||||
"stat/common/utils"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
//ServeJSON 返回数据并处理多语言
|
||||
func ServerJSON(c *gin.Context, code CodeType, data interface{}) {
|
||||
if code != Success { //发生错误,记录日志
|
||||
var version, sysType, devType string
|
||||
admin, _ := utils.GetAdmin(c)
|
||||
|
||||
log.Warn("Error:",
|
||||
log.Any("admin", admin),
|
||||
log.Any("IP", c.ClientIP()),
|
||||
log.Any("Version", version),
|
||||
log.Any("SysType", sysType),
|
||||
log.Any("DevType", devType),
|
||||
log.Any("Router", c.Request.RequestURI),
|
||||
log.Any("PID", os.Getpid()),
|
||||
log.Any("Error", data),
|
||||
log.Any("Code", code))
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": code,
|
||||
"msg": code.Msg(),
|
||||
"tip": code.Tip(),
|
||||
"data": data,
|
||||
"time": time.Now().UTC().Format("2006-01-02T15:04:05.000Z"),
|
||||
})
|
||||
} else {
|
||||
if data != nil {
|
||||
t := reflect.TypeOf(data)
|
||||
if !(t.Kind() == reflect.Map || t.Kind() == reflect.Struct) {
|
||||
//log.Warn("[===TypeError===] Return Data Type error is not struct", log.Any("path", c.Request.URL.Path))
|
||||
}
|
||||
}
|
||||
// 返回数据加密
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": code,
|
||||
"msg": code.Msg(),
|
||||
"tip": code.Tip(),
|
||||
"data": data,
|
||||
"time": time.Now().UTC().Format("2006-01-02T15:04:05.000Z"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//GetUID 获取用户uid
|
||||
//oauth.Auth执行后uid有效
|
||||
func GetUID(ctx *gin.Context) (uid uint64, err error) {
|
||||
val, exists := ctx.Get("USER_ID")
|
||||
if !exists {
|
||||
err = errors.New("userId not exist")
|
||||
return
|
||||
}
|
||||
uid, ok := val.(uint64)
|
||||
if !ok {
|
||||
return 0, errors.New("userId type error")
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package stdLog
|
||||
|
||||
var codeAndMsg = map[CodeType]string{
|
||||
Success: "success",
|
||||
Failure: "Failed",
|
||||
ParamError: "Param error",
|
||||
AdminAccountNotExists: "Account is not exists",
|
||||
AdminPasswordError: "Password error",
|
||||
AdminGetAuthorityFailed: "Get authority failed",
|
||||
NoToken: "no token",
|
||||
ParamDeductionRatioError: "invalid deduction ratio",
|
||||
ChannelNotExists: "channel not exists",
|
||||
AgentAccountNotExists: "Account is not exists",
|
||||
AgentPasswordError: "Password error",
|
||||
AuthorityRoleExists: "authority role is existed",
|
||||
AgentHasLocked: "agent has been locked",
|
||||
DisableWithdraw: "trader disabled",
|
||||
NotSupportWithdraw: "not support withdraw",
|
||||
AccountExists: "account is exists",
|
||||
InvalidPhoneNumber: "Invalid phone number",
|
||||
AgentAccountBanned: "account is banned",
|
||||
GoogleAuthFailed: "google two step auth failed",
|
||||
AdvertiseLocExists: "advertise location exists",
|
||||
WithdrawBalanceLess: "balance not enough",
|
||||
WithdrawMobileError: "mobile is different",
|
||||
VerifySmsCodeFailed: "sms code is not correct",
|
||||
WithdrawFailed: "withdraw failed",
|
||||
AdvertiseInvalid: "invalid advertisement",
|
||||
AppInfoError: "application info error",
|
||||
InvalidPrice: "invliad price",
|
||||
ChannelExists: "channel exists",
|
||||
QueryDataError: "query data error",
|
||||
UpdateDataError: "update data error",
|
||||
DelDataError: "delete data error",
|
||||
DataNotExist: "data does not exist",
|
||||
ActivityNoExistErr: "activity does not exist",
|
||||
LotteryNumReachedTheLimit: "lottery num reached the limit",
|
||||
LotteryCodeIsInvalid: "lottery code is invalid",
|
||||
LotteryCodeIsUsed: "lottery code is used",
|
||||
ApplicationLocExists: "application location exists",
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package stdLog
|
||||
|
||||
var codeAndTip = map[CodeType]string{
|
||||
Success: "成功",
|
||||
Failure: "失败",
|
||||
ParamError: "参数错误",
|
||||
AdminAccountNotExists: "该账号不存在",
|
||||
AdminPasswordError: "密码错误",
|
||||
AdminGetAuthorityFailed: "获取权限失败",
|
||||
NoToken: "没有token",
|
||||
ParamDeductionRatioError: "无效的扣量值",
|
||||
ChannelNotExists: "渠道不存在",
|
||||
AgentAccountNotExists: "该账号不存在",
|
||||
AgentPasswordError: "密码错误",
|
||||
AuthorityRoleExists: "该权限角色已存在",
|
||||
AgentHasLocked: "禁止登陆",
|
||||
DisableWithdraw: "禁止交易",
|
||||
NotSupportWithdraw: "不支持提现",
|
||||
AccountExists: "账号已存在",
|
||||
InvalidPhoneNumber: "手机号码无效",
|
||||
AgentAccountBanned: "该账号已禁用",
|
||||
GoogleAuthFailed: "两步验证失败",
|
||||
AdvertiseLocExists: "该广告位置已经存在,请重新输入位置id",
|
||||
WithdrawBalanceLess: "余额不足,请确认!",
|
||||
WithdrawMobileError: "手机号码不一致,请确认!",
|
||||
VerifySmsCodeFailed: "验证码不正确,请确认!",
|
||||
WithdrawFailed: "提现失败!",
|
||||
AdvertiseInvalid: "无效的广告",
|
||||
AppInfoError: "应用信息不正确",
|
||||
InvalidPrice: "无效的金额",
|
||||
ChannelExists: "渠道已存在",
|
||||
QueryDataError: "数据查询失败",
|
||||
UpdateDataError: "更新数据失败",
|
||||
DelDataError: "删除数据失败",
|
||||
DataNotExist: "数据不存在",
|
||||
ActivityNoExistErr: "活动不存在",
|
||||
LotteryNumReachedTheLimit: "今日抽奖次数达到限制",
|
||||
LotteryCodeIsInvalid: "兑换码无效",
|
||||
LotteryCodeIsUsed: "兑换码已使用",
|
||||
ApplicationLocExists: "该广告位置已经存在,请重新输入位置id",
|
||||
}
|
||||
Reference in New Issue
Block a user