Esempio n. 1
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"}`)
}
Esempio n. 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)+`}`)
}
Esempio n. 3
0
// Like 喜欢(或取消喜欢)
func (LikeController) Like(ctx echo.Context) error {
	form := ctx.FormParams()
	if !util.CheckInt(form, "objtype") || !util.CheckInt(form, "flag") {
		return fail(ctx, 1, "参数错误")
	}

	user := ctx.Get("user").(*model.Me)
	objid := goutils.MustInt(ctx.Param("objid"))
	objtype := goutils.MustInt(ctx.FormValue("objtype"))
	likeFlag := goutils.MustInt(ctx.FormValue("flag"))

	err := logic.DefaultLike.LikeObject(ctx, user.Uid, objid, objtype, likeFlag)
	if err != nil {
		return fail(ctx, 2, "服务器内部错误")
	}

	return success(ctx, nil)
}
Esempio n. 4
0
// 喜欢(或取消喜欢)
// uri: /like/{objid:[0-9]+}.json
func LikeHandler(rw http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	user, _ := filter.CurrentUser(req)

	if !util.CheckInt(req.PostForm, "objtype") || !util.CheckInt(req.PostForm, "flag") {
		fmt.Fprint(rw, `{"ok": 0, "error":"参数错误"}`)
		return
	}

	uid := user["uid"].(int)
	objid := util.MustInt(vars["objid"])
	objtype := util.MustInt(req.PostFormValue("objtype"))
	likeFlag := util.MustInt(req.PostFormValue("flag"))

	err := service.LikeObject(uid, objid, objtype, likeFlag)
	if err != nil {
		fmt.Fprint(rw, `{"ok": 0, "error":"服务器内部错误"}`)
		return
	}

	fmt.Fprint(rw, `{"ok": 1, "msg":"success", "data":""}`)
}