Browse Source

修改兑换码存储方式

huangyuhao 1 year ago
parent
commit
99bafe7247
2 changed files with 53 additions and 34 deletions
  1. 48 26
      models/exchangecode.go
  2. 5 8
      views/exchangecode.tpl

+ 48 - 26
models/exchangecode.go

@@ -36,6 +36,11 @@ var exchangeCodeList ExchangeCodeInfoArray
36
 var ecMutex sync.Mutex
36
 var ecMutex sync.Mutex
37
 var MAIN_KEY = "exchange:code"
37
 var MAIN_KEY = "exchange:code"
38
 
38
 
39
+// 兑换码key
40
+func get_exchange_code_key(pcode string) string {
41
+	return fmt.Sprintf("%s:%s", MAIN_KEY, pcode)
42
+}
43
+
39
 // 初始化兑换码
44
 // 初始化兑换码
40
 func InitExchangeCodeInfo() {
45
 func InitExchangeCodeInfo() {
41
 	pool := utils.GetAccountRedisPool()
46
 	pool := utils.GetAccountRedisPool()
@@ -46,17 +51,26 @@ func InitExchangeCodeInfo() {
46
 	rd := pool.Get()
51
 	rd := pool.Get()
47
 	defer rd.Close()
52
 	defer rd.Close()
48
 
53
 
49
-	result, err := redis.Values(rd.Do("hgetall", MAIN_KEY))
50
-	if err != nil {
51
-		return
52
-	}
53
-	for i := 0; i < len(result)/2; i++ {
54
-
55
-		info := &ExchangeCodeInfo{}
56
-		_err := json.Unmarshal(result[i*2+1].([]byte), info)
57
-		if _err == nil {
58
-			exchangeCodeList = append(exchangeCodeList, info)
59
-		}
54
+	result, _ := redis.Values(rd.Do("smembers", MAIN_KEY))
55
+	for _, v := range result {
56
+		pcode := v.(string)
57
+		pKey := get_exchange_code_key(pcode)
58
+		awardTimes, _ := redis.Int64(rd.Do("hget", pKey, "awardTimes"))
59
+		creatTime, _ := redis.Int64(rd.Do("hget", pKey, "creatTime"))
60
+		modifyTime, _ := redis.Int64(rd.Do("hget", pKey, "modifyTime"))
61
+		expireTime, _ := redis.Int64(rd.Do("hget", pKey, "expireTime"))
62
+		items, _ := redis.String(rd.Do("hget", pKey, "items"))
63
+
64
+		var _items []*ExchangeItemsInfo
65
+		json.Unmarshal([]byte(items), &_items)
66
+		exchangeCodeList = append(exchangeCodeList, &ExchangeCodeInfo{
67
+			Pcode:      pcode,
68
+			AwardTimes: awardTimes,
69
+			CreateTime: creatTime,
70
+			ModifyTime: modifyTime,
71
+			ExpireTime: expireTime,
72
+			Items:      _items,
73
+		})
60
 	}
74
 	}
61
 
75
 
62
 	beego.Info("load exchange code info", len(exchangeCodeList))
76
 	beego.Info("load exchange code info", len(exchangeCodeList))
@@ -90,16 +104,20 @@ func AddExchangeCodeInfo(hf *ExchangeCodeInfo) error {
90
 	hf.CreateTime = time.Now().Unix()
104
 	hf.CreateTime = time.Now().Unix()
91
 	hf.ModifyTime = hf.CreateTime
105
 	hf.ModifyTime = hf.CreateTime
92
 
106
 
93
-	buff, err := json.Marshal(hf)
94
-	_, err = rd.Do("hset", MAIN_KEY, hf.Pcode, buff)
107
+	pKey := get_exchange_code_key(hf.Pcode)
108
+	rd.Do("hset", pKey, "awardTimes", hf.AwardTimes)
109
+	rd.Do("hset", pKey, "creatTime", hf.CreateTime)
110
+	rd.Do("hset", pKey, "modifyTime", hf.ModifyTime)
111
+	rd.Do("hset", pKey, "expireTime", hf.ExpireTime)
112
+	items, _ := json.Marshal(hf.Items)
113
+	rd.Do("hset", pKey, "items", items)
114
+	rd.Do("sadd", MAIN_KEY, hf.Pcode)
95
 
115
 
96
-	if err == nil {
97
-		ecMutex.Lock()
98
-		exchangeCodeList = append(exchangeCodeList, hf)
99
-		ecMutex.Unlock()
100
-	}
116
+	ecMutex.Lock()
117
+	exchangeCodeList = append(exchangeCodeList, hf)
118
+	ecMutex.Unlock()
101
 
119
 
102
-	return err
120
+	return nil
103
 }
121
 }
104
 
122
 
105
 // 修改兑换码
123
 // 修改兑换码
@@ -117,11 +135,13 @@ func UpdateExchangeCodeInfo(hf *ExchangeCodeInfo) error {
117
 
135
 
118
 	hf.ModifyTime = time.Now().Unix()
136
 	hf.ModifyTime = time.Now().Unix()
119
 
137
 
120
-	buff, err := json.Marshal(hf)
121
-	_, err = rd.Do("hset", MAIN_KEY, hf.Pcode, buff)
122
-	if err != nil {
123
-		return err
124
-	}
138
+	pKey := get_exchange_code_key(hf.Pcode)
139
+	rd.Do("hset", pKey, "awardTimes", hf.AwardTimes)
140
+	rd.Do("hset", pKey, "modifyTime", hf.ModifyTime)
141
+	rd.Do("hset", pKey, "expireTime", hf.ExpireTime)
142
+	items, _ := json.Marshal(hf.Items)
143
+	rd.Do("hset", pKey, "items", items)
144
+	rd.Do("sadd", MAIN_KEY, hf.Pcode)
125
 
145
 
126
 	ecMutex.Lock()
146
 	ecMutex.Lock()
127
 	for i := 0; i < len(exchangeCodeList); i++ {
147
 	for i := 0; i < len(exchangeCodeList); i++ {
@@ -137,7 +157,7 @@ func UpdateExchangeCodeInfo(hf *ExchangeCodeInfo) error {
137
 	}
157
 	}
138
 	ecMutex.Unlock()
158
 	ecMutex.Unlock()
139
 
159
 
140
-	return err
160
+	return nil
141
 
161
 
142
 }
162
 }
143
 
163
 
@@ -153,7 +173,9 @@ func DelExchangeCodeInfoById(pcode string) error {
153
 	ecMutex.Lock()
173
 	ecMutex.Lock()
154
 	defer ecMutex.Unlock()
174
 	defer ecMutex.Unlock()
155
 
175
 
156
-	_, err := rd.Do("hdel", MAIN_KEY, pcode)
176
+	rd.Do("srem", MAIN_KEY, pcode)
177
+	pKey := get_exchange_code_key(pcode)
178
+	_, err := rd.Do("del", pKey)
157
 	if err != nil {
179
 	if err != nil {
158
 		log.Println(err)
180
 		log.Println(err)
159
 		return err
181
 		return err

+ 5 - 8
views/exchangecode.tpl

@@ -283,24 +283,21 @@
283
           return;
283
           return;
284
         }
284
         }
285
         // 兑换码字符检查
285
         // 兑换码字符检查
286
-        var isMatch = true
287
         for (var i = 0; i < pcode.length; i ++){
286
         for (var i = 0; i < pcode.length; i ++){
287
+          var isMatch = false
288
           for (var j = 0; j < randChats.length; j ++) {
288
           for (var j = 0; j < randChats.length; j ++) {
289
             if (pcode[i] == randChats[j]){
289
             if (pcode[i] == randChats[j]){
290
               console.info("兑换码字符检查 i:"+i+";"+pcode[i])
290
               console.info("兑换码字符检查 i:"+i+";"+pcode[i])
291
               console.info("兑换码字符检查 j:"+j+";"+randChats[j])
291
               console.info("兑换码字符检查 j:"+j+";"+randChats[j])
292
-              isMatch = false;
292
+              isMatch = true;
293
               break;
293
               break;
294
             }
294
             }
295
           }
295
           }
296
-          if (isMatch == false) {
297
-            break;
296
+          if (isMatch == false){
297
+            alert("兑换码错误!请使用大写字母");
298
+            return;
298
           }
299
           }
299
         }
300
         }
300
-        if (isMatch == false){
301
-          alert("兑换码错误!请使用大写字母");
302
-          return;
303
-        }
304
 
301
 
305
         if (awardTimes <= 0){
302
         if (awardTimes <= 0){
306
           alert("请正确设置领奖次数");
303
           alert("请正确设置领奖次数");