Пример #1
0
// 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
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})
	}
}
Пример #3
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":""}`)
}
Пример #4
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":""}`)
}
Пример #5
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})
}
Пример #6
0
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
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":"个人资料更新成功!"}`)
}
Пример #8
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")
}
Пример #9
0
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
0
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
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})
}
Пример #12
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})
}
Пример #13
0
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
0
// 更新状态
// 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
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":""}`)
}
Пример #16
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})
}
Пример #17
0
// /admin/community/project/query.html
func ProjectQueryHandler(rw http.ResponseWriter, req *http.Request) {
	curPage, limit := parsePage(req)

	conds := parseConds(req, []string{"id", "domain", "title"})

	projects, total := service.FindProjectByPage(conds, curPage, limit)

	if projects == nil {
		logger.Errorln("[ProjectQueryHandler]sql find error")
		rw.WriteHeader(http.StatusInternalServerError)
		return
	}

	data := map[string]interface{}{
		"datalist":   projects,
		"total":      total,
		"totalPages": (total + limit - 1) / limit,
		"page":       curPage,
		"limit":      limit,
	}

	// 设置内容模板
	req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/project/query.html")
	filter.SetData(req, data)
}
Пример #18
0
// 登录
// uri : /account/login
func LoginHandler(rw http.ResponseWriter, req *http.Request) {
	username := req.FormValue("username")
	if username == "" || req.Method != "POST" {
		req.Form.Set(filter.CONTENT_TPL_KEY, "/template/login.html")
		return
	}
	// 处理用户登录
	passwd := req.FormValue("passwd")
	userLogin, err := service.Login(username, passwd)
	if err != nil {
		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)

	// 支持跳转到源页面
	uri := "/"
	values := filter.NewFlash(rw, req).Flashes("uri")
	if values != nil {
		uri = values[0].(string)
	}
	logger.Debugln("uri===", uri)
	util.Redirect(rw, req, uri)
}
Пример #19
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":""}`)
}
Пример #20
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资料更新成功!"}`)
}
Пример #21
0
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)
}
Пример #22
0
// /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)
}
Пример #23
0
func LicenceHandler(rw http.ResponseWriter, req *http.Request) {
	if req.Method != "POST" {
		// 获取用户信息
		err := config.LoadLicence()
		if err != nil {
			logger.Errorln(err)
			fmt.Fprint(rw, `{"errno": 1, "msg":"`, "读取Licence配置数据失败", `"}`)
			return
		}
		// 设置模板数据
		filter.SetData(req, map[string]interface{}{"config": config.Licence})
		req.Form.Set(filter.CONTENT_TPL_KEY, "/template/trade/licence.html")
		return
	} else {
		config.Licence["user_email"] = req.FormValue("user_email")
		config.Licence["licence"] = req.FormValue("licence")

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

		fmt.Fprint(rw, `{"errno": 0, "msg":"更新Licence配置成功!"}`)
	}
}
Пример #24
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":""}`)
}
Пример #25
0
// /admin/crawl/article/new
func CrawlArticleHandler(rw http.ResponseWriter, req *http.Request) {
	var data = make(map[string]interface{})

	if req.PostFormValue("submit") == "1" {
		urls := strings.Split(req.PostFormValue("urls"), "\n")

		var errMsg string
		for _, articleUrl := range urls {
			_, err := service.ParseArticle(strings.TrimSpace(articleUrl), false)

			if err != nil {
				errMsg = err.Error()
			}
		}

		if errMsg != "" {
			data["ok"] = 0
			data["error"] = errMsg
		} else {
			data["ok"] = 1
			data["msg"] = "添加成功"
		}
	} else {

		// 设置内容模板
		req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/article/new.html")
	}

	filter.SetData(req, data)
}
Пример #26
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})
}
Пример #27
0
// 重置密码
// 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)
}
Пример #28
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":"更新引擎配置成功!"}`)
	}
}
Пример #29
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)
}
Пример #30
0
// 所有用户(分页)
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})
}