Skip to content

Commit 954b4fa

Browse files
authored
Merge pull request #108 from zjutjh/dev
Dev
2 parents bf1e434 + a514444 commit 954b4fa

File tree

10 files changed

+41
-115
lines changed

10 files changed

+41
-115
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.idea*
22
.DS_Store
3+
cspell.json
34
config.yaml
45
.vscode*
56
wejh-go

app/controllers/yxyController/electricityController/electricityController.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type SubscribeLowBatteryAlertReq struct {
2525
Threshold int `json:"threshold"`
2626
}
2727

28+
// GetBalance 获取电费余额
2829
func GetBalance(c *gin.Context) {
2930
var postForm CampusForm
3031
err := c.ShouldBindQuery(&postForm)
@@ -41,15 +42,10 @@ func GetBalance(c *gin.Context) {
4142
apiException.AbortWithException(c, apiException.NotBindYxy, nil)
4243
return
4344
}
44-
token, err := yxyServices.GetElecAuthToken(user.YxyUid)
45-
if err != nil {
46-
apiException.AbortWithException(c, apiException.ServerError, err)
47-
return
48-
}
4945
if postForm.Campus != "mgs" {
5046
postForm.Campus = "zhpf"
5147
}
52-
balance, err := yxyServices.ElectricityBalance(*token, postForm.Campus)
48+
balance, err := yxyServices.ElectricityBalance(user.YxyUid, postForm.Campus)
5349
if errors.Is(err, apiException.NotBindCard) {
5450
_ = yxyServices.Unbind(user.ID, user.YxyUid, true)
5551
apiException.AbortWithError(c, err)
@@ -64,6 +60,7 @@ func GetBalance(c *gin.Context) {
6460
utils.JsonSuccessResponse(c, balance)
6561
}
6662

63+
// GetRechargeRecords 获取充值记录
6764
func GetRechargeRecords(c *gin.Context) {
6865
var postForm recordForm
6966
err := c.ShouldBindJSON(&postForm)
@@ -80,15 +77,10 @@ func GetRechargeRecords(c *gin.Context) {
8077
apiException.AbortWithException(c, apiException.NotBindYxy, nil)
8178
return
8279
}
83-
token, err := yxyServices.GetElecAuthToken(user.YxyUid)
84-
if err != nil {
85-
apiException.AbortWithException(c, apiException.ServerError, err)
86-
return
87-
}
8880
if postForm.Campus != "mgs" {
8981
postForm.Campus = "zhpf"
9082
}
91-
roomStrConcat, err := yxyServices.GetElecRoomStrConcat(*token, postForm.Campus, user.YxyUid)
83+
roomStrConcat, err := yxyServices.GetElecRoomStrConcat(user.YxyUid, postForm.Campus)
9284
if errors.Is(err, apiException.NotBindCard) {
9385
_ = yxyServices.Unbind(user.ID, user.YxyUid, true)
9486
apiException.AbortWithError(c, err)
@@ -100,7 +92,7 @@ func GetRechargeRecords(c *gin.Context) {
10092
apiException.AbortWithException(c, apiException.ServerError, err)
10193
return
10294
}
103-
records, err := yxyServices.ElectricityRechargeRecords(*token, postForm.Campus, postForm.Page, *roomStrConcat)
95+
records, err := yxyServices.ElectricityRechargeRecords(user.YxyUid, postForm.Campus, postForm.Page, *roomStrConcat)
10496
if errors.Is(err, apiException.CampusMismatch) {
10597
apiException.AbortWithError(c, err)
10698
return
@@ -111,10 +103,10 @@ func GetRechargeRecords(c *gin.Context) {
111103
utils.JsonSuccessResponse(c, records.List)
112104
}
113105

106+
// GetConsumptionRecords 获取电费使用记录
114107
func GetConsumptionRecords(c *gin.Context) {
115108
var postForm CampusForm
116-
err := c.ShouldBindQuery(&postForm)
117-
if err != nil {
109+
if err := c.ShouldBindQuery(&postForm); err != nil {
118110
apiException.AbortWithException(c, apiException.ParamError, err)
119111
return
120112
}
@@ -127,15 +119,10 @@ func GetConsumptionRecords(c *gin.Context) {
127119
apiException.AbortWithException(c, apiException.NotBindYxy, nil)
128120
return
129121
}
130-
token, err := yxyServices.GetElecAuthToken(user.YxyUid)
131-
if err != nil {
132-
apiException.AbortWithException(c, apiException.ServerError, err)
133-
return
134-
}
135122
if postForm.Campus != "mgs" {
136123
postForm.Campus = "zhpf"
137124
}
138-
roomStrConcat, err := yxyServices.GetElecRoomStrConcat(*token, postForm.Campus, user.YxyUid)
125+
roomStrConcat, err := yxyServices.GetElecRoomStrConcat(user.YxyUid, postForm.Campus)
139126
if errors.Is(err, apiException.NotBindCard) {
140127
_ = yxyServices.Unbind(user.ID, user.YxyUid, true)
141128
apiException.AbortWithError(c, err)
@@ -147,7 +134,7 @@ func GetConsumptionRecords(c *gin.Context) {
147134
apiException.AbortWithException(c, apiException.ServerError, err)
148135
return
149136
}
150-
records, err := yxyServices.GetElecConsumptionRecords(*token, postForm.Campus, *roomStrConcat)
137+
records, err := yxyServices.GetElecConsumptionRecords(user.YxyUid, postForm.Campus, *roomStrConcat)
151138
if errors.Is(err, apiException.CampusMismatch) {
152139
apiException.AbortWithError(c, err)
153140
return

app/services/yxyServices/cacheService.go

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,22 @@ package yxyServices
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"time"
78
r "wejh-go/config/redis"
89

9-
"golang.org/x/sync/singleflight"
10-
1110
"github.com/go-redis/redis/v8"
1211
)
1312

1413
var (
1514
ctx = context.Background()
16-
g singleflight.Group
1715
)
1816

19-
func GetElecRoomStrConcat(token, campus, yxyUid string) (*string, error) {
17+
func GetElecRoomStrConcat(yxyUid, campus string) (*string, error) {
2018
cacheKey := "elec:room_str_concat:" + campus + ":" + yxyUid
2119
cachedRoomStrConcat, err := r.RedisClient.Get(ctx, cacheKey).Result()
22-
if err == redis.Nil {
23-
balance, err := ElectricityBalance(token, campus)
20+
if errors.Is(err, redis.Nil) {
21+
balance, err := ElectricityBalance(yxyUid, campus)
2422
if err != nil {
2523
return nil, err
2624
}
@@ -35,37 +33,11 @@ func GetElecRoomStrConcat(token, campus, yxyUid string) (*string, error) {
3533
return &cachedRoomStrConcat, nil
3634
}
3735

38-
func GetElecAuthToken(yxyUid string) (*string, error) {
39-
cacheKey := "elec:auth_token:" + yxyUid
40-
cachedToken, err := r.RedisClient.Get(ctx, cacheKey).Result()
41-
if err == redis.Nil {
42-
// 使用 singleflight 防止缓存击穿
43-
token, err, _ := g.Do(cacheKey, func() (interface{}, error) {
44-
t, e := Auth(yxyUid)
45-
if e != nil {
46-
return nil, e
47-
}
48-
e = r.RedisClient.Set(ctx, cacheKey, *t, 7*24*time.Hour).Err()
49-
if e != nil {
50-
return nil, e
51-
}
52-
return t, nil
53-
})
54-
if err != nil {
55-
return nil, err
56-
}
57-
return token.(*string), nil
58-
} else if err != nil {
59-
return nil, err
60-
}
61-
return &cachedToken, nil
62-
}
63-
64-
func GetElecConsumptionRecords(token, campus, roomStrConcat string) (*EleConsumptionRecords, error) {
36+
func GetElecConsumptionRecords(yxyUid, campus, roomStrConcat string) (*EleConsumptionRecords, error) {
6537
cacheKey := "elec:consumption_records:" + roomStrConcat
6638
cachedRecords, err := r.RedisClient.Get(ctx, cacheKey).Result()
67-
if err == redis.Nil {
68-
records, err := ElectricityConsumptionRecords(token, campus, roomStrConcat)
39+
if errors.Is(err, redis.Nil) {
40+
records, err := ElectricityConsumptionRecords(yxyUid, campus, roomStrConcat)
6941
if err != nil {
7042
return nil, err
7143
}
@@ -91,17 +63,3 @@ func GetElecConsumptionRecords(token, campus, roomStrConcat string) (*EleConsump
9163
}
9264
return &records, nil
9365
}
94-
95-
func SetCardAuthToken(yxyUid, token string) error {
96-
cacheKey := "card:auth_token:" + yxyUid
97-
return r.RedisClient.Set(ctx, cacheKey, token, 7*24*time.Hour).Err()
98-
}
99-
100-
func GetCardAuthToken(yxyUid string) (*string, error) {
101-
cacheKey := "card:auth_token:" + yxyUid
102-
cachedToken, err := r.RedisClient.Get(ctx, cacheKey).Result()
103-
if err == redis.Nil {
104-
return nil, err
105-
}
106-
return &cachedToken, nil
107-
}

app/services/yxyServices/electricityService.go

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import (
88
"github.com/mitchellh/mapstructure"
99
)
1010

11-
type AuthResp struct {
12-
Token string `json:"token" mapstructure:"token"`
13-
}
14-
1511
type ElecBalance struct {
1612
DisplayRoomName string `json:"display_room_name" mapstructure:"display_room_name"`
1713
RoomStrConcat string `json:"room_str_concat" mapstructure:"room_str_concat"`
@@ -32,34 +28,13 @@ type EleConsumptionRecords struct {
3228
} `json:"list" mapstructure:"list"`
3329
}
3430

35-
func Auth(uid string) (*string, error) {
36-
params := url.Values{}
37-
Url, err := url.Parse(string(yxyApi.Auth))
38-
if err != nil {
39-
return nil, err
40-
}
41-
params.Set("uid", uid)
42-
Url.RawQuery = params.Encode()
43-
urlPath := Url.String()
44-
resp, err := FetchHandleOfGet(yxyApi.YxyApi(urlPath))
45-
if err != nil {
46-
return nil, err
47-
}
48-
var data AuthResp
49-
err = mapstructure.Decode(resp.Data, &data)
50-
if err != nil {
51-
return nil, err
52-
}
53-
return &data.Token, nil
54-
}
55-
56-
func ElectricityBalance(token, campus string) (*ElecBalance, error) {
31+
func ElectricityBalance(uid, campus string) (*ElecBalance, error) {
5732
params := url.Values{}
5833
Url, err := url.Parse(string(yxyApi.ElectricityBalance))
5934
if err != nil {
6035
return nil, err
6136
}
62-
params.Set("token", token)
37+
params.Set("uid", uid)
6338
params.Set("campus", campus)
6439
Url.RawQuery = params.Encode()
6540
urlPath := Url.String()
@@ -82,13 +57,13 @@ func ElectricityBalance(token, campus string) (*ElecBalance, error) {
8257
return &data, nil
8358
}
8459

85-
func ElectricityRechargeRecords(token, campus, page, roomStrConcat string) (*RechargeRecords, error) {
60+
func ElectricityRechargeRecords(uid, campus, page, roomStrConcat string) (*RechargeRecords, error) {
8661
params := url.Values{}
8762
Url, err := url.Parse(string(yxyApi.RechargeRecords))
8863
if err != nil {
8964
return nil, err
9065
}
91-
params.Set("token", token)
66+
params.Set("uid", uid)
9267
params.Set("campus", campus)
9368
params.Set("page", page)
9469
params.Set("room_str_concat", roomStrConcat)
@@ -111,13 +86,13 @@ func ElectricityRechargeRecords(token, campus, page, roomStrConcat string) (*Rec
11186
return &data, nil
11287
}
11388

114-
func ElectricityConsumptionRecords(token, campus, roomStrConcat string) (*EleConsumptionRecords, error) {
89+
func ElectricityConsumptionRecords(uid, campus, roomStrConcat string) (*EleConsumptionRecords, error) {
11590
params := url.Values{}
11691
Url, err := url.Parse(string(yxyApi.ElectricityConsumption))
11792
if err != nil {
11893
return nil, err
11994
}
120-
params.Set("token", token)
95+
params.Set("uid", uid)
12196
params.Set("campus", campus)
12297
params.Set("room_str_concat", roomStrConcat)
12398
Url.RawQuery = params.Encode()

app/utils/circuitBreaker/circuitBreaker.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ type CircuitBreaker struct {
1313
SnapShot *sync.Map
1414
}
1515

16-
func init() {
16+
func Init() {
17+
18+
Probe = NewLiveNessProbe(cbConfig.GetLiveNessConfig())
19+
1720
lb := LoadBalance{
1821
zfLB: &randomLB{},
1922
oauthLB: &randomLB{},

app/utils/circuitBreaker/liveNessProbe.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ import (
1818

1919
var Probe *LiveNessProbe
2020

21-
func init() {
22-
Probe = NewLiveNessProbe(cbConfig.GetLiveNessConfig())
23-
}
24-
2521
type LiveNessProbe struct {
2622
sync.Mutex
2723
ApiMap map[string]funnelApi.LoginType

config/config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package config
22

33
import (
4-
"github.com/spf13/viper"
54
"log"
5+
6+
"github.com/spf13/viper"
67
)
78

89
var Config = viper.New()

config/redis/redis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type redisConfig struct {
1212
var RedisClient *redis.Client
1313
var RedisInfo redisConfig
1414

15-
func init() {
15+
func Init() {
1616
info := getConfig()
1717

1818
RedisClient = redis.NewClient(&redis.Options{

config/wechat/wechat.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@ const (
1717

1818
var MiniProgram *miniprogram.MiniProgram
1919

20-
func init() {
20+
func Init() {
2121
config := getConfigs()
2222

2323
wc := wechat.NewWechat()
2424
var wcCache cache.Cache
2525
switch config.Driver {
2626
case string(Redis):
2727
wcCache = setRedis(wcCache)
28-
break
2928
case string(Memory):
3029
wcCache = cache.NewMemory()
31-
break
3230
default:
3331
zap.L().Fatal("Config Error")
3432
}

main.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ package main
33
import (
44
"context"
55
"errors"
6-
"github.com/gin-contrib/cors"
7-
"github.com/gin-gonic/gin"
8-
"go.uber.org/zap"
96
"log"
107
"net/http"
118
"os/signal"
@@ -17,15 +14,25 @@ import (
1714
"wejh-go/config/config"
1815
"wejh-go/config/database"
1916
"wejh-go/config/logger"
17+
"wejh-go/config/redis"
2018
"wejh-go/config/router"
2119
"wejh-go/config/session"
20+
"wejh-go/config/wechat"
21+
22+
"github.com/gin-contrib/cors"
23+
"github.com/gin-gonic/gin"
24+
"go.uber.org/zap"
2225
)
2326

2427
func main() {
2528
if err := logger.Init(); err != nil {
2629
log.Fatal(err.Error())
2730
}
31+
redis.Init()
2832
database.Init()
33+
circuitBreaker.Init()
34+
wechat.Init()
35+
2936
r := gin.Default()
3037
r.Use(cors.Default())
3138
r.Use(midwares.ErrHandler())

0 commit comments

Comments
 (0)