mirror of
https://github.com/ProudMuBai/GoFilm.git
synced 2026-02-15 06:54:41 +08:00
add BAM
This commit is contained in:
217
server/controller/CronController.go
Normal file
217
server/controller/CronController.go
Normal file
@@ -0,0 +1,217 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"server/logic"
|
||||
"server/model/system"
|
||||
"server/plugin/spider"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ------------------------------------------------------ 定时任务管理 ------------------------------------------------------
|
||||
|
||||
// FilmCronTaskList 获取所有的定时任务信息
|
||||
func FilmCronTaskList(c *gin.Context) {
|
||||
tl := logic.CL.GetFilmCrontab()
|
||||
if len(tl) <= 0 {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "暂无任务定时任务信息",
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"data": tl,
|
||||
})
|
||||
}
|
||||
|
||||
// GetFilmCronTask 通过Id获取对应的定时任务信息
|
||||
func GetFilmCronTask(c *gin.Context) {
|
||||
id := c.DefaultQuery("id", "")
|
||||
if id == "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "定时任务信息获取失败,任务Id不能为空",
|
||||
})
|
||||
return
|
||||
}
|
||||
task, err := logic.CL.GetFilmCrontabById(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": fmt.Sprint("定时任务信息获取失败", err.Error()),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"data": task,
|
||||
})
|
||||
}
|
||||
|
||||
// FilmCronAdd 添加定时任务
|
||||
func FilmCronAdd(c *gin.Context) {
|
||||
var vo = system.FilmCronVo{}
|
||||
// 获取请求参数
|
||||
if err := c.ShouldBindJSON(&vo); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "请求参数异常",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 校验请求参数
|
||||
if err := validTaskAddVo(vo); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
// 去除cron表达式左右空格
|
||||
vo.Spec = strings.TrimSpace(vo.Spec)
|
||||
// 执行 定时任务信息保存逻辑
|
||||
if err := logic.CL.AddFilmCrontab(vo); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": fmt.Sprint("定时任务添加失败: ", err.Error()),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "定时任务添加成功",
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// FilmCronUpdate 更新定时任务信息
|
||||
func FilmCronUpdate(c *gin.Context) {
|
||||
var t = system.FilmCollectTask{}
|
||||
// 获取请求参数
|
||||
if err := c.ShouldBindJSON(&t); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "请求参数异常",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 校验必要参数
|
||||
if err := validTaskInfo(t); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
// 获取未更新的task信息
|
||||
task, err := logic.CL.GetFilmCrontabById(t.Id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": fmt.Sprint("更新失败: ", err.Error()),
|
||||
})
|
||||
return
|
||||
}
|
||||
// 将task的可变更属性进行变更
|
||||
task.Ids = t.Ids
|
||||
task.Time = t.Time
|
||||
task.State = t.State
|
||||
task.Remark = t.Remark
|
||||
// 将变更后的task更新到系统中
|
||||
logic.CL.UpdateFilmCron(task)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": fmt.Sprintf("定时任务[%s]更新成功", task.Id),
|
||||
})
|
||||
}
|
||||
|
||||
// ChangeTaskState 开启 | 关闭Id 对应的定时任务
|
||||
func ChangeTaskState(c *gin.Context) {
|
||||
var t = system.FilmCollectTask{}
|
||||
// 获取请求参数
|
||||
if err := c.ShouldBindJSON(&t); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "请求参数异常",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 获取未更新的task信息
|
||||
task, err := logic.CL.GetFilmCrontabById(t.Id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": fmt.Sprint("更新失败: ", err.Error()),
|
||||
})
|
||||
return
|
||||
}
|
||||
// 修改task的状态
|
||||
task.State = t.State
|
||||
// 将变更后的task更新到系统中
|
||||
logic.CL.UpdateFilmCron(task)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": fmt.Sprintf("定时任务[%s]更新成功", task.Id),
|
||||
})
|
||||
}
|
||||
|
||||
// DelFilmCron 删除定时任务
|
||||
func DelFilmCron(c *gin.Context) {
|
||||
id := c.DefaultQuery("id", "")
|
||||
if id == "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "删除失败,任务Id不能为空",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 如果Id不为空则执行删除逻辑
|
||||
if err := logic.CL.DelFilmCrontab(id); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": fmt.Sprintf("定时任务[%s]已删除", id),
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// -------------------------------------------------- 参数校验 --------------------------------------------------
|
||||
|
||||
// 定时任务必要属性校验
|
||||
func validTaskInfo(t system.FilmCollectTask) error {
|
||||
if len(t.Id) <= 0 {
|
||||
return errors.New("参数校验失败, 任务Id信息不能为空")
|
||||
}
|
||||
if t.Time == 0 {
|
||||
return errors.New("参数校验失败, 采集时长不能为零值")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 任务添加参数校验
|
||||
func validTaskAddVo(vo system.FilmCronVo) error {
|
||||
if vo.Model != 0 && vo.Model != 1 {
|
||||
return errors.New("参数校验失败, 未定义的任务类型")
|
||||
}
|
||||
if vo.Time == 0 {
|
||||
return errors.New("参数校验失败, 采集时长不能为零值")
|
||||
}
|
||||
if err := spider.ValidSpec(vo.Spec); err != nil {
|
||||
return errors.New(fmt.Sprint("参数校验失败 cron表达式校验失败: ", err.Error()))
|
||||
}
|
||||
if vo.Model == 1 && (vo.Ids == nil || len(vo.Ids) <= 0) {
|
||||
return errors.New("参数校验失败, 自定义更新未绑定任何资源站点")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
59
server/controller/FileController.go
Normal file
59
server/controller/FileController.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"path/filepath"
|
||||
"server/config"
|
||||
"server/logic"
|
||||
"server/model/system"
|
||||
"server/plugin/common/util"
|
||||
)
|
||||
|
||||
// SingleUpload 单文件上传, 暂定为图片上传
|
||||
func SingleUpload(c *gin.Context) {
|
||||
// 获取执行操作的用户信息
|
||||
v, ok := c.Get(config.AuthUserClaims)
|
||||
if !ok {
|
||||
system.Failed("上传失败, 当前用户信息异常", c)
|
||||
return
|
||||
}
|
||||
// 结合搜文件内容
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
system.Failed(err.Error(), c)
|
||||
return
|
||||
}
|
||||
// 创建文件保存路径, 如果不存在则创建
|
||||
//if _, err = os.Stat(config.ImageDir); os.IsNotExist(err) {
|
||||
// err = os.MkdirAll(config.ImageDir, os.ModePerm)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
//}
|
||||
|
||||
// 生成文件名, 保存文件到服务器
|
||||
fileName := fmt.Sprintf("%s/%s%s", config.FilmPictureUploadDir, util.RandomString(8), filepath.Ext(file.Filename))
|
||||
err = c.SaveUploadedFile(file, fileName)
|
||||
if err != nil {
|
||||
system.Failed(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
uc := v.(*system.UserClaims)
|
||||
// 记录图片信息到系统表中, 并获取返回的图片访问路径
|
||||
link := logic.FileL.SingleFileUpload(fileName, int(uc.UserID))
|
||||
// 返回图片访问地址以及成功的响应
|
||||
system.Success(link, "上传成功", c)
|
||||
|
||||
}
|
||||
|
||||
// PhotoWall 照片墙数据
|
||||
func PhotoWall(c *gin.Context) {
|
||||
|
||||
// 获取系统保存的文件的图片分页数据
|
||||
page := system.Page{PageSize: 10, Current: 1}
|
||||
// 获取分页数据
|
||||
pl := logic.FileL.GetPhotoPage(&page)
|
||||
system.Success(pl, "图片分页数据获取成功", c)
|
||||
}
|
||||
176
server/controller/FilmController.go
Normal file
176
server/controller/FilmController.go
Normal file
@@ -0,0 +1,176 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"server/logic"
|
||||
"server/model/system"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// FilmSearchPage 获取影视分页数据
|
||||
func FilmSearchPage(c *gin.Context) {
|
||||
var s = system.SearchVo{Paging: &system.Page{}}
|
||||
var err error
|
||||
// 检索参数
|
||||
s.Name = c.DefaultQuery("name", "")
|
||||
s.Pid, err = strconv.ParseInt(c.DefaultQuery("pid", "0"), 10, 64)
|
||||
if err != nil {
|
||||
system.Failed("影片分页数据获取失败, 请求参数异常", c)
|
||||
return
|
||||
}
|
||||
s.Cid, err = strconv.ParseInt(c.DefaultQuery("cid", "0"), 10, 64)
|
||||
if err != nil {
|
||||
system.Failed("影片分页数据获取失败, 请求参数异常", c)
|
||||
return
|
||||
}
|
||||
s.Plot = c.DefaultQuery("plot", "")
|
||||
s.Area = c.DefaultQuery("area", "")
|
||||
s.Language = c.DefaultQuery("language", "")
|
||||
year := c.DefaultQuery("year", "")
|
||||
if year == "" {
|
||||
s.Year = 0
|
||||
} else {
|
||||
s.Year, err = strconv.ParseInt(year, 10, 64)
|
||||
if err != nil {
|
||||
system.Failed("影片分页数据获取失败, 请求参数异常", c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
s.Remarks = c.DefaultQuery("remarks", "")
|
||||
// 处理时间参数
|
||||
begin := c.DefaultQuery("beginTime", "")
|
||||
if begin == "" {
|
||||
s.BeginTime = 0
|
||||
} else {
|
||||
beginTime, e := time.ParseInLocation(time.DateTime, begin, time.Local)
|
||||
if e != nil {
|
||||
system.Failed("影片分页数据获取失败, 请求参数异常", c)
|
||||
return
|
||||
}
|
||||
s.BeginTime = beginTime.Unix()
|
||||
}
|
||||
end := c.DefaultQuery("endTime", "")
|
||||
if end == "" {
|
||||
s.EndTime = 0
|
||||
} else {
|
||||
endTime, e := time.ParseInLocation(time.DateTime, end, time.Local)
|
||||
if e != nil {
|
||||
system.Failed("影片分页数据获取失败, 请求参数异常", c)
|
||||
return
|
||||
}
|
||||
s.EndTime = endTime.Unix()
|
||||
}
|
||||
|
||||
// 分页参数
|
||||
s.Paging.Current, err = strconv.Atoi(c.DefaultQuery("current", "1"))
|
||||
s.Paging.PageSize, err = strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
||||
// 如果分页数据超出指定范围则设置为默认值
|
||||
if s.Paging.PageSize <= 0 || s.Paging.PageSize > 500 {
|
||||
s.Paging.PageSize = 10
|
||||
}
|
||||
if err != nil {
|
||||
system.Failed("影片分页数据获取失败, 请求参数异常", c)
|
||||
return
|
||||
}
|
||||
// 提供检索tag options
|
||||
options := logic.FL.GetSearchOptions()
|
||||
// 检索条件
|
||||
sl := logic.FL.GetFilmPage(s)
|
||||
system.Success(gin.H{
|
||||
"params": s,
|
||||
"list": sl,
|
||||
"options": options,
|
||||
}, "影片分页信息获取成功", c)
|
||||
}
|
||||
|
||||
// FilmAdd 手动添加影片
|
||||
func FilmAdd(c *gin.Context) {
|
||||
// 获取请求参数
|
||||
var fd = system.FilmDetailVo{}
|
||||
if err := c.ShouldBindJSON(&fd); err != nil {
|
||||
system.Failed("影片添加失败, 影片参数提交异常", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 如果绑定成功则执行影片信息处理保存逻辑
|
||||
if err := logic.FL.SaveFilmDetail(fd); err != nil {
|
||||
system.Failed(fmt.Sprint("影片添加失败, 影片信息保存错误: ", err.Error()), c)
|
||||
return
|
||||
}
|
||||
system.SuccessOnlyMsg("影片信息添加成功", c)
|
||||
}
|
||||
|
||||
//----------------------------------------------------影片分类处理----------------------------------------------------
|
||||
|
||||
// FilmClassTree 影片分类树数据
|
||||
func FilmClassTree(c *gin.Context) {
|
||||
// 获取影片分类树信息
|
||||
tree := logic.FL.GetFilmClassTree()
|
||||
system.Success(tree, "影片分类信息获取成功", c)
|
||||
return
|
||||
}
|
||||
|
||||
// FindFilmClass 获取指定ID对应的影片分类信息
|
||||
func FindFilmClass(c *gin.Context) {
|
||||
idStr := c.DefaultQuery("id", "")
|
||||
if idStr == "" {
|
||||
system.Failed("影片分类信息获取失败, 分类Id不能为空", c)
|
||||
return
|
||||
}
|
||||
// 转化id类型为int
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
system.Failed("影片分类信息获取失败, 参数分类Id格式异常", c)
|
||||
return
|
||||
}
|
||||
// 通过Id返回对应的分类信息
|
||||
class := logic.FL.GetFilmClassById(id)
|
||||
if class == nil {
|
||||
system.Failed("影片分类信息获取失败, 分类信息不存在", c)
|
||||
return
|
||||
}
|
||||
system.Success(class, "分类信息查找成功", c)
|
||||
}
|
||||
|
||||
func UpdateFilmClass(c *gin.Context) {
|
||||
// 获取修改后的分类信息
|
||||
var class = system.CategoryTree{}
|
||||
if err := c.ShouldBindJSON(&class); err != nil {
|
||||
system.Failed("更新失败, 请求参数异常", c)
|
||||
return
|
||||
}
|
||||
if class.Id == 0 {
|
||||
system.Failed("更新失败, 分类Id缺失", c)
|
||||
return
|
||||
}
|
||||
// 修改分类信息
|
||||
if err := logic.FL.UpdateClass(class); err != nil {
|
||||
system.Failed(err.Error(), c)
|
||||
return
|
||||
}
|
||||
system.SuccessOnlyMsg("影片分类信息更新成功", c)
|
||||
}
|
||||
|
||||
// DelFilmClass 删除指定ID对应的影片分类
|
||||
func DelFilmClass(c *gin.Context) {
|
||||
idStr := c.DefaultQuery("id", "")
|
||||
if idStr == "" {
|
||||
system.Failed("影片分类信息获取失败, 分类Id不能为空", c)
|
||||
return
|
||||
}
|
||||
// 转化id类型为int
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
system.Failed("影片分类信息获取失败, 参数分类Id格式异常", c)
|
||||
return
|
||||
}
|
||||
// 通过ID删除对应分类信息
|
||||
if err = logic.FL.DelClass(id); err != nil {
|
||||
system.Failed(err.Error(), c)
|
||||
return
|
||||
}
|
||||
system.SuccessOnlyMsg("当前分类已删除成功", c)
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"server/logic"
|
||||
"server/model"
|
||||
"server/model/system"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -26,7 +26,8 @@ func Index(c *gin.Context) {
|
||||
|
||||
// CategoriesInfo 分类信息获取
|
||||
func CategoriesInfo(c *gin.Context) {
|
||||
data := logic.IL.GetCategoryInfo()
|
||||
//data := logic.IL.GetCategoryInfo()
|
||||
data := logic.IL.GetNavCategory()
|
||||
|
||||
if data == nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -55,7 +56,7 @@ func FilmDetail(c *gin.Context) {
|
||||
// 获取影片详情信息
|
||||
detail := logic.IL.GetFilmDetail(id)
|
||||
// 获取相关推荐影片数据
|
||||
page := model.Page{Current: 0, PageSize: 14}
|
||||
page := system.Page{Current: 0, PageSize: 14}
|
||||
relateMovie := logic.IL.RelateMovie(detail, &page)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
@@ -82,7 +83,7 @@ func FilmPlayInfo(c *gin.Context) {
|
||||
// 获取影片详情信息
|
||||
detail := logic.IL.GetFilmDetail(id)
|
||||
// 推荐影片信息
|
||||
page := model.Page{Current: 0, PageSize: 14}
|
||||
page := system.Page{Current: 0, PageSize: 14}
|
||||
relateMovie := logic.IL.RelateMovie(detail, &page)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
@@ -101,7 +102,7 @@ func SearchFilm(c *gin.Context) {
|
||||
keyword := c.DefaultQuery("keyword", "")
|
||||
currStr := c.DefaultQuery("current", "1")
|
||||
current, _ := strconv.Atoi(currStr)
|
||||
page := model.Page{PageSize: 10, Current: current}
|
||||
page := system.Page{PageSize: 10, Current: current}
|
||||
bl := logic.IL.SearchFilmInfo(strings.TrimSpace(keyword), &page)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -115,7 +116,7 @@ func SearchFilm(c *gin.Context) {
|
||||
|
||||
// FilmTagSearch 通过tag获取满足条件的对应影片
|
||||
func FilmTagSearch(c *gin.Context) {
|
||||
params := model.SearchTagsVO{}
|
||||
params := system.SearchTagsVO{}
|
||||
pidStr := c.DefaultQuery("Pid", "")
|
||||
cidStr := c.DefaultQuery("Category", "")
|
||||
yStr := c.DefaultQuery("Year", "")
|
||||
@@ -137,7 +138,7 @@ func FilmTagSearch(c *gin.Context) {
|
||||
// 设置分页信息
|
||||
currentStr := c.DefaultQuery("current", "1")
|
||||
current, _ := strconv.Atoi(currentStr)
|
||||
page := model.Page{PageSize: 49, Current: current}
|
||||
page := system.Page{PageSize: 49, Current: current}
|
||||
logic.IL.GetFilmsByTags(params, &page)
|
||||
// 获取当前分类Title
|
||||
// 返回对应信息
|
||||
@@ -175,7 +176,7 @@ func FilmClassify(c *gin.Context) {
|
||||
pid, _ := strconv.ParseInt(pidStr, 10, 64)
|
||||
title := logic.IL.GetPidCategory(pid)
|
||||
// 2. 设置分页信息
|
||||
page := model.Page{PageSize: 21, Current: 1}
|
||||
page := system.Page{PageSize: 21, Current: 1}
|
||||
// 3. 获取当前分类下的 最新上映, 排行榜, 最近更新 影片信息
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
@@ -207,7 +208,7 @@ func FilmCategory(c *gin.Context) {
|
||||
// 2 设置分页信息
|
||||
currentStr := c.DefaultQuery("current", "1")
|
||||
current, _ := strconv.Atoi(currentStr)
|
||||
page := model.Page{PageSize: 49, Current: current}
|
||||
page := system.Page{PageSize: 49, Current: current}
|
||||
// 2.1 如果不存在cid则根据Pid进行查询
|
||||
if cidStr == "" {
|
||||
// 2.2 如果存在pid则根据pid进行查找
|
||||
|
||||
374
server/controller/ManageController.go
Normal file
374
server/controller/ManageController.go
Normal file
@@ -0,0 +1,374 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"server/logic"
|
||||
"server/model/system"
|
||||
"server/plugin/SystemInit"
|
||||
"server/plugin/common/util"
|
||||
"server/plugin/spider"
|
||||
)
|
||||
|
||||
func ManageIndex(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "hahah",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// ------------------------------------------------------ 影视采集 ------------------------------------------------------
|
||||
|
||||
// FilmSourceList 采集站点信息
|
||||
func FilmSourceList(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"data": logic.ML.GetFilmSourceList(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// FindFilmSource 通过ID返回对应的资源站数据
|
||||
func FindFilmSource(c *gin.Context) {
|
||||
id := c.Query("id")
|
||||
if id == "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "参数异常, 资源站标识不能为空",
|
||||
})
|
||||
return
|
||||
}
|
||||
fs := logic.ML.GetFilmSource(id)
|
||||
if fs == nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "数据异常,资源站信息不存在",
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"data": fs,
|
||||
})
|
||||
}
|
||||
|
||||
func FilmSourceAdd(c *gin.Context) {
|
||||
var s = system.FilmSource{}
|
||||
// 获取请求参数
|
||||
if err := c.ShouldBindJSON(&s); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "请求参数异常",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 校验必要参数
|
||||
if err := validFilmSource(s); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
// 如果采集站开启图片同步, 且采集站为附属站点则返回错误提示
|
||||
if s.SyncPictures && (s.Grade == system.SlaveCollect) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "附属站点无法开启图片同步功能",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 执行 spider
|
||||
if err := spider.CollectApiTest(s); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "资源接口测试失败, 请确认接口有效再添加",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 测试通过后将资源站信息添加到list
|
||||
if err := logic.ML.SaveFilmSource(s); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": fmt.Sprint("资源站添加失败: ", err.Error()),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "添加成功",
|
||||
})
|
||||
}
|
||||
|
||||
func FilmSourceUpdate(c *gin.Context) {
|
||||
var s = system.FilmSource{}
|
||||
// 获取请求参数
|
||||
if err := c.ShouldBindJSON(&s); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "请求参数异常",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 校验必要参数
|
||||
if err := validFilmSource(s); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
// 如果采集站开启图片同步, 且采集站为附属站点则返回错误提示
|
||||
if s.SyncPictures && (s.Grade == system.SlaveCollect) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "附属站点无法开启图片同步功能",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 校验Id信息是否为空
|
||||
if s.Id == "" {
|
||||
c.JSON(http.StatusOK, gin.H{"status": StatusFailed, "message": "参数异常, 资源站标识不能为空"})
|
||||
return
|
||||
}
|
||||
fs := logic.ML.GetFilmSource(s.Id)
|
||||
if fs == nil {
|
||||
c.JSON(http.StatusOK, gin.H{"status": StatusFailed, "message": "数据异常,资源站信息不存在"})
|
||||
return
|
||||
}
|
||||
// 如果 uri发生变更则执行spider测试
|
||||
if fs.Uri != s.Uri {
|
||||
// 执行 spider
|
||||
if err := spider.CollectApiTest(s); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "资源接口测试失败, 请确认更新的数据接口是否有效",
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
// 更新资源站信息
|
||||
if err := logic.ML.UpdateFilmSource(s); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": fmt.Sprint("资源站更新失败: ", err.Error()),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "更新成功",
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func FilmSourceChange(c *gin.Context) {
|
||||
var s = system.FilmSource{}
|
||||
// 获取请求参数
|
||||
if err := c.ShouldBindJSON(&s); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "请求参数异常",
|
||||
})
|
||||
return
|
||||
}
|
||||
if s.Id == "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "参数异常, 资源站标识不能为空",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 查找对应的资源站点信息
|
||||
fs := logic.ML.GetFilmSource(s.Id)
|
||||
if fs == nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "数据异常,资源站信息不存在",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 如果采集站开启图片同步, 且采集站为附属站点则返回错误提示
|
||||
if s.SyncPictures && (fs.Grade == system.SlaveCollect) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "附属站点无法开启图片同步功能",
|
||||
})
|
||||
return
|
||||
}
|
||||
if s.State != fs.State || s.SyncPictures != fs.SyncPictures {
|
||||
// 执行更新操作
|
||||
s := system.FilmSource{Id: fs.Id, Name: fs.Name, Uri: fs.Uri, ResultModel: fs.ResultModel,
|
||||
Grade: fs.Grade, SyncPictures: s.SyncPictures, CollectType: fs.CollectType, State: s.State}
|
||||
// 更新资源站信息
|
||||
if err := logic.ML.UpdateFilmSource(s); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": fmt.Sprint("资源站更新失败: ", err.Error()),
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "更新成功",
|
||||
})
|
||||
}
|
||||
|
||||
func FilmSourceDel(c *gin.Context) {
|
||||
id := c.Query("id")
|
||||
if len(id) <= 0 {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "资源站ID信息不能为空",
|
||||
})
|
||||
return
|
||||
}
|
||||
if err := logic.ML.DelFilmSource(id); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": fmt.Sprint("删除资源站失败: ", err.Error()),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "删除成功:",
|
||||
})
|
||||
}
|
||||
|
||||
// FilmSourceTest 测试影视站点数据是否可用
|
||||
func FilmSourceTest(c *gin.Context) {
|
||||
var s = system.FilmSource{}
|
||||
// 获取请求参数
|
||||
if err := c.ShouldBindJSON(&s); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "请求参数异常",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 校验必要参数
|
||||
if err := validFilmSource(s); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
// 执行 spider
|
||||
if err := spider.CollectApiTest(s); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "测试成功!!!",
|
||||
})
|
||||
}
|
||||
|
||||
// GetNormalFilmSource 获取状态为启用的采集站信息
|
||||
func GetNormalFilmSource(c *gin.Context) {
|
||||
// 获取所有的采集站信息
|
||||
var l []system.FilmTaskOptions
|
||||
for _, v := range logic.ML.GetFilmSourceList() {
|
||||
if v.State {
|
||||
l = append(l, system.FilmTaskOptions{Id: v.Id, Name: v.Name})
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"data": l,
|
||||
})
|
||||
}
|
||||
|
||||
// ------------------------------------------------------ 站点基本配置 ------------------------------------------------------
|
||||
|
||||
// SiteBasicConfig 网站基本配置
|
||||
func SiteBasicConfig(c *gin.Context) {
|
||||
system.Success(logic.ML.GetSiteBasicConfig(), "网站基本信息获取成功", c)
|
||||
}
|
||||
|
||||
// UpdateSiteBasic 更新网站配置信息
|
||||
func UpdateSiteBasic(c *gin.Context) {
|
||||
// 获取请求参数 && 校验关键配置项
|
||||
bc := system.BasicConfig{}
|
||||
if err := c.ShouldBindJSON(&bc); err == nil {
|
||||
// 对参数进行校验
|
||||
if !util.ValidDomain(bc.Domain) && !util.ValidIPHost(bc.Domain) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "域名格式校验失败: ",
|
||||
})
|
||||
return
|
||||
}
|
||||
if len(bc.SiteName) <= 0 {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "网站名称不能为空: ",
|
||||
})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"status": StatusOk, "message": fmt.Sprint("参数提交失败: ", err)})
|
||||
return
|
||||
}
|
||||
|
||||
// 保存更新后的配置信息
|
||||
if err := logic.ML.UpdateSiteBasic(bc); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": fmt.Sprint("网站配置更新失败: ", err),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "更新成功: ",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// ResetSiteBasic 重置网站配置信息为初始化状态
|
||||
func ResetSiteBasic(c *gin.Context) {
|
||||
// 执行配置初始化方法直接覆盖当前基本配置信息
|
||||
SystemInit.BasicConfigInit()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "重置成功: ",
|
||||
})
|
||||
}
|
||||
|
||||
// ------------------------------------------------------ 参数校验 ------------------------------------------------------
|
||||
func validFilmSource(fs system.FilmSource) error {
|
||||
// 资源名称不能为空 且长度不能超过20
|
||||
if len(fs.Name) <= 0 || len(fs.Name) > 20 {
|
||||
return errors.New("资源名称不能为空且长度不能超过20")
|
||||
}
|
||||
// Uri 采集链接测试格式
|
||||
if !util.ValidURL(fs.Uri) {
|
||||
return errors.New("资源链接格式异常, 请输入规范的URL链接")
|
||||
}
|
||||
// 校验接口类型是否是 JSON || XML
|
||||
if fs.ResultModel != system.JsonResult && fs.ResultModel != system.XmlResult {
|
||||
return errors.New("接口类型异常, 请提交正确的接口类型")
|
||||
}
|
||||
// 校验采集类型是否符合规范
|
||||
switch fs.CollectType {
|
||||
case system.CollectVideo, system.CollectArticle, system.CollectActor, system.CollectRole, system.CollectWebSite:
|
||||
return nil
|
||||
default:
|
||||
return errors.New("资源类型异常, 未知的资源类型")
|
||||
}
|
||||
}
|
||||
|
||||
func apiValidityCheck() {
|
||||
|
||||
}
|
||||
67
server/controller/ProvideController.go
Normal file
67
server/controller/ProvideController.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"server/logic"
|
||||
"server/model/system"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// 提供用于第三方站点采集的API
|
||||
|
||||
// HandleProvide 返回视频列表信息
|
||||
func HandleProvide(c *gin.Context) {
|
||||
// 将请求参数封装为一个map
|
||||
var params = map[string]string{
|
||||
"t": c.DefaultQuery("t", ""),
|
||||
//"pg": c.DefaultQuery("pg", ""),
|
||||
"wd": c.DefaultQuery("wd", ""),
|
||||
"h": c.DefaultQuery("h", ""),
|
||||
"ids": c.DefaultQuery("ids", ""),
|
||||
}
|
||||
// 设置分页信息
|
||||
currentStr := c.DefaultQuery("pg", "1")
|
||||
pageSizeStr := c.DefaultQuery("limit", "20")
|
||||
current, _ := strconv.Atoi(currentStr)
|
||||
pageSize, _ := strconv.Atoi(pageSizeStr)
|
||||
page := system.Page{PageSize: pageSize, Current: current}
|
||||
// ac-请求类型 t-类别ID pg-页码 wd-搜索关键字 h=几小时内的数据 ids-数据ID 多个ID逗好分割
|
||||
var ac string = c.DefaultQuery("ac", "")
|
||||
switch ac {
|
||||
case "list":
|
||||
c.JSON(http.StatusOK, logic.PL.GetFilmListPage(params, &page))
|
||||
case "detail", "videolist":
|
||||
c.JSON(http.StatusOK, logic.PL.GetFilmDetailPage(params, &page))
|
||||
default:
|
||||
c.JSON(http.StatusOK, logic.PL.GetFilmListPage(params, &page))
|
||||
}
|
||||
}
|
||||
|
||||
// HandleProvideXml 处理返回xml格式的数据
|
||||
func HandleProvideXml(c *gin.Context) {
|
||||
// 将请求参数封装为一个map
|
||||
var params = map[string]string{
|
||||
"t": c.DefaultQuery("t", ""),
|
||||
//"pg": c.DefaultQuery("pg", ""),
|
||||
"wd": c.DefaultQuery("wd", ""),
|
||||
"h": c.DefaultQuery("h", ""),
|
||||
"ids": c.DefaultQuery("ids", ""),
|
||||
}
|
||||
// 设置分页信息
|
||||
currentStr := c.DefaultQuery("pg", "1")
|
||||
pageSizeStr := c.DefaultQuery("limit", "20")
|
||||
current, _ := strconv.Atoi(currentStr)
|
||||
pageSize, _ := strconv.Atoi(pageSizeStr)
|
||||
page := system.Page{PageSize: pageSize, Current: current}
|
||||
// ac-请求类型 t-类别ID pg-页码 wd-搜索关键字 h=几小时内的数据 ids-数据ID 多个ID逗好分割
|
||||
var ac string = c.DefaultQuery("ac", "")
|
||||
switch ac {
|
||||
case "list":
|
||||
c.XML(http.StatusOK, logic.PL.GetFilmListXmlPage(params, &page))
|
||||
case "detail", "videolist":
|
||||
c.XML(http.StatusOK, logic.PL.GetFilmDetailXmlPage(params, &page))
|
||||
default:
|
||||
c.XML(http.StatusOK, logic.PL.GetFilmListXmlPage(params, &page))
|
||||
}
|
||||
}
|
||||
@@ -1,65 +1,135 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"server/config"
|
||||
"server/logic"
|
||||
"server/model/system"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// SpiderRe 数据清零重开
|
||||
func SpiderRe(c *gin.Context) {
|
||||
// 获取指令参数
|
||||
cip := c.Query("cipher")
|
||||
if cip != config.SpiderCipher {
|
||||
// CollectFilm 开启ID对应的资源站的采集任务
|
||||
func CollectFilm(c *gin.Context) {
|
||||
id := c.DefaultQuery("id", "")
|
||||
hourStr := c.DefaultQuery("h", "0")
|
||||
if id == "" || hourStr == "0" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "指令错误无法进行此操作",
|
||||
"message": "采集任务开启失败, 缺乏必要参数",
|
||||
})
|
||||
return
|
||||
}
|
||||
go logic.SL.ReZero()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "ReZero 任务执行已成功开启",
|
||||
})
|
||||
}
|
||||
|
||||
// FixFilmDetail 修复因网络异常造成的影片详情数据丢失
|
||||
func FixFilmDetail(c *gin.Context) {
|
||||
// 获取指令参数
|
||||
cip := c.Query("cipher")
|
||||
if cip != config.SpiderCipher {
|
||||
h, err := strconv.Atoi(hourStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "指令错误无法进行此操作",
|
||||
"message": "采集任务开启失败, hour(时长)参数不符合规范",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 如果指令正确,则执行详情数据获取
|
||||
go logic.SL.FixDetail()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "FixDetail 任务执行已成功开启",
|
||||
})
|
||||
}
|
||||
|
||||
// RefreshSitePlay 清空附属站点影片数据并重新获取
|
||||
func RefreshSitePlay(c *gin.Context) {
|
||||
// 获取指令参数
|
||||
cip := c.Query("cipher")
|
||||
if cip != config.SpiderCipher {
|
||||
// 执行采集逻处理逻辑
|
||||
if err = logic.SL.StartCollect(id, h); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "指令错误无法进行此操作",
|
||||
"message": fmt.Sprint("采集任务开启失败: ", err.Error()),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 执行多站点播放数据重置
|
||||
go logic.SL.SpiderMtPlayRe()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "SpiderMtPlayRe 任务执行已成功开启",
|
||||
"message": "采集任务已成功开启!!!",
|
||||
})
|
||||
}
|
||||
|
||||
// StarSpider 开启并执行采集任务
|
||||
func StarSpider(c *gin.Context) {
|
||||
var cp system.CollectParams
|
||||
// 获取请求参数
|
||||
if err := c.ShouldBindJSON(&cp); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "请求参数异常",
|
||||
})
|
||||
return
|
||||
}
|
||||
if cp.Time == 0 {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "采集开启失败,采集时长不能为0",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 根据 Batch 执行对应的逻辑
|
||||
if cp.Batch {
|
||||
// 执行批量采集
|
||||
if cp.Ids == nil || len(cp.Ids) <= 0 {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "批量采集开启失败, 关联的资源站信息为空",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 执行批量采集
|
||||
logic.SL.BatchCollect(cp.Time, cp.Ids)
|
||||
} else {
|
||||
if len(cp.Id) <= 0 {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "批量采集开启失败, 资源站Id获取失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
if err := logic.SL.StartCollect(cp.Id, cp.Time); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": fmt.Sprint("采集任务开启失败: ", err.Error()),
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
// 返回成功执行的信息
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "采集任务已成功开启!!!",
|
||||
})
|
||||
}
|
||||
|
||||
// SpiderReset 重置影视数据, 清空库存, 从零开始
|
||||
func SpiderReset(c *gin.Context) {
|
||||
var cp system.CollectParams
|
||||
// 获取请求参数
|
||||
if err := c.ShouldBindJSON(&cp); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "请求参数异常",
|
||||
})
|
||||
return
|
||||
}
|
||||
if cp.Time == 0 {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "采集开启失败,采集时长不能为0",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 后期加入一些前置验证
|
||||
if len(cp.Id) <= 0 {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "SpiderReset Failed, 资源站Id获取失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
logic.SL.ZeroCollect(cp.Time)
|
||||
}
|
||||
|
||||
// CoverFilmClass 重置覆盖影片分类信息
|
||||
func CoverFilmClass(c *gin.Context) {
|
||||
// 执行分类采集, 覆盖当前分类信息
|
||||
if err := logic.SL.FilmClassCollect(); err != nil {
|
||||
system.Failed(err.Error(), c)
|
||||
return
|
||||
}
|
||||
system.SuccessOnlyMsg("影视分类信息重置成功, 请稍等片刻后刷新页面", c)
|
||||
}
|
||||
|
||||
144
server/controller/UserController.go
Normal file
144
server/controller/UserController.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http"
|
||||
"server/config"
|
||||
"server/logic"
|
||||
"server/model/system"
|
||||
"server/plugin/common/util"
|
||||
)
|
||||
|
||||
// Login 管理员登录接口
|
||||
func Login(c *gin.Context) {
|
||||
var u system.User
|
||||
if err := c.ShouldBindJSON(&u); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "数据格式异常!!!",
|
||||
})
|
||||
return
|
||||
}
|
||||
if len(u.UserName) <= 0 || len(u.Password) <= 0 {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "用户名和密码信息不能为空!!!",
|
||||
})
|
||||
return
|
||||
}
|
||||
token, err := logic.UL.UserLogin(u.UserName, u.Password)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.Header("new-token", token)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "登录成功!!!",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Logout 退出登录
|
||||
func Logout(c *gin.Context) {
|
||||
// 获取已登录的用户信息
|
||||
v, ok := c.Get(config.AuthUserClaims)
|
||||
if !ok {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "登录信息异常!!!",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 清除redis中存储的对应token
|
||||
uc, ok := v.(*system.UserClaims)
|
||||
if !ok {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "登录信息异常!!!",
|
||||
})
|
||||
return
|
||||
}
|
||||
err := system.ClearUserToken(uc.UserID)
|
||||
if err != nil {
|
||||
log.Println("user logOut err: ", err)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "logout success!!!",
|
||||
})
|
||||
}
|
||||
|
||||
// UserPasswordChange 修改用户密码
|
||||
func UserPasswordChange(c *gin.Context) {
|
||||
// 接收密码修改参数
|
||||
var params map[string]string
|
||||
if err := c.ShouldBindJSON(¶ms); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "数据格式异常!!!",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 校验参数是否存在空值
|
||||
if params["password"] == "" || params["newPassword"] == "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "原密码和新密码不能为空!!!",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 校验新密码是否符合规范
|
||||
if err := util.ValidPwd(params["newPassword"]); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": fmt.Sprint("密码格式校验失败: ", err.Error()),
|
||||
})
|
||||
return
|
||||
}
|
||||
// 获取已登录的用户信息
|
||||
v, ok := c.Get(config.AuthUserClaims)
|
||||
if !ok {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": "登录信息异常!!!",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 从context中获取用户的登录信息
|
||||
uc := v.(*system.UserClaims)
|
||||
if err := logic.UL.ChangePassword(uc.UserName, params["password"], params["newPassword"]); err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusFailed,
|
||||
"message": fmt.Sprint("密码修改失败: ", err.Error()),
|
||||
})
|
||||
return
|
||||
}
|
||||
// 密码修改成功后不主动使token失效, 以免影响体验
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": StatusOk,
|
||||
"message": "密码修改成功",
|
||||
})
|
||||
}
|
||||
|
||||
func UserInfo(c *gin.Context) {
|
||||
// 从context中获取用户的相关信息
|
||||
v, ok := c.Get(config.AuthUserClaims)
|
||||
if !ok {
|
||||
system.Failed("用户信息获取失败, 未获取到用户授权信息", c)
|
||||
return
|
||||
}
|
||||
uc, ok := v.(*system.UserClaims)
|
||||
if !ok {
|
||||
system.Failed("用户信息获取失败, 户授权信息异常", c)
|
||||
return
|
||||
}
|
||||
// 通过用户ID获取用户基本信息
|
||||
info := logic.UL.GetUserInfo(uc.UserID)
|
||||
system.Success(info, "成功获取用户信息", c)
|
||||
}
|
||||
Reference in New Issue
Block a user