func (this *LoginController) Login() { username := this.GetString("username") //密码未加密 password := this.GetString("password") encryptPassword := lib.StrToMD5(password) //rememberme, _ := this.GetBool("rememberme") if username == "" || password == "" { this.Data["json"] = &Rsp{Success: false, Msg: "用户名或密码为空,登录失败,请重新输入用户信息!"} this.ServeJson() return } //判断超级管理员登录 if username == beego.AppConfig.String("conf::admin_user") { if encryptPassword == beego.AppConfig.String("conf::admin_pass") { this.SetSession("adminUser", username) this.SetSession("adminRealname", beego.AppConfig.String("conf::admin_realname")) this.Data["json"] = &Rsp{Success: true, Msg: "登录成功!"} this.ServeJson() return } } //判断普通登录 o := orm.NewOrm() qs := o.QueryTable("user") var loginUser models.User err := qs.Filter("username", username).Filter("password", encryptPassword).One(&loginUser) if err != nil { beego.Error(err) this.Data["json"] = &Rsp{Success: false, Msg: "用户登录失败,请重新输入用户信息!"} this.ServeJson() return } if loginUser.UserName != username || loginUser.PassWord != encryptPassword { this.Data["json"] = &Rsp{Success: false, Msg: "用户登录失败,请重新输入用户信息!"} this.ServeJson() return } loginUser.LastLogin = time.Now() _, err = o.Update(&loginUser) if err != nil { beego.Error(err) } //用户验证成功,记录用户session this.SetSession("currentUser", loginUser) this.Data["json"] = &Rsp{Success: true, Msg: "登录成功!"} this.ServeJson() }
func (this *UserController) Add() { oid, err := this.GetInt("oid") username := this.GetString("username") realname := this.GetString("realname") password := this.GetString("password") confirmpassword := this.GetString("confirmpassword") if password != confirmpassword { this.Data["json"] = &Rsp{Success: false, Msg: "密码与确认密码不相同,插入数据出错!"} this.ServeJson() return } mobile := this.GetString("mobile") phone := this.GetString("phone") idcard := this.GetString("idcard") o := orm.NewOrm() qs := o.QueryTable("user") num, _ := qs.Filter("username", username).Count() if num > 0 { this.Data["json"] = &Rsp{Success: false, Msg: "已有相同用户名的用户,插入数据出错!"} this.ServeJson() return } var organization models.OrganizationUnit qsou := o.QueryTable("organization_unit") err = qsou.Filter("id", oid).One(&organization) if err != nil { beego.Error(err) this.Data["json"] = &Rsp{Success: false, Msg: "无法找到对应的组织机构,插入数据出错!"} this.ServeJson() return } var user models.User user.OrgUnit = &organization user.UserName = username user.PassWord = lib.StrToMD5(password) user.RealName = realname user.Mobile = mobile user.Phone = phone user.IdCard = idcard _, err = o.Insert(&user) if err != nil { beego.Error(err) this.Data["json"] = &Rsp{Success: false, Msg: "无法新增用户,插入数据出错!"} this.ServeJson() return } this.Data["json"] = &Rsp{Success: true, Msg: "成功新增用户!"} this.ServeJson() }
func (this *UserController) Modify() { id, err := this.GetInt("id") o := orm.NewOrm() qs := o.QueryTable("user") var user models.User err = qs.Filter("id", id).One(&user) if err != nil { beego.Error(err) this.Data["json"] = &Rsp{Success: false, Msg: "用户数据不存在,编辑用户失败!"} this.ServeJson() return } oid, _ := this.GetInt("oid") username := this.GetString("username") realname := this.GetString("realname") password := this.GetString("password") confirmpassword := this.GetString("confirmpassword") if password != confirmpassword { this.Data["json"] = &Rsp{Success: false, Msg: "密码与确认密码不相同,编辑用户出错!"} this.ServeJson() return } mobile := this.GetString("mobile") phone := this.GetString("phone") idcard := this.GetString("idcard") if user.UserName != username { num, _ := qs.Filter("username", username).Count() if num > 0 { this.Data["json"] = &Rsp{Success: false, Msg: "已有相同用户名的用户,编辑用户出错!"} this.ServeJson() return } } var organization models.OrganizationUnit qsou := o.QueryTable("organization_unit") err = qsou.Filter("id", oid).One(&organization) if err != nil { beego.Error(err) this.Data["json"] = &Rsp{Success: false, Msg: "无法找到对应的组织机构,编辑用户出错!"} this.ServeJson() return } user.OrgUnit = &organization user.UserName = username if password != "" { user.PassWord = lib.StrToMD5(password) } user.RealName = realname user.Mobile = mobile user.Phone = phone user.IdCard = idcard user.Updated = time.Now() _, err = o.Update(&user) if err != nil { beego.Error(err) this.Data["json"] = &Rsp{Success: false, Msg: "编辑用户出错!"} this.ServeJson() return } this.Data["json"] = &Rsp{Success: true, Msg: "成功编辑用户!"} this.ServeJson() }