Bläddra i källkod

Merge branch 'dev2.2' into dev2.3

6 år sedan
förälder
incheckning
b1cf7e1aaf

BIN
api文档/剑鱼数据开放平台接口_v1.0.1(关键词).doc


BIN
api文档/剑鱼数据开放平台接口_v1.0.1(启信宝).doc


BIN
api文档/剑鱼数据开放平台接口_v1.0.1(启信宝).pdf


BIN
api文档/剑鱼数据开放平台接口_v1.0.2(关键词).doc


BIN
api文档/剑鱼数据开放平台接口_v1.0.2(关键词).pdf


BIN
api文档/剑鱼数据开放平台接口_v1.0.2(启信宝).doc


BIN
api文档/剑鱼数据开放平台接口_v1.0.3(启信宝) .doc


BIN
api文档/剑鱼数据开放平台接口_v1.0.3(启信宝) .pdf


BIN
api文档/剑鱼数据开放平台接口_v1.0.4(企业名称).doc


BIN
api文档/剑鱼数据开放平台接口_v1.0.4(启信宝).pdf


+ 25 - 19
jyservice/src/config.json

@@ -1,7 +1,13 @@
 {
     "webport": ":8801",
+    "jkmail": {
+        "to": "renzheng@topnet.net.cn",
+        "api": "http://10.171.112.160:19281/_send/_mail",
+        "cron-report": "0 30 8 * * 2",
+        "cron-check": "0 0 17 * * ?"
+    },
     "mongodb": {
-        "addr": "192.168.3.207:27080",
+        "addr": "192.168.3.207:27082",
         "db": "jyqyfw",
         "pool": 10
     },
@@ -9,21 +15,21 @@
         "addr": "jyqyfw=192.168.3.207:1380",
         "pool": 50
     },
-	"cassandra": [
+    "cassandra": [
         "192.168.3.18"
     ],
-    "cassandrasize":5,
-	"loglevel":5,
+    "cassandrasize": 5,
+    "loglevel": 5,
     "plan": {
         "A": {
             "title": 1,
             "detail": 1,
             "publishtime": 1,
             "href": 1,
-			"id":1,
-			"department":1,
-			"matchscore":1,
-			"remark":1
+            "id": 1,
+            "department": 1,
+            "matchscore": 1,
+            "remark": 1
         },
         "B": {
             "title": 1,
@@ -36,17 +42,17 @@
             "bidamount": 1,
             "bidopentime": 1,
             "buyer": 1,
-			"buyertel":1,
-			"buyerperson":1,
-			"city":1,
-			"id":1,
-			"department":1,
-			"matchscore":1,
-			"remark":1,
-			"priority":1,
-			"industry":1,
-			"matchtype":1,
-			"matchkey":1
+            "buyertel": 1,
+            "buyerperson": 1,
+            "city": 1,
+            "id": 1,
+            "department": 1,
+            "matchscore": 1,
+            "remark": 1,
+            "priority": 1,
+            "industry": 1,
+            "matchtype": 1,
+            "matchkey": 1
         }
     }
 }

+ 19 - 12
jyservice/src/main.go

@@ -1,24 +1,31 @@
 package main
 
 import (
-	"net/http"
-	//ca "ucbsutil/cassandra"
 	"usermanager"
 	"utils"
 
+	"github.com/fvbock/endless"
 	log "github.com/sirupsen/logrus"
 )
 
-func main() {
-	//初始化cassandra
-	//ca.InitCassandra("jianyu", (int)(utils.Sysconfig["cassandrasize"].(float64)), util.ObjArrToStringArr(utils.Sysconfig["cassandra"].([]interface{})))
-
-	go Server()
-	//定时更新redis中的数据到数据库
-	log.Info("Start server...")
-	<-make(chan struct{}, 0)
+func Server() {
+	endless.ListenAndServe(utils.Sysconfig["webport"].(string), usermanager.Middleware(usermanager.Route))
 }
 
-func Server() {
-	http.ListenAndServe(utils.Sysconfig["webport"].(string), usermanager.Middleware(usermanager.Route))
+//import (
+//	"net/http"
+//	"usermanager"
+//	"utils"
+
+//	log "github.com/sirupsen/logrus"
+//)
+
+//func Server() {
+//	http.ListenAndServe(utils.Sysconfig["webport"].(string), usermanager.Middleware(usermanager.Route))
+//}
+
+func main() {
+	log.Info("Start server...")
+	Server()
+	//定时更新redis中的数据到数据库
 }

+ 7 - 2
jyservice/src/usermanager/getdata.go

@@ -113,14 +113,19 @@ func CheckUserInfo(access_token string, day, next, all int) (bcheck bool, appid
 						limittime := int(endtime - tnUnix)
 						redis.Put(REDISDB, "limittime_"+appid, limittime, limittime) //存入值
 						validtime = limittime
+					} else {
+						d["code"] = CODE_E4
+						d["msg"] = MSG_E4
 					}
 				}
 			}
 		}
 		limittodaykey := fmt.Sprintf("limittoday_%d_%s", tn.Day(), appid)
 		if validtime == nil {
-			d["code"] = CODE_ERR_E1
-			d["msg"] = MSG_ERR_E
+			if d["code"] == nil {
+				d["code"] = CODE_ERR_E1
+				d["msg"] = MSG_ERR_E
+			}
 		} else {
 			limittime := util.IntAll(validtime)
 			if limittime > 0 { //在有效期内,判断今日调用次数,判断服务的总条数

+ 74 - 14
jyservice/src/usermanager/user.go

@@ -9,7 +9,9 @@ import (
 	"fmt"
 	"net/http"
 	"qfw/util"
+	"qfw/util/redis"
 	"regexp"
+	"sync"
 	"time"
 	. "utils"
 
@@ -60,34 +62,92 @@ func GetAppid(tn int64) (appid string) {
 func NewService(w http.ResponseWriter, r *http.Request) {
 	appid := r.FormValue("appid")
 	plan := r.FormValue("plan")
+	endtime := r.FormValue("endtime") //时间戳1532500000
 	ret := JSON{"msg": "err!"}
 	if appid != "" && plan != "" && Mgo.Count("user", `{"appid":"`+appid+`"}`) == 1 {
 		res, bp := Mgo.FindOne("serviceplan", &map[string]interface{}{
 			"_id": plan,
 		})
+		//查找服务套餐
 		if bp && res != nil && *res != nil {
-			limit := (*res)["limit"]
+			limit := util.IntAll((*res)["limit"]) //套餐中条数
 			t := time.Now()
-			end := t.AddDate(1, 0, 1)
+			st := t.Unix()
+			end := t.AddDate(1, 0, 1) //计算一年后的时间
 			end1 := time.Date(end.Year(), end.Month(), end.Day(), 0, 0, 0, 0, time.Local)
+			et := end1.Unix()
+			//如果参数中传递了到期时间,就替换计算的时间
+			if endtime != "" && len(endtime) == 10 {
+				et1 := util.Int64All(endtime)
+				if et1 > 0 {
+					et = et1
+				} else {
+					log.Println("传入日期参数错误", endtime)
+				}
+			}
 			//log.Debug(t.Unix(), "-", end.Unix(), util.FormatDate(&t, util.Date_Full_Layout), util.FormatDate(&end1, util.Date_Full_Layout))
 			id := Mgo.Save("buyrecord", &map[string]interface{}{"appid": appid, "plan": plan, "createtime": t.Unix(), "starttime": t.Unix(), "endtime": end1.Unix()})
 			if id != "" {
-				//后结考虑延期时间
-				if Mgo.Update("user", `{"appid":"`+appid+`"}`, &map[string]interface{}{
-					"$set": map[string]interface{}{
-						"plan": map[string]interface{}{
-							"name":      plan,
-							"starttime": t.Unix(),
-							"endtime":   end1.Unix(),
-							"recordid":  id,
-							"current":   limit,
+				//取出此appid的锁
+				GetDataMapLock.Lock()
+				appidLock := GetDataMap[appid]
+				if appidLock == nil {
+					appidLock = &sync.Mutex{}
+					GetDataMap[appid] = appidLock
+				}
+				GetDataMapLock.Unlock()
+				appidLock.Lock()
+				defer appidLock.Unlock()
+				//查找用户信息
+				u, b := Mgo.FindOne("user", `{"appid":"`+appid+`"}`)
+				if b && u != nil {
+					oldplan := (*u)["plan"]
+					limitnum := redis.Get(REDISDB, "limitnum_"+appid) //取redis中的剩余量
+					oplimit := 0
+					if oldplan != nil {
+						op := oldplan.(map[string]interface{})
+						opst := util.Int64All(op["starttime"])
+						opet := util.Int64All(op["endtime"])
+						oplimit = util.IntAll(op["current"])
+						if limitnum != nil {
+							oplimit = util.IntAll(limitnum)
+						}
+						if opst > 0 && opet > 0 && oplimit > -1 {
+							st = opst
+							if et < opet {
+								et = opet
+								log.Println("结束日期没原来日期大,替换")
+							}
+							limit += oplimit
+						}
+					}
+					if Mgo.Update("user", `{"appid":"`+appid+`"}`, &map[string]interface{}{
+						"$set": map[string]interface{}{
+							"plan": map[string]interface{}{
+								"name":      plan,
+								"starttime": st,
+								"endtime":   et,
+								"recordid":  id,
+								"current":   limit,
+							},
 						},
-					},
-				}, false, false) {
-					ret["msg"] = "ok"
+					}, false, false) {
+						ret["msg"] = "ok"
+						redis.Del(REDISDB, "limitnum_"+appid) //清除在redis中剩余量
+						Mgo.UpdateById("buyrecord", id, &map[string]interface{}{
+							"$set": map[string]interface{}{
+								"beforecount": oplimit,
+								"aftercount":  limit,
+							},
+						})
+						log.Println("更新套餐成功:", appid)
+					}
+				} else {
+					log.Println("appid不存在", appid)
 				}
 			}
+		} else {
+			log.Println("查找套餐不存在", appid, plan)
 		}
 	}
 	WriteJSON(w, &ret)

+ 98 - 0
jyservice/src/utils/task.go

@@ -0,0 +1,98 @@
+package utils
+
+import (
+	"fmt"
+	"net/http"
+	"time"
+
+	log "github.com/sirupsen/logrus"
+
+	qu "qfw/util"
+
+	"github.com/robfig/cron"
+	. "gopkg.in/mgo.v2/bson"
+)
+
+//定时任务,邮件服务
+var (
+	api    string
+	tomail string
+)
+
+func task() {
+	jkmail, _ := Sysconfig["jkmail"].(map[string]interface{})
+	if jkmail != nil {
+		log.Debug("enter task...")
+		api, _ = jkmail["api"].(string)
+		tomail, _ = jkmail["to"].(string)
+		c := cron.New()
+		//c.AddFunc(jkmail["cron-report"].(string), report)
+		c.AddFunc(jkmail["cron-check"].(string), check)
+		c.Start()
+		defer c.Stop()
+		select {}
+	}
+}
+
+//出报告,每周一次,信息量情况和调用情况,暂时不用
+func report() {
+	log.Debug("每周出报告")
+
+}
+
+//监控报警,量小于30天、15天、7天告警,取最近7天的求出最大值
+var LM = map[int64]bool{1: true, 3: true, 7: true, 15: true, 30: true, 45: true}
+
+func check() {
+	log.Debug("每天信息量监控")
+	sess := Mgo.GetMgoConn()
+	defer Mgo.DestoryMongoConn(sess)
+	var res []M
+	now := time.Now()
+
+	nowZero := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
+	threeBefor := time.Date(now.Year(), now.Month(), now.Day()-4, 0, 0, 0, 0, time.Local)
+	sess.DB(Mgo.DbName).C("usermail").Pipe([]M{
+		M{"$match": M{"createtime": M{"$gte": threeBefor.Unix(), "$lt": nowZero.Unix()}}},
+		M{"$group": M{"_id": M{"appid": "$appid", "day": M{"$trunc": M{"$divide": []interface{}{M{"$subtract": []interface{}{nowZero.Unix(), "$createtime"}}, 86400}}}}, "sum": M{"$sum": 1}}},
+	}).All(&res)
+	if res != nil && len(res) > 0 {
+		MaxMap := map[string]int64{}
+		for _, v := range res {
+			vm, _ := v["_id"].(M) //用M否则会报错
+			appid := qu.ObjToString(vm["appid"])
+			v1 := MaxMap[appid]
+			v2 := qu.Int64All(v["sum"])
+			if v2 > v1 {
+				MaxMap[appid] = v2
+			}
+		}
+		//查询用户信息
+		us, b := Mgo.Find("user", `{}`, nil, `{"appid":1,"username":1,"plan":1}`, false, -1, -1)
+		alert := ""
+		if b && us != nil {
+			for _, v := range *us {
+				id, _ := v["appid"].(string)
+				name, _ := v["username"].(string)
+				plan, _ := v["plan"].(map[string]interface{})
+				current := qu.Int64All(plan["current"])
+				endtime := qu.Int64All(plan["endtime"])
+				last := (endtime - now.Unix()) / 86400
+				yc := last * MaxMap[id]
+				if LM[last] || yc > current { //到期发告警或者余额不足告警
+					alert += fmt.Sprintf("%s:%s__剩余天数:%d__剩余条数:%d__预测条数:%d<br/>", id, name, last, current, yc)
+				}
+				log.Debug(id, name, "剩余天数:", last, "剩余条数:", current, "预测:", yc)
+			}
+		}
+		if alert != "" {
+			SendMail("企业级服务每日监控", alert)
+		}
+	}
+
+}
+
+func SendMail(zt, str string) {
+	res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", api, tomail, zt, str))
+	log.Debug("sendemail", res, err)
+}

+ 1 - 0
jyservice/src/utils/utils.go

@@ -64,6 +64,7 @@ func init() {
 	redis.InitRedisBySize(util.ObjToString(redisconf["addr"]), util.IntAllDef(redisconf["pool"], 10), 20, 240)
 	log.Info("Util init over...")
 	initrsa()
+	go task()
 }
 
 func GetDayMinMax(t time.Time) (int64, int64) {