accountlogin.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package controllers
  2. import (
  3. "github.com/astaxie/beego"
  4. "box-gm/models"
  5. "box-gm/utils"
  6. // "log"
  7. "time"
  8. )
  9. type AccountLoginController struct {
  10. beego.Controller
  11. }
  12. type LoginInfo struct {
  13. Username string `form:"username"`
  14. Passward string `form:"pwd"`
  15. }
  16. func (c *AccountLoginController) Get() {
  17. c.SetSession("user", nil)
  18. c.Data["title"] = beego.AppConfig.String("apptitle")
  19. // if beego.AppConfig.String("run_test") == "true" {
  20. // c.Data["lu"] = "admin"
  21. // c.Data["lp"] = "123456"
  22. // }
  23. c.TplName = "accountlogin.tpl"
  24. }
  25. func (c *AccountLoginController) Post() {
  26. username := c.GetString("username")
  27. password := c.GetString("password")
  28. if username == "" || password == "" {
  29. c.Data["json"] = &map[string]interface{}{"status": false, "info": "帐号密码不能为空"}
  30. c.ServeJSON()
  31. return
  32. }
  33. user := models.GetUserByUsername(username)
  34. // log.Printf("AccountLoginController post user[%v] password[%s]", user, utils.Strtomd5(password))
  35. if user == nil || user.Password != utils.Strtomd5(password) {
  36. c.Data["json"] = &map[string]interface{}{"status": false, "info": "密码错误"}
  37. c.ServeJSON()
  38. return
  39. }
  40. c.SetSession("user", &LoginInfo{username, password})
  41. user.LastLoginTs = time.Now().Unix()
  42. models.UpdateUser(user)
  43. //c.Data["json"] = &map[string]interface{}{"status": true, "info": "登陆成功"}
  44. //c.ServeJSON()
  45. c.Ctx.Redirect(302, "/index")
  46. }