|
@@ -0,0 +1,198 @@
|
|
|
|
+/**
|
|
|
|
+数据获取服务
|
|
|
|
+**/
|
|
|
|
+package usermanager
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "net/http"
|
|
|
|
+ "qfw/util"
|
|
|
|
+ "qfw/util/redis"
|
|
|
|
+ "strings"
|
|
|
|
+ "time"
|
|
|
|
+ . "utils"
|
|
|
|
+
|
|
|
|
+ log "github.com/sirupsen/logrus"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+//day:0
|
|
|
|
+//next:0
|
|
|
|
+const (
|
|
|
|
+ GETDATA_LIMIT_TIMES = 1001 //获取数据次数
|
|
|
|
+ QUERY_LIMIT = 100
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+func GetData(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ d := JSON{}
|
|
|
|
+ access_token := r.FormValue("access_token")
|
|
|
|
+ log.Debug(access_token)
|
|
|
|
+ if access_token != "" {
|
|
|
|
+ at := RsaDecrypt(access_token)
|
|
|
|
+ b := false
|
|
|
|
+ if at != "" {
|
|
|
|
+ tn := time.Now().Unix()
|
|
|
|
+ arr := strings.Split(at, ",")
|
|
|
|
+ if len(arr) == 3 { //时间,appid,key
|
|
|
|
+ b = true
|
|
|
|
+ t := util.Int64All(arr[0])
|
|
|
|
+ des := tn - t
|
|
|
|
+ log.Debug("des", des)
|
|
|
|
+ if des >= 0 && des <= TOKEN_TIMEOUT {
|
|
|
|
+ appid := arr[1]
|
|
|
|
+ redis_token := redis.GetStr(REDISDB, "token_"+appid)
|
|
|
|
+ log.Debug("redis_token", redis_token, access_token)
|
|
|
|
+ if redis_token == access_token { //token验证通过,验证今日次数、总条数、服务时间
|
|
|
|
+ //"limittoday_" //值小于0时禁止
|
|
|
|
+ //"limitnum_" 值为小于零时实时判断
|
|
|
|
+ //"limittime_" 只判断一次,过期重新判断,用户续费时可以设置此值过期
|
|
|
|
+ limittime := redis.GetInt(REDISDB, "limittime_"+appid)
|
|
|
|
+ var THISNUM interface{}
|
|
|
|
+ if limittime < 1 {
|
|
|
|
+ res, bflag := Mgo.FindOneByField("user", &map[string]interface{}{
|
|
|
|
+ "appid": appid,
|
|
|
|
+ }, `{"plan":1}`)
|
|
|
|
+ if bflag && res != nil && *res != nil {
|
|
|
|
+ if m1 := util.ObjToMap((*res)["plan"]); m1 != nil {
|
|
|
|
+ THISNUM = (*m1)["current"]
|
|
|
|
+ starttime := util.Int64All((*m1)["starttime"])
|
|
|
|
+ endtime := util.Int64All((*m1)["endtime"])
|
|
|
|
+ if starttime <= tn && tn <= endtime { //在服务时间内
|
|
|
|
+ limittime = int(endtime - tn)
|
|
|
|
+ redis.Put(REDISDB, "limittime_"+appid, limittime, limittime) //存入值
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if limittime > 0 { //在服务期内
|
|
|
|
+ //判断今日次数
|
|
|
|
+ limittoday := redis.Incr(REDISDB, "limittoday_"+appid)
|
|
|
|
+ if limittoday > GETDATA_LIMIT_TIMES { //当天调用超过次数
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ if limittoday == 1 { //设值过期时间
|
|
|
|
+ now := time.Now()
|
|
|
|
+ tomorrow := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, time.Local)
|
|
|
|
+ err := redis.SetExpire(REDISDB, "limittoday_"+appid, int(tomorrow.Unix()-now.Unix()))
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Error("set expire err!", err.Error())
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ limitnum := redis.Get(REDISDB, "limitnum_"+appid) //值
|
|
|
|
+ if limitnum == nil { //值为空时从数据库中提取
|
|
|
|
+ if THISNUM == nil {
|
|
|
|
+ res1, bflag1 := Mgo.FindOneByField("user", &map[string]interface{}{
|
|
|
|
+ "appid": appid,
|
|
|
|
+ }, `{"plan":1}`)
|
|
|
|
+ if bflag1 && res1 != nil && *res1 != nil {
|
|
|
|
+ if m1 := util.ObjToMap((*res1)["plan"]); m1 != nil {
|
|
|
|
+ THISNUM = (*m1)["current"]
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if THISNUM != nil {
|
|
|
|
+ num := util.IntAll(THISNUM)
|
|
|
|
+ if num > 0 {
|
|
|
|
+ bparam := true
|
|
|
|
+ redis.Put(REDISDB, "limitnum_"+appid, num, 0)
|
|
|
|
+ //通过,可以返回取数据 day:0,all:1,next:0 ,100条
|
|
|
|
+ query := map[string]interface{}{"appid": appid}
|
|
|
|
+ i_all := util.IntAll(r.FormValue("all"))
|
|
|
|
+ if i_all == 1 {
|
|
|
|
+
|
|
|
|
+ } else if i_all != 0 {
|
|
|
|
+ bparam = false
|
|
|
|
+ } else {
|
|
|
|
+ query["bget"] = 0
|
|
|
|
+ }
|
|
|
|
+ i_day := util.IntAll(r.FormValue("all"))
|
|
|
|
+ createtime := map[string]interface{}{}
|
|
|
|
+ switch i_day { //保留最近5天数据
|
|
|
|
+ case 0:
|
|
|
|
+
|
|
|
|
+ case -1:
|
|
|
|
+ case -2:
|
|
|
|
+ case -3:
|
|
|
|
+ case -4:
|
|
|
|
+ default:
|
|
|
|
+ bparam = false
|
|
|
|
+ }
|
|
|
|
+ if i_day > -5 && i_day < 1 {
|
|
|
|
+ min, max := GetDayMinMax(time.Now().AddDate(0, 0, i_day))
|
|
|
|
+ createtime["$gte"] = min
|
|
|
|
+ createtime["$lte"] = max
|
|
|
|
+ query["createtime"] = createtime
|
|
|
|
+ } else {
|
|
|
|
+ bparam = false
|
|
|
|
+ }
|
|
|
|
+ if bparam {
|
|
|
|
+ log.Debug("query:", query)
|
|
|
|
+ i_next := util.IntAll(r.FormValue("next"))
|
|
|
|
+ data, bdata := Mgo.Find("usermail", query, `{"_id":1}`, `{"_id":0,"title":1,"detail":1,"publishtime":1,"href":1}`, false, i_next, QUERY_LIMIT)
|
|
|
|
+ if bdata && data != nil && *data != nil && len(*data) > 0 {
|
|
|
|
+ if i_all != 1 {
|
|
|
|
+ //更新
|
|
|
|
+ a1 := (*data)[0]
|
|
|
|
+ idq := map[string]interface{}{
|
|
|
|
+ "$gte": a1["_id"],
|
|
|
|
+ }
|
|
|
|
+ q1 := map[string]interface{}{
|
|
|
|
+ "appid": appid,
|
|
|
|
+ }
|
|
|
|
+ if len(*data) > 0 {
|
|
|
|
+ a2 := (*data)[len(*data)-1]
|
|
|
|
+ idq["$lte"] = a2["_id"]
|
|
|
|
+ }
|
|
|
|
+ q1["_id"] = idq
|
|
|
|
+ bupdate := Mgo.Update("usermail", q1, map[string]interface{}{
|
|
|
|
+ "$set": map[string]interface{}{
|
|
|
|
+ "bget": 1,
|
|
|
|
+ },
|
|
|
|
+ }, false, false)
|
|
|
|
+ if !bupdate {
|
|
|
|
+ log.Error("update get data error:", appid)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ d["data"] = *data
|
|
|
|
+ if len(*data) == QUERY_LIMIT {
|
|
|
|
+ d["next"] = i_next + QUERY_LIMIT
|
|
|
|
+ } else {
|
|
|
|
+ d["next"] = -1
|
|
|
|
+ }
|
|
|
|
+ } else { //没有数据
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ d["code"] = CODE_TOKEN_EXPIRE
|
|
|
|
+ d["msg"] = MSG_TOKEN_EXPIRE
|
|
|
|
+ }
|
|
|
|
+ } else { //超时
|
|
|
|
+ d["code"] = CODE_TOKEN_EXPIRE
|
|
|
|
+ d["msg"] = MSG_TOKEN_EXPIRE
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if !b {
|
|
|
|
+ d["code"] = CODE_E0
|
|
|
|
+ d["msg"] = MSG_E0
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ d["code"] = CODE_E1
|
|
|
|
+ d["msg"] = MSG_E1
|
|
|
|
+ }
|
|
|
|
+ WriteJSON(w, &d)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//sess := Mgo.GetMgoConn()
|
|
|
|
+//defer Mgo.DestoryMongoConn(sess)
|
|
|
|
+//it := sess.DB(Mgo.DbName).C("usermail").Find(&query).Sort("-publishtime").Select(map[string]interface{}{
|
|
|
|
+// "_id": 0, "title": 1, "detail": 1, "publishtime": 1, "href": 1,
|
|
|
|
+//}).Skip(i_next).Limit(100).Iter()
|
|
|
|
+//n := 0
|
|
|
|
+//for tmp := make(map[string]interface{}); it.Next(&tmp); n++ {
|
|
|
|
+
|
|
|
|
+//}
|