本文整理汇总了Golang中filter.SetData函数的典型用法代码示例。如果您正苦于以下问题:Golang SetData函数的具体用法?Golang SetData怎么用?Golang SetData使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SetData函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: UserModifyHandler
// 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)
}
示例2: TopicsHandler
// 社区帖子列表页
// uri: /topics{view:(|/popular|/no_reply|/last)}
func TopicsHandler(rw http.ResponseWriter, req *http.Request, ismobile bool) {
logger.Traceln("User-Agent")
logger.Traceln(req.Header["User-Agent"][0])
nodes := genNodes()
// 设置内容模板
page, _ := strconv.Atoi(req.FormValue("p"))
if page == 0 {
page = 1
}
vars := mux.Vars(req)
order := ""
where := ""
switch vars["view"] {
case "/popular":
where = "like>0"
case "/last":
order = "ctime DESC"
}
var PAGE_NUM int
if ismobile {
PAGE_NUM = 30
} else {
PAGE_NUM = 10
}
topics, total := service.FindTopics(page, PAGE_NUM, where, order)
logger.Traceln(total)
logger.Traceln(len(topics))
logger.Traceln(PAGE_NUM)
pageHtml := service.GetPageHtml(page, total, PAGE_NUM)
if ismobile {
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/list_mobile.html")
} else {
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/list.html")
}
// 设置模板数据
switch vars["view"] {
case "/popular":
filter.SetData(req, map[string]interface{}{"popular": 1, "topics": topics, "page": template.HTML(pageHtml), "nodes": nodes})
case "/last":
filter.SetData(req, map[string]interface{}{"last": 1, "topics": topics, "page": template.HTML(pageHtml), "nodes": nodes})
default:
filter.SetData(req, map[string]interface{}{"active": 1, "topics": topics, "page": template.HTML(pageHtml), "nodes": nodes})
}
}
示例3: NewResourceHandler
// 发布新资源
// 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":""}`)
}
示例4: ModifyResourceHandler
// 修改資源
// 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":""}`)
}
示例5: ArticleDetailHandler
// 文章详细页
// 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})
}
示例6: NewRoleHandler
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)
}
示例7: AccountEditHandler
// 用户编辑个人信息
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":"个人资料更新成功!"}`)
}
示例8: UserHomeHandler
// 用户个人首页
// 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")
}
示例9: ModifyAuthorityHandler
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)
}
示例10: AuthQueryHandler
func AuthQueryHandler(rw http.ResponseWriter, req *http.Request) {
curPage, limit := parsePage(req)
conds := parseConds(req, []string{"route", "name"})
authorities, total := service.FindAuthoritiesByPage(conds, curPage, limit)
if authorities == nil {
logger.Errorln("[AuthQueryHandler]sql find error")
rw.WriteHeader(http.StatusInternalServerError)
return
}
data := map[string]interface{}{
"datalist": authorities,
"total": total,
"totalPages": (total + limit - 1) / limit,
"page": curPage,
"limit": limit,
}
// 设置内容模板
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/authority/query.html")
filter.SetData(req, data)
}
示例11: CatResourcesHandler
// 某个分类的资源列表
// uri: /resources/cat/{catid:[0-9]+}
func CatResourcesHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
catid := vars["catid"]
resources := service.FindResourcesByCatid(catid)
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/index.html")
filter.SetData(req, map[string]interface{}{"activeResources": "active", "resources": resources, "categories": model.AllCategory, "curCatid": catid})
}
示例12: TopicDetailHandler
// 社区帖子详细页
// 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})
}
示例13: ModifyArticleHandler
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)
}
示例14: UpdateProjectStatusHandler
// 更新状态
// 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)
}
示例15: ModifyTopicHandler
// 修改主题
// 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":""}`)
}