diff --git a/models/mgoUserInfo.go b/models/mgoUserInfo.go new file mode 100644 index 0000000..5f70b04 --- /dev/null +++ b/models/mgoUserInfo.go @@ -0,0 +1,13 @@ +package models + +import ( + "gopkg.in/mgo.v2/bson" +) + +type MgoUserInfo struct { + Id bson.ObjectId `bson:"_id"` + Uid string + Name string + Nick string + Avatar string +} diff --git a/service/dao.go b/service/dao.go index b0fc21d..6eb99cf 100644 --- a/service/dao.go +++ b/service/dao.go @@ -6,3 +6,4 @@ import ( ) var MgoParamDao = gocloud.NewDaoMgo(&comm.DbMongo, "Data", "param") +var MgoUserInfoDao = gocloud.NewDaoMgo(&comm.DbMongo, "Data", "userInfo") diff --git a/service/mgoService/info.go b/service/mgoService/info.go new file mode 100644 index 0000000..89d9770 --- /dev/null +++ b/service/mgoService/info.go @@ -0,0 +1,51 @@ +package mgoService + +import ( + "GoClouds/models" + "GoClouds/service" + "GoClouds/service/userService" + gocloud "github.com/mgr9525/go-cloud" + ruisUtil "github.com/mgr9525/go-ruisutil" + "gopkg.in/macaron.v1" + "gopkg.in/mgo.v2" + "gopkg.in/mgo.v2/bson" +) + +func GetUserInfoUid(uid string) *models.MgoUserInfo { + ses := service.MgoUserInfoDao.NewSession() + defer ses.Close() + + e := new(models.MgoUserInfo) + err := ses.C().Find(bson.M{"uid": uid}).One(e) + if err != nil { + if err == mgo.ErrNotFound { + usr := userService.FindXid(uid) + if usr == nil { + return nil + } + e.Id = bson.NewObjectId() + e.Uid = usr.Xid + e.Name = usr.Name + e.Nick = usr.Nick + err := ses.C().Insert(e) + if err != nil { + return nil + } + } else { + return nil + } + } + return e +} +func GetCurrentUserInfo(c *macaron.Context) *models.MgoUserInfo { + defer ruisUtil.Recovers("GetCurrentUser", nil) + tkm := gocloud.GetToken(c) + if tkm == nil { + return nil + } + id := (*tkm)["id"].(string) + if len(id) <= 0 { + return nil + } + return GetUserInfoUid(id) +}