hsun 1 tydzień temu
rodzic
commit
a78781da21

+ 13 - 5
controllers/user.go

@@ -150,12 +150,20 @@ func (this *UserController) GetVerifyCode() {
 		return
 	}
 
-	phoneVerifyCacheSvc := &services.VerifyCacheIncrService{}
-	if e := phoneVerifyCacheSvc.VerifyCacheIncr(phoneKey, 15*int(time.Minute.Seconds())); e != nil {
-		utils.FileLog.Info(fmt.Sprintf("验证码手机号临时缓存失败, %v", e))
+	// 设置缓存
+	if go_redis.IsExist(phoneKey) {
+		if e := go_redis.Incr(phoneKey); e != nil {
+			utils.FileLog.Info(fmt.Sprintf("验证码手机号临时缓存失败, %v", e))
+		}
+	} else {
+		go_redis.Set(phoneKey, 1, 15*time.Minute)
 	}
-	if e := phoneVerifyCacheSvc.VerifyCacheIncr(phoneCountKey, int(utils.SetKeyExpireToday().Seconds())); e != nil {
-		utils.FileLog.Info(fmt.Sprintf("验证码手机号当日缓存失败, %v", e))
+	if go_redis.IsExist(phoneCountKey) {
+		if e := go_redis.Incr(phoneCountKey); e != nil {
+			utils.FileLog.Info(fmt.Sprintf("验证码手机号当日缓存失败, %v", e))
+		}
+	} else {
+		go_redis.Set(phoneCountKey, 1, utils.SetKeyExpireToday())
 	}
 
 	br.Ret = 200

+ 11 - 0
services/go_redis/redis.go

@@ -18,7 +18,18 @@ func IsExist(key string) (ok bool) {
 	if result > 0 {
 		ok = true
 	}
+	return
+}
 
+// Set 给key设置值,并设置过期时间
+func Set(key string, val interface{}, timeout time.Duration) (ok bool) {
+	result, err := utils.Redis.Set(context.TODO(), key, val, timeout).Result()
+	if err != nil {
+		return false
+	}
+	if result == "OK" {
+		ok = true
+	}
 	return
 }
 

+ 0 - 12
services/lua/verify_code.lua

@@ -1,12 +0,0 @@
--- 定义Lua脚本
-local key = KEYS[1]      -- 从KEYS数组获取要检查的Redis key
-local expireTime = tonumber(ARGV[1]) -- 从ARGV数组获取过期时间
-
--- 检查key是否存在并执行相应操作
-if redis.call('EXISTS', key) == 1 then
-    -- 存在:将key的值加一
-    return redis.call('INCR', key)
-else
-    -- 不存在:设置key的值设置为1
-    return redis.call('SET', key, 1, 'EX', expireTime) -- 'EX' 设置过期时间(秒)
-end

+ 0 - 17
services/verify_code.go

@@ -1,17 +0,0 @@
-package services
-
-import (
-	"context"
-	_ "embed"
-	"eta/eta_mini_api/utils"
-)
-
-//go:embed lua\verify_code.lua
-var luaVerifyCacheIncr string
-
-type VerifyCacheIncrService struct {
-}
-
-func (v *VerifyCacheIncrService) VerifyCacheIncr(key string, expireInSeconds int) error {
-	return utils.Redis.Eval(context.Background(), luaVerifyCacheIncr, []string{key}, expireInSeconds).Err()
-}