func (self *Settings) Get() { self.TplNames = "settings.html" beego.ReadFromRequest(&self.Controller) sess_userid, _ := self.GetSession("userid").(int64) if usr, err := model.GetUser(sess_userid); usr != nil && err == nil { self.Data["usr"] = *usr return } }
func (self *TimelineHandler) Get() { self.TplNames = "timeline.html" username := self.GetString(":username") uid, _ := self.GetInt(":userid") switch { case username != "": if usr, err := model.GetUserByUsername(username); usr != nil && err == nil { if tls, err := model.GetTimelines(0, 0, "created", usr.Id); err == nil { self.Data["timelines_username"] = username self.Data["timelines"] = *tls return } } else { self.Redirect("/timeline/", 302) } case uid != 0: if usr, err := model.GetUser(uid); usr != nil && err == nil { if tls, err := model.GetTimelines(0, 0, "hotness", usr.Id); err == nil { self.Data["timelines"] = *tls return } } else { self.Redirect("/timeline/", 302) } case uid == 0 && username == "": //首页 if tls, err := model.GetTimelines(0, 0, "hotness", 0); err == nil { self.Data["timelines"] = *tls return } else { self.Redirect("/timeline/", 302) } default: self.Redirect("/timeline/", 302) } }
func (self *SignupHandler) Post() { self.TplNames = "signup.html" flash := beego.NewFlash() email := strings.ToLower(self.GetString("email")) username := strings.ToLower(self.GetString("username")) password := self.GetString("password") repassword := self.GetString("repassword") if password == "" { flash.Error("密码为空~") flash.Store(&self.Controller) return } if password != repassword { flash.Error("两次密码不匹配~") flash.Store(&self.Controller) return } if helper.CheckPassword(password) == false { flash.Error("密码含有非法字符或密码过短(至少4~30位密码)!") flash.Store(&self.Controller) return } if username == "" { flash.Error("用户名是为永久性设定,不能少于4个字或多于30个字,请慎重考虑,不能为空~") flash.Store(&self.Controller) return } if helper.CheckUsername(username) == false { flash.Error("用户名是为永久性设定,不能少于4个字或多于30个字,请慎重考虑,不能为空~") flash.Store(&self.Controller) return } if helper.CheckEmail(email) == false { flash.Error("Email格式不合符规格~") flash.Store(&self.Controller) return } if usrinfo, err := model.GetUserByEmail(email); usrinfo != nil { flash.Error("此账号不能使用~") flash.Store(&self.Controller) return } else if err != nil { flash.Error("检索账号期间出错~") flash.Store(&self.Controller) return } if usrid, err := model.AddUser(email, username, "", "", helper.Encrypt_hash(password, nil), 1); err != nil { flash.Error("用户注册信息写入数据库时发生错误~") flash.Store(&self.Controller) return } else { if usrinfo, err := model.GetUser(usrid); err == nil { //注册账号成功,以下自动登录并设置session self.SetSession("userid", usrid) self.SetSession("username", usrinfo.Username) self.SetSession("userrole", usrinfo.Role) self.SetSession("useremail", usrinfo.Email) self.SetSession("usercontent", usrinfo.Content) flash.Notice("账号登录成功~") flash.Store(&self.Controller) //session 写入后直接跳到首页 self.Redirect("/", 302) } else { flash.Notice("注册账号成功,请手动登录~") flash.Store(&self.Controller) //注册成功后直接跳转到登录页 self.Redirect("/signin/", 302) } } }
func (self *Settings) Profile() { flash := beego.NewFlash() self.TplNames = "settings.html" sess_userid, _ := self.GetSession("userid").(int64) username := self.GetString("username") email := self.GetString("email") nickname := self.GetString("nickname") realname := self.GetString("realname") content := self.GetString("content") birth := self.GetString("birth") fmt.Println(birth) province := self.GetString("province") city := self.GetString("city") company := self.GetString("company") address := self.GetString("address") postcode := self.GetString("postcode") mobile := self.GetString("mobile") website := self.GetString("website") sex, _ := self.GetInt("sex") qq := self.GetString("qq") msn := self.GetString("msn") weibo := self.GetString("weibo") if username == "" { flash.Error("用户名不能为空!") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } if email == "" { flash.Error("Email是你的主账号,和主要联系方式,不能留空~") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } if content == "" { flash.Error("为了让别人更了解你,请务必填写你的个人签名~") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } if helper.CheckUsername(username) == false { flash.Error("用户名包含非法字符,或不合符规格(限4~30个字符)~") flash.Store(&self.Controller) return } if helper.CheckEmail(email) == false { flash.Error("Email格式不合符规格~") flash.Store(&self.Controller) return } if usrinfo, err := model.GetUser(sess_userid); usrinfo != nil && err == nil { usrinfo.Username = username usrinfo.Email = email usrinfo.Nickname = nickname usrinfo.Realname = realname usrinfo.Content = content usrinfo.Birth = time.Now() usrinfo.Province = province usrinfo.City = city usrinfo.Company = company usrinfo.Address = address usrinfo.Postcode = postcode usrinfo.Mobile = mobile usrinfo.Website = website usrinfo.Sex = sex usrinfo.Qq = qq usrinfo.Msn = msn usrinfo.Weibo = weibo if _, err := model.PutUser(usrinfo.Id, usrinfo); err == nil { //更新session self.SetSession("userid", usrinfo.Id) self.SetSession("username", usrinfo.Username) self.SetSession("userrole", usrinfo.Role) self.SetSession("useremail", usrinfo.Email) self.SetSession("usercontent", usrinfo.Content) flash.Notice("设置个人信息成功~") } else { flash.Error("设置个人信息失败~") } flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } else { flash.Error("该账号不存在~") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } }
func (self *Settings) Avatar() { flash := beego.NewFlash() self.TplNames = "settings.html" targetFolder := "/file/" file, handler, e := self.GetFile("avatar") uid := self.GetSession("userid").(int64) if e != nil { flash.Error("SettingsHandler获取文件错误1," + fmt.Sprint(e)) flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } else { if handler != nil { ext := strings.ToLower(path.Ext(handler.Filename)) filename := helper.MD5(time.Now().String()) + ext ipath := targetFolder + time.Now().Format("03/04/") //ipath := targetFolder + helper.FixedpathByNumber(2, 2) os.MkdirAll("."+ipath, 0644) path := ipath + filename f, err := os.OpenFile("."+path, os.O_WRONLY|os.O_CREATE, 0644) if err != nil { flash.Error("SettingsHandler获取文件错误2!") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } else { io.Copy(f, file) defer file.Close() defer f.Close() input_file := "." + path output_file := "." + path output_size := "72x72" output_align := "center" background := "#f0f0f0" newpath := "" //所有上传的图片都会被缩略处理 if err := helper.Thumbnail("crop", input_file, output_file, output_size, output_align, background); err != nil { flash.Error(fmt.Sprint(err)) flash.Store(&self.Controller) if e := os.Remove(path); e != nil { fmt.Println("SettingsHandler清除残余文件出错:", e) } self.Redirect("/settings/", 302) return } else { f.Close() //手动关闭 不然下面会导致重命名文件出错~ //所有文件以该加密方式哈希生成文件名 从而实现针对到用户个体的文件权限识别 filehash, _ := helper.Filehash(helper.Url2local(path), nil) fname := helper.Encrypt_hash(filehash+strconv.Itoa(int(uid)), nil) newpath = ipath + fname + ext if err := os.Rename(helper.Url2local(path), helper.Url2local(newpath)); err != nil { fmt.Println("重命名文件出错:", err) } //文件权限校验 通过说明文件上传转换过程中没发生错误 //首先读取被操作文件的hash值 和 用户请求中的文件hash值 以及 用户当前id的string类型 进行验证 if fhashed, _ := helper.Filehash(helper.Url2local(newpath), nil); helper.Validate_hash(fname, fhashed+strconv.Itoa(int(uid))) { //收到的头像图片存储都设置ctype为 10 与其他图片类型区分开 if _, err := model.AddImage(helper.Url2local(newpath), 0, 10, uid); err != nil { fmt.Print("model.AddImage:", err) } usr, _ := model.GetUser(uid) if usr.Avatar != "" { os.Remove(helper.Url2local(usr.Avatar)) } if usr.AvatarLarge != "" { os.Remove(helper.Url2local(usr.AvatarLarge)) } if usr.AvatarMedium != "" { os.Remove(helper.Url2local(usr.AvatarMedium)) } if usr.AvatarSmall != "" { os.Remove(helper.Url2local(usr.AvatarSmall)) } usr.Avatar = newpath usr.AvatarLarge = newpath usr.AvatarMedium = newpath usr.AvatarSmall = newpath model.PutUser(uid, usr) //hash, _ := utils.Filehash(output_file) //fileInfo, err := os.Stat(output_file) //var fsize int64 = 0 //if err == nil { // fsize = fileInfo.Size() / 1024 //} flash.Notice("成功设置头像为:", handler.Filename) flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } else { flash.Error("SettingsHandler图片添加水印失败!") flash.Store(&self.Controller) if e := os.Remove(helper.Url2local(newpath)); e != nil { fmt.Println("SettingsHandler清除错误水印遗留文件", newpath, "出错:", e) } self.Redirect("/settings/", 302) return } } } } else { flash.Error("SettingsHandler获取文件错误3!") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } } }
func (self *Settings) Password() { flash := beego.NewFlash() self.TplNames = "settings.html" sess_userid, _ := self.GetSession("userid").(int64) curpass := self.GetString("curpass") newpassword := self.GetString("password") newrepassword := self.GetString("repassword") if curpass == "" { flash.Error("当前密码不能为空!") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } if newpassword == "" { flash.Error("设置密码不能为空!") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } if newrepassword == "" { flash.Error("重验设置密码不能为空!") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } if newpassword != newrepassword { flash.Error("两次密码不一致!") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } if helper.CheckPassword(curpass) == false { flash.Error("当前密码含有非法字符或当前密码过短(至少4~30位密码)!") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } if helper.CheckPassword(newpassword) == false { flash.Error("设置密码含有非法字符或设置密码过短(至少4~30位密码)!") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } if usrinfo, err := model.GetUser(sess_userid); usrinfo != nil && err == nil { if helper.Validate_hash(usrinfo.Password, curpass) { usrinfo.Password = helper.Encrypt_hash(newpassword, nil) if _, err := model.PutUser(usrinfo.Id, usrinfo); err == nil { flash.Notice("设置密码成功~") } else { flash.Error("设置密码失败~") } flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } else { flash.Error("密码无法通过校验~") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } } else { flash.Error("该账号不存在~") flash.Store(&self.Controller) self.Redirect("/settings/", 302) return } }
func (self *NewReplyHandler) Post() { tid, _ := self.GetInt(":tid") sess_userid, _ := self.GetSession("userid").(int64) author := self.GetString("author") email := self.GetString("email") website := self.GetString("website") rc := self.GetString("comment") //不等于0,即是注册用户或管理层 此时把ctype设置为1 主要是为了区分游客 if sess_userid != 0 { if tid > 0 && rc != "" { if usr, err := model.GetUser(sess_userid); err == nil { //为安全计,先行保存回应,顺手获得rid,在后面顺手再更新替换@通知的链接 if rid, err := model.AddReply(tid, sess_userid, 1, rc, usr.Username, usr.Content, usr.Email, usr.Website); err != nil { fmt.Println("#", rid, ":", err) } else { //如果回应内容中有@通知 则处理以下事件 if users := helper.AtUsers(rc); len(users) > 0 { if tp, err := model.GetTopic(tid); err == nil { todo := []string{} for _, v := range users { //判断被通知之用户名是否真实存在 if u, e := model.GetUserByUsername(v); e == nil && u != nil { //存在的则加入待操作列 todo = append(todo, v) //替换被通知用户的用户名带上用户主页链接 rc = strings.Replace(rc, "@"+v, "<a href='/user/"+u.Username+"/' title='"+u.Nickname+"' target='_blank'><span>@</span><span>"+u.Username+"</span></a>", -1) //发送通知内容到用户的 时间线 model.AddTimeline(usr.Username+"在「"+tp.Title+"」的回应里提到了你~", rc+"[<a href='/topic/"+self.GetString(":tid")+"/#reply-"+strconv.Itoa(int(rid))+"'>"+tp.Title+"</a>]", tp.Cid, tp.Nid, u.Id, usr.Username, usr.Content) } } if len(todo) > 0 { model.SetReplyContentByRid(rid, rc) } } } self.Redirect("/topic/"+self.GetString(":tid")+"/#reply-"+strconv.Itoa(int(rid)), 302) return } } self.Redirect("/topic/"+self.GetString(":tid")+"/", 302) } else if tid > 0 { self.Redirect("/topic/"+self.GetString(":tid")+"/", 302) } else { self.Redirect("/", 302) } } else { //游客回应 此时把ctype设置为-1 游客不开放@通知功能 if author != "" && email != "" && tid > 0 && rc != "" { if rid, err := model.AddReply(tid, sess_userid, -1, rc, author, "", email, website); err != nil { fmt.Println("#", rid, ":", err) self.Redirect("/topic/"+self.GetString(":tid")+"/", 302) } else { self.Redirect("/topic/"+self.GetString(":tid")+"/#reply-"+strconv.Itoa(int(rid)), 302) } } else if tid > 0 { self.Redirect("/topic/"+self.GetString(":tid")+"/", 302) } else { self.Redirect("/", 302) } } }
func (self *TimelineHandler) Post() { self.TplNames = "timeline.html" sess_userid, _ := self.GetSession("userid").(int64) sess_username, _ := self.GetSession("username").(string) tl := self.GetString("timeline") //不等于0,即是注册用户或管理层 if sess_userid != 0 { if tl != "" { //获取当前用户信息 if usr, err := model.GetUser(sess_userid); usr != nil && err == nil { //前一条是记录发送者自己的timeline 所以这里的接受者是自己 记录完自己的timeline后 再处理自己timeline的内容里的@通知 if lid, err := model.AddTimeline("", tl, 0, 0, sess_userid, usr.Username, usr.Content); err != nil { fmt.Println("#", lid, ":", err) } else { //如果 内容tl中有@通知 则处理以下事件 if users := helper.AtUsers(tl); len(users) > 0 { //todo := []string{} //被通知列表 k是uid v是username todolist := map[int64]string{} for _, v := range users { //判断被通知之用户名是否真实存在 if u, e := model.GetUserByUsername(v); e == nil && u != nil { //存在的则加入待操作列 //todo = append(todo, v) todolist[u.Id] = u.Username //替换被通知用户的用户名带上用户主页链接 tl = strings.Replace(tl, "@"+v, "<a href='/user/"+u.Username+"/' title='"+u.Nickname+"' target='_blank'><span>@</span><span>"+u.Username+"</span></a>", -1) //发送通知内容到被通知用户的 时光记录 注意这里的uid不再是sess_userid 而是u.Id model.AddTimeline(usr.Username+"提到了你~", tl+"[<a href='/user/"+usr.Username+"/#timeline-"+strconv.Itoa(int(lid))+"'>"+usr.Username+"</a>]", 0, 0, u.Id, usr.Username, usr.Content) } } //如果有@通知操作 则重新替换一次发送者已存档的内容 if len(todolist) > 0 { model.SetTimelineContentByRid(lid, tl) } } //处理@link if atpagez, _ := helper.AtPages(tl); len(atpagez) > 0 { tid := int64(0) if tid, tl, err = model.AtLinksPostImagesOnTopic(tl); err == nil { model.SetTimelineContentByRid(lid, tl+" <a href='/topic/"+strconv.Itoa(int(tid))+"/' target='_blank'>[#美图合辑("+strconv.Itoa(int(tid))+")#]</a>") } } self.Redirect("/user/"+sess_username+"/#timeline-"+strconv.Itoa(int(lid)), 302) return } } self.Redirect("/user/"+sess_username+"/", 302) } else { self.Redirect("/timeline/", 302) } } else { self.Redirect("/timeline/", 302) } }