|
@@ -39,8 +39,8 @@ func init() {
|
|
|
}
|
|
|
|
|
|
const (
|
|
|
- LIMIT_TIME = 30000 //签名超时时间
|
|
|
- LIMIT_COUNT = 3000 //每天接口限制
|
|
|
+ LIMIT_TIME = 300 //签名超时时间
|
|
|
+ LIMIT_COUNT = 3000 //每天接口限制
|
|
|
REDISDB = "jyOpenAPI"
|
|
|
CODE_E1 = 40000
|
|
|
MSG_E1 = "签名错误"
|
|
@@ -73,68 +73,71 @@ func OpenAction(w http.ResponseWriter, r *http.Request) {
|
|
|
var rCode = 0
|
|
|
var rMsg = ""
|
|
|
defer util.Catch()
|
|
|
- //if o.Method() == "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_E1 //签名错误
|
|
|
- rMsg = "签名错误"
|
|
|
- } else {
|
|
|
- ttmp1 := strconv.Itoa(timestamp)
|
|
|
- ttmp2, _ := strconv.ParseInt(ttmp1, 10, 64)
|
|
|
- if (ttamp - ttmp2) > LIMIT_TIME {
|
|
|
- rCode = CODE_E3 //签名过期
|
|
|
- rMsg = "签名过期"
|
|
|
+ if r.Method == "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_E1 //签名错误
|
|
|
+ rMsg = "签名错误"
|
|
|
} else {
|
|
|
- apicount := redis.GetInt(REDISDB, "jyopenapi-"+appid)
|
|
|
- log.Println("每天调用次数:", apicount, rMsg)
|
|
|
- if apicount > LIMIT_COUNT {
|
|
|
- rCode = CODE_E4 //调用接口超过限制
|
|
|
- rMsg = "调用接口超过限制"
|
|
|
+ ttmp1 := strconv.Itoa(timestamp)
|
|
|
+ ttmp2, _ := strconv.ParseInt(ttmp1, 10, 64)
|
|
|
+ if (ttamp - ttmp2) > LIMIT_TIME {
|
|
|
+ rCode = CODE_E3 //签名过期
|
|
|
+ rMsg = "签名过期"
|
|
|
} else {
|
|
|
- if pagenum == 0 {
|
|
|
- pagenum = 1
|
|
|
- }
|
|
|
- res, _ := mongodb.FindOneByField("user", &map[string]interface{}{
|
|
|
- "appid": appid,
|
|
|
- }, nil)
|
|
|
- if len(*res) > 0 {
|
|
|
- comip := GetAPIIP(r)
|
|
|
- log.Println((*res)["userip"], "---", comip)
|
|
|
- if (*res)["userip"] == comip {
|
|
|
- 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)},
|
|
|
- })
|
|
|
- ordernum := util.ObjToString((*res)["ordernum"])
|
|
|
- if sign == signature {
|
|
|
- if action == "getdata" {
|
|
|
- rData["data"], rData["count"] = GetData(pagenum, keyword, ordernum)
|
|
|
- redis.Put(REDISDB, "jyopenapi-"+appid, apicount+1, 0)
|
|
|
+ apicount := redis.GetInt(REDISDB, "jyopenapi-"+appid)
|
|
|
+ log.Println("每天调用次数:", apicount)
|
|
|
+ if apicount >= LIMIT_COUNT {
|
|
|
+ rCode = CODE_E4 //调用接口超过限制
|
|
|
+ rMsg = "调用接口超过限制"
|
|
|
+ } else {
|
|
|
+ if pagenum == 0 {
|
|
|
+ pagenum = 1
|
|
|
+ }
|
|
|
+ res, _ := mongodb.FindOneByField("user", &map[string]interface{}{
|
|
|
+ "appid": appid,
|
|
|
+ }, nil)
|
|
|
+ if len(*res) > 0 {
|
|
|
+ comip := GetAPIIP(r)
|
|
|
+ if (*res)["userip"] == comip {
|
|
|
+ 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)},
|
|
|
+ })
|
|
|
+ ordernum := util.ObjToString((*res)["ordernum"])
|
|
|
+ if sign == signature {
|
|
|
+ if action == "getdata" {
|
|
|
+ rData["data"], rData["count"] = GetData(pagenum, keyword, ordernum)
|
|
|
+ redis.Put(REDISDB, "jyopenapi-"+appid, apicount+1, 0)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ rCode = CODE_E1 //签名错误
|
|
|
+ rMsg = "签名错误"
|
|
|
}
|
|
|
} else {
|
|
|
- rCode = CODE_E1 //签名错误
|
|
|
- rMsg = "签名错误"
|
|
|
}
|
|
|
} else {
|
|
|
+ rCode = CODE_E1 //签名错误
|
|
|
+ rMsg = "签名错误"
|
|
|
}
|
|
|
- } else {
|
|
|
- rCode = CODE_E1 //签名错误
|
|
|
- rMsg = "签名错误"
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ } else {
|
|
|
+ rCode = CODE_E1 //签名错误
|
|
|
+ rMsg = "签名错误"
|
|
|
}
|
|
|
if rCode == 0 {
|
|
|
w.Header().Set("Accept-Charset", "utf-8")
|
|
@@ -152,14 +155,10 @@ func OpenAction(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
|
|
|
//
|
|
|
-func GetData(pagenum int, keyword string, ordernum string) (list []map[string]interface{}, count int64) {
|
|
|
- query1 := `{"query": {"bool": {"must":[`
|
|
|
- query1 += `{"term":{"winner":"` + keyword + `"}}`
|
|
|
- query1 += `],"should": [],"minimum_should_match": 0}}}`
|
|
|
- count = elastic.Count(INDEX, TYPE, query1)
|
|
|
+func GetData(pagenum int, keyword string, ordernum string) (list []map[string]interface{}, count int) {
|
|
|
start := (pagenum - 1) * LIMIT_LIST_COUNT
|
|
|
query := `{"TERM_winner":"` + keyword + `"}`
|
|
|
- res := elastic.GetPage(INDEX, TYPE, query, `{"publishtime":-1}`, `"_id","title","publishtime"`, start, LIMIT_LIST_COUNT)
|
|
|
+ res, ct := elastic.GetOAPage(INDEX, TYPE, query, `{"publishtime":-1}`, `"_id","title","publishtime"`, start, LIMIT_LIST_COUNT)
|
|
|
if len(*res) > 0 {
|
|
|
for _, v := range *res {
|
|
|
v["href"] = "/article/" + util.EncodeArticleId2ByCheck(util.ObjToString(v["_id"]), ordernum) + ".html"
|
|
@@ -167,6 +166,7 @@ func GetData(pagenum int, keyword string, ordernum string) (list []map[string]in
|
|
|
}
|
|
|
}
|
|
|
list = *res
|
|
|
+ count = ct
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -209,7 +209,6 @@ func GET(action string, param [][]string) (signedStr string) {
|
|
|
percentEncode("/") + "&" +
|
|
|
percentEncode(reqStr)
|
|
|
str = SP(str, "%3A", "%253A", -1)
|
|
|
- log.Println("-11-:", str)
|
|
|
h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(SECRET+"&"))
|
|
|
io.WriteString(h, str)
|
|
|
signedStr = base64.StdEncoding.EncodeToString(h.Sum(nil))
|