wangshan 7 gadi atpakaļ
vecāks
revīzija
c30e980156

+ 17 - 22
src/apiservice/oamanager/content.go

@@ -8,7 +8,7 @@ import (
 	"qfw/util/redis"
 	"regexp"
 	"strings"
-	"sync"
+
 	"time"
 
 	"github.com/go-xweb/xweb"
@@ -21,9 +21,6 @@ type Content struct {
 
 var regex = regexp.MustCompile("(Android|Mobile)")
 
-//对map的同步
-var lock sync.Mutex
-
 func init() {
 	xweb.AddAction(&Content{})
 }
@@ -32,7 +29,7 @@ func (c *Content) Article(id string) {
 	client := c.Header("User-Agent")
 	c.Request.Proto = GetIp(c.Request) //ip
 	var data map[string]interface{}
-	addLog(c.Request)
+	go addLog(c.Request)
 	bm := false //访问日志
 	bm = regex.MatchString(client)
 	sid_openid := util.DecodeArticleId2ByCheck(id)[0] //解密id
@@ -64,24 +61,22 @@ 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)
-	lock.Unlock()
+	mongodb.Save("log", logs)
 }
 
 //获取平台类型

+ 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
 }

+ 52 - 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")
@@ -166,6 +177,26 @@ func OpenAction(w http.ResponseWriter, r *http.Request) {
 		w.Write([]byte(fmt.Sprintf("{\"code\":%d,\"msg\":\"%s\"}", rCode, rMsg)))
 	}
 }
+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(),
+	}
+	id := mongodb.Save("apilog", logs)
+	if len(id) < 1 {
+		log.Println("保存调用接口日志失败")
+	}
+}
 
 //
 func GetData(pagenum int, keyword string, ordernum int) (list []map[string]interface{}) {