Skip to content

Commit fed6ee2

Browse files
committed
add: 添加一些辅助函数
1 parent ad6840a commit fed6ee2

4 files changed

Lines changed: 53 additions & 17 deletions

File tree

app/func.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func StringToHump(s string) string {
115115
return string(data[:])
116116
}
117117

118+
// GetRoot 获取项目根目录,在一些单元测试中可以便捷获取
118119
func GetRoot() string {
119120
return app.GetBean("config").(app.GetRoot).GetRoot()
120121
}

app/http/context.go

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"github.com/sirupsen/logrus"
77
"net/http"
88
"strings"
9+
"time"
910
)
1011

12+
const UserKey = "user"
1113
const UserIdKey = "user_id"
1214

1315
// UserModel 不能赋值指针
@@ -28,33 +30,33 @@ type Ctx struct {
2830
UserInfo interface{}
2931
}
3032

31-
func (receiver Ctx) Success(data interface{}) {
33+
func (receiver *Ctx) Success(data interface{}) {
3234
receiver.JSON(http.StatusOK, map[string]interface{}{
3335
"data": data,
3436
"code": 0,
3537
"msg": "",
3638
})
3739
}
3840

39-
func (receiver Ctx) Fail(err error) {
41+
func (receiver *Ctx) Fail(err error) {
4042
receiver.JSON(http.StatusOK, map[string]interface{}{
4143
"code": 1,
4244
"msg": err.Error(),
4345
})
4446
}
4547

46-
func (receiver Ctx) Gin() *gin.Context {
48+
func (receiver *Ctx) Gin() *gin.Context {
4749
return receiver.Context
4850
}
4951

50-
func (receiver Ctx) User() interface{} {
52+
func (receiver *Ctx) User() interface{} {
5153
if receiver.UserInfo == nil {
5254
receiver.InitUser()
5355
}
5456
return receiver.UserInfo
5557
}
5658

57-
func (receiver Ctx) Id() uint64 {
59+
func (receiver *Ctx) Id() uint64 {
5860
u, ok := receiver.Context.Get(UserIdKey)
5961
if !ok {
6062
logrus.Fatal("id 不存在, todo Context.Set(UserIdKey, Uid)")
@@ -63,15 +65,15 @@ func (receiver Ctx) Id() uint64 {
6365
return u.(uint64)
6466
}
6567

66-
func (receiver Ctx) IdStr() string {
68+
func (receiver *Ctx) IdStr() string {
6769
u, ok := receiver.Context.Get(UserIdKey)
6870
if !ok {
6971
return ""
7072
}
7173
return u.(string)
7274
}
7375

74-
func (receiver Ctx) Token() string {
76+
func (receiver *Ctx) Token() string {
7577
tokenString := receiver.Context.GetHeader("Authorization")
7678

7779
if strings.HasPrefix(tokenString, "Bearer ") {
@@ -80,9 +82,15 @@ func (receiver Ctx) Token() string {
8082
return tokenString
8183
}
8284

83-
func (receiver Ctx) InitUser() {
85+
func (receiver *Ctx) InitUser() {
8486
if receiver.UserInfo == nil {
85-
uid, ok := receiver.Context.Get(UserIdKey)
87+
u, ok := receiver.Context.Get(UserKey)
88+
if ok {
89+
receiver.UserInfo = u
90+
return
91+
}
92+
93+
uid, ok := receiver.Get(UserIdKey)
8694
if ok {
8795
user := UserModel
8896
database.DB().Model(UserModel).First(&user, uid)
@@ -100,4 +108,30 @@ type Context interface {
100108
Id() uint64
101109
IdStr() string
102110
User() interface{}
111+
112+
// 下面是补充 gin.Context 的方法
113+
114+
JSON(code int, obj interface{})
115+
String(code int, format string, values ...interface{})
116+
Param(key string) string
117+
Query(key string) string
118+
PostForm(key string) string
119+
BindJSON(obj interface{}) error
120+
Status(code int)
121+
Set(key string, value interface{})
122+
Get(key string) (value interface{}, exists bool)
123+
AbortWithStatus(code int)
124+
Next()
125+
126+
GetString(key string) string
127+
GetBool(key string) bool
128+
GetInt(key string) int
129+
GetInt64(key string) int64
130+
GetFloat64(key string) float64
131+
GetTime(key string) time.Time
132+
GetDuration(key string) time.Duration
133+
GetStringSlice(key string) []string
134+
GetStringMap(key string) map[string]interface{}
135+
GetStringMapString(key string) map[string]string
136+
GetStringMapStringSlice(key string) map[string][]string
103137
}

bootstrap/services/http_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ func (receiver *HttpServer) SetPort(port int) {
4646
func (receiver *HttpServer) RunListener() {
4747
err := receiver.GetEngine().Run(":" + receiver.port)
4848
if err != nil {
49-
logrus.WithFields(logrus.Fields{"port": receiver.port}).Error("http发送错误")
49+
logrus.WithFields(logrus.Fields{"port": receiver.port}).Error("http server 启动发生错误")
5050
}
5151
}

bootstrap/services/redis_server.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package services
22

33
import (
44
"context"
5+
"errors"
56
"github.com/go-redis/redis/v8"
67
log "github.com/sirupsen/logrus"
78
"time"
@@ -22,7 +23,7 @@ func (r Redis) Get(key string) *redis.StringCmd {
2223
func (r Redis) GetString(key string) (string, bool) {
2324
cmd := r.Client.Get(context.Background(), key)
2425
err := cmd.Err()
25-
if err == redis.Nil {
26+
if errors.Is(err, redis.Nil) {
2627
return "", false
2728
} else if err != nil {
2829
log.Error(err)
@@ -34,7 +35,7 @@ func (r Redis) GetString(key string) (string, bool) {
3435
func (r Redis) GetInt(key string) (int, bool) {
3536
i, err := r.Client.Get(context.Background(), key).Int()
3637
if err != nil {
37-
if err == redis.Nil {
38+
if errors.Is(err, redis.Nil) {
3839
return 0, false
3940
}
4041
log.Errorf("GetInt %v", err)
@@ -45,7 +46,7 @@ func (r Redis) GetInt(key string) (int, bool) {
4546
func (r Redis) GetInt64(key string) (int64, bool) {
4647
i, err := r.Client.Get(context.Background(), key).Int64()
4748
if err != nil {
48-
if err == redis.Nil {
49+
if errors.Is(err, redis.Nil) {
4950
return 0, false
5051
}
5152
log.Errorf("GetInt64 %v", err)
@@ -56,7 +57,7 @@ func (r Redis) GetInt64(key string) (int64, bool) {
5657
func (r Redis) GetFloat32(key string) (float32, bool) {
5758
i, err := r.Client.Get(context.Background(), key).Float32()
5859
if err != nil {
59-
if err == redis.Nil {
60+
if errors.Is(err, redis.Nil) {
6061
return 0, false
6162
}
6263
log.Errorf("GetFloat32 %v", err)
@@ -67,7 +68,7 @@ func (r Redis) GetFloat32(key string) (float32, bool) {
6768
func (r Redis) GetFloat64(key string) (float64, bool) {
6869
i, err := r.Client.Get(context.Background(), key).Float64()
6970
if err != nil {
70-
if err == redis.Nil {
71+
if errors.Is(err, redis.Nil) {
7172
return 0, false
7273
}
7374
log.Errorf("GetFloat32 %v", err)
@@ -78,7 +79,7 @@ func (r Redis) GetFloat64(key string) (float64, bool) {
7879
func (r Redis) GetBool(key string) (bool, bool) {
7980
i, err := r.Client.Get(context.Background(), key).Bool()
8081
if err != nil {
81-
if err == redis.Nil {
82+
if errors.Is(err, redis.Nil) {
8283
return false, false
8384
}
8485
log.Errorf("GetFloat32 %v", err)
@@ -89,7 +90,7 @@ func (r Redis) GetBool(key string) (bool, bool) {
8990
func (r Redis) Incr(key string) (int64, bool) {
9091
cmd := r.Client.Incr(context.Background(), key)
9192
err := cmd.Err()
92-
if err == redis.Nil {
93+
if errors.Is(err, redis.Nil) {
9394
return 0, true
9495
} else if err != nil {
9596
log.Error(err)

0 commit comments

Comments
 (0)