Exemplo n.º 1
0
func (this *LoginController) Post() {
	username := this.GetString("login_username")
	password := this.GetString("login_password")

	loginRet := rpc.JsonResult{}
	if username == "" || password == "" {
		loginRet.Result = "请输入用户名和密码!"
	}
	params := t.Params{
		"username": username,
		"password": password,
	}
	code, user := svc.Get("user", params)

	if code != "success" {
		beego.Error("User:%s login error code: %s", username, code)
		loginRet.Result = "用户名或密码错误!"
		this.Data["json"] = &loginRet
		this.ServeJson()
		return
	}
	loginRet = this.setSessionFromUser(user)
	if loginRet.Ret == "success" {
		this.SetSession(SessionUserUserName, username)
	}
	this.Data["json"] = &loginRet
	this.ServeJson()
}
Exemplo n.º 2
0
func (this *LoginController) setSessionFromUser(user map[string]interface{}) rpc.JsonResult {
	loginRet := rpc.JsonResult{}
	role, ok := user["role"]
	if !ok {
		loginRet.Ret = "faild"
		loginRet.Result = "获取权限失败"
		return loginRet
	}
	sn, ok := user["sn"]
	if !ok {
		loginRet.Ret = "faild"
		loginRet.Result = "获取权限失败"
		return loginRet
	}
	department, ok := user["department"]
	if !ok {
		loginRet.Ret = "faild"
		loginRet.Result = "获取权限失败"
		return loginRet
	}
	name, ok := user[s.Name]
	if !ok {
		loginRet.Ret = "faild"
		loginRet.Result = "获取权限失败"
		return loginRet
	}
	this.SetSession(SessionUserRole, role)
	this.SetSession(SessionUserSn, sn)
	this.SetSession(SessionUserName, name)
	this.SetSession(SessionUserDepartment, department)
	loginRet.Ret = "success"
	loginRet.Result = "登录成功"
	return loginRet
}