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() }
func (this *FileController) UploadFiles() { taskid, err := this.GetInt("taskid") if err != nil { beego.Error(err) this.Data["json"] = &FileRsp{Success: false, Msg: "无法读取任务id!"} this.ServeJson() return } o := orm.NewOrm() qstask := o.QueryTable("survey_task") var surveytask models.SurveyTask err = qstask.Filter("id", taskid).One(&surveytask) if err != nil { beego.Error(err) this.Redirect("/errorpage/500", 302) return } tnow := time.Now() // year, month, day := t.Date() // t = time.Date(year, month, day, 0, 0, 0, 0, time.Local) if tnow.Before(surveytask.TaskStarted) { this.Data["json"] = &FileRsp{Success: false, Msg: "调研还未开始!"} this.ServeJson() return } if tnow.After(surveytask.TaskEnded) { this.Data["json"] = &FileRsp{Success: false, Msg: "调研已经结束!"} this.ServeJson() return } //判断是否是普通用户登录 v := this.GetSession("currentUser") user, ok := v.(models.User) if !ok { this.Data["json"] = &FileRsp{Success: false, Msg: "只有注册用户才能参与填报,请退出并重新登录!"} this.ServeJson() return } o.Read(user.OrgUnit) //存储上传的文件 file, fileHeader, err := this.GetFile("upload-file") if err != nil { beego.Error(err) this.Data["json"] = &FileRsp{Success: false, Msg: "无法读取表单上传文件数据!"} this.ServeJson() return } defer file.Close() curdir, err := os.Getwd() var tofile string t := time.Now() tdate := t.Format("20060102") tofilepath := "/" + beego.AppConfig.String("conf::uploads_dir") + "/" + tdate + "/" tofilename := lib.StrToMD5("survey" + t.String() + fileHeader.Filename) err = os.MkdirAll(curdir+tofilepath, 0755) if err != nil { beego.Error(err) this.Data["json"] = &FileRsp{Success: false, Msg: "无法创建文件夹!"} this.ServeJson() return } tofile = curdir + tofilepath + tofilename f, err := os.OpenFile(tofile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { beego.Error(err) this.Data["json"] = &FileRsp{Success: false, Msg: "无法创建文件!"} this.ServeJson() return } defer f.Close() io.Copy(f, file) //文件信息写入数据库 // qsfile := o.QueryTable("file") taskfile := new(models.File) taskfile.FileName = fileHeader.Filename taskfile.FilePath = tofilepath + tofilename taskfile.FileExt = filepath.Ext(fileHeader.Filename) //获取文件大小 if sizeInterface, ok := file.(Size); ok { taskfile.FileSize = int(sizeInterface.Size()) } taskfile.SurveyTask = &surveytask taskfile.OrganizationUnit = user.OrgUnit taskfile.User = &user fileid, err := o.Insert(taskfile) if err != nil { beego.Error(err) //入库出错,删除已上传文件 err = os.Remove(tofile) this.Data["json"] = &FileRsp{Success: false, Msg: "文件写入失败!"} this.ServeJson() return } this.Data["json"] = &FileRsp{Success: true, Id: int(fileid), FileName: taskfile.FileName, FileSize: taskfile.FileSize} this.ServeJson() }