//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) } }
//Contact 联系处理 func (c *Controller) Contact(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { if err := unitl.ReadHtml(w, "contact", nil); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } else { unitl.ReadHtml(w, "404", nil) } }
//后台首页 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) } }
//Upload 处理用户上传文件 func (c *Controller) Upload(w http.ResponseWriter, r *http.Request) { sess, _ := globalSessions.SessionStart(w, r) defer sess.SessionRelease(w) if r.Method == "POST" { if sess.Get("studentName") == nil || sess.Get("studentId") == nil { http.Redirect(w, r, "/", http.StatusNotFound) return } err := unitl.Uploadfile(w, r, "uploadfile") if err != nil { errinfo := make(map[string]interface{}) errinfo["title"] = "文件上传错误" errinfo["info"] = err.Error() unitl.ReadHtml(w, "err", errinfo) } } else { unitl.ReadHtml(w, "404", nil) } }
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) } }
//更新数据 func (this Controller) Update(w http.ResponseWriter, r *http.Request) { sess, _ := globalSessions.SessionStart(w, r) defer sess.SessionRelease(w) if r.Method == "POST" { if sess.Get("adminName") == nil || sess.Get("adminId") == nil { http.Redirect(w, r, "admin/login", 404) } fmt.Println(r.FormValue("id")) } else { unitl.ReadHtml(w, "404", nil) } }
//Post 数据提交页面 func (c *Controller) Post(w http.ResponseWriter, r *http.Request) { //启动session sess, _ := globalSessions.SessionStart(w, r) defer sess.SessionRelease(w) if r.Method == "GET" { data := make(map[string]interface{}) data["studentName"] = sess.Get("studentName") data["studentId"] = sess.Get("studentId") if data["studentName"] == nil || data["studentId"] == nil { http.Redirect(w, r, "/", http.StatusFound) } if err := unitl.ReadHtml(w, "post", data); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } }