|
@@ -7,8 +7,12 @@ import (
|
|
|
"qfw/consts"
|
|
|
"qfw/creditlog"
|
|
|
"qfw/util"
|
|
|
+ "qfw/util/mongodb"
|
|
|
+ "qfw/util/msg"
|
|
|
"qfw/util/redis"
|
|
|
qrpc "qfw/util/rpc"
|
|
|
+ "runtime"
|
|
|
+ "strings"
|
|
|
"sync"
|
|
|
"time"
|
|
|
)
|
|
@@ -18,6 +22,7 @@ type CreditRpc struct{}
|
|
|
var Message map[string]string
|
|
|
var Score map[string]int
|
|
|
var Rpcserver string
|
|
|
+var Hour int
|
|
|
|
|
|
//用户访问redis加锁
|
|
|
var SN = 100
|
|
@@ -34,8 +39,27 @@ func GetMutex(uid string) *sync.Mutex {
|
|
|
return SLM[util.HashCode(uid)%SN]
|
|
|
}
|
|
|
|
|
|
+func getCreditById(userId string) int {
|
|
|
+ user := mongodb.FindById("user", userId, `{"_id":0,"i_credit":1}`)
|
|
|
+ if *user != nil {
|
|
|
+ return util.IntAll((*user)["i_credit"])
|
|
|
+ }
|
|
|
+ return 0
|
|
|
+}
|
|
|
+
|
|
|
//增加积分
|
|
|
func (c *CreditRpc) InCreadit(param *qrpc.CreditData, replay *int) error {
|
|
|
+ defer func() {
|
|
|
+ if r := recover(); r != nil {
|
|
|
+ for skip := 1; ; skip++ {
|
|
|
+ _, file, line, ok := runtime.Caller(skip)
|
|
|
+ if !ok {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ go log.Printf("%v,%v\n", file, line)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
//a类为即时保存
|
|
|
//b、c、d、e为定时保存
|
|
|
log.Println("---请求", param)
|
|
@@ -44,21 +68,25 @@ func (c *CreditRpc) InCreadit(param *qrpc.CreditData, replay *int) error {
|
|
|
first := param.Code[:1]
|
|
|
//num := Score[param.code]
|
|
|
creditDoc := map[string]interface{}{
|
|
|
- "s_uid": param.Uid,
|
|
|
- "s_code": param.Code,
|
|
|
- "i_type": 1,
|
|
|
- "l_date": time.Now().Unix(),
|
|
|
+ "s_uid": param.Uid,
|
|
|
+ "s_code": param.Code,
|
|
|
+ "s_operation": Message["txt_"+param.Code],
|
|
|
+ "i_type": 1,
|
|
|
+ "l_date": time.Now().Unix(),
|
|
|
}
|
|
|
+ i_scorenow := getCreditById(param.Uid)
|
|
|
switch first {
|
|
|
case "a": //立即生效
|
|
|
creditDoc["i_score"] = Score[param.Code]
|
|
|
+ creditDoc["i_scorenow"] = i_scorenow + Score[param.Code]
|
|
|
if creditlog.Save(creditDoc) {
|
|
|
- *replay = 1
|
|
|
+ *replay = creditDoc["i_score"].(int)
|
|
|
}
|
|
|
case "d", "e":
|
|
|
creditDoc["i_score"] = util.If(param.Num > 0, param.Num, Score[param.Code]).(int)
|
|
|
+ creditDoc["i_scorenow"] = i_scorenow + util.If(param.Num > 0, param.Num, Score[param.Code]).(int)
|
|
|
if creditlog.Save(creditDoc) {
|
|
|
- *replay = 1
|
|
|
+ *replay = creditDoc["i_score"].(int)
|
|
|
}
|
|
|
case "b":
|
|
|
key := param.Code + "_" + param.Uid
|
|
@@ -83,10 +111,11 @@ func (c *CreditRpc) InCreadit(param *qrpc.CreditData, replay *int) error {
|
|
|
thist := newobj[1]
|
|
|
newobj[1] = thist + 1
|
|
|
creditDoc["i_score"] = Score[param.Code+"_1"] + int(thist)*Score[param.Code]
|
|
|
+ creditDoc["i_scorenow"] = i_scorenow + Score[param.Code+"_1"] + int(thist)*Score[param.Code]
|
|
|
redis.Put(consts.RedisDB, key, newobj, GetSubSecond(int(newobj[2]-thist)))
|
|
|
redis.Put(consts.RedisDB, daykey, true, GetSubSecond(1))
|
|
|
creditlog.AddLog(creditDoc)
|
|
|
- *replay = 1
|
|
|
+ *replay = creditDoc["i_score"].(int)
|
|
|
} else {
|
|
|
//不是连续签到
|
|
|
bcon = false
|
|
@@ -101,10 +130,11 @@ func (c *CreditRpc) InCreadit(param *qrpc.CreditData, replay *int) error {
|
|
|
newobj[1] = 1
|
|
|
newobj[2] = int64(Score[param.Code+"_n"])
|
|
|
creditDoc["i_score"] = Score[param.Code+"_1"]
|
|
|
+ creditDoc["i_scorenow"] = i_scorenow + 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)
|
|
|
- *replay = 1
|
|
|
+ *replay = creditDoc["i_score"].(int)
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
@@ -118,8 +148,9 @@ func (c *CreditRpc) InCreadit(param *qrpc.CreditData, replay *int) error {
|
|
|
}
|
|
|
redis.Put(consts.RedisDB, key, newobj, GetSubSecond(1))
|
|
|
creditDoc["i_score"] = Score[param.Code]
|
|
|
+ creditDoc["i_scorenow"] = i_scorenow + Score[param.Code]
|
|
|
creditlog.AddLog(creditDoc)
|
|
|
- *replay = 1
|
|
|
+ *replay = creditDoc["i_score"].(int)
|
|
|
}
|
|
|
} else {
|
|
|
times := Score[param.Code+"_n"]
|
|
@@ -129,9 +160,10 @@ func (c *CreditRpc) InCreadit(param *qrpc.CreditData, replay *int) error {
|
|
|
newobj[0] = 1
|
|
|
}
|
|
|
creditDoc["i_score"] = Score[param.Code]
|
|
|
+ creditDoc["i_scorenow"] = i_scorenow + Score[param.Code]
|
|
|
redis.Put(consts.RedisDB, key, newobj, GetSubSecond(1))
|
|
|
creditlog.AddLog(creditDoc)
|
|
|
- *replay = 1
|
|
|
+ *replay = creditDoc["i_score"].(int)
|
|
|
}
|
|
|
}
|
|
|
lock.Unlock()
|
|
@@ -145,19 +177,22 @@ func (c *CreditRpc) InCreadit(param *qrpc.CreditData, replay *int) error {
|
|
|
key := param.Code + "_" + param.Uid + "_" + objid.(string)
|
|
|
if redis.Get(consts.RedisDB, key) == nil {
|
|
|
creditDoc["i_score"] = Score[param.Code]
|
|
|
+ creditDoc["i_scorenow"] = i_scorenow + Score[param.Code]
|
|
|
creditDoc["s_objid"] = objid.(string)
|
|
|
newDoc := map[string]interface{}{
|
|
|
- "s_uid": objid.(string),
|
|
|
- "s_code": param.Code,
|
|
|
- "i_type": 1,
|
|
|
- "s_sourceid": param.Uid,
|
|
|
- "l_date": time.Now().Unix(),
|
|
|
- "i_score": Score[param.Code],
|
|
|
+ "s_uid": objid.(string),
|
|
|
+ "s_code": param.Code,
|
|
|
+ "s_operation": Message["txt_"+param.Code],
|
|
|
+ "i_type": 1,
|
|
|
+ "s_sourceid": param.Uid,
|
|
|
+ "l_date": time.Now().Unix(),
|
|
|
+ "i_score": Score[param.Code],
|
|
|
+ "i_scorenow": i_scorenow + Score[param.Code],
|
|
|
}
|
|
|
creditlog.AddLog(creditDoc)
|
|
|
creditlog.AddLog(newDoc)
|
|
|
redis.Put(consts.RedisDB, key, true, GetSubSecond(1))
|
|
|
- *replay = 1
|
|
|
+ *replay = creditDoc["i_score"].(int)
|
|
|
}
|
|
|
}
|
|
|
} else if param.Code == "c2" { //评价
|
|
@@ -166,25 +201,28 @@ func (c *CreditRpc) InCreadit(param *qrpc.CreditData, replay *int) error {
|
|
|
key := param.Code + "_" + param.Uid + "_" + objid.(string)
|
|
|
if redis.Get(consts.RedisDB, key) == nil {
|
|
|
creditDoc["i_score"] = Score[param.Code]
|
|
|
+ creditDoc["i_scorenow"] = i_scorenow + Score[param.Code]
|
|
|
creditDoc["o_param"] = param.OtherParam
|
|
|
creditlog.AddLog(creditDoc)
|
|
|
redis.Put(consts.RedisDB, key, true, GetSubSecond(1))
|
|
|
- *replay = 1
|
|
|
+ *replay = creditDoc["i_score"].(int)
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
creditDoc["i_score"] = Score[param.Code]
|
|
|
+ creditDoc["i_scorenow"] = i_scorenow + Score[param.Code]
|
|
|
creditDoc["o_param"] = param.OtherParam
|
|
|
creditlog.AddLog(creditDoc)
|
|
|
- *replay = 1
|
|
|
+ *replay = creditDoc["i_score"].(int)
|
|
|
}
|
|
|
lock.Unlock()
|
|
|
default: //定时任务
|
|
|
sc := util.If(param.Num > 0, param.Num, Score[param.Code]).(int)
|
|
|
if sc > 0 {
|
|
|
creditDoc["i_score"] = sc
|
|
|
+ creditDoc["i_scorenow"] = i_scorenow + sc
|
|
|
creditlog.AddLog(creditDoc)
|
|
|
- *replay = 1
|
|
|
+ *replay = creditDoc["i_score"].(int)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -193,50 +231,93 @@ func (c *CreditRpc) InCreadit(param *qrpc.CreditData, replay *int) error {
|
|
|
|
|
|
//消费积分,增加扣费方式、手动还是自动
|
|
|
func (c *CreditRpc) OutCreadit(param *qrpc.CreditData, replay *int) error {
|
|
|
+ defer func() {
|
|
|
+ if r := recover(); r != nil {
|
|
|
+ for skip := 1; ; skip++ {
|
|
|
+ _, file, line, ok := runtime.Caller(skip)
|
|
|
+ if !ok {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ go log.Printf("%v,%v\n", file, line)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
*replay = 0
|
|
|
lock := GetMutex(param.Uid)
|
|
|
lock.Lock()
|
|
|
if len(param.Code) > 1 && Message[("txt_"+param.Code)] != "" {
|
|
|
+ i_scorenow := getCreditById(param.Uid)
|
|
|
first := param.Code[:1]
|
|
|
now := time.Now()
|
|
|
creditDoc := map[string]interface{}{
|
|
|
- "s_uid": param.Uid,
|
|
|
- "s_umid": param.Umid,
|
|
|
- "s_code": param.Code,
|
|
|
- "i_type": 0,
|
|
|
- "l_date": now.Unix(),
|
|
|
- "i_score": param.Num,
|
|
|
+ "s_uid": param.Uid,
|
|
|
+ "s_umid": param.Umid,
|
|
|
+ "s_code": param.Code,
|
|
|
+ "s_operation": Message["txt_"+param.Code],
|
|
|
+ "i_type": 0,
|
|
|
+ "l_date": now.Unix(),
|
|
|
}
|
|
|
if param.OtherParam != nil && param.OtherParam["i_way"] != nil {
|
|
|
creditDoc["i_way"] = param.OtherParam["i_way"]
|
|
|
}
|
|
|
+ var newDate time.Time
|
|
|
switch first {
|
|
|
case "A":
|
|
|
- creditDoc["l_enddate"] = now.AddDate(0, 1, 0).Unix()
|
|
|
+ newDate = GetTimeByNow(now, Hour).AddDate(0, 1, 1)
|
|
|
+ creditDoc["l_enddate"] = newDate.Unix()
|
|
|
creditDoc["i_valid"] = 1
|
|
|
if param.OtherParam != nil {
|
|
|
- creditDoc["s_type"] = param.OtherParam["s_type"]
|
|
|
+ str := []string{}
|
|
|
+ for k, _ := range param.OtherParam {
|
|
|
+ str = append(str, k)
|
|
|
+ }
|
|
|
+ creditDoc["s_type"] = strings.Join(str, ",")
|
|
|
+ creditDoc["i_score"] = Score[param.Code] * len(str)
|
|
|
+ creditDoc["i_scorenow"] = i_scorenow + Score[param.Code]*len(str)
|
|
|
}
|
|
|
case "B":
|
|
|
+ creditDoc["i_score"] = param.Num
|
|
|
+ creditDoc["i_scorenow"] = i_scorenow + param.Num
|
|
|
creditDoc["i_givestatus"] = 0
|
|
|
}
|
|
|
if creditlog.Save(creditDoc) {
|
|
|
*replay = 1
|
|
|
//发送微信通知扣积分成功
|
|
|
- go SendWeixin(param.Num, param.Umid, "")
|
|
|
+ if first == "A" {
|
|
|
+ go SendMsgWebAndWx(Message["swordfish_payTitle"], fmt.Sprintf(Message["swordfish_pay"], creditDoc["i_score"], creditDoc["i_scorenow"], util.FormatDate(&newDate, util.Date_Full_Layout)), param.Uid, param.Umid)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
lock.Unlock()
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-//发送微信积分变动通知
|
|
|
-func SendWeixin(num int, uid, info string) {
|
|
|
+//发送微信和站内信通知
|
|
|
+func SendMsgWebAndWx(title, content, userId, umid string) {
|
|
|
+ m := &msg.Msg{
|
|
|
+ Msgtype: 1,
|
|
|
+ Title: title,
|
|
|
+ Content: content,
|
|
|
+ ReceiveId: userId,
|
|
|
+ }
|
|
|
+ go m.SaveMsg()
|
|
|
+ go SendManagerNotifyMsg(&qrpc.NotifyMsg{Openid: umid, Title: title, Detail: "管理员", Remark: content})
|
|
|
|
|
|
}
|
|
|
|
|
|
//微信远程调用,实现模板发送消息
|
|
|
func SendManagerNotifyMsg(p *qrpc.NotifyMsg) {
|
|
|
+ defer func() {
|
|
|
+ if r := recover(); r != nil {
|
|
|
+ for skip := 1; ; skip++ {
|
|
|
+ _, file, line, ok := runtime.Caller(skip)
|
|
|
+ if !ok {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ go log.Printf("%v,%v\n", file, line)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
client, err := rpc.DialHTTP("tcp", Rpcserver)
|
|
|
if err != nil {
|
|
|
log.Println(err.Error())
|
|
@@ -255,3 +336,8 @@ func GetSubSecond(n int) int {
|
|
|
tom := time.Date(now.Year(), now.Month(), now.Day()+n, 0, 0, 0, 0, time.Local)
|
|
|
return int(tom.Unix() - now.Unix())
|
|
|
}
|
|
|
+
|
|
|
+func GetTimeByNow(now time.Time, nhour int) time.Time {
|
|
|
+ tom := time.Date(now.Year(), now.Month(), now.Day(), nhour, 0, 0, 0, time.Local)
|
|
|
+ return tom
|
|
|
+}
|