func EC(paramMap map[string]string) interface{} { HostAdd := util.GetMMSDAServer() client := mcpacknpc.NewClient(HostAdd) request := packEC(paramMap) reply := new(Reply) start := time.Now().Nanosecond() //test client.Call(request, reply) returnda := parserReply(reply) beego.Notice("++++daTime+++++", (time.Now().Nanosecond() - start)) //test if paramMap["tp"] == "api" { return returnda } else { baidu := baidubase64.NewBaiduB64() retMap := make(map[string]interface{}, 1) retByte, err := json.Marshal(returnda) if err != nil { beego.Notice("json error:", err) return returnda } retMap["data"] = baidu.BaiduB64Encode(string(retByte), 0) //retMap["data"] = base64.StdEncoding.EncodeToString(retByte) return retMap } }
//检查验证信息是否过期和是否正确存在 func CheckVcode(ucode Pair) (string, error) { if ucode.Vcode == "" { return "", errors.New("效验id为空,可能是非法访问") } var vcode Pair s1 := GetSession() defer s1.Close() coll := s1.DB("test").C("uvcode") err := coll.Find(bson.M{"vcode": ucode.Vcode}).One(&vcode) if err != nil { beego.Notice(err.Error()) return "", err } //按照本地时区解析时间 loc, _ := time.LoadLocation("Local") t1, _ := time.ParseInLocation("2006-01-02 15:04:05", vcode.Nowtime, loc) beego.Notice(t1.Format("2006-01-02 15:04:05")) t2 := time.Now().Local() beego.Notice(t2.Format("2006-01-02 15:04:05")) u1 := t2.Sub(t1) beego.Notice(u1.String()) //设置过期时间为5分钟 if u1.Minutes() > 5 { coll.Remove(bson.M{"vcode": ucode.Vcode}) return "", errors.New("该密匙已经过期") } beego.Notice(vcode.UserName) //验证完成后从数据库里移除 coll.Remove(bson.M{"vcode": ucode.Vcode}) return vcode.UserName, nil }
func GetMMSDAServer() []string { mmsdaIdc := beego.AppConfig.DefaultString("mmsdaIdc", "tc") bnsIP := MemCache.Get(MMSDAConf) HostAdd, ok := bnsIP.([]string) beego.Notice("MemCache.Get", len(HostAdd)) if !ok || len(HostAdd) == 0 { bnsClient := bns.NewClient() bnsName := beego.AppConfig.DefaultString(MMSDAConf, "group.mmsda.MMS.all:proxy") HostAdd, _ = bns.GetAddr(bnsClient, bnsName, mmsdaIdc) MemCache.Put(MMSDAConf, HostAdd, 120*time.Second) beego.Notice("BNS.Get", len(HostAdd)) } return HostAdd }
// @router /api/users/signup [post] func (u *UserController) Adduser() { var user models.User user.UserName = u.GetString("username") user.PassWord = u.GetString("password") user.TrueName = u.GetString("truename") user.StuNum = u.GetString("stunum") user.School = u.GetString("school") user.SchoolEx = u.GetString("schoolex") user.Email = u.GetString("email") user.UserPic = "defult.png" beego.Notice(user) err := models.CheckUserIsRight(user) if err != nil { u.Data["json"] = models.ErrorContext{Err: err.Error()} u.ServeJson() } else { err = models.AddUser(user) if err != nil { u.Data["json"] = models.ErrorContext{Err: err.Error()} u.ServeJson() } else { u.Data["json"] = models.ErrorContext{Data: "注册成功"} u.ServeJson() } } }
func AddUser(u User) error { beego.Notice(u) s1 := GetSession() beego.Notice(s1) defer s1.Close() coll := s1.DB("test").C("users") n, err := coll.Find(bson.M{"username": u.UserName}).Count() if err != nil { return err } if n > 0 { return errors.New("用户名已经被注册了") } err = coll.Insert(&u) if err != nil { return err } return nil }
//检查输入合法性。后台永远不要相信用户输入。 //待完成。多数检查还未完成 func CheckFromIsRight(f From) error { beego.Notice(f) if len(f.Title) == 0 || len(f.Content) == 0 { return errors.New("标题或简介不能为空") } if len(f.BeginDay) == 0 || len(f.EndDay) == 0 { return errors.New("起始或结束时间不能为空") } //需要检查过期时间和当前时间 return nil }
// func DebugOutputColor(log string, level int) { // switch level { // case 1: // color.Red(log) // case 2: // color.Yellow(log) // case 3: // color.Green(log) // case 4: // color.Blue(log) // } // } func DebugOutputBeego(log string, level int) { switch level { case 1: beego.Error(log) case 2: beego.Notice(log) case 3: beego.Informational(log) case 4: beego.Debug(log) } }
/** * 获取RedisClient实例,但是不能使用单例 **/ func GetRedisCilent() *redis_bns.RedisClient { redisIdc := beego.AppConfig.DefaultString("redisIdc", "bj") bnsName := beego.AppConfig.DefaultString("redis_bnsName", "group.proxy-graph-f2e-new.OPENSTORAGE.all") maxIdle := beego.AppConfig.DefaultInt("redis_maxIdle", 100) // max Idle connection connectTimeout := beego.AppConfig.DefaultInt("redis_connectTimeout", 60) // connection timeout in ms readTimeout := beego.AppConfig.DefaultInt("redis_readTimeout", 100) // read redis server timeout in ms writeTimeout := beego.AppConfig.DefaultInt("redis_writeTimeout", 100) // write redis server timeout in ms idleTimeout := beego.AppConfig.DefaultInt("redis_idleTimeout", 60000) var state module_state2.State state.Init() beego.Notice("GetRedisCilent") redisClient := redis_bns.NewRedisClient(bnsName, redisIdc, maxIdle, connectTimeout, readTimeout, writeTimeout, idleTimeout, &state) return redisClient }
// @router /api/users/login [post] func (u *UserController) UserLogin() { var user models.User user.UserName = u.GetString("username") user.PassWord = u.GetString("password") beego.Notice(user) err := models.CheckUserWhenLogin(user) if err != nil { u.Data["json"] = models.ErrorContext{Err: err.Error()} u.ServeJson() } else { u.SetSession("longined", user.UserName) u.Data["json"] = models.ErrorContext{Data: "登陆成功"} u.ServeJson() } }
// @router /register [post] func (c *RegistController) Register() { var usr models.User c.ParseForm(&usr) beego.Notice(usr) valid := validation.Validation{} usr.Valid(&valid) if valid.HasErrors() { c.Redirect("/", 302) } n := models.RegisterUser(&usr) if n <= 0 { c.Redirect("/", 302) } c.LoginSetSession(n) c.Redirect("/", 302) }
// @router /register [post] func (c *RegisterController) Register() { var usr models.User c.ParseForm(&usr) beego.Notice(usr) valid := validation.Validation{} usr.Valid(&valid) if valid.HasErrors() { c.Abort("401") } n := models.RegisterUser(&usr) if n <= 0 { c.Abort("401") } c.MainController.LoginSetSession(n) c.MainController.Get() }
//20151006添加添加用户头像逻辑 // @router /api/users/changs/userheadpic [post] func (u *UserController) AddUserHeadPic() { picfliter := [2]string{"jpg", "png"} var uc models.UserCore var ok bool uc.UserName, ok = u.GetSession("longined").(string) if !ok { u.Data["json"] = models.ErrorContext{Err: "发生了未知错误"} u.ServeJson() } else { f, fh, err := u.GetFile("userheadpic") if err != nil { beego.Error(err.Error()) u.Abort("404") } defer f.Close() arr := strings.Split(fh.Filename, ".") ok = false for _, v := range picfliter { if v == arr[len(arr)-1] { ok = true } } if ok { picname := fmt.Sprintf("%s.%s", uc.UserName, arr[len(arr)-1]) picpath := "./static/img/UserPic/headPic/" + picname beego.Notice(picpath) uc.UserPic = picname fheadle, err := os.OpenFile(picpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) if err != nil { beego.Error(err.Error()) } defer fheadle.Close() io.Copy(fheadle, f) err = models.AddUserPic(uc) if err != nil { beego.Alert(err.Error()) u.Data["json"] = models.ErrorContext{Err: "发生了未知错误"} u.ServeJson() } else { u.Data["json"] = models.ErrorContext{Data: "上传成功"} u.ServeJson() } } } }
func (self *Server) Handle() (bool, error) { self.body = self.ctx.Input.RequestBody var err error if len(self.body) < (PROTOCOL_LENGTH + PACKAGER_LENGTH) { return false, errors.New("read request body error.") } header, err := self.getHeader() if err != nil { beego.Error(err) return false, err } request, err := self.getRequest(header) if err != nil { beego.Error(err) return false, err } response := NewResponse() response.Status = ERR_OKEY response.Protocol = header self.call(request, response) self.sendResponse(response) if response.Status != ERR_OKEY { beego.Warn(request.Id, request.Method, response.Error) } else { beego.Notice(request.Id, request.Method, "OKEY") } return true, nil }
// @router /api/users/forget [post] func (u *UserController) PreForgotPassWord() { var user models.User tempdata := u.GetString("data") json.Unmarshal([]byte(tempdata), &user) beego.Notice(user) err := models.CheckUSerNameAndEmail(user) if err != nil { u.Data["json"] = models.ErrorContext{Err: err.Error()} } else { u.Data["json"] = models.ErrorContext{Data: "验证通过"} var vcode models.Pair vcode.UserName = user.UserName key := models.MakeVcode(vcode.UserName) vcode.Vcode = key vcode.Nowtime = time.Now().Format("2006-01-02 15:04:05") //models.ForGotSend(user.Email, vcode.Vcode) models.SaveVcode(vcode) } u.ServeJson() }
//向后台添加项目 // @router /api/froms/changs/publish [post] func (f *FromController) FromPublishByUser() { var from models.From tmp := f.GetString("data") beego.Notice(tmp) json.Unmarshal([]byte(tmp), &from) //加入当前时间 from.BeginDay = time.Now().Format("2006-01-02 15:04:05") //获得当前用户 from.MasterName, _ = f.GetSession("longined").(string) //检查数据正确性 err := models.CheckFromIsRight(from) models.FullFrom(&from) if err != nil { f.Data["json"] = models.ErrorContext{Err: err.Error()} f.ServeJson() } else { models.AddFrom(from) f.Data["json"] = models.ErrorContext{Data: "添加成功"} f.ServeJson() } }
// @router /api/users/forgetresult [post] func (u *UserController) ForgotPassWordFianl() { var us models.User var vcode models.Pair var err error var forgotreq forgotReq tempdata := u.GetString("data") json.Unmarshal([]byte(tempdata), &forgotreq) vcode.Vcode = forgotreq.Vcode beego.Notice(vcode.Vcode) us.UserName, err = models.CheckVcode(vcode) if err != nil { u.Data["json"] = models.ErrorContext{Err: err.Error()} } else { us.PassWord = forgotreq.Pass err := models.UserRestPassWord(us) if err != nil { u.Data["json"] = models.ErrorContext{Err: "发生了未知的错误!"} } else { u.Data["json"] = models.ErrorContext{Data: "重置成功!"} } } u.ServeJson() }