// 评论(或回复) // uri: /comment/{objid:[0-9]+}.json func CommentHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) user, _ := filter.CurrentUser(req) if !util.CheckInt(req.PostForm, "objtype") { fmt.Fprint(rw, `{"errno": 1, "error":"参数错误"}`) return } // 入库 comment, err := service.PostComment(user["uid"].(int), util.MustInt(vars["objid"]), req.PostForm) if err != nil { fmt.Fprint(rw, `{"errno": 1, "error":"服务器内部错误"}`) return } buf, err := json.Marshal(comment) if err != nil { logger.Errorln("[RecentCommentHandler] json.marshal error:", err) fmt.Fprint(rw, `{"errno": 1, "error":"解析json出错"}`) return } fmt.Fprint(rw, `{"errno": 0, "error":"", "data":`+string(buf)+`}`) }
// 用户编辑个人信息 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":"个人资料更新成功!"}`) }
func ModifyAuthorityHandler(rw http.ResponseWriter, req *http.Request) { var data = make(map[string]interface{}) if req.PostFormValue("submit") == "1" { user, _ := filter.CurrentUser(req) username := user["username"].(string) errMsg, err := service.SaveAuthority(req.PostForm, username) if err != nil { data["ok"] = 0 data["error"] = errMsg } else { data["ok"] = 1 data["msg"] = "修改成功" } } else { menu1, menu2 := service.GetMenus() allmenu2, _ := json.Marshal(menu2) authority := service.FindAuthority(req.FormValue("aid")) if authority == nil || authority.Aid == 0 { rw.WriteHeader(http.StatusInternalServerError) return } // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/authority/modify.html") data["allmenu1"] = menu1 data["allmenu2"] = string(allmenu2) data["authority"] = authority } filter.SetData(req, data) }
// 更新状态 // uri: /admin/community/project/update_status func UpdateProjectStatusHandler(rw http.ResponseWriter, req *http.Request) { id, err := strconv.Atoi(req.PostFormValue("id")) if err != nil { rw.WriteHeader(http.StatusBadRequest) return } status, err := strconv.Atoi(req.FormValue("status")) if err != nil { rw.WriteHeader(http.StatusBadRequest) return } var data = make(map[string]interface{}) user, _ := filter.CurrentUser(req) err = service.UpdateProjectStatus(id, status, user["username"].(string)) if err != nil { logger.Errorln("UpdateProjectStatusHandler error:", err) data["ok"] = 0 data["error"] = "更新状态失败" } else { data["ok"] = 1 data["msg"] = "更新状态成功" } filter.SetData(req, data) }
// 修改主题 // uri: /topics/modify{json:(|.json)} func ModifyTopicHandler(rw http.ResponseWriter, req *http.Request) { tid := req.FormValue("tid") if tid == "" { util.Redirect(rw, req, "/topics") return } nodes := service.GenNodes() vars := mux.Vars(req) // 请求编辑主题页面 if req.Method != "POST" || vars["json"] == "" { topic := service.FindTopic(tid) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/new.html") filter.SetData(req, map[string]interface{}{"nodes": nodes, "topic": topic, "activeTopics": "active"}) return } user, _ := filter.CurrentUser(req) err := service.PublishTopic(user, req.PostForm) if err != nil { if err == service.NotModifyAuthorityErr { rw.WriteHeader(http.StatusForbidden) return } fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`) return } fmt.Fprint(rw, `{"ok": 1, "data":""}`) }
// 项目详情 // uri: /p/{uniq} func ProjectDetailHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) project := service.FindProject(vars["uniq"]) if project == nil { util.Redirect(rw, req, "/projects") return } likeFlag := 0 hadCollect := 0 user, ok := filter.CurrentUser(req) if ok { uid := user["uid"].(int) likeFlag = service.HadLike(uid, project.Id, model.TYPE_PROJECT) hadCollect = service.HadFavorite(uid, project.Id, model.TYPE_PROJECT) } service.Views.Incr(req, model.TYPE_PROJECT, project.Id) // 为了阅读数即时看到 project.Viewnum++ req.Form.Set(filter.CONTENT_TPL_KEY, "/template/projects/detail.html,/template/common/comment.html") filter.SetData(req, map[string]interface{}{"activeProjects": "active", "project": project, "likeflag": likeFlag, "hadcollect": hadCollect}) }
// uri: /admin/user/user/modify func UserModifyHandler(rw http.ResponseWriter, req *http.Request) { uidStr := req.PostFormValue("uid") var ( uid int err error ) if uid, err = strconv.Atoi(uidStr); err != nil { logger.Errorln("[UserModifyHandler] invalid uid") rw.WriteHeader(http.StatusNotFound) return } var data = make(map[string]interface{}) user, _ := filter.CurrentUser(req) if user["uid"].(int) == uid { data["ok"] = 0 data["error"] = "不能改自己的角色!" filter.SetData(req, data) return } service.AllocUserRoles(uid, req.PostForm["roleids"]) data["ok"] = 1 data["msg"] = "success" filter.SetData(req, data) }
// 发布新资源 // uri: /resources/new{json:(|.json)} func NewResourceHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) title := req.PostFormValue("title") // 请求新建资源页面 if title == "" || req.Method != "POST" || vars["json"] == "" { req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/new.html") filter.SetData(req, map[string]interface{}{"activeResources": "active", "categories": service.AllCategory}) return } errMsg := "" resForm := req.PostFormValue("form") if resForm == model.LinkForm { if req.PostFormValue("url") == "" { errMsg = "url不能为空" } } else { if req.PostFormValue("content") == "" { errMsg = "内容不能为空" } } if errMsg != "" { fmt.Fprint(rw, `{"ok": 0, "error":"`+errMsg+`"}`) return } user, _ := filter.CurrentUser(req) err := service.PublishResource(user, req.PostForm) if err != nil { fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误,请稍候再试!"}`) return } fmt.Fprint(rw, `{"ok": 1, "data":""}`) }
// 修改資源 // uri: /resources/modify{json:(|.json)} func ModifyResourceHandler(rw http.ResponseWriter, req *http.Request) { id := req.FormValue("id") if id == "" { util.Redirect(rw, req, "/resources") return } vars := mux.Vars(req) // 请求编辑資源页面 if req.Method != "POST" || vars["json"] == "" { resource := service.FindResourceById(id) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/new.html") filter.SetData(req, map[string]interface{}{"resource": resource, "activeResources": "active", "categories": service.AllCategory}) return } user, _ := filter.CurrentUser(req) err := service.PublishResource(user, req.PostForm) if err != nil { if err == service.NotModifyAuthorityErr { rw.WriteHeader(http.StatusForbidden) return } fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`) return } fmt.Fprint(rw, `{"ok": 1, "data":""}`) }
// 文章详细页 // uri: /articles/{id:[0-9]+} func ArticleDetailHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) article, prevNext, err := service.FindArticlesById(vars["id"]) if err != nil { util.Redirect(rw, req, "/articles") return } if article == nil || article.Id == 0 || article.Status == model.StatusOffline { util.Redirect(rw, req, "/articles") return } likeFlag := 0 hadCollect := 0 user, ok := filter.CurrentUser(req) if ok { uid := user["uid"].(int) likeFlag = service.HadLike(uid, article.Id, model.TYPE_ARTICLE) hadCollect = service.HadFavorite(uid, article.Id, model.TYPE_ARTICLE) } service.Views.Incr(req, model.TYPE_ARTICLE, article.Id) // 为了阅读数即时看到 article.Viewnum++ // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/articles/detail.html,/template/common/comment.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeArticles": "active", "article": article, "prev": prevNext[0], "next": prevNext[1], "likeflag": likeFlag, "hadcollect": hadCollect}) }
func ModifyArticleHandler(rw http.ResponseWriter, req *http.Request) { var data = make(map[string]interface{}) if req.PostFormValue("submit") == "1" { user, _ := filter.CurrentUser(req) errMsg, err := service.ModifyArticle(user, req.PostForm) if err != nil { data["ok"] = 0 data["error"] = errMsg } else { data["ok"] = 1 data["msg"] = "修改成功" } } else { article, err := service.FindArticleById(req.FormValue("id")) if err != nil { rw.WriteHeader(http.StatusInternalServerError) return } // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/article/modify.html") data["article"] = article data["statusSlice"] = model.StatusSlice data["langSlice"] = model.LangSlice } filter.SetData(req, data) }
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资料更新成功!"}`) }
// 新建帖子 // uri: /topics/new{json:(|.json)} func NewTopicHandler(rw http.ResponseWriter, req *http.Request) { nodes := genNodes() vars := mux.Vars(req) title := req.FormValue("title") // 请求新建帖子页面 if title == "" || req.Method != "POST" || vars["json"] == "" { req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/new.html") filter.SetData(req, map[string]interface{}{"nodes": nodes}) return } user, _ := filter.CurrentUser(req) // 入库 topic := model.NewTopic() topic.Uid = user["uid"].(int) topic.Nid = util.MustInt(req.FormValue("nid")) topic.Title = req.FormValue("title") topic.Content = req.FormValue("content") errMsg, err := service.PublishTopic(topic) if err != nil { fmt.Fprint(rw, `{"errno": 1, "error":"`, errMsg, `"}`) return } fmt.Fprint(rw, `{"errno": 0, "error":""}`) }
// /admin/reading/publish func PublishReadingHandler(rw http.ResponseWriter, req *http.Request) { var data = make(map[string]interface{}) if req.PostFormValue("submit") == "1" { user, _ := filter.CurrentUser(req) errMsg, err := service.SaveReading(req.PostForm, user["username"].(string)) if err != nil { data["ok"] = 0 data["error"] = errMsg } else { data["ok"] = 1 data["msg"] = "操作成功" } } else { id, err := strconv.Atoi(req.FormValue("id")) if err == nil && id != 0 { data["reading"], err = service.FindReadingById(id) if err != nil { rw.WriteHeader(http.StatusInternalServerError) return } } // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/reading/modify.html") } filter.SetData(req, data) }
func NewRoleHandler(rw http.ResponseWriter, req *http.Request) { var data = make(map[string]interface{}) if req.PostFormValue("submit") == "1" { user, _ := filter.CurrentUser(req) username := user["username"].(string) errMsg, err := service.SaveRole(req.PostForm, username) if err != nil { data["ok"] = 0 data["error"] = errMsg } else { data["ok"] = 1 data["msg"] = "添加成功" } } else { menu1, menu2 := service.GetMenus() menu3 := service.GeneralAuthorities() // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/role/new.html") data["allmenu1"] = menu1 data["allmenu2"] = menu2 data["allmenu3"] = menu3 } filter.SetData(req, data) }
func ModifyRuleHandler(rw http.ResponseWriter, req *http.Request) { var data = make(map[string]interface{}) if req.PostFormValue("submit") == "1" { user, _ := filter.CurrentUser(req) errMsg, err := service.ModifyTopic(user, req.PostForm) if err != nil { data["ok"] = 0 data["error"] = errMsg } else { data["ok"] = 1 data["msg"] = "修改成功" } } else { topic, replies, err := service.FindTopicByTid(req.FormValue("tid")) if err != nil { rw.WriteHeader(http.StatusInternalServerError) return } // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/topic/modify.html") data["topic"] = topic data["replies"] = replies data["nodes"] = service.GenNodes() } filter.SetData(req, data) }
// 修改项目 // uri: /project/modify{json:(|.json)} func ModifyProjectHandler(rw http.ResponseWriter, req *http.Request) { id := req.FormValue("id") if id == "" { util.Redirect(rw, req, "/projects") return } vars := mux.Vars(req) // 请求编辑项目页面 if req.Method != "POST" || vars["json"] == "" { project := service.FindProject(id) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/projects/new.html") filter.SetData(req, map[string]interface{}{"project": project, "activeProjects": "active"}) return } user, _ := filter.CurrentUser(req) err := service.PublishProject(user, req.PostForm) if err != nil { if err == service.NotModifyAuthorityErr { rw.WriteHeader(http.StatusForbidden) return } fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`) return } fmt.Fprint(rw, `{"ok": 1, "data":""}`) }
// 社区帖子详细页 // uri: /topics/{tid:[0-9]+} func TopicDetailHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) topic, replies, err := service.FindTopicByTid(vars["tid"]) if err != nil { util.Redirect(rw, req, "/topics") return } likeFlag := 0 hadCollect := 0 user, ok := filter.CurrentUser(req) if ok { uid := user["uid"].(int) tid := topic["tid"].(int) likeFlag = service.HadLike(uid, tid, model.TYPE_TOPIC) hadCollect = service.HadFavorite(uid, tid, model.TYPE_TOPIC) } service.Views.Incr(req, model.TYPE_TOPIC, util.MustInt(vars["tid"])) // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/detail.html,/template/common/comment.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topic": topic, "replies": replies, "likeflag": likeFlag, "hadcollect": hadCollect}) }
// 收藏(取消收藏) // uri: /favorite/{objid:[0-9]+}.json func FavoriteHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) user, _ := filter.CurrentUser(req) if !util.CheckInt(req.PostForm, "objtype") { fmt.Fprint(rw, `{"ok": 0, "error":"参数错误"}`) return } var err error objtype := util.MustInt(req.PostFormValue("objtype")) collect := util.MustInt(req.PostFormValue("collect")) if collect == 1 { err = service.SaveFavorite(user["uid"].(int), util.MustInt(vars["objid"]), objtype) } else { err = service.CancelFavorite(user["uid"].(int), util.MustInt(vars["objid"]), objtype) } if err != nil { fmt.Fprint(rw, `{"ok": 0, "error":"`+err.Error()+`""}`) return } fmt.Fprint(rw, `{"ok": 1, "message":"success"}`) }
// 添加新用户表单页面 func NewUserHandler(rw http.ResponseWriter, req *http.Request) { user, _ := filter.CurrentUser(req) tpl, err := template.ParseFiles(ROOT+"/template/admin/common.html", ROOT+"/template/admin/newuser.html") if err != nil { fmt.Fprintln(rw, err) return } tpl.Execute(rw, user) }
// 所有用户(分页) func UsersHandler(rw http.ResponseWriter, req *http.Request) { user, _ := filter.CurrentUser(req) users, err := service.FindUsers() if err != nil { // TODO: } // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/users.html") filter.SetData(req, map[string]interface{}{"user": user, "users": users}) }
// 喜欢(或取消喜欢) // uri: /like/{objid:[0-9]+}.json func LikeHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) user, _ := filter.CurrentUser(req) // 入库 err := service.PostComment(util.MustInt(vars["objid"]), util.MustInt(req.FormValue("objtype")), user["uid"].(int), req.FormValue("content"), req.FormValue("objname")) if err != nil { fmt.Fprint(rw, `{"errno": 1, "error":"服务器内部错误"}`) return } fmt.Fprint(rw, `{"errno": 0, "error":""}`) }
// 更换头像 // uri: /account/change_avatar.json func ChangeAvatarHandler(rw http.ResponseWriter, req *http.Request) { curUser, _ := filter.CurrentUser(req) avatar, ok := req.PostForm["avatar"] if !ok { fmt.Fprint(rw, `{"ok": 0, "error":"非法请求!"}`) return } err := service.ChangeAvatar(curUser["uid"].(int), avatar[0]) if err != nil { fmt.Fprint(rw, `{"ok": 0, "error":"更换头像失败"}`) return } fmt.Fprint(rw, `{"ok": 1, "msg":"更换头像成功!"}`) }
// 首页 func IndexHandler(rw http.ResponseWriter, req *http.Request) { // nodes := service.GenNodes() num := 10 topicsList := make([]map[string]interface{}, num) // 置顶的topic topTopics, _ := service.FindTopics(1, num, "top=1", "ctime DESC") if len(topTopics) < num { // 获取最新帖子 newTopics, _ := service.FindTopics(1, num-len(topTopics), "top=0", "ctime DESC") topicsList = append(topTopics, newTopics...) } // 获取热门帖子 //hotTopics := service.FindHotTopics() // 获得最新博文 // blogs := service.FindNewBlogs() recentArticles := service.FindArticles("0", "10") // 获取当前用户喜欢对象信息 var likeFlags map[int]int if len(recentArticles) > 0 { user, ok := filter.CurrentUser(req) if ok { uid := user["uid"].(int) likeFlags, _ = service.FindUserLikeObjects(uid, model.TYPE_ARTICLE, recentArticles[0].Id, recentArticles[len(recentArticles)-1].Id) } } // Golang 资源 resources := service.FindResources("0", "10") /* start, end := 0, len(resources) if n := end - 10; n > 0 { start = rand.Intn(n) end = start + 10 } */ // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/index.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"topics": topicsList, "articles": recentArticles, "likeflags": likeFlags, "resources": resources}) }
// 重置密码 // uri: /account/resetpwd func ResetPasswdHandler(rw http.ResponseWriter, req *http.Request) { if _, ok := filter.CurrentUser(req); ok { util.Redirect(rw, req, "/") return } uuid := req.FormValue("code") if uuid == "" { util.Redirect(rw, req, "/account/login") return } req.Form.Set(filter.CONTENT_TPL_KEY, "/template/user/reset_pwd.html") data := map[string]interface{}{"activeUsers": "active"} passwd := req.FormValue("passwd") email, ok := resetPwdMap[uuid] if !ok { // 是提交重置密码 if passwd != "" && req.Method == "POST" { data["error"] = template.HTML(`非法请求!<p>将在<span id="jumpTo">3</span>秒后跳转到<a href="/" id="jump_url">首页</a></p>`) } else { data["error"] = template.HTML(`链接无效或过期,请重新操作。<a href="/account/forgetpwd">忘记密码?</a>`) } filter.SetData(req, data) return } data["valid"] = true data["code"] = uuid // 提交修改密码 if passwd != "" && req.Method == "POST" { // 简单校验 if len(passwd) < 6 || len(passwd) > 32 { data["error"] = "密码长度必须在6到32个字符之间" } else if passwd != req.FormValue("pass2") { data["error"] = "两次密码输入不一致" } else { // 更新密码 _, err := service.UpdatePasswd(email, passwd) if err != nil { data["error"] = "对不起,服务器错误,请重试!" } else { data["success"] = template.HTML(`密码重置成功,<p>将在<span id="jumpTo">3</span>秒后跳转到<a href="/account/login" id="jump_url">登录</a>页面</p>`) } } } filter.SetData(req, data) }
// 创建wiki页 // uri: /wiki/new{json:(|.json)} func NewWikiPageHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) title := req.FormValue("title") if title == "" || req.Method != "POST" || vars["json"] == "" { req.Form.Set(filter.CONTENT_TPL_KEY, "/template/wiki/new.html") filter.SetData(req, map[string]interface{}{"activeWiki": "active"}) return } user, _ := filter.CurrentUser(req) // 入库 ok := service.CreateWiki(user["uid"].(int), req.Form) if !ok { fmt.Fprint(rw, `{"errno": 1, "error":"服务器内部错误,请稍候再试!"}`) return } fmt.Fprint(rw, `{"errno": 0, "data":{"uri":"`+req.FormValue("uri")+`"}}`) }
// 社区帖子详细页 // uri: /topics/{tid:[0-9]+} func TopicDetailHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) uid := 0 user, ok := filter.CurrentUser(req) if ok { uid = user["uid"].(int) } // TODO:刷屏暂时不处理 topic, replies, err := service.FindTopicByTid(vars["tid"]) if err != nil || topic == nil || topic["tid"] == nil { logger.Traceln("------") logger.Traceln(vars["tid"]) i, _ := strconv.Atoi(vars["tid"]) total := service.TopicsTotal() if i >= total { i = 0 } if i <= 0 { i = total - 1 } for ; i <= total; i++ { logger.Traceln(i) topic, replies, err = service.FindTopicByTid(strconv.Itoa(i)) if err == nil && topic != nil && topic["tid"] != nil { break } } } logger.Traceln("------end..........") if err != nil || topic == nil || topic["tid"] == nil { NotFoundHandler(rw, req) return } // 增加浏览量 service.IncrTopicView(vars["tid"], uid) topic["prev_tid"] = topic["tid"].(int) - 1 topic["next_tid"] = topic["tid"].(int) + 1 // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/detail.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topic": topic, "replies": replies}) }
// 消息列表 // uri: /message/{msgtype:(system|inbox|outbox)} func MessageHandler(rw http.ResponseWriter, req *http.Request) { user, _ := filter.CurrentUser(req) uid := user["uid"].(int) vars := mux.Vars(req) msgtype := vars["msgtype"] var messages []map[string]interface{} if msgtype == "system" { messages = service.FindSysMsgsByUid(strconv.Itoa(uid)) } else if msgtype == "inbox" { messages = service.FindToMsgsByUid(strconv.Itoa(uid)) } else { messages = service.FindFromMsgsByUid(strconv.Itoa(uid)) } req.Form.Set(filter.CONTENT_TPL_KEY, "/template/messages/list.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"messages": messages, "msgtype": msgtype}) }
// 社区帖子详细页 // uri: /topics/{tid:[0-9]+} func TopicDetailHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) topic, replies, err := service.FindTopicByTid(vars["tid"]) if err != nil { // TODO: } uid := 0 user, ok := filter.CurrentUser(req) if ok { uid = user["uid"].(int) } // TODO:刷屏暂时不处理 // 增加浏览量 service.IncrTopicView(vars["tid"], uid) // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/detail.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topic": topic, "replies": replies}) }
// 新建项目 // uri: /project/new{json:(|.json)} func NewProjectHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) name := req.PostFormValue("name") // 请求新建项目页面 if name == "" || req.Method != "POST" || vars["json"] == "" { project := model.NewOpenProject() req.Form.Set(filter.CONTENT_TPL_KEY, "/template/projects/new.html") filter.SetData(req, map[string]interface{}{"project": project, "activeProjects": "active"}) return } user, _ := filter.CurrentUser(req) err := service.PublishProject(user, req.PostForm) if err != nil { fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`) return } fmt.Fprint(rw, `{"ok": 1, "data":""}`) }