func ReminderHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) username := req.FormValue("username") curUser, _ := filter.CurrentUser(req) if username == "" || req.Method != "POST" || vars["json"] == "" { // 获取用户信息 user := service.FindUserByUsername(curUser["username"].(string)) // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeUsers": "active", "user": user}) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/user/reminder.html") return } // 只能编辑自己的信息 if username != curUser["username"].(string) { fmt.Fprint(rw, `{"errno": 1, "error": "非法请求"}`) return } // open传递过来的是“on”或没传递 if req.FormValue("Emailnotice") == "on" { req.Form.Set("Emailnotice", "1") } else { req.Form.Set("Emailnotice", "0") } // 更新个人信息 errMsg, err := service.UpdateUserReminder(req.Form) if err != nil { fmt.Fprint(rw, `{"errno": 1, "error":"`, errMsg, `"}`) return } fmt.Fprint(rw, `{"errno": 0, "msg":"个人reminder资料更新成功!"}`) }
// 用户编辑个人信息 func AccountEditHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) curUser, _ := filter.CurrentUser(req) if req.Method != "POST" || vars["json"] == "" { // 获取用户信息 user := service.FindUserByUsername(curUser["username"].(string)) // 设置模板数据 filter.SetData(req, map[string]interface{}{"user": user, "default_avatars": service.DefaultAvatars}) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/user/edit.html") return } req.PostForm.Set("username", curUser["username"].(string)) if req.PostFormValue("open") != "1" { req.PostForm.Set("open", "0") } // 更新个人信息 errMsg, err := service.UpdateUser(req.PostForm) if err != nil { fmt.Fprint(rw, `{"ok": 0, "error":"`, errMsg, `"}`) return } fmt.Fprint(rw, `{"ok": 1, "msg":"个人资料更新成功!"}`) }
// 用户个人首页 // URI: /user/{username} func UserHomeHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) username := vars["username"] // 获取用户信息 user := service.FindUserByUsername(username) if user == nil { util.Redirect(rw, req, "/users") return } topics := service.FindRecentTopics(user.Uid, "5") resources := service.FindUserRecentResources(user.Uid) resourceCats := make(map[int]string) for _, resource := range resources { resourceCats[resource.Catid] = service.GetCategoryName(resource.Catid) } projects := service.FindUserRecentProjects(user.Username) comments := service.FindRecentComments(user.Uid, -1, "5") // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeUsers": "active", "topics": topics, "resources": resources, "resource_cats": resourceCats, "projects": projects, "comments": comments, "user": user}) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/user/profile.html") }
// 用户个人首页 // URI: /user/{username} func UserHomeHandler(rw http.ResponseWriter, req *http.Request) { req.Form.Set(filter.CONTENT_TPL_KEY, "/template/user/profile.html") vars := mux.Vars(req) username := vars["username"] // 获取用户信息 user := service.FindUserByUsername(username) if user != nil { topics := service.FindRecentTopics(user.Uid) comments := service.FindRecentComments(user.Uid, model.TYPE_TOPIC) replies := service.FindRecentReplies(comments) // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeUsers": "active", "topics": topics, "replies": replies, "user": user}) } }
// 我的(某人的)收藏 // uri: /favorites/{username} func SomeoneFavoritesHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) username := vars["username"] user := service.FindUserByUsername(username) if user == nil { util.Redirect(rw, req, "/") return } objtype, err := strconv.Atoi(req.FormValue("objtype")) if err != nil { objtype = model.TYPE_ARTICLE } p, err := strconv.Atoi(req.FormValue("p")) if err != nil { p = 1 } data := map[string]interface{}{"objtype": objtype, "user": user} rows := 20 favorites, total := service.FindUserFavorites(user.Uid, objtype, (p-1)*rows, rows) if total > 0 { objids := util.Models2Intslice(favorites, "Objid") switch objtype { case model.TYPE_TOPIC: data["topics"] = service.FindTopicsByIds(objids) case model.TYPE_ARTICLE: data["articles"] = service.FindArticlesByIds(objids) case model.TYPE_RESOURCE: data["resources"] = service.FindResourcesByIds(objids) case model.TYPE_WIKI: // data["wikis"] = service.FindArticlesByIds(objids) case model.TYPE_PROJECT: data["projects"] = service.FindProjectsByIds(objids) } } uri := fmt.Sprintf("/favorites/%s?objtype=%d&p=%d", user.Username, objtype, p) data["pageHtml"] = service.GenPageHtml(p, rows, total, uri) // 设置模板数据 filter.SetData(req, data) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/favorite.html") }
// 发短消息 // uri: /message/send{json:(|.json)} func SendMessageHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) content := req.FormValue("content") // 请求新建帖子页面 if content == "" || req.Method != "POST" || vars["json"] == "" { user := service.FindUserByUsername(req.FormValue("username")) filter.SetData(req, map[string]interface{}{"user": user}) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/messages/send.html") return } user, _ := filter.CurrentUser(req) to := util.MustInt(req.FormValue("to")) success := service.SendMessageTo(user["uid"].(int), to, content) if !success { fmt.Fprint(rw, `{"errno": 1, "error":"对不起,发送失败,请稍候再试!"}`) return } fmt.Fprint(rw, `{"errno": 0, "error":""}`) }