This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
92 lines (82 loc) · 1.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
scujwc "github.com/mohuishou/scujwc-go"
)
func main() {
port := flag.String("p", "6777", "请输入需要绑定的端口号")
dist := flag.String("d", "dist/", "请输入前端目录路径")
flag.Parse()
log.Printf("即将监听端口: %s", *port)
http.Handle("/", http.FileServer(http.Dir(*dist)))
http.HandleFunc("/gpa", gpaAll)
http.HandleFunc("/gpa/all", gpaAll)
http.HandleFunc("/gpa/not-pass", gpaNotPass)
if err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", *port), nil); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func login(w http.ResponseWriter, r *http.Request) (scujwc.Jwc, error) {
var s scujwc.Jwc
if r.Method != "POST" {
return s, errors.New("请求方式错误")
}
result, _ := ioutil.ReadAll(r.Body)
v, err := url.ParseQuery(string(result))
if err != nil {
return s, err
}
uid := url.QueryEscape(v.Get("uid"))
password := url.QueryEscape(v.Get("password"))
s, err = scujwc.NewJwc(uid, password)
if err != nil {
return s, err
}
return s, nil
}
func gpa(w http.ResponseWriter, r *http.Request) {
s, err := login(w, r)
if err != nil {
errorRetrun(w, err, nil)
return
}
grade, err := s.GPA()
if err != nil {
errorRetrun(w, err, nil)
return
}
successReturn(w, "本学期成绩获取成功", grade)
}
func gpaAll(w http.ResponseWriter, r *http.Request) {
s, err := login(w, r)
if err != nil {
errorRetrun(w, err, nil)
return
}
grade, err := s.GPAAll()
if err != nil {
errorRetrun(w, err, nil)
return
}
successReturn(w, "所有成绩获取成功", grade)
}
func gpaNotPass(w http.ResponseWriter, r *http.Request) {
s, err := login(w, r)
if err != nil {
errorRetrun(w, err, nil)
return
}
grade, err := s.GPANotPass()
if err != nil {
errorRetrun(w, err, nil)
return
}
successReturn(w, "不及格成绩获取成功", grade)
}