Exemple #1
0
// @Title 登录验证
// @router /index [post]
func (this *ViewController) Login() {
	//	recevie the login key
	login_key := this.GetString("login_key")
	if len(login_key) <= 0 && login_key != models.Str2Sha1(this.Ctx.Input.Cookie("beegosessionID")) {
		this.Redirect("/index", 302)
		this.StopRun()
	}
	account := this.GetString("account")
	pwd := this.GetString("pwd")
	user_type := this.GetString("type")

	//	植入cookie
	token := fmt.Sprintf("%d", models.GetMathRand(10240))
	this.Ctx.SetCookie("token", token, 3600)
	if this.GetSession("token") != nil {
		this.DelSession("token")
	}
	this.SetSession("token", token)

	switch user_type {
	case "学生":
		edu := this.GetString("edu")
		if edu == "on" {
			if !models.StudentExist(account) {
				if data, ok, cookies, _ := Edu.Sign_in(account, pwd, user_type); ok {
					if _, err := Edu.GetStudentProfile(data, cookies); err == nil {
						if err = models.AddStudent(&models.Student{Id: account, EduPwd: pwd}); err == nil {
							//	设置session
							if this.GetSession("id") != nil {
								this.DelSession("id")
							}
							this.SetSession("id", account)
							if this.GetSession("type") != nil {
								this.DelSession("type")
							}
							this.SetSession("type", user_type)

							this.Redirect("/view/student/eduLoading", 302)
							this.StopRun()
						}
					}
				}
			} else {
				user, err := models.GetStudentById(account)
				if err == nil {
					if user.EduPwd == pwd {
						//	设置session
						if this.GetSession("id") != nil {
							this.DelSession("id")
						}
						this.SetSession("id", account)
						if this.GetSession("type") != nil {
							this.DelSession("type")
						}
						this.SetSession("type", user_type)

						//	login success
						this.Redirect("/view/student/table", 302)
						this.StopRun()
					}
				}
			}
		}
		if models.StudentExist(account) == true {
			user, err := models.GetStudentById(account)
			if err == nil {
				if user.Password == pwd {
					//	设置session
					if this.GetSession("id") != nil {
						this.DelSession("id")
					}
					this.SetSession("id", account)
					if this.GetSession("type") != nil {
						this.DelSession("type")
					}
					this.SetSession("type", user_type)

					//	login success
					this.Redirect("/view/student/table", 302)
					this.StopRun()
				}
			}
		}
	case "教师":
		id, _ := strconv.ParseInt(account, 10, 64)
		if models.TeacherExist(id) == true {
			user, err := models.GetTeacherById(id)
			if err == nil {
				if user.Password == pwd {
					//	设置session
					if this.GetSession("id") != nil {
						this.DelSession("id")
					}
					this.SetSession("id", account)
					if this.GetSession("type") != nil {
						this.DelSession("type")
					}
					this.SetSession("type", user_type)

					//	login success
					this.Redirect("/view/teacher/table", 302)
					this.StopRun()
				}
			}
		}
	case "教务":
		if models.AdminExist(account) == true {
			user, err := models.GetAdminById(account)
			if err == nil {
				if user.Password == pwd {
					//	设置session
					if this.GetSession("id") != nil {
						this.DelSession("id")
					}
					this.SetSession("id", account)
					if this.GetSession("type") != nil {
						this.DelSession("type")
					}
					this.SetSession("type", user_type)

					//	login success
					this.Redirect("/view/admin/index", 302)
					this.StopRun()
				}
			}
		}
	}

	this.Redirect("/", 302)
	this.StopRun()
}
// @Title 导入教务系统
// @router /eduLoading [get]
func (this *APIStudentController) EduLoading() {
	//	recevie data
	if user_type := this.GetSession("type").(string); user_type != "学生" {
		return
	}
	ws, err := websocket.Upgrade(this.Ctx.ResponseWriter, this.Ctx.Request, nil, 1024, 1024)
	defer func() {
		ws.WriteJSON(&Edu.WsData{Done: true, Data: "操作结束..."})
		ws.Close()
	}()
	if _, ok := err.(websocket.HandshakeError); ok {
		http.Error(this.Ctx.ResponseWriter, "Not a websocket handshake", 400)
		return
	} else if err != nil {
		models.Info("eduLoading", err)
		return
	}
	id := this.GetString("id")
	if user, err := models.GetStudentById(id); err == nil {
		if data, ok, cookies, _ := Edu.Sign_in(id, user.EduPwd, "学生"); ok {
			ws.WriteJSON(&Edu.WsData{Done: false, Data: "登录成功..."})
			ws.WriteJSON(&Edu.WsData{Done: false, Data: "正在获取个人信息..."})
			if userinfo, err := Edu.GetStudentProfile(data, cookies); err == nil {
				ws.WriteJSON(&Edu.WsData{Done: false, Data: "读取个人信息成功..."})
				if err = Edu.UpdateStudentProfile(user, userinfo); err != nil {
					ws.WriteJSON(&Edu.WsData{Done: false, Data: "更新个人信息失败..."})
					return
				}
				ws.WriteJSON(&Edu.WsData{Done: false, Data: "更新个人信息成功..."})
				ws.WriteJSON(&Edu.WsData{Done: false, Data: "正在读取个人课表..."})

				//	add edu log
				userinfo_json, err := json.Marshal(userinfo)
				if err == nil {
					Edu.EduLogCreate(&Edu.EduLog{Student: user, Content: string(userinfo_json), Type: "userProfile", Result: 1})
				}

				// get user schedules
				if schedules, err := Edu.GetStudentSchedule(user, data, cookies); err == nil {
					ws.WriteJSON(&Edu.WsData{Done: false, Data: "读取个人课表成功..."})
					ws.WriteJSON(&Edu.WsData{Done: false, Data: "正在读取历史成绩..."})
					if userscore, err := Edu.GetStudentScore(user, data, cookies); err == nil {
						//	通过遍历课程表 与 历史成绩 添加 教师课程表 添加 历史成绩
						if err := Edu.ControlSQLByScheduleAndScore(user, schedules, userscore); err == nil {
							ws.WriteJSON(&Edu.WsData{Done: false, Data: "添加历史成绩成功..."})
							return
						}
						ws.WriteJSON(&Edu.WsData{Done: false, Data: "添加历史成绩失败..."})
						return
					}
					ws.WriteJSON(&Edu.WsData{Done: false, Data: "读取历史成绩失败..."})
					return
				}
				ws.WriteJSON(&Edu.WsData{Done: false, Data: "读取个人课表失败..."})
				return
			}
			ws.WriteJSON(&Edu.WsData{Done: false, Data: "读取个人信息失败..."})
			return
		}
	}
}