Esempio n. 1
0
func (c Article) DoEdit(topic *model.Topic) revel.Result {
	//topic.Body = template.HTML(c.Request.PostFormValue("editormd-html-code"))
	_, ok := c.Session["user"]
	if !ok {
		c.Session["preUrl"] = fmt.Sprintf("/edit/%s", topic.Id)
		c.Flash.Error("亲,登录之后才能发布哦!")
		return c.Redirect("/login")
	}
	dao, err := model.NewDao()
	defer dao.Close()
	oldTopic := dao.FindTopicById(c.Request.PostFormValue("Id"))
	if oldTopic == nil {
		return c.Redirect("/")
	}
	oldTopic.Title = topic.Title
	oldTopic.Tag = topic.Tag
	//	if topic.Tag == 4 {
	//		oldTopic.Status = 0
	//	}
	id, err := dao.EditTopic(oldTopic)
	if err != nil {
		revel.ERROR.Printf("Unable to save topic: %v error %v", topic, err)
		c.Response.Status = 500
		return c.RenderError(err)
	}
	revel.INFO.Printf("The id: %s", id)
	utils.DeleteFile(id)
	err = utils.UploadString(c.Request.PostFormValue("editormd-html-code"), fmt.Sprintf("%s.html", oldTopic.Id.Hex()))
	err = utils.UploadString(c.Request.PostFormValue("Body"), fmt.Sprintf("%s_code.html", id))
	return c.Redirect("/show/%s", id)
}
Esempio n. 2
0
func (c App) ChoosePhoto(name string) revel.Result {
	if username, ok := c.Session["user"]; ok {
		if name == "" {
			return c.Redirect("/")
		}
		if username != name {
			return c.Redirect("/user?name=%s", name)
		}
		dao, err := model.NewDao()
		defer dao.Close()
		if err != nil {
			return c.Redirect("/")
		}
		user := dao.GetUserByName(name)
		if user == nil {
			return c.Redirect("/")
		}
		user.Logo = c.Request.PostFormValue("defaultAvatars")
		dao.UpdateUser(user)
		return c.Redirect("/user?name=%s", name)
	} else {
		c.Session["preUrl"] = fmt.Sprintf("/user?name=%s", name)
		c.Flash.Error("亲,登录超时,请重新登录!")
		return c.Redirect("/login")
	}
}
Esempio n. 3
0
func (c App) DoRegister(user *model.User) revel.Result {
	user.Flag = false
	user.Vip = 0
	user.Logo = fmt.Sprintf("default%d.jpg", rand.Intn(9))
	//todo check
	dao, err := model.NewDao()
	defer dao.Close()
	if err != nil {
		c.Flash.Error("用户保存失败,请稍后重试!")
		return c.Redirect("/register")
	}
	hasUser := dao.GetUserByName(user.Name)
	if hasUser != nil && hasUser.Name != "" {
		c.Flash.Error("用户名已存在,请登录或找回密码!")
		return c.Redirect("/register")
	}
	hasUser = dao.GetUserByEmail(user.Email)
	if hasUser != nil && hasUser.Name != "" {
		c.Flash.Error("邮箱/手机 已存在,请登录或找回密码!")
		return c.Redirect("/register")
	}
	err = dao.InserUser(user)
	if err != nil {
		c.Flash.Error("用户保存失败,请稍后重试!")
		return c.Redirect("/register")
	}
	c.Session["user"] = user.Name
	return c.Redirect("/")
}
Esempio n. 4
0
func (c Article) DoPush(topic *model.Topic) revel.Result {
	//topic.Body = template.HTML(c.Request.PostFormValue("editormd-html-code"))
	uName, ok := c.Session["user"]
	if !ok {
		c.Session["preUrl"] = "/push"
		c.Flash.Error("亲,登录之后才能发布哦!")
		return c.Redirect("/login")
	}
	topic.UName = uName
	dao, err := model.NewDao()
	topic.Status = 1
	//	if topic.Tag == 4 {
	//		topic.Status = 0
	//	}
	defer dao.Close()
	id, err := dao.InserTopic(topic)
	if err != nil {
		revel.ERROR.Printf("Unable to save topic: %v error %v", topic, err)
		c.Response.Status = 500
		return c.RenderError(err)
	}
	err = utils.UploadString(c.Request.PostFormValue("editormd-html-code"), fmt.Sprintf("%s.html", id))
	err = utils.UploadString(c.Request.PostFormValue("Body"), fmt.Sprintf("%s_code.html", id))
	return c.Redirect("/show/%s", id)
}
Esempio n. 5
0
func (c *App) getUser(username string) *model.User {
	dao, err := model.NewDao()
	defer dao.Close()
	if err != nil {
		return nil
	}
	hasUser := dao.GetUserByName(username)
	if hasUser != nil && hasUser.Name != "" {
		return hasUser
	}
	return nil
}
Esempio n. 6
0
func (c Activity) Index(page int) revel.Result {
	dao, err := model.NewDao()
	defer dao.Close()
	if err != nil {
		revel.ERROR.Printf("Unable to open db:error %v", err)
		return c.Redirect("/")
	}
	topics, pageno, totalPage := dao.GetTopicsWithTag(page, 9)
	page = pageno
	goods := dao.GetGoodTopics(0, 5)
	return c.Render(topics, page, totalPage, goods)
}
Esempio n. 7
0
func (c App) ShowUser(name string) revel.Result {
	if name == "" {
		return c.Redirect("/")
	}
	dao, err := model.NewDao()
	defer dao.Close()
	if err != nil {
		return c.Redirect("/")
	}
	userShow := dao.GetUserByName(name)
	topics := dao.GetTopicsByUserName(name, 10)
	return c.Render(userShow, topics)
}
Esempio n. 8
0
func (c Mobile) Regist() revel.Result {
	user := new(model.User)
	username := c.Request.PostFormValue("name")
	if username == "" {
		user.Email = "请输入用户名"
		return c.RenderJson(user)
	}
	email := c.Request.PostFormValue("email")
	if email == "" {
		user.Email = "请输入邮箱/手机"
		return c.RenderJson(user)
	}
	pass := c.Request.PostFormValue("pass")
	if pass == "" {
		user.Email = "请输入密码"
		return c.RenderJson(user)
	}
	user.Name = username
	user.Email = email
	user.Pass = pass
	dao, err := model.NewDao()
	defer dao.Close()
	if err != nil {
		user.Name = ""
		user.Email = "数据保存失败了,请稍后再试!"
		return c.RenderJson(user)
	}
	hasUser := dao.GetUserByName(user.Name)
	if hasUser != nil && hasUser.Name != "" {
		user.Name = ""
		user.Email = "用户名已存在,请重新输入,或登录或找回密码!"
		return c.RenderJson(user)
	}
	hasUser = dao.GetUserByEmail(user.Email)
	if hasUser != nil && hasUser.Name != "" {
		user.Name = ""
		user.Email = "邮箱/手机 已存在,请重新输入,或登录或找回密码!"
		return c.RenderJson(user)
	}
	user.Flag = true
	user.Vip = 0
	user.Logo = fmt.Sprintf("default%d.jpg", rand.Intn(9))
	revel.ERROR.Printf("user: %v", user)
	err = dao.InserUser(user)
	if err != nil {
		user.Name = ""
		user.Email = "数据保存失败了,请稍后再试!"
		return c.RenderJson(user)
	}
	return c.RenderJson(user)
}
Esempio n. 9
0
func (c Article) Index(page int) revel.Result {
	dao, err := model.NewDao()
	defer dao.Close()
	if err != nil {
		revel.ERROR.Printf("Unable to open db:error %v", err)
		c.Response.Status = 500
		return c.RenderError(err)
	}
	topics, pageno, totalPage := dao.GetTopicsWithTag(page, 3)
	page = pageno
	goods := dao.GetGoodTopics(0, 5)
	abouts := dao.GetGoodTopics(10, 5)
	return c.Render(topics, page, totalPage, goods, abouts)
}
Esempio n. 10
0
func (c Mobile) GetUser() revel.Result {
	dao, err := model.NewDao()
	defer dao.Close()
	if err != nil {
		revel.ERROR.Printf("Unable to open db:error %v", err)
		return c.RenderJson("{}")
	}
	username := c.Request.PostFormValue("name")
	if username == "" {
		return c.RenderJson("{}")
	}
	revel.INFO.Printf("username: %s", username)
	user := dao.GetUserByName(username)
	return c.RenderJson(user)
}
Esempio n. 11
0
func (c Article) Push() revel.Result {
	if _, ok := c.Session["user"]; !ok {
		c.Session["preUrl"] = "/push"
		c.Flash.Error("亲,登录之后才发布哦!")
		return c.Redirect("/login")
	}
	dao, err := model.NewDao()
	defer dao.Close()
	if err != nil {
		revel.ERROR.Printf("Unable to open db:error %v", err)
		c.Response.Status = 500
		return c.RenderError(err)
	}
	tags := dao.GetTags()
	return c.Render(tags)
}
Esempio n. 12
0
func (c App) Index(page, tag int) revel.Result {
	revel.INFO.Printf("The page: %d", page)
	dao, err := model.NewDao()
	defer dao.Close()
	if err != nil {
		revel.ERROR.Printf("Unable to open db:error %v", err)
		c.Response.Status = 500
		return c.RenderError(err)
	}
	goods := dao.GetGoodTopics(0, 5)
	abouts := dao.GetGoodTopics(10, 5)
	if tag > 0 {
		topics, pageno, totalPage := dao.GetTopicsWithTag(page, tag)
		page = pageno
		return c.Render(topics, page, totalPage, tag, goods, abouts)
	} else {
		topics, pageno, totalPage := dao.GetTopics(page)
		page = pageno
		return c.Render(topics, page, totalPage, tag, goods, abouts)
	}
}
Esempio n. 13
0
func (c Mobile) GetTopics() revel.Result {
	dao, err := model.NewDao()
	defer dao.Close()
	if err != nil {
		revel.ERROR.Printf("Unable to open db:error %v", err)
		return c.RenderJson("{}")
	}
	p := c.Request.PostFormValue("page")
	page, err := strconv.Atoi(p)
	if err != nil {
		page = 1
	}
	t := c.Request.PostFormValue("tag")
	tag, er := strconv.Atoi(t)
	if er != nil {
		tag = 1
	}
	revel.INFO.Printf("page: %s", p)
	topics := dao.GetTopicsWithTagForMob(page, tag)
	return c.RenderJson(topics)
}
Esempio n. 14
0
func (c Article) Show(id string) revel.Result {
	dao, err := model.NewDao()
	defer dao.Close()
	if err != nil {
		c.Response.Status = 500
		return c.RenderError(err)
	}
	topic := dao.FindTopicById(id)
	if topic == nil {
		return c.Redirect("/")
	}
	if topic.Status == 0 {
		return c.RenderTemplate("Article/result.html")
	}
	ip := strings.Split(c.Request.RemoteAddr, ":")[0]
	revel.INFO.Printf("Ip : %s", ip)
	var obj CacheBean
	key := fmt.Sprintf("%s.%s", ip, id)
	err = cache.Get(key, &obj)
	if err == nil {
		revel.INFO.Printf("Cache : %v", obj)
	} else {
		revel.ERROR.Printf("Cache Errr : %v", err)
		if !obj.HasCache {
			obj.HasCache = true
			go cache.Set(key, obj, 10*time.Minute)
			topic.Read = topic.Read + 1
			dao.UpdateTopic(topic)
		}
	}
	content, ret := utils.GetHTMLContent(id)
	if ret != nil {
		c.Response.Status = 500
		return c.RenderError(ret)
	}
	topic.Body = template.HTML(content)
	replays, repcnt := dao.GetReplays(id)
	return c.Render(topic, replays, repcnt)
}
Esempio n. 15
0
func (c Article) DoComment(id string) revel.Result {
	user, ok := c.Session["user"]
	if !ok {
		c.Session["preUrl"] = fmt.Sprintf("/show/%s", id)
		c.Flash.Error("亲,登录之后才能回复哦!")
		return c.Redirect("/login")
	}
	replay := new(model.Replay)
	replay.TopicId = id
	replay.Content = c.Request.PostFormValue("content")
	replay.UName = user
	dao, _ := model.NewDao()
	defer dao.Close()
	topic := dao.FindTopicById(id)
	if topic == nil {
		return c.Redirect("/")
	}
	topic.Comment = topic.Comment + 1
	dao.InserReplay(replay)
	dao.EditTopic(topic)
	return c.Redirect("/show/%s", id)
}
Esempio n. 16
0
func (c Mobile) Show(id string) revel.Result {
	dao, err := model.NewDao()
	defer dao.Close()
	if err != nil {
		c.Response.Status = 500
		return c.RenderError(err)
	}
	topic := dao.FindTopicById(id)
	if topic == nil {
		return c.Redirect("/")
	}
	if topic.Status == 0 {
		return c.RenderTemplate("Article/result.html")
	}
	content, ret := utils.GetHTMLContent(id)
	if ret != nil {
		c.Response.Status = 500
		return c.RenderError(ret)
	}
	revel.INFO.Printf("content: %v", content)
	return c.RenderHtml(content)
}
Esempio n. 17
0
func (c App) DoLogin(user *model.User) revel.Result {
	if user.Name != "" && user.Pass != "" {
		dao, err := model.NewDao()
		defer dao.Close()
		if err != nil {
			c.Flash.Error("用户登录失败,请稍后重试!")
			return c.Redirect("/login")
		}
		loginUser := dao.GetUserForLogin(user.Name, user.Pass)
		if loginUser == nil || loginUser.Name == "" {
			c.Flash.Error("用户名或密码错误!")
			return c.Redirect("/login")
		}
		c.Session["user"] = loginUser.Name
		c.RenderArgs["theUser"] = loginUser
		if preUrl, ok := c.Session["preUrl"]; ok {
			delete(c.Session, "preUrl")
			return c.Redirect(preUrl)
		}
		return c.Redirect("/")
	}
	c.Flash.Error("用户名或密码错误!")
	return c.Redirect("/login")
}
Esempio n. 18
0
func (c Article) Edit(id string) revel.Result {
	if _, ok := c.Session["user"]; !ok {
		c.Session["preUrl"] = fmt.Sprintf("/edit/%s", id)
		c.Flash.Error("亲,登录之后才能编辑哦!")
		return c.Redirect("/login")
	}
	dao, err := model.NewDao()
	if err != nil {
		c.Response.Status = 500
		return c.RenderError(err)
	}
	topic := dao.FindTopicById(id)
	if topic == nil {
		return c.Redirect("/")
	}
	content, ret := utils.GetCodeContent(id)
	if ret != nil {
		c.Response.Status = 500
		return c.RenderError(ret)
	}
	topic.Body = template.HTML(content)
	tags := dao.GetTags()
	return c.Render(topic, tags)
}