2022-11-25 23:22:44 +08:00
|
|
|
|
// Package simple
|
|
|
|
|
// @Link https://github.com/bufanyun/hotgo
|
2023-02-23 17:53:04 +08:00
|
|
|
|
// @Copyright Copyright (c) 2023 HotGo CLI
|
2022-11-25 23:22:44 +08:00
|
|
|
|
// @Author Ms <133814250@qq.com>
|
|
|
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
|
|
|
|
package simple
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2023-02-08 20:29:34 +08:00
|
|
|
|
"github.com/gogf/gf/v2/crypto/gmd5"
|
|
|
|
|
"github.com/gogf/gf/v2/encoding/gbase64"
|
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2022-11-25 23:22:44 +08:00
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
2023-05-10 23:54:50 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/grpool"
|
2022-11-25 23:22:44 +08:00
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2023-02-08 20:29:34 +08:00
|
|
|
|
"hotgo/internal/consts"
|
|
|
|
|
"hotgo/utility/encrypt"
|
2022-11-25 23:22:44 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-06-09 19:13:26 +08:00
|
|
|
|
// AppName 应用名称
|
|
|
|
|
func AppName(ctx context.Context) string {
|
|
|
|
|
return g.Cfg().MustGet(ctx, "appName", "hotgo").String()
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 20:24:13 +08:00
|
|
|
|
// FilterMaskDemo 过滤演示环境下的配置隐藏字段
|
|
|
|
|
func FilterMaskDemo(ctx context.Context, src g.Map) g.Map {
|
2023-05-30 12:09:40 +08:00
|
|
|
|
if src == nil {
|
2023-05-29 20:24:13 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !g.Cfg().MustGet(ctx, "hotgo.isDemo", false).Bool() {
|
|
|
|
|
return src
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 12:09:40 +08:00
|
|
|
|
for k := range src {
|
2023-05-29 20:24:13 +08:00
|
|
|
|
if _, ok := consts.ConfigMaskDemoField[k]; ok {
|
|
|
|
|
src[k] = consts.DemoTips
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return src
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 18:37:40 +08:00
|
|
|
|
// DecryptText 解密文本
|
|
|
|
|
func DecryptText(text string) (string, error) {
|
|
|
|
|
str, err := gbase64.Decode([]byte(text))
|
2023-02-08 20:29:34 +08:00
|
|
|
|
if err != nil {
|
2023-05-15 18:37:40 +08:00
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
str, err = encrypt.AesECBDecrypt(str, consts.RequestEncryptKey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
2023-02-08 20:29:34 +08:00
|
|
|
|
}
|
2023-05-15 18:37:40 +08:00
|
|
|
|
return string(str), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CheckPassword 检查密码
|
|
|
|
|
func CheckPassword(input, salt, hash string) (err error) {
|
|
|
|
|
// 解密密码
|
|
|
|
|
password, err := DecryptText(input)
|
2023-02-08 20:29:34 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 18:37:40 +08:00
|
|
|
|
if hash != gmd5.MustEncryptString(password+salt) {
|
2023-02-08 20:29:34 +08:00
|
|
|
|
err = gerror.New("用户密码不正确")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 14:18:22 +08:00
|
|
|
|
// SafeGo 安全的调用协程,遇到错误时输出错误日志而不是抛出panic
|
2022-11-25 23:22:44 +08:00
|
|
|
|
func SafeGo(ctx context.Context, f func(ctx context.Context), level ...interface{}) {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
var newLevel = glog.LEVEL_ERRO
|
|
|
|
|
if len(level) > 0 {
|
|
|
|
|
newLevel = gconv.Int(level[0])
|
|
|
|
|
}
|
2022-11-25 23:22:44 +08:00
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
err := grpool.AddWithRecover(ctx, func(ctx context.Context) {
|
2022-11-25 23:22:44 +08:00
|
|
|
|
f(ctx)
|
2023-05-10 23:54:50 +08:00
|
|
|
|
}, func(ctx context.Context, err error) {
|
|
|
|
|
Logf(newLevel, ctx, "SafeGo exec failed:%+v", err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
Logf(newLevel, ctx, "SafeGo AddWithRecover err:%+v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-11-25 23:22:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Logf(level int, ctx context.Context, format string, v ...interface{}) {
|
|
|
|
|
switch level {
|
|
|
|
|
case glog.LEVEL_DEBU:
|
2023-05-30 12:09:40 +08:00
|
|
|
|
g.Log().Debugf(ctx, format, v...)
|
2022-11-25 23:22:44 +08:00
|
|
|
|
case glog.LEVEL_INFO:
|
2023-05-30 12:09:40 +08:00
|
|
|
|
g.Log().Infof(ctx, format, v...)
|
2022-11-25 23:22:44 +08:00
|
|
|
|
case glog.LEVEL_NOTI:
|
2023-05-30 12:09:40 +08:00
|
|
|
|
g.Log().Noticef(ctx, format, v...)
|
2022-11-25 23:22:44 +08:00
|
|
|
|
case glog.LEVEL_WARN:
|
2023-05-30 12:09:40 +08:00
|
|
|
|
g.Log().Warningf(ctx, format, v...)
|
2022-11-25 23:22:44 +08:00
|
|
|
|
case glog.LEVEL_ERRO:
|
2023-05-30 12:09:40 +08:00
|
|
|
|
g.Log().Errorf(ctx, format, v...)
|
2022-11-25 23:22:44 +08:00
|
|
|
|
case glog.LEVEL_CRIT:
|
2023-05-30 12:09:40 +08:00
|
|
|
|
g.Log().Criticalf(ctx, format, v...)
|
2022-11-25 23:22:44 +08:00
|
|
|
|
case glog.LEVEL_PANI:
|
2023-05-30 12:09:40 +08:00
|
|
|
|
g.Log().Panicf(ctx, format, v...)
|
2022-11-25 23:22:44 +08:00
|
|
|
|
case glog.LEVEL_FATA:
|
2023-05-30 12:09:40 +08:00
|
|
|
|
g.Log().Fatalf(ctx, format, v...)
|
2022-11-25 23:22:44 +08:00
|
|
|
|
default:
|
2023-05-30 12:09:40 +08:00
|
|
|
|
g.Log().Errorf(ctx, format, v...)
|
2022-11-25 23:22:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|