// Login 登录处理 func (m *ProfileController) Login() { if m.Ctx.Request.Method == "GET" { } else if m.Ctx.Request.Method == "POST" { account := strings.TrimSpace(m.GetString("account")) password := strings.TrimSpace(m.GetString("password")) remember := m.GetString("remember") // fmt.Println("account = " + account) // fmt.Println("password = "******"" && password != "" { var admin models.Admin admin.Account = account // if account == "admin" && password == "123456" { // admin.NickName = "我是管理员" // admin.LastIP = m.GetClientIP() // admin.Password = toolkit.SHA256([]byte(password)) // admin.LastTime = toolkit.GetTime() // admin.Token = toolkit.GenUID() // admin.Email = "*****@*****.**" // admin.Status = 1 // admin.Head = "default.png" // admin.Insert() // m.Redirect(beego.AppConfig.String("adminurl"), 302) // return // } if admin.Read("account") != nil || admin.Password != toolkit.SHA256([]byte(password)) { // 用户名或密码输入错误 fmt.Println("账号或密码填写错误") m.Data["errmsg"] = "账号或密码填写错误" } else { // 登录成功 token := toolkit.GenUID() admin.LastIP = m.GetClientIP() admin.LastTime = toolkit.GetTime() admin.Token = token admin.Update() key := []byte(beego.AppConfig.String("aeskey")) result, err := toolkit.AesEncrypt([]byte(m.GetClientIP()+"|"+token), key) if err != nil { return } auth := base64.StdEncoding.EncodeToString(result) fmt.Println("auth = " + auth) if remember == "yes" { m.Ctx.SetCookie("auth", auth, 7*86400) } else { m.Ctx.SetCookie("auth", auth) } m.Redirect(beego.AppConfig.String("adminurl"), 302) } } else { // 用户名或密码为空 fmt.Println("账号或密码为空") m.Data["errmsg"] = "账号或密码不能为空" } } m.TplName = beego.AppConfig.String("adminpath") + "/login.html" }
// Head 上传头像 func (m *ProfileController) Head() { admin := models.Admin{Account: m.userName} o := orm.NewOrm() if err := o.Read(&admin, "Account"); err != nil { m.error(err.Error()) } if m.Ctx.Request.Method == "POST" { fmt.Printf("upload head...\n") file, handler, err := m.GetFile("head") defer file.Close() if err != nil { m.Data["head_errmsg"] = "上传文件错误: " + err.Error() } else { if fileSize, ok := file.(Size); ok { size := float64(fileSize.Size()) / (1024 * 1024) fmt.Printf("filesize = %fMB\n", toolkit.Round(size, 3)) // if size > 5 { // m.Data["head_errmsg"] = "上传文件错误: 文件大小超出5M" // } else { // 能够获取到正确的文件,保存该文件 fmt.Printf("filename = %s\n", handler.Filename) _, fileSuffix := toolkit.SplitFileNameAndSuffix(handler.Filename) fileName := toolkit.GenUID() + fileSuffix savePath := filepath.Join(beego.AppConfig.String("headpath"), fileName) fmt.Printf("filepath = %s\n", savePath) // 保存文件时需要相对路径,即 ./static/upload/head/ err = m.SaveToFile("head", filepath.Join(".", savePath)) if err != nil { m.Data["head_errmsg"] = "上传文件错误: " + err.Error() } else { admin.Head = fileName if num, err := o.Update(&admin, "head"); err == nil { if num > 0 { m.Data["head_result"] = true } else { m.Data["head_errmsg"] = "上传文件错误: 服务器保存文件失败" } } else { m.Data["profile_errmsg"] = "上传文件错误: 服务器保存文件失败" } } // } } else { m.Data["head_errmsg"] = "获取上传文件错误: 无法获取文件大小" } } } format := admin.LastTime.Format("2006-01-02 15:04:05") m.Data["time"] = format m.Data["head"] = beego.AppConfig.String("headpath") + "/" + admin.Head m.Data["admin"] = admin m.display("profile") }