github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/routers/root/RSigninHandler.go (about)

     1  package root
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/insionng/makross"
     7  	"github.com/insionng/makross/cache"
     8  	"github.com/insionng/makross/captcha"
     9  
    10  	"time"
    11  
    12  	"github.com/insionng/yougam/helper"
    13  	"github.com/insionng/yougam/models"
    14  )
    15  
    16  func GetRSigninHandler(self *makross.Context) error {
    17  
    18  	if _, okay := self.Session.Get("SignedUser").(*models.User); okay {
    19  		//如果已登录root
    20  		return self.Redirect("/root/dashboard/")
    21  	} else {
    22  		return self.Render("root/signin")
    23  	}
    24  
    25  }
    26  
    27  func PostRSigninHandler(self *makross.Context) error {
    28  
    29  	username := self.FormValue("username")
    30  	password := self.FormValue("password")
    31  
    32  	if cpt := captcha.Store(self); cpt.VerifyReq(self) {
    33  		if len(username) > 0 && len(password) > 0 {
    34  
    35  			if usr, err := models.GetUserByUsername(username); usr != nil && err == nil {
    36  				if helper.ValidateHash(usr.Password, password) && usr.Role == -1000 {
    37  
    38  					//登录成功设置self.Session.on
    39  					self.Session.Set("SignedUser", usr)
    40  					cache.Store(self).Set(fmt.Sprintf("User:%v", usr.Id), usr, 60*60*60)
    41  					models.PutSignin2User(usr.Id, time.Now().Unix(), usr.SigninCount+1, self.RealIP())
    42  					self.Flash.Success("登录成功!", false)
    43  					return self.Redirect("/root/dashboard/")
    44  
    45  				} else {
    46  					self.Flash.Error("密码错误!", false)
    47  					return self.Redirect("/root/signin/")
    48  
    49  				}
    50  			} else {
    51  				self.Flash.Error("不存在此用户!", false)
    52  				return self.Redirect("/root/signin/")
    53  
    54  			}
    55  		} else {
    56  			if len(username) > 0 {
    57  				self.Flash.Error("用户名不能为空!", false)
    58  			} else {
    59  				self.Flash.Error("密码不能为空!", false)
    60  			}
    61  			return self.Redirect("/root/signin/")
    62  
    63  		}
    64  	} else {
    65  		self.Flash.Error("验证码错误~", false)
    66  		return self.Redirect("/root/signin/")
    67  
    68  	}
    69  
    70  }