2022-08-01 09:51:45 +00:00
|
|
|
// Package web
|
|
|
|
// @Description: 封装了所以web相关的内容
|
2022-04-20 13:31:46 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"net/http"
|
2022-08-26 00:25:07 +00:00
|
|
|
"strings"
|
2022-04-20 13:31:46 +00:00
|
|
|
|
2022-10-18 04:20:02 +00:00
|
|
|
"github.com/gin-contrib/gzip"
|
2022-04-20 13:31:46 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2022-04-25 10:39:54 +00:00
|
|
|
|
2022-08-03 10:04:52 +00:00
|
|
|
"github.com/johlanse/study_xxqg/conf"
|
|
|
|
"github.com/johlanse/study_xxqg/utils"
|
2022-09-06 14:56:18 +00:00
|
|
|
"github.com/johlanse/study_xxqg/utils/update"
|
2022-04-20 13:31:46 +00:00
|
|
|
)
|
|
|
|
|
2022-08-01 09:51:45 +00:00
|
|
|
// 将静态文件嵌入到可执行程序中来
|
2022-04-20 13:31:46 +00:00
|
|
|
//go:embed xxqg/build
|
|
|
|
var static embed.FS
|
|
|
|
|
|
|
|
// RouterInit
|
|
|
|
// @Description:
|
|
|
|
// @return *gin.Engine
|
|
|
|
func RouterInit() *gin.Engine {
|
2022-07-24 07:56:12 +00:00
|
|
|
router := gin.Default()
|
2022-08-07 10:39:15 +00:00
|
|
|
router.RemoveExtraSlash = true
|
2022-08-01 09:51:45 +00:00
|
|
|
router.Use(cors())
|
2022-10-18 04:20:02 +00:00
|
|
|
router.Use(gzip.Gzip(1, gzip.WithExcludedExtensions([]string{"js", "css", "map", "png", "ico"})))
|
2022-07-30 10:55:00 +00:00
|
|
|
|
2022-08-01 09:51:45 +00:00
|
|
|
// 挂载静态文件
|
2022-04-20 13:31:46 +00:00
|
|
|
router.StaticFS("/static", http.FS(static))
|
2022-08-01 09:51:45 +00:00
|
|
|
// 访问首页时跳转到对应页面
|
2022-04-21 09:03:04 +00:00
|
|
|
router.GET("/", func(ctx *gin.Context) {
|
|
|
|
ctx.Redirect(301, "/static/xxqg/build/home.html")
|
|
|
|
})
|
2022-04-25 10:39:54 +00:00
|
|
|
|
2022-08-05 10:08:05 +00:00
|
|
|
router.GET("/about", func(context *gin.Context) {
|
2022-08-26 00:25:07 +00:00
|
|
|
context.JSON(200, Resp{
|
|
|
|
Code: 200,
|
|
|
|
Message: "",
|
|
|
|
Data: utils.GetAbout(),
|
|
|
|
Success: true,
|
|
|
|
Error: "",
|
|
|
|
})
|
2022-08-05 10:08:05 +00:00
|
|
|
})
|
|
|
|
|
2022-09-06 14:56:18 +00:00
|
|
|
router.POST("/restart", check(), func(ctx *gin.Context) {
|
|
|
|
if ctx.GetInt("level") == 1 {
|
|
|
|
ctx.JSON(200, Resp{
|
|
|
|
Code: 200,
|
|
|
|
Message: "",
|
|
|
|
Data: nil,
|
|
|
|
Success: true,
|
|
|
|
Error: "",
|
|
|
|
})
|
|
|
|
utils.Restart()
|
|
|
|
} else {
|
|
|
|
ctx.JSON(200, Resp{
|
|
|
|
Code: 401,
|
|
|
|
Message: "",
|
|
|
|
Data: nil,
|
|
|
|
Success: false,
|
|
|
|
Error: "",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
router.POST("/update", check(), func(ctx *gin.Context) {
|
|
|
|
if ctx.GetInt("level") == 1 {
|
|
|
|
update.SelfUpdate("", conf.GetVersion())
|
|
|
|
ctx.JSON(200, Resp{
|
|
|
|
Code: 200,
|
|
|
|
Message: "",
|
|
|
|
Data: nil,
|
|
|
|
Success: true,
|
|
|
|
Error: "",
|
|
|
|
})
|
|
|
|
utils.Restart()
|
|
|
|
} else {
|
|
|
|
ctx.JSON(200, Resp{
|
|
|
|
Code: 401,
|
|
|
|
Message: "",
|
|
|
|
Data: nil,
|
|
|
|
Success: false,
|
|
|
|
Error: "",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-08-15 07:07:03 +00:00
|
|
|
if utils.FileIsExist("./config/flutter_xxqg/") {
|
|
|
|
router.StaticFS("/flutter_xxqg", http.Dir("./config/flutter_xxqg/"))
|
|
|
|
}
|
2022-08-01 09:51:45 +00:00
|
|
|
// 对权限的管理组
|
2022-04-25 10:39:54 +00:00
|
|
|
auth := router.Group("/auth")
|
2022-08-01 09:51:45 +00:00
|
|
|
// 用户登录的接口
|
|
|
|
auth.POST("/login", userLogin())
|
|
|
|
// 检查登录状态的token是否正确
|
|
|
|
auth.POST("/check/:token", checkToken())
|
2022-04-25 10:39:54 +00:00
|
|
|
|
2022-08-01 09:51:45 +00:00
|
|
|
// 对于用户可自定义挂载文件的目录
|
2022-08-07 10:39:15 +00:00
|
|
|
if utils.FileIsExist("./config/dist/") {
|
|
|
|
router.StaticFS("/dist", http.Dir("./config/dist/"))
|
2022-04-25 10:39:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 14:56:18 +00:00
|
|
|
config := router.Group("/config", check())
|
|
|
|
|
|
|
|
config.GET("", configGet())
|
|
|
|
config.POST("", configSet())
|
2022-09-25 15:58:04 +00:00
|
|
|
config.GET("/file", configFileGet())
|
|
|
|
config.POST("/file", configFileSet())
|
2022-09-06 14:56:18 +00:00
|
|
|
|
2022-08-01 09:51:45 +00:00
|
|
|
// 对用户管理的组
|
2022-04-25 10:39:54 +00:00
|
|
|
user := router.Group("/user", check())
|
2022-04-20 13:31:46 +00:00
|
|
|
// 添加用户
|
2022-07-24 07:56:12 +00:00
|
|
|
user.POST("", addUser())
|
2022-08-01 09:51:45 +00:00
|
|
|
// 获取所以已登陆的用户
|
2022-08-04 10:03:53 +00:00
|
|
|
user.GET("", getUsers())
|
2022-09-10 13:43:29 +00:00
|
|
|
|
|
|
|
user.GET("/expired", getExpiredUser())
|
|
|
|
|
2022-08-01 09:51:45 +00:00
|
|
|
// 删除用户
|
2022-08-04 10:03:53 +00:00
|
|
|
user.DELETE("", deleteUser())
|
2022-07-29 07:55:42 +00:00
|
|
|
|
2022-08-01 09:51:45 +00:00
|
|
|
// 获取用户成绩
|
2022-04-20 13:31:46 +00:00
|
|
|
router.GET("/score", getScore())
|
2022-08-01 09:51:45 +00:00
|
|
|
// 让一个用户开始学习
|
2022-04-20 13:31:46 +00:00
|
|
|
router.POST("/study", study())
|
2022-08-01 09:51:45 +00:00
|
|
|
// 让一个用户停止学习
|
2022-04-25 10:39:54 +00:00
|
|
|
router.POST("/stop_study", check(), stopStudy())
|
2022-08-01 09:51:45 +00:00
|
|
|
// 获取程序当天的运行日志
|
2022-04-25 10:39:54 +00:00
|
|
|
router.GET("/log", check(), getLog())
|
2022-04-20 13:31:46 +00:00
|
|
|
|
2022-08-01 09:51:45 +00:00
|
|
|
// 登录xxqg的三个接口
|
2022-08-14 04:14:46 +00:00
|
|
|
router.GET("/sign/", sign())
|
|
|
|
router.GET("/login/*proxyPath", generate())
|
2022-04-25 10:39:54 +00:00
|
|
|
router.POST("/login/*proxyPath", check(), generate())
|
2022-04-20 13:31:46 +00:00
|
|
|
return router
|
|
|
|
}
|
2022-04-25 10:39:54 +00:00
|
|
|
|
|
|
|
func check() gin.HandlerFunc {
|
2022-07-27 10:21:49 +00:00
|
|
|
config := conf.GetConfig()
|
2022-04-25 10:39:54 +00:00
|
|
|
return func(ctx *gin.Context) {
|
2022-08-26 00:25:07 +00:00
|
|
|
token := ctx.GetHeader("Authorization")
|
|
|
|
token = strings.Split(token, " ")[1]
|
2022-08-30 07:13:09 +00:00
|
|
|
if token == "" {
|
2022-08-26 00:25:07 +00:00
|
|
|
ctx.JSON(401, Resp{
|
|
|
|
Code: 401,
|
2022-04-25 10:39:54 +00:00
|
|
|
Message: "the auth fail",
|
|
|
|
Data: nil,
|
|
|
|
Success: false,
|
|
|
|
Error: "",
|
|
|
|
})
|
|
|
|
ctx.Abort()
|
2022-08-30 07:13:09 +00:00
|
|
|
} else if utils.StrMd5(config.Web.Account+config.Web.Password) == token {
|
|
|
|
ctx.Set("level", 1)
|
|
|
|
ctx.Set("token", token)
|
|
|
|
ctx.Next()
|
|
|
|
} else if checkCommonUser(token) {
|
|
|
|
ctx.Set("level", 2)
|
|
|
|
ctx.Set("token", token)
|
2022-04-25 10:39:54 +00:00
|
|
|
ctx.Next()
|
2022-08-30 07:13:09 +00:00
|
|
|
} else {
|
|
|
|
ctx.JSON(401, Resp{
|
|
|
|
Code: 401,
|
|
|
|
Message: "the auth fail",
|
|
|
|
Data: nil,
|
|
|
|
Success: false,
|
|
|
|
Error: "",
|
|
|
|
})
|
|
|
|
ctx.Abort()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCommonUser(token string) bool {
|
|
|
|
config := conf.GetConfig()
|
|
|
|
for key, value := range config.Web.CommonUser {
|
|
|
|
if token == utils.StrMd5(key+value) {
|
|
|
|
return true
|
2022-04-25 10:39:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 07:13:09 +00:00
|
|
|
return false
|
2022-04-25 10:39:54 +00:00
|
|
|
}
|