|
@@ -1,6 +1,7 @@
|
|
|
package oamanager
|
|
|
|
|
|
import (
|
|
|
+ . "apiservice/config"
|
|
|
"apiservice/tools"
|
|
|
"bytes"
|
|
|
"compress/gzip"
|
|
@@ -39,8 +40,8 @@ func init() {
|
|
|
}
|
|
|
|
|
|
const (
|
|
|
- LIMIT_TIME = 300 //签名超时时间
|
|
|
- LIMIT_COUNT = 3000 //每天接口限制
|
|
|
+ LIMIT_TIME = 30000 //签名超时时间
|
|
|
+ LIMIT_COUNT = 3000 //每天接口限制
|
|
|
REDISDB = "jyOpenAPI"
|
|
|
CODE_E1 = 40000
|
|
|
MSG_E1 = "签名错误"
|
|
@@ -54,10 +55,9 @@ const (
|
|
|
MSG_E5 = "内部错误"
|
|
|
CODE_E6 = 40005
|
|
|
MSG_E6 = "IP未授权"
|
|
|
- MSG_SUCCESS = "请求成功"
|
|
|
INDEX = "bidding"
|
|
|
TYPE = "bidding"
|
|
|
- LIMIT_LIST_COUNT = 100
|
|
|
+ LIMIT_LIST_COUNT = 10
|
|
|
)
|
|
|
|
|
|
var APPID, SECRET string
|
|
@@ -69,11 +69,12 @@ func (o *Oamanager) Index() error {
|
|
|
|
|
|
//
|
|
|
func OpenAction(w http.ResponseWriter, r *http.Request) {
|
|
|
+ defer util.Catch()
|
|
|
+ oaday := strconv.Itoa(time.Now().Day())
|
|
|
rData := make(map[string]interface{})
|
|
|
var rCode = 0
|
|
|
var rMsg = ""
|
|
|
- defer util.Catch()
|
|
|
- if r.Method == "POST" {
|
|
|
+ if r.Method == "POST" { //POST请求
|
|
|
var (
|
|
|
action = r.FormValue("action")
|
|
|
keyword = r.FormValue("keyword")
|
|
@@ -83,31 +84,35 @@ func OpenAction(w http.ResponseWriter, r *http.Request) {
|
|
|
ttamp = time.Now().Unix()
|
|
|
timestamp, _ = strconv.Atoi(r.FormValue("timestamp"))
|
|
|
)
|
|
|
- if appid == "" || action == "" || keyword == "" || timestamp == 0 || signature == "" { //
|
|
|
- rCode = CODE_E1 //签名错误
|
|
|
- rMsg = "签名错误"
|
|
|
+ if appid == "" || action == "" || keyword == "" || timestamp == 0 || signature == "" { //参数验证
|
|
|
+ rCode = CODE_E2 //签名错误
|
|
|
+ rMsg = MSG_E2
|
|
|
} else {
|
|
|
ttmp1 := strconv.Itoa(timestamp)
|
|
|
ttmp2, _ := strconv.ParseInt(ttmp1, 10, 64)
|
|
|
- if (ttamp - ttmp2) > LIMIT_TIME {
|
|
|
+ if (ttamp - ttmp2) > LIMIT_TIME { //签名时间验证
|
|
|
rCode = CODE_E3 //签名过期
|
|
|
- rMsg = "签名过期"
|
|
|
+ rMsg = MSG_E3
|
|
|
} else {
|
|
|
- apicount := redis.GetInt(REDISDB, "jyopenapi-"+appid)
|
|
|
+ apicount := redis.GetInt(REDISDB, "jyoacount-"+appid+oaday)
|
|
|
log.Println("每天调用次数:", apicount)
|
|
|
- if apicount >= LIMIT_COUNT {
|
|
|
+ if apicount >= LIMIT_COUNT { //调用接口上限判断
|
|
|
rCode = CODE_E4 //调用接口超过限制
|
|
|
- rMsg = "调用接口超过限制"
|
|
|
+ rMsg = MSG_E4
|
|
|
} else {
|
|
|
if pagenum == 0 {
|
|
|
pagenum = 1
|
|
|
}
|
|
|
- res, _ := mongodb.FindOneByField("user", &map[string]interface{}{
|
|
|
+ res, ok := mongodb.FindOneByField("user", &map[string]interface{}{
|
|
|
"appid": appid,
|
|
|
}, nil)
|
|
|
- if len(*res) > 0 {
|
|
|
- comip := GetAPIIP(r)
|
|
|
- if (*res)["userip"] == comip {
|
|
|
+ if !ok {
|
|
|
+ rCode = CODE_E5 //内部错误
|
|
|
+ rMsg = MSG_E5
|
|
|
+ }
|
|
|
+ if len(*res) > 0 && (*res)["status"] == 1 { //查看用户是否存在
|
|
|
+ userip := GetAPIIP(r)
|
|
|
+ if (*res)["userip"] == userip { //验证用户ip
|
|
|
APPID = appid
|
|
|
SECRET = util.ObjToString((*res)["secret"])
|
|
|
sign := GET(action, [][]string{
|
|
@@ -116,28 +121,34 @@ func OpenAction(w http.ResponseWriter, r *http.Request) {
|
|
|
[]string{"timestamp", strconv.Itoa(timestamp)},
|
|
|
[]string{"pagenum", strconv.Itoa(pagenum)},
|
|
|
})
|
|
|
- ordernum := util.ObjToString((*res)["ordernum"])
|
|
|
- if sign == signature {
|
|
|
+ log.Println(signature, "签名串:", sign)
|
|
|
+ ordernum := (*res)["ordernum"].(int)
|
|
|
+ if sign == signature { //签名串验证
|
|
|
if action == "getdata" {
|
|
|
- rData["data"], rData["count"] = GetData(pagenum, keyword, ordernum)
|
|
|
- redis.Put(REDISDB, "jyopenapi-"+appid, apicount+1, 0)
|
|
|
+ rData["data"] = GetData(pagenum, keyword, ordernum)
|
|
|
+ redis.Put(REDISDB, "jyoacount-"+appid+oaday, apicount+1, 24*60*60)
|
|
|
+ } else {
|
|
|
+ rCode = CODE_E1 //签名错误
|
|
|
+ rMsg = MSG_E1
|
|
|
}
|
|
|
} else {
|
|
|
rCode = CODE_E1 //签名错误
|
|
|
- rMsg = "签名错误"
|
|
|
+ rMsg = MSG_E1
|
|
|
}
|
|
|
} else {
|
|
|
+ rCode = CODE_E6 //IP未授权
|
|
|
+ rMsg = MSG_E6
|
|
|
}
|
|
|
} else {
|
|
|
rCode = CODE_E1 //签名错误
|
|
|
- rMsg = "签名错误"
|
|
|
+ rMsg = MSG_E1
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
rCode = CODE_E1 //签名错误
|
|
|
- rMsg = "签名错误"
|
|
|
+ rMsg = MSG_E1
|
|
|
}
|
|
|
if rCode == 0 {
|
|
|
w.Header().Set("Accept-Charset", "utf-8")
|
|
@@ -155,18 +166,21 @@ func OpenAction(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
|
|
|
//
|
|
|
-func GetData(pagenum int, keyword string, ordernum string) (list []map[string]interface{}, count int) {
|
|
|
+func GetData(pagenum int, keyword string, ordernum int) (list []map[string]interface{}) {
|
|
|
+ // query1 := `{"query": {"bool": {"must":[`
|
|
|
+ // query1 += `{"term":{"winner":"` + keyword + `"}}`
|
|
|
+ // query1 += `],"should": [],"minimum_should_match": 0}}}`
|
|
|
+ // count = elastic.Count(INDEX, TYPE, query1)
|
|
|
start := (pagenum - 1) * LIMIT_LIST_COUNT
|
|
|
query := `{"TERM_winner":"` + keyword + `"}`
|
|
|
- res, ct := elastic.GetOAPage(INDEX, TYPE, query, `{"publishtime":-1}`, `"_id","title","publishtime"`, start, LIMIT_LIST_COUNT)
|
|
|
+ res, _ := 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"
|
|
|
+ v["href"] = Sysconfig["jyaddress"].(string) + "/article/" + util.EncodeArticleId2ByCheck(util.ObjToString(v["_id"]), strconv.Itoa(ordernum)) + ".html"
|
|
|
delete(v, "_id")
|
|
|
}
|
|
|
}
|
|
|
list = *res
|
|
|
- count = ct
|
|
|
return
|
|
|
}
|
|
|
|