-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor the classroom service
- Loading branch information
1 parent
7cf5c99
commit 9d3fafa
Showing
10 changed files
with
155 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,11 @@ | ||
# 空教室模块 | ||
|
||
**此模块用于研究生获取空教室信息** create by 李梓玄 2023.10.17 | ||
**此模块用于研究生获取空教室信息** create by 李梓玄 2023.10.17 update by 朱胤帆 2024.9.29 | ||
|
||
此模块基于kitex框架(微服务框架):https://github.com/cloudwego/kitex | ||
|
||
此模块基于jwch库(教务处的信息爬取库):https://github.com/west2-online/jwch | ||
|
||
## 接口文档 | ||
飞书文档: | ||
|
||
TODOOOOOOOOOOOOOOOOOOO! | ||
|
||
## 接口的传入参数与返回值 | ||
|
||
参阅项目根目录下的`idl/empty_room.thrift`文件 | ||
|
||
如需修改接口的传入参数与返回值,参照thrift语法修改`empty_room.thrift`后,在`cmd/empty_room`执行 | ||
|
||
```shell | ||
# 更多信息参照 cmd/classroom/Makefile | ||
make gen | ||
``` | ||
|
||
其中请求中的参数`building`需要传入一个教学楼Map的key,代码如下 | ||
|
||
```go | ||
var ( | ||
buildingMap = map[string]string{ | ||
"x3": "公共教学楼西3", | ||
"x2": "公共教学楼西2", | ||
"x1": "公共教学楼西1", | ||
"zl": "公共教学楼中楼", | ||
"d1": "公共教学楼东1", | ||
"d2": "公共教学楼东2", | ||
"d3": "公共教学楼东3", | ||
"wkl": "公共教学楼文科楼", | ||
"wk": "公共教学楼文科楼", | ||
} | ||
) | ||
|
||
``` | ||
|
||
该Map定义于jwch库中: https://github.com/west2-online/jwch | ||
|
||
## 接口逻辑 | ||
|
||
**因为研究生的教学管理系统没有获取空教室功能,所以本接口基于一个默认的本科生账号密码去本科教学管理系统获取** | ||
|
||
此模块暂时只能获取旗山校区的空教室信息 | ||
|
||
### `GetEmptyRoom` | ||
|
||
1. 验证token handler | ||
2. 当未传入账号密码时,使用默认账号密码 handler | ||
3. 根据request信息拼接key,从`redis`中寻找缓存 | ||
4. 如果命中缓存,则返回空教室信息 | ||
5. 如果未命中缓存,调用`jwch`库进行登录、获取空教室信息 | ||
6. 返回空教室信息,同时开启一个Goroutine 将空教室信息写入 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/pkg/errors" | ||
"github.com/west2-online/fzuhelper-server/config" | ||
"github.com/west2-online/fzuhelper-server/pkg/constants" | ||
"github.com/west2-online/fzuhelper-server/pkg/logger" | ||
"github.com/west2-online/jwch" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// ScheduledGetClassrooms 定期获取空教室信息 | ||
func ScheduledGetClassrooms() error { | ||
ctx := context.Background() | ||
//定义jwch的stu客户端 | ||
stu := jwch.NewStudent().WithUser(config.DefaultUser.Account, config.DefaultUser.Password) | ||
//登录,id和cookies会自动保存在client中 | ||
stu.Login() | ||
|
||
var dates []string | ||
currentTime := time.Now() | ||
//设定一周时间 | ||
for i := 0; i < 7; i++ { | ||
date := currentTime.AddDate(0, 0, i).Format("2006-01-02") | ||
dates = append(dates, date) | ||
} | ||
//构建jwch的请求参数 | ||
for _, date := range dates { | ||
for _, campus := range constants.CampusArray { | ||
for startTime := 1; startTime <= 11; startTime++ { | ||
for endTime := startTime; endTime <= 11; endTime++ { | ||
args := jwch.EmptyRoomReq{ | ||
Campus: campus, | ||
Time: date, | ||
Start: strconv.Itoa(startTime), | ||
End: strconv.Itoa(endTime), | ||
} | ||
var res []string | ||
var err error | ||
if campus == "旗山校区" { | ||
res, err = stu.GetQiShanEmptyRoom(args) | ||
} else { | ||
res, err = stu.GetEmptyRoom(args) | ||
} | ||
if err != nil { | ||
logger.LoggerObj.Errorf("classroom.cache.GetClassrooms: %v", err) | ||
return errors.Wrap(err, "classroom.cache.GetClassrooms failed") | ||
} | ||
if campus == "厦门工艺美院" { | ||
go SetXiaMenEmptyRoomCache(ctx, date, args.Start, args.End, res) | ||
} else { | ||
key := fmt.Sprintf("%s.%s.%s.%s", args.Time, args.Campus, args.Start, args.End) | ||
go SetEmptyRoomCache(ctx, key, res) | ||
} | ||
logger.LoggerObj.Debugf("classroom.cache.GetClassrooms add task %v", args) | ||
} | ||
} | ||
logger.LoggerObj.Infof("classroom.cache.CGetClassrooms add all tasks of campus %v in the day %v", campus, date) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.