|
@@ -4,10 +4,13 @@
|
|
|
package usermanager
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
+ // "encoding/json"
|
|
|
"net/http"
|
|
|
"qfw/util"
|
|
|
"qfw/util/redis"
|
|
|
"strings"
|
|
|
+ "sync"
|
|
|
"time"
|
|
|
. "utils"
|
|
|
|
|
@@ -21,178 +24,211 @@ const (
|
|
|
QUERY_LIMIT = 100
|
|
|
)
|
|
|
|
|
|
+var (
|
|
|
+ GetDataMap = map[string]*sync.Mutex{}
|
|
|
+ GetDataMapLock = &sync.Mutex{}
|
|
|
+)
|
|
|
+
|
|
|
+//取增量数据
|
|
|
func GetData(w http.ResponseWriter, r *http.Request) {
|
|
|
- d := JSON{}
|
|
|
+ defer util.Catch()
|
|
|
access_token := r.FormValue("access_token")
|
|
|
- log.Debug(access_token)
|
|
|
+ _, _, d := CheckUserInfo(access_token, 0, 0, 0)
|
|
|
+ WriteJSON(w, &d)
|
|
|
+}
|
|
|
+
|
|
|
+//取全量数据
|
|
|
+func GetAllData(w http.ResponseWriter, r *http.Request) {
|
|
|
+ defer util.Catch()
|
|
|
+ access_token := r.FormValue("access_token")
|
|
|
+ day := util.IntAll(r.FormValue("day"))
|
|
|
+ next := util.IntAll(r.FormValue("next"))
|
|
|
+ _, _, d := CheckUserInfo(access_token, day, next, 1)
|
|
|
+ WriteJSON(w, &d)
|
|
|
+}
|
|
|
+
|
|
|
+func CheckUserInfo(access_token string, day, next, all int) (bcheck bool, appid string, d JSON) {
|
|
|
+ d = JSON{}
|
|
|
+ //第一层判断token是否失效或格式不对
|
|
|
if access_token != "" {
|
|
|
at := RsaDecrypt(access_token)
|
|
|
- b := false
|
|
|
+ log.Debug("token:", at)
|
|
|
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]
|
|
|
+ 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 { //没有数据
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ log.Debug("redis_token", "\t", redis_token, "\t", access_token)
|
|
|
+ if redis_token != "" && redis_token == access_token { //token验证通过,验证今日次数、总条数、服务时间
|
|
|
+ bcheck = true
|
|
|
} else {
|
|
|
d["code"] = CODE_TOKEN_EXPIRE
|
|
|
d["msg"] = MSG_TOKEN_EXPIRE
|
|
|
}
|
|
|
- } else { //超时
|
|
|
+ } else {
|
|
|
d["code"] = CODE_TOKEN_EXPIRE
|
|
|
d["msg"] = MSG_TOKEN_EXPIRE
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if !b {
|
|
|
- d["code"] = CODE_E0
|
|
|
- d["msg"] = MSG_E0
|
|
|
- }
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ if !bcheck && len(d) == 0 {
|
|
|
d["code"] = CODE_E1
|
|
|
d["msg"] = MSG_E1
|
|
|
}
|
|
|
- WriteJSON(w, &d)
|
|
|
+ //第二层判断用户调用权限
|
|
|
+ if bcheck {
|
|
|
+ bcheck = false
|
|
|
+ tn := time.Now()
|
|
|
+ tnUnix := tn.Unix()
|
|
|
+ GetDataMapLock.Lock()
|
|
|
+ appidLock := GetDataMap[appid]
|
|
|
+ if appidLock == nil {
|
|
|
+ appidLock = &sync.Mutex{}
|
|
|
+ GetDataMap[appid] = appidLock
|
|
|
+ }
|
|
|
+ GetDataMapLock.Unlock()
|
|
|
+ appidLock.Lock()
|
|
|
+ defer appidLock.Unlock()
|
|
|
+ //"limittoday_" //值小于0时禁止
|
|
|
+ //"limitnum_" 值为小于零时实时判断
|
|
|
+ //"limittime_" 只判断一次,过期重新判断,用户续费时可以设置此值过期
|
|
|
+ validtime := redis.Get(REDISDB, "limittime_"+appid)
|
|
|
+ if validtime == nil {
|
|
|
+ if res, bflag := Mgo.FindOneByField("user", &map[string]interface{}{
|
|
|
+ "appid": appid,
|
|
|
+ }, `{"plan":1}`); bflag && res != nil && *res != nil && len(*res) > 0 {
|
|
|
+ if m1 := util.ObjToMap((*res)["plan"]); m1 != nil {
|
|
|
+ starttime := util.Int64All((*m1)["starttime"])
|
|
|
+ endtime := util.Int64All((*m1)["endtime"])
|
|
|
+ if starttime <= tnUnix && tnUnix <= endtime { //在服务时间内
|
|
|
+ limittime := int(endtime - tnUnix)
|
|
|
+ redis.Put(REDISDB, "limittime_"+appid, limittime, limittime) //存入值
|
|
|
+ validtime = limittime
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ limittodaykey := fmt.Sprintf("limittoday_%d_%s", tn.Day(), appid)
|
|
|
+ if validtime == nil {
|
|
|
+ d["code"] = CODE_ERR_E1
|
|
|
+ d["msg"] = MSG_ERR_E
|
|
|
+ } else {
|
|
|
+ limittime := util.IntAll(validtime)
|
|
|
+ if limittime > 0 { //在有效期内,判断今日调用次数,判断服务的总条数
|
|
|
+ limittoday := redis.GetInt(REDISDB, limittodaykey)
|
|
|
+ if limittoday > GETDATA_LIMIT_TIMES { //当天调用超过次数
|
|
|
+ d["code"] = CODE_E3
|
|
|
+ d["msg"] = MSG_E3
|
|
|
+ } else {
|
|
|
+ if limittoday == 0 {
|
|
|
+ _, max := GetDayMinMax(tn)
|
|
|
+ redis.Put(REDISDB, limittodaykey, 0, int(max-tnUnix))
|
|
|
+ }
|
|
|
+ //判断剩余服务条数
|
|
|
+ limitnum := redis.Get(REDISDB, "limitnum_"+appid) //值
|
|
|
+ if limitnum == nil { //值为空时从数据库中提取
|
|
|
+ if res1, bflag1 := Mgo.FindOneByField("user", &map[string]interface{}{
|
|
|
+ "appid": appid,
|
|
|
+ }, `{"plan":1}`); bflag1 && res1 != nil && *res1 != nil {
|
|
|
+ if m1 := util.ObjToMap((*res1)["plan"]); m1 != nil {
|
|
|
+ limitnum = (*m1)["current"]
|
|
|
+ if limitnum != nil {
|
|
|
+ redis.Put(REDISDB, "limitnum_"+appid, limitnum, 0)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if limitnum == nil {
|
|
|
+ d["code"] = CODE_ERR_E3
|
|
|
+ d["msg"] = MSG_ERR_E
|
|
|
+ } else {
|
|
|
+ num := util.IntAll(limitnum)
|
|
|
+ if num < 1 {
|
|
|
+ d["code"] = CODE_E5
|
|
|
+ d["msg"] = MSG_E5
|
|
|
+ } else {
|
|
|
+ bcheck = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ d["code"] = CODE_E4
|
|
|
+ d["msg"] = MSG_E4
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //判断通过
|
|
|
+ if bcheck { //取数据
|
|
|
+ next, infos := getDataByAppid(appid, day, next, all, limittodaykey)
|
|
|
+ if all == 1 && next > 0 {
|
|
|
+ d["next"] = next
|
|
|
+ }
|
|
|
+ if len(infos) == 0 {
|
|
|
+ d["data"] = []map[string]interface{}{}
|
|
|
+ } else {
|
|
|
+ d["data"] = infos
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
-//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++ {
|
|
|
+//获取数据
|
|
|
+func getDataByAppid(appid string, i_day, next, i_all int, limittodaykey string) (i_next int, infos []map[string]interface{}) {
|
|
|
+ query := map[string]interface{}{"appid": appid}
|
|
|
+ blastid := false
|
|
|
+ if i_all == 0 { //必须是当天才能使用lastid按顺序取信息
|
|
|
+ blastid = true
|
|
|
+ lastid := redis.GetStr(REDISDB, "lastid_"+appid)
|
|
|
+ if lastid != "" {
|
|
|
+ query["_id"] = map[string]interface{}{
|
|
|
+ "$gt": util.StringTOBsonId(lastid),
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ createtime := map[string]interface{}{}
|
|
|
+ if !blastid && i_day > -6 && i_day < 1 {
|
|
|
+ min, max := GetDayMinMax(time.Now().AddDate(0, 0, i_day))
|
|
|
+ createtime["$gte"] = min
|
|
|
+ createtime["$lte"] = max
|
|
|
+ query["createtime"] = createtime
|
|
|
+ }
|
|
|
|
|
|
-//}
|
|
|
+ log.Debug("query:", query)
|
|
|
+ i_next = next
|
|
|
+ data, bdata := Mgo.Find("usermail", query, `{"_id":1}`, `{"title":1,"detail":1,"publishtime":1,"href":1}`, false, i_next, QUERY_LIMIT)
|
|
|
+ if bdata && data != nil && *data != nil && len(*data) > 0 {
|
|
|
+ infos = *data
|
|
|
+ if blastid {
|
|
|
+ lastid := util.BsonIdToSId((*data)[len(*data)-1]["_id"])
|
|
|
+ if lastid != "" {
|
|
|
+ redis.Put(REDISDB, "lastid_"+appid, lastid, DesZero())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for _, v := range infos {
|
|
|
+ delete(v, "_id")
|
|
|
+ }
|
|
|
+ if len(*data) == QUERY_LIMIT && !blastid {
|
|
|
+ i_next = i_next + QUERY_LIMIT
|
|
|
+ } else {
|
|
|
+ i_next = -1
|
|
|
+ }
|
|
|
+ //处理总条数
|
|
|
+ redis.Decrby(REDISDB, "limitnum_"+appid, len(*data))
|
|
|
+ go Mgo.Save("userdatalog", map[string]interface{}{
|
|
|
+ "appid": appid,
|
|
|
+ "datalen": len(*data),
|
|
|
+ "date": time.Now().Unix(),
|
|
|
+ })
|
|
|
+ } else { //没有数据
|
|
|
+ i_next = 0
|
|
|
+ }
|
|
|
+ //处理今日调用次数
|
|
|
+ redis.Incr(REDISDB, limittodaykey)
|
|
|
+ return
|
|
|
+}
|