浏览代码

连续签到

renzheng 9 年之前
父节点
当前提交
50bd12fe2c
共有 3 个文件被更改,包括 70 次插入21 次删除
  1. 2 2
      credit/src/config.json
  2. 6 0
      credit/src/main.go
  3. 62 19
      credit/src/qfw/creditrpc/creditrpc.go

+ 2 - 2
credit/src/config.json

@@ -18,8 +18,8 @@
 		"a7":10,
 		"a8":10,
 		"b1":10,
-		"b1_1":20,
-		"b1_n":3,
+		"b1_1":10,
+		"b1_n":7,
 		"b2":10,
 		"b2_1":20,
 		"b2_n":3,

+ 6 - 0
credit/src/main.go

@@ -1,3 +1,7 @@
+/*
+信用积分
+*/
+
 package main
 
 import (
@@ -176,6 +180,7 @@ func TimerSwordFish() {
 	}()
 }
 
+//自动扣费
 func doSubCredit(userId, uid, typeName, code string, next *time.Time) {
 	user := mongodb.FindById("user", uid, `{"i_credit":1,"o_msgset."`+typeName+`.i_status":1,"_id":0}`)
 	if *user != nil {
@@ -220,6 +225,7 @@ func doSubCredit(userId, uid, typeName, code string, next *time.Time) {
 	}
 }
 
+//发送微信和站内信通知
 func SendMsgWebAndWx(title, content, userId, uid string) {
 	m := &msg.Msg{
 		Msgtype:   1,

+ 62 - 19
credit/src/qfw/creditrpc/creditrpc.go

@@ -1,6 +1,7 @@
 package creditrpc
 
 import (
+	"fmt"
 	"log"
 	"net/rpc"
 	"qfw/consts"
@@ -56,31 +57,73 @@ func (c *CreditRpc) InCreadit(param *qrpc.CreditData, replay *int) error {
 			}
 		case "b":
 			key := param.Code + "_" + param.Uid
-			//[0/1,time,alltimes] //是否完成,次数,总次数
+			//[0/1,time,alltimes] //是否完成,次数,总次数,
 			lock := GetMutex(param.Uid)
 			lock.Lock()
-			obj := redis.Get(consts.RedisDB, key)
-			if obj != nil {
-				newobj := obj.([]int)
-				if newobj[0] == 0 {
+			if param.Code == "b1" { //签到
+				now := time.Now()
+				daykey := key + "_" + fmt.Sprintf("%d", now.Day())
+				qdObj := redis.Get(consts.RedisDB, daykey)
+				if qdObj != nil {
+					//已经签到了,不用处理
+				} else {
+					//[0/1,time,alltimes] //这次签到日期,次数,总次数,
+					obj := redis.Get(consts.RedisDB, key)
+					bcon := true
+					if obj != nil {
+						newobj := obj.([]int64)
+						//比较日期是不是连续签到,并且在有效次数内
+						if newobj[1] < newobj[2] && time.Unix(newobj[0], 0).AddDate(0, 0, 1).Day() == now.Day() {
+							newobj[0] = now.Unix()
+							thist := newobj[1]
+							newobj[1] = thist + 1
+							creditDoc["i_score"] = Score[param.Code+"_1"]
+							redis.Put(consts.RedisDB, key, newobj, GetSubSecond(int(newobj[2]-thist)))
+							redis.Put(consts.RedisDB, daykey, true, GetSubSecond(1))
+							creditlog.AddLog(creditDoc)
+						} else {
+							//不是连续签到
+							bcon = false
+						}
+					} else {
+						bcon = false
+					}
+					if !bcon {
+						//第一天签到
+						newobj := make([]int64, 3)
+						newobj[0] = time.Now().Unix()
+						newobj[1] = 1
+						newobj[2] = int64(Score[param.Code+"_n"])
+						creditDoc["i_score"] = Score[param.Code+"_1"]
+						redis.Put(consts.RedisDB, key, newobj, GetSubSecond(int(newobj[2])))
+						redis.Put(consts.RedisDB, daykey, true, GetSubSecond(1))
+						creditlog.AddLog(creditDoc)
+					}
+				}
+			} else {
+				obj := redis.Get(consts.RedisDB, key)
+				if obj != nil {
+					newobj := obj.([]int)
+					if newobj[0] == 0 {
+						newobj[1] += 1
+						if newobj[1] == Score[param.Code+"_n"] {
+							newobj[0] = 1
+						}
+						redis.Put(consts.RedisDB, key, newobj, GetSubSecond(1))
+						creditDoc["i_score"] = Score[param.Code]
+						creditlog.AddLog(creditDoc)
+					}
+				} else {
+					times := Score[param.Code+"_n"]
+					newobj := []int{0, 0, times}
 					newobj[1] += 1
-					if newobj[1] == Score[param.Code+"_n"] {
+					if newobj[1] == times {
 						newobj[0] = 1
 					}
-					redis.Put(consts.RedisDB, key, newobj, GetSubSecond())
 					creditDoc["i_score"] = Score[param.Code]
+					redis.Put(consts.RedisDB, key, newobj, GetSubSecond(1))
 					creditlog.AddLog(creditDoc)
 				}
-			} else {
-				times := Score[param.Code+"_n"]
-				newobj := []int{0, 0, times}
-				newobj[1] += 1
-				if newobj[1] == times {
-					newobj[0] = 1
-				}
-				creditDoc["i_score"] = Score[param.Code]
-				redis.Put(consts.RedisDB, key, newobj, GetSubSecond())
-				creditlog.AddLog(creditDoc)
 			}
 			lock.Unlock()
 			//
@@ -147,8 +190,8 @@ func SendManagerNotifyMsg(p *qrpc.NotifyMsg) {
 }
 
 //获取离明天的秒差
-func GetSubSecond() int {
+func GetSubSecond(n int) int {
 	now := time.Now()
-	tom := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, time.Local)
+	tom := time.Date(now.Year(), now.Month(), now.Day()+n, 0, 0, 0, 0, time.Local)
 	return int(tom.Unix() - now.Unix())
 }