Пример #1
0
// 用户个人首页
// 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")
}
Пример #2
0
// 评论(或回复)
// 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)+`}`)
}
Пример #3
0
// 某个分类的资源列表
// 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})
}
Пример #4
0
// 社区帖子详细页
// 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})
}
Пример #5
0
// 社区帖子列表页
// uri: /topics{view:(|/popular|/no_reply|/last)}
func TopicsHandler(rw http.ResponseWriter, req *http.Request) {
	nodes := service.GenNodes()
	// 设置内容模板
	page, _ := strconv.Atoi(req.FormValue("p"))
	if page == 0 {
		page = 1
	}
	vars := mux.Vars(req)
	order := ""
	where := ""
	view := ""
	switch vars["view"] {
	case "/no_reply":
		view = "no_reply"
		where = "lastreplyuid=0"
	case "/last":
		view = "last"
		order = "ctime DESC"
	}

	topics, total := service.FindTopics(page, 0, where, order)
	pageHtml := service.GetPageHtml(page, total, req.URL.Path)
	req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/list.html")
	// 设置模板数据
	filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topics": topics, "page": template.HTML(pageHtml), "nodes": nodes, "view": view})
}
Пример #6
0
// 用户编辑个人信息
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":"个人资料更新成功!"}`)
}
Пример #7
0
// 收藏(取消收藏)
// 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"}`)
}
Пример #8
0
// 项目详情
// 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})
}
Пример #9
0
// 点击 【我要晨读】,记录点击数,跳转
// uri: /readings/{id:[0-9]+}
func IReadingHandler(rw http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	url := service.IReading(vars["id"])

	util.Redirect(rw, req, url)
	return
}
Пример #10
0
// 新建帖子
// 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":""}`)
}
Пример #11
0
// 修改主题
// 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":""}`)
}
Пример #12
0
// 发布新资源
// 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":""}`)
}
Пример #13
0
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资料更新成功!"}`)
}
Пример #14
0
// 修改项目
// 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":""}`)
}
Пример #15
0
// 修改資源
// 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":""}`)
}
Пример #16
0
// 文章详细页
// 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})
}
Пример #17
0
func EngineHandler(rw http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	msgtype := vars["msgtype"]

	if req.Method != "POST" && msgtype == "" {
		// 获取用户信息
		err := config.LoadOption()
		if err != nil {
			logger.Errorln(err)
			fmt.Fprint(rw, `{"errno": 1, "msg":"`, "读取引擎配置数据失败", `"}`)
			return
		}
		// 设置模板数据
		filter.SetData(req, map[string]interface{}{"config": config.Option})
		req.Form.Set(filter.CONTENT_TPL_KEY, "/template/trade/engine.html")
		return
	} else if req.Method != "POST" && msgtype == "ajax" {
		Option_json, err := json.Marshal(config.Option)
		if err != nil {
			logger.Errorln(err)
			fmt.Fprint(rw, `{"errno": 1, "msg":"`, "读取引擎配置数据失败", `"}`)
		} else {
			fmt.Fprint(rw, string(Option_json))
		}
		return
	} else {
		logger.Debugln("===[", req.FormValue("disable_trading"), "]")
		if req.FormValue("disable_trading") == "on" {
			config.Option["disable_trading"] = "1"
		} else {
			config.Option["disable_trading"] = "0"
		}

		logger.Debugln("===[", req.FormValue("disable_backtesting"), "]")
		if req.FormValue("disable_backtesting") == "on" {
			config.Option["disable_backtesting"] = "1"
		} else {
			config.Option["disable_backtesting"] = "0"
		}

		config.Option["tick_interval"] = req.FormValue("tick_interval")
		config.Option["shortEMA"] = req.FormValue("shortEMA")
		config.Option["longEMA"] = req.FormValue("longEMA")

		config.Option["tradeAmount"] = req.FormValue("tradeAmount")

		config.Option["totalHour"] = req.FormValue("totalHour")

		// 更新个人信息
		err := config.SaveOption()
		if err != nil {
			fmt.Fprint(rw, `{"errno": 1, "msg":"`, "写入引擎配置数据失败", `"}`)
			return
		}

		fmt.Fprint(rw, `{"errno": 0, "msg":"更新引擎配置成功!"}`)
	}
}
Пример #18
0
// 喜欢(或取消喜欢)
// 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":""}`)
}
Пример #19
0
// 某节点下其他帖子
func OtherTopicsHandler(rw http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	topics := service.FindTopicsByNid(vars["nid"], vars["tid"])
	data, err := json.Marshal(topics)
	if err != nil {
		logger.Errorln("[OtherTopicsHandler] json.marshal error:", err)
		fmt.Fprint(rw, `{"errno": 1, "error":"解析json出错"}`)
		return
	}
	fmt.Fprint(rw, `{"errno": 0, "data":`+string(data)+`}`)
}
Пример #20
0
// 展示wiki页
// uri: /wiki/{uri}
func WikiContentHandler(rw http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	uri := vars["uri"]
	wiki := service.FindWiki(uri)
	if wiki == nil {
		NotFoundHandler(rw, req)
		return
	}
	req.Form.Set(filter.CONTENT_TPL_KEY, "/template/wiki/content.html")
	// 设置模板数据
	filter.SetData(req, map[string]interface{}{"activeWiki": "active", "wiki": wiki})
}
Пример #21
0
func TradeHandler(rw http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	msgtype := vars["msgtype"]

	if req.Method != "POST" || msgtype == "" {
		// 获取用户信息
		err := config.LoadTrade()
		if err != nil {
			logger.Errorln(err)
			fmt.Fprint(rw, `{"errno": 1, "msg":"`, "读取Trade配置数据失败", `"}`)
			return
		}
		// 设置模板数据
		filter.SetData(req, map[string]interface{}{"trade": config.TradeOption})
		req.Form.Set(filter.CONTENT_TPL_KEY, "/template/trade/trade.html")
		return
	} else if req.Method == "POST" {
		if msgtype == "dobuy" {
			config.TradeOption["buyprice"] = req.FormValue("buyprice")
			config.TradeOption["buyamount"] = req.FormValue("buyamount")
		} else if msgtype == "dosell" {
			config.TradeOption["sellprice"] = req.FormValue("sellprice")
			config.TradeOption["sellamount"] = req.FormValue("sellamount")
		} else {
			fmt.Fprint(rw, `{"errno": 1, "msg":"`, "无效的POST请求", `"}`)
			return
		}

		// 更新个人信息
		err := config.SaveTrade()
		if err != nil {
			fmt.Fprint(rw, `{"errno": 1, "msg":"`, "写入Trade配置数据失败", `"}`)
			return
		}

		huobi := huobiapi.NewHuobi()
		var ret bool
		if msgtype == "dobuy" {
			ret = huobi.Do_buy(config.TradeOption["buyprice"], config.TradeOption["buyamount"])
		} else if msgtype == "dosell" {
			ret = huobi.Do_sell(config.TradeOption["sellprice"], config.TradeOption["sellamount"])
		}

		if ret != true {
			fmt.Fprint(rw, `{"errno": 1, "msg":"`, "交易委托失败", `"}`)
			return
		} else {
			fmt.Fprint(rw, `{"errno": 1, "msg":"`, "交易委托成功", `"}`)
			return
		}
	}
}
Пример #22
0
// 社区帖子列表页
// 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})
	}
}
Пример #23
0
// 用户个人首页
// 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})
	}
}
Пример #24
0
// 某节点下的帖子列表
// uri: /topics/node{nid:[0-9]+}
func NodesHandler(rw http.ResponseWriter, req *http.Request) {
	page, _ := strconv.Atoi(req.FormValue("p"))
	if page == 0 {
		page = 1
	}
	vars := mux.Vars(req)
	topics, total := service.FindTopics(page, 0, "nid="+vars["nid"])
	pageHtml := service.GetPageHtml(page, total)
	// 当前节点信息
	node := model.GetNode(util.MustInt(vars["nid"]))
	req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/node.html")
	// 设置模板数据
	filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topics": topics, "page": template.HTML(pageHtml), "total": total, "node": node})
}
Пример #25
0
// 某个分类的资源列表
// uri: /resources/cat/{catid:[0-9]+}
func CatResourcesHandler(rw http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	catid := vars["catid"]

	page, _ := strconv.Atoi(req.FormValue("p"))
	if page == 0 {
		page = 1
	}

	resources, total := service.FindResourcesByCatid(catid, page)
	pageHtml := service.GetPageHtml(page, total, req.URL.Path)

	req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/index.html")
	filter.SetData(req, map[string]interface{}{"activeResources": "active", "resources": resources, "categories": service.AllCategory, "page": template.HTML(pageHtml), "curCatid": util.MustInt(catid)})
}
Пример #26
0
// 我的(某人的)收藏
// 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")
}
Пример #27
0
// 社区帖子详细页
// 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})
}
Пример #28
0
// 消息列表
// 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})
}
Пример #29
0
// 创建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")+`"}}`)
}
Пример #30
0
// 登录
// uri : /account/login{json:(|.json)}
func LoginHandler(rw http.ResponseWriter, req *http.Request) {
	username := req.PostFormValue("username")
	if username == "" || req.Method != "POST" {
		filter.SetData(req, map[string]interface{}{"error": "非法请求"})
		req.Form.Set(filter.CONTENT_TPL_KEY, "/template/login.html")
		return
	}

	vars := mux.Vars(req)

	suffix := vars["json"]

	// 处理用户登录
	passwd := req.PostFormValue("passwd")
	userLogin, err := service.Login(username, passwd)
	if err != nil {
		if suffix != "" {
			logger.Errorln("login error:", err)
			fmt.Fprint(rw, `{"ok":0,"error":"`+err.Error()+`"}`)
			return
		}

		req.Form.Set(filter.CONTENT_TPL_KEY, "/template/login.html")
		filter.SetData(req, map[string]interface{}{"username": username, "error": err.Error()})
		return
	}
	logger.Debugf("remember_me is %q\n", req.FormValue("remember_me"))
	// 登录成功,种cookie
	setCookie(rw, req, userLogin.Username)

	if suffix != "" {
		fmt.Fprint(rw, `{"ok":1,"msg":"success"}`)
		return
	}

	// 支持跳转到源页面
	uri := "/"
	values := filter.NewFlash(rw, req).Flashes("uri")
	if values != nil {
		uri = values[0].(string)
	}
	logger.Debugln("uri===", uri)
	util.Redirect(rw, req, uri)
}