//Login 处理用户登录 func (c *Controller) Login(w http.ResponseWriter, r *http.Request) { sess, _ := globalSessions.SessionStart(w, r) defer sess.SessionRelease(w) //验证学号与学校(如果信息正确,将信息写入session中) if r.Method == "POST" { stud := db.NewModel("students") defer stud.Close() student, err := stud.Where("num").Find(unitl.Trim(r,"num")) if err != nil { errInfo := make(map[string]interface{}) errInfo["title"] = "账号密码错误" errInfo["info"] = "账号或密码错误" unitl.ReadHtml(w, "err", errInfo) } sch := db.NewModel("school") school,_ := sch.Where("id").Find(unitl.Trim(r,"school")) if unitl.GetMD5Hash(unitl.Trim(r,"pwd")) != student["pwd"] && school["id"] != student["school_id"] { unitl.ReadHtml(w, "accounterr", nil) } //将信息写入session作为认证凭据 sess.Set("studentName", student["name"]) sess.Set("studentId", student["num"]) http.Redirect(w, r, "/post", http.StatusFound) } else { unitl.ReadHtml(w, "404", nil) } }
func (this Controller) Login(w http.ResponseWriter, r *http.Request) { sess, _ := globalSessions.SessionStart(w, r) defer sess.SessionRelease(w) r.ParseForm() if r.Method == "GET" { unitl.ReadHtml(w, "admin/login", nil) } else { //数据库查询 m := db.NewModel("admin") s, err := m.Where("accounts").Find(r.FormValue("accounts")) if err != nil { errinfo := make(map[string]interface{}) errinfo["title"] = "账号或者密码错误" errinfo["info"] = "账号或者密码错误" unitl.ReadHtml(w, "err", errinfo) return } if unitl.GetMD5Hash(r.FormValue("pwd")) != s["pwd"] { errinfo := make(map[string]interface{}) errinfo["title"] = "账号或者密码错误" errinfo["info"] = "账号或者密码错误" unitl.ReadHtml(w, "err", errinfo) return } //将信息写入session作为认证凭据 sess.Set("adminName", s["name"]) sess.Set("adminId", s["accounts"]) http.Redirect(w, r, "/admin/index", http.StatusFound) } }
//后台首页 func (this Controller) Index(w http.ResponseWriter, r *http.Request) { sess, _ := globalSessions.SessionStart(w, r) defer sess.SessionRelease(w) if r.Method == "GET" { if sess.Get("adminName") == nil || sess.Get("adminId") == nil { http.Redirect(w, r, "/admin/login", http.StatusFound) } stdinfo := db.NewModel("info") tmp, err := stdinfo.FindAll() if err != nil { unitl.CheckErr(err) } //fmt.Println(stdinfos) //var stdInfos []interface{} /*for _, v := range tmp { stdInfos = append(stdInfos, v) }*/ //fmt.Println(stdInfos) //data := make(map[string]interface{}) //stdinfo2 := make(map[string]interface{}) //stdinfo2["info"] = stdinfos //data["info"] = stdInfos unitl.ReadHtml(w, "admin/index", tmp) } }
func (this Controller) Edit(w http.ResponseWriter, r *http.Request) { sess, _ := globalSessions.SessionStart(w, r) defer sess.SessionRelease(w) if r.Method == "GET" { if sess.Get("adminName") == nil || sess.Get("adminId") == nil { unitl.ReadHtml(w, "404", nil) } index := r.URL.RequestURI()[12:] m := db.NewModel("info") s, err := m.Find(index) if err != nil { unitl.ReadHtml(w, "404", nil) } unitl.ReadHtml(w, "admin/edit", s) } }
//Index 首页 func (c *Controller) Index(w http.ResponseWriter, r *http.Request) { //展示首页 if r.Method == "GET" { //获取客户端ip与天气信息 var data = make(map[string]interface{}) //数据存储变量(天气,学校列表) s := r.RemoteAddr ip := s[0 : len(s)-6] if ip == "127.0.0.1" { data["err"] = "获取天气失败" } else { city, _ := unitl.GetCity(ip) data, _ = unitl.GetWeather(city) data["and"] = "~" } //获取学校 var schoolInfo []interface{} school := db.NewModel("school") defer school.Close() tmp, err := school.FindAll() if err != nil { unitl.CheckErr(err) } for _, v := range tmp { schoolInfo = append(schoolInfo, v) } data["school"] = schoolInfo //加载首页魔板 if err := unitl.ReadHtml(w, "index", data); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } else { unitl.ReadHtml(w, "404", nil) } }