示例#1
0
func Sign_in(id, pwd, user_type string) ([]byte, bool, []*http.Cookie, error) {
	if user_type != "学生" && user_type != "教师" {
		return nil, false, nil, models.ErrorInfo("Sign_in", errors.New("user type is error:"+user_type))
	}
	if len(id) <= 0 || len(pwd) <= 0 {
		return nil, false, nil, models.ErrorInfo("Sign_in", errors.New("id or pwd is error:["+id+"_"+pwd+"]"))
	}
	data, cookies, err := client.SendRequest("GET", Host_url, "", nil, nil)
	if err != nil {
		return nil, false, nil, models.ErrorInfo("Sign_in", err)
	}
	vierstate, eventvalidation, err := getLoginKey(string(data))
	if err != nil {
		return nil, false, nil, models.ErrorInfo("Sign_in", err)
	}
	//	sigin...
	post_arg := url.Values{
		"__VIEWSTATE":       {vierstate},
		"__EVENTVALIDATION": {eventvalidation},
		"TextBox1":          {id},
		"TextBox2":          {pwd},
		"RadioButtonList1":  {models.Utf82gbk(user_type)},
		"Button1":           {""},
	}
	//	登陆
	main_data, _, err := client.SendRequest("POST", Host_url+"/default2.aspx", post_arg.Encode(), cookies, header)
	if err != nil {
		return nil, false, nil, models.ErrorInfo("Sign_in", err)
	}
	//	判断是否登录失败
	if strings.Contains(string(main_data), "<title>登录</title>") {
		return nil, false, nil, models.ErrorInfo("Sign_in", errors.New("id or pwd not match"))
	}
	return main_data, true, cookies, nil
}
示例#2
0
func GetStudentScore(user *models.Student, data []byte, cookies []*http.Cookie) (map[string]map[string]string, error) {
	userscorehref, _ := getUserScoreHref(string(data))
	userscore_param, _, err := client.SendRequest("GET", Host_url+"/"+userscorehref, "", cookies, header)
	if err != nil {
		return nil, models.ErrorInfo("GetStudentScore", errors.New("连接失败,错误原因:"+err.Error()))
	}
	vierstate, eventvalidation, _ := getLoginKey(string(userscore_param))
	userscore_info, _, err := client.SendRequest("POST", Host_url+"/"+userscorehref, url.Values{
		"__VIEWSTATE":       {vierstate},
		"__EVENTVALIDATION": {eventvalidation},
		"ddIXN":             {""},
		"ddIXQ":             {""},
		"Button1":           {models.Utf82gbk("按学期查询")},
	}.Encode(), cookies, header)
	if err != nil {
		return nil, models.ErrorInfo("GetStudentScore", errors.New("连接失败,错误原因:"+err.Error()))
	}
	userscore, _ := getUserScore(string(userscore_info))
	for _, v := range userscore {
		if len(v) > 0 {
			if exist := models.CourseExist(v["课程名称"]); !exist {
				if exist := models.DepartmentExist(v["学院名称"]); !exist {
					if _, err := models.AddDepartment(&models.Department{Name: v["学院名称"]}); err == nil {
						EduLogCreate(&EduLog{Student: user, Content: v["学院名称"], Type: "department", Result: 1})
					} else {
						EduLogCreate(&EduLog{Student: user, Content: v["学院名称"], Type: "department", Result: 0})
					}
				}
				if department, err := models.GetDepartmentByName(v["学院名称"]); err == nil {
					f, _ := strconv.ParseFloat(v["学分"], 64)
					if err := models.AddCourse(&models.Course{Id: v["课程代码"], Name: v["课程名称"], Department: department, Remark: v["课程归属"], Type: v["课程性质"], Credit: f}); err == nil {
						EduLogCreate(&EduLog{Student: user, Content: v["课程名称"], Type: "course", Result: 1})
					} else {
						EduLogCreate(&EduLog{Student: user, Content: v["课程名称"], Type: "course", Result: 0})
					}
				}
			}
		}
	}
	return userscore, nil
}