unknown há 7 anos atrás
pai
commit
95c87aa771

+ 59 - 15
src/apiservice/oamanager/content.go

@@ -1,6 +1,7 @@
 package oamanager
 
 import (
+	"log"
 	"net"
 	"net/http"
 	"qfw/util"
@@ -24,6 +25,13 @@ var regex = regexp.MustCompile("(Android|Mobile)")
 //对map的同步
 var lock sync.Mutex
 
+//内存缓存日志数量,超过此数量存库
+var nc = 500
+
+//内存缓存日志map
+var arr = make([]map[string]interface{}, 0)
+var arrapi = make([]map[string]interface{}, 0)
+
 func init() {
 	xweb.AddAction(&Content{})
 }
@@ -64,23 +72,59 @@ func addLog(req *http.Request) {
 	s_url := req.RequestURI
 	date := timeNow.Unix()
 	logs := map[string]interface{}{
-		"l_date":    date,
-		"s_ip":      req.Proto,
-		"s_refer":   ref,
-		"i_year":    timeNow.Year(),
-		"i_month":   timeNow.Month(),
-		"i_day":     timeNow.Day(),
-		"i_hour":    timeNow.Hour(),
-		"i_minutes": timeNow.Minute(),
-		//"s_describe": req.Form,
-		"s_client": agent,
-		"s_os":     GetOS(agent),
-		"s_browse": GetBrowse(agent),
-		"s_method": req.Method,
-		"s_url":    s_url,
+		"l_date":     date,
+		"s_ip":       req.Proto,
+		"s_refer":    ref,
+		"i_year":     timeNow.Year(),
+		"i_month":    timeNow.Month(),
+		"i_day":      timeNow.Day(),
+		"i_hour":     timeNow.Hour(),
+		"i_minutes":  timeNow.Minute(),
+		"s_describe": req.Form,
+		"s_client":   agent,
+		"s_os":       GetOS(agent),
+		"s_browse":   GetBrowse(agent),
+		"s_method":   req.Method,
+		"s_url":      s_url,
 	}
 	lock.Lock()
-	mongodb.SaveBulk("log", logs)
+	arr = append(arr, logs)
+	if len(arr) >= nc {
+		tmp := arr
+		arr = make([]map[string]interface{}, 0)
+		go func() {
+			log.Println("save..article..log", len(tmp))
+			mongodb.SaveBulk("log", tmp...)
+		}()
+	}
+	lock.Unlock()
+}
+
+func APIlog(username, action, keyword, appid, rMsg, signature string, pagenum, timestamp, rCode, apicount int) {
+	defer util.Catch()
+	logs := map[string]interface{}{
+		"s_username":  username,
+		"s_keyword":   keyword,
+		"s_appid":     appid,
+		"s_rMsg":      rMsg,
+		"s_signature": signature,
+		"s_action":    action,
+		"i_pagenum":   pagenum,
+		"i_timestamp": timestamp,
+		"i_rCode":     rCode,
+		"i_apicount":  apicount,
+		"l_date":      time.Now().Unix(),
+	}
+	lock.Lock()
+	arrapi = append(arrapi, logs)
+	if len(arrapi) >= nc {
+		tmp := arrapi
+		arrapi = make([]map[string]interface{}, 0)
+		go func() {
+			log.Println("save..article..log", len(tmp))
+			mongodb.SaveBulk("apilog", tmp...)
+		}()
+	}
 	lock.Unlock()
 }
 

+ 1 - 1
src/apiservice/oamanager/createuser.go

@@ -83,6 +83,6 @@ func GetAppid(tn int64) (appid string) {
 			break
 		}
 	}
-	appid = "jyoa" + appid
+	appid = "jyo_" + appid
 	return
 }

+ 32 - 21
src/apiservice/oamanager/oamanager.go

@@ -57,7 +57,7 @@ const (
 	MSG_E6           = "IP未授权"
 	INDEX            = "bidding"
 	TYPE             = "bidding"
-	LIMIT_LIST_COUNT = 10
+	LIMIT_LIST_COUNT = 100
 )
 
 var APPID, SECRET string
@@ -74,16 +74,18 @@ func OpenAction(w http.ResponseWriter, r *http.Request) {
 	rData := make(map[string]interface{})
 	var rCode = 0
 	var rMsg = ""
+	var username string
+	var apicount int
+	var (
+		action       = r.FormValue("action")
+		keyword      = r.FormValue("keyword")
+		appid        = r.FormValue("appid")
+		pagenum, _   = strconv.Atoi(r.FormValue("pagenum"))
+		signature    = r.FormValue("signature")
+		ttamp        = time.Now().Unix()
+		timestamp, _ = strconv.Atoi(r.FormValue("timestamp"))
+	)
 	if r.Method == "POST" { //POST请求
-		var (
-			action       = r.FormValue("action")
-			keyword      = r.FormValue("keyword")
-			appid        = r.FormValue("appid")
-			pagenum, _   = strconv.Atoi(r.FormValue("pagenum"))
-			signature    = r.FormValue("signature")
-			ttamp        = time.Now().Unix()
-			timestamp, _ = strconv.Atoi(r.FormValue("timestamp"))
-		)
 		if appid == "" || action == "" || keyword == "" || timestamp == 0 || signature == "" { //参数验证
 			rCode = CODE_E2 //签名错误
 			rMsg = MSG_E2
@@ -94,15 +96,12 @@ func OpenAction(w http.ResponseWriter, r *http.Request) {
 				rCode = CODE_E3 //签名过期
 				rMsg = MSG_E3
 			} else {
-				apicount := redis.GetInt(REDISDB, "jyoacount-"+appid+oaday)
+				apicount = redis.GetInt(REDISDB, "jyoacount-"+appid+oaday)
 				log.Println("每天调用次数:", apicount)
 				if apicount >= LIMIT_COUNT { //调用接口上限判断
 					rCode = CODE_E4 //调用接口超过限制
 					rMsg = MSG_E4
 				} else {
-					if pagenum == 0 {
-						pagenum = 1
-					}
 					res, ok := mongodb.FindOneByField("user", &map[string]interface{}{
 						"appid": appid,
 					}, nil)
@@ -113,19 +112,30 @@ func OpenAction(w http.ResponseWriter, r *http.Request) {
 					if len(*res) > 0 && (*res)["status"] == 1 { //查看用户是否存在
 						userip := GetAPIIP(r)
 						log.Println(userip, "-ip--", (*res)["userip"])
+						var sign string
 						if (*res)["userip"] == userip { //验证用户ip
 							APPID = appid
 							SECRET = util.ObjToString((*res)["secret"])
-							sign := GET(action, [][]string{
-								[]string{"keyword", keyword},
-								[]string{"appid", appid},
-								[]string{"timestamp", strconv.Itoa(timestamp)},
-								//[]string{"pagenum", strconv.Itoa(pagenum)},
-							})
-							log.Println(action, "--", keyword, "---", appid, "--", timestamp, "--", pagenum)
+							if r.FormValue("pagenum") == "" {
+								pagenum = 1
+								sign = GET(action, [][]string{
+									[]string{"keyword", keyword},
+									[]string{"appid", appid},
+									[]string{"timestamp", strconv.Itoa(timestamp)},
+									//[]string{"pagenum", strconv.Itoa(pagenum)},
+								})
+							} else {
+								sign = GET(action, [][]string{
+									[]string{"keyword", keyword},
+									[]string{"appid", appid},
+									[]string{"timestamp", strconv.Itoa(timestamp)},
+									[]string{"pagenum", strconv.Itoa(pagenum)},
+								})
+							}
 							log.Println(signature, "签名串:", sign)
 							ordernum := (*res)["ordernum"].(int)
 							if sign == signature { //签名串验证
+								username = util.ObjToString((*res)["username"])
 								if action == "getdata" {
 									rData["data"] = GetData(pagenum, keyword, ordernum)
 									redis.Put(REDISDB, "jyoacount-"+appid+oaday, apicount+1, 24*60*60)
@@ -152,6 +162,7 @@ func OpenAction(w http.ResponseWriter, r *http.Request) {
 		rCode = CODE_E1 //签名错误
 		rMsg = MSG_E1
 	}
+	go APIlog(username, action, keyword, appid, rMsg, signature, pagenum, timestamp, rCode, apicount)
 	if rCode == 0 {
 		w.Header().Set("Accept-Charset", "utf-8")
 		w.Header().Set("Content-Type", "application/json")