|
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"weather/models"
|
|
|
|
|
|
"weather/service"
|
|
|
|
|
|
"weather/utils"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
|
|
_ "github.com/astaxie/beego/session/mysql"
|
|
|
|
|
|
"github.com/astaxie/beego/validation"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type LoginController struct {
|
|
|
|
|
|
beego.Controller
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *LoginController) Get() {
|
|
|
|
|
|
c.TplName = "login.tpl"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *LoginController) Regist() {
|
|
|
|
|
|
c.TplName = "regist.tpl"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *LoginController) RegistSubmit() {
|
|
|
|
|
|
u := models.Account{}
|
|
|
|
|
|
if err := c.ParseForm(&u); err != nil {
|
|
|
|
|
|
c.Ctx.WriteString("jsoninfo is empty")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
valid := validation.Validation{}
|
|
|
|
|
|
valid.Required(u.Username, "username")
|
|
|
|
|
|
valid.Required(u.Password, "password")
|
|
|
|
|
|
valid.MaxSize(u.Username, 35, "username")
|
|
|
|
|
|
valid.MinSize(u.Username, 5, "username")
|
|
|
|
|
|
valid.MaxSize(u.Password, 35, "password")
|
|
|
|
|
|
valid.MinSize(u.Password, 5, "password")
|
|
|
|
|
|
if valid.HasErrors() {
|
|
|
|
|
|
// 如果有错误信息,证明验证没通过
|
|
|
|
|
|
//c.TplName = "login.tpl"
|
|
|
|
|
|
fmt.Println("登录校验失败")
|
|
|
|
|
|
c.Redirect("/", 302)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Println(u.Username)
|
|
|
|
|
|
fmt.Println(u.Password)
|
|
|
|
|
|
|
|
|
|
|
|
accountService := new(service.AccountService)
|
|
|
|
|
|
loginSuccess := accountService.RegistAccount(u)
|
|
|
|
|
|
fmt.Println(loginSuccess)
|
|
|
|
|
|
|
|
|
|
|
|
//c.Ctx.Output.Cookie("uid", "235812")
|
|
|
|
|
|
//c.SetSession("uid", "235812")
|
|
|
|
|
|
//c.Ctx.Redirect(301, "/v1/main")
|
|
|
|
|
|
c.TplName = "login.tpl"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *LoginController) SetSess(session int64) {
|
|
|
|
|
|
c.SetSession(getSessName(), session)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetSess 获取session,返回userID和是否成功获取。若失败,则表示需要重新登录
|
|
|
|
|
|
func (c *LoginController) GetSess() (int64, bool) {
|
|
|
|
|
|
if session := c.getSess(); session != nil {
|
|
|
|
|
|
if data, ok := session.(int64); ok {
|
|
|
|
|
|
return data, true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0, false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *LoginController) DelSess() {
|
|
|
|
|
|
c.DelSession(getSessName())
|
|
|
|
|
|
c.DestroySession()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *LoginController) ValidSessionUserID() (int64, error) {
|
|
|
|
|
|
if session := c.getSess(); session != nil {
|
|
|
|
|
|
if data, ok := session.(int64); ok {
|
|
|
|
|
|
return data, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0, utils.NewError(10000, "未登录")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *LoginController) getSess() interface{} {
|
|
|
|
|
|
return c.GetSession(getSessName())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getSessName() string {
|
|
|
|
|
|
return utils.GetSessName()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *LoginController) Post() {
|
|
|
|
|
|
u := models.Account{}
|
|
|
|
|
|
if err := c.ParseForm(&u); err != nil {
|
|
|
|
|
|
c.Ctx.WriteString("jsoninfo is empty")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
valid := validation.Validation{}
|
|
|
|
|
|
valid.Required(u.Username, "username")
|
|
|
|
|
|
valid.Required(u.Password, "password")
|
|
|
|
|
|
valid.MaxSize(u.Username, 35, "username")
|
|
|
|
|
|
valid.MinSize(u.Username, 5, "username")
|
|
|
|
|
|
valid.MaxSize(u.Password, 35, "password")
|
|
|
|
|
|
valid.MinSize(u.Password, 5, "password")
|
|
|
|
|
|
if valid.HasErrors() {
|
|
|
|
|
|
// 如果有错误信息,证明验证没通过
|
|
|
|
|
|
//c.TplName = "login.tpl"
|
|
|
|
|
|
fmt.Println("登录校验失败")
|
|
|
|
|
|
c.Redirect("/", 302)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Println(u.Username)
|
|
|
|
|
|
fmt.Println(u.Password)
|
|
|
|
|
|
|
|
|
|
|
|
accountService := new(service.AccountService)
|
|
|
|
|
|
accountResult := accountService.LoginAccount(u)
|
|
|
|
|
|
fmt.Println(accountResult)
|
|
|
|
|
|
if accountResult != nil {
|
|
|
|
|
|
fmt.Println("登录成功")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
fmt.Println("登录失败")
|
|
|
|
|
|
c.Redirect("/", 302)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.SetSess(int64(accountResult.Id))
|
|
|
|
|
|
fmt.Println(c.getSess())
|
|
|
|
|
|
|
|
|
|
|
|
//c.Ctx.Output.Cookie("uid", "235812")
|
|
|
|
|
|
//c.SetSession("uid", "235812")
|
|
|
|
|
|
//c.Ctx.Redirect(301, "/v1/main")
|
|
|
|
|
|
c.TplName = "success.tpl"
|
|
|
|
|
|
}
|