311 lines
7.3 KiB
Go
311 lines
7.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"knowledge/api/dao"
|
|
"knowledge/api/dto"
|
|
"knowledge/api/model"
|
|
"knowledge/api/requests"
|
|
"knowledge/api/responses"
|
|
"knowledge/api/service"
|
|
"knowledge/consts"
|
|
"knowledge/global"
|
|
"knowledge/utils"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type Share struct{}
|
|
|
|
// GetBackgroundImage 获取背景图
|
|
func (r *Share) GetBackgroundImage(c *gin.Context) {
|
|
shareRequest := requests.ShareRequest{}
|
|
req := shareRequest.GetBackgroundImage
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
questionQaDao := dao.QuestionQaDao{}
|
|
|
|
// 获取题库数据
|
|
maps := make(map[string]interface{})
|
|
maps["qa_share_id"] = req.QaShareId
|
|
questionQa, err := questionQaDao.GetQuestionQa(maps)
|
|
if err != nil || questionQa == nil {
|
|
responses.FailWithMessage("题库不存在", c)
|
|
return
|
|
}
|
|
|
|
if questionQa.QaStatus == 2 {
|
|
responses.FailWithMessage("题库已失效", c)
|
|
return
|
|
}
|
|
|
|
// 检测过期时间
|
|
now := time.Now()
|
|
qaExpireTime := time.Time(questionQa.QaExpireTime)
|
|
if qaExpireTime.Before(now) {
|
|
responses.FailWithMessage("题库已失效", c)
|
|
return
|
|
}
|
|
|
|
image := utils.AddOssDomain(questionQa.Image)
|
|
responses.OkWithData(image, c)
|
|
}
|
|
|
|
// ShareAuth 授权
|
|
func (r *Share) ShareAuth(c *gin.Context) {
|
|
shareRequest := requests.ShareRequest{}
|
|
req := shareRequest.ShareAuth
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 参数验证
|
|
if err := global.Validate.Struct(req); err != nil {
|
|
responses.FailWithMessage(utils.Translate(err), c)
|
|
return
|
|
}
|
|
|
|
questionQaDao := dao.QuestionQaDao{}
|
|
|
|
// 获取题库数据
|
|
maps := make(map[string]interface{})
|
|
maps["qa_share_id"] = req.QaShareId
|
|
questionQa, err := questionQaDao.GetQuestionQa(maps)
|
|
if err != nil || questionQa == nil {
|
|
responses.FailWithMessage("题库不存在", c)
|
|
return
|
|
}
|
|
|
|
if questionQa.QaStatus == 2 {
|
|
responses.FailWithMessage("题库已失效", c)
|
|
return
|
|
}
|
|
|
|
if questionQa.QaPassword != req.QaPassword {
|
|
responses.FailWithMessage("密码错误", c)
|
|
return
|
|
}
|
|
|
|
// 检测过期时间
|
|
now := time.Now()
|
|
qaExpireTime := time.Time(questionQa.QaExpireTime)
|
|
if qaExpireTime.Before(now) {
|
|
responses.FailWithMessage("题库已失效", c)
|
|
return
|
|
}
|
|
|
|
token := &utils.Token{
|
|
Client: 1,
|
|
QaId: fmt.Sprintf("%d", questionQa.QaId),
|
|
}
|
|
|
|
// 生成jwt
|
|
ttl := 5 * time.Hour
|
|
jwt, err := token.NewJWT(ttl)
|
|
if err != nil {
|
|
responses.FailWithMessage("题库错误", c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
g := dto.ShareAuthDto{}
|
|
|
|
g.Token = jwt
|
|
|
|
responses.OkWithData(g, c)
|
|
}
|
|
|
|
// GetQuestionQa 获取知识问答数据
|
|
func (r *Share) GetQuestionQa(c *gin.Context) {
|
|
// 获取知识问答id
|
|
qaId := c.GetInt64("QaId")
|
|
if qaId == 0 {
|
|
c.JSON(http.StatusUnauthorized, gin.H{
|
|
"message": "请输入密码后查看",
|
|
"code": consts.TokenError,
|
|
"data": "",
|
|
})
|
|
return
|
|
}
|
|
|
|
questionQaDao := dao.QuestionQaDao{}
|
|
|
|
// 获取题库数据
|
|
questionQa, err := questionQaDao.GetQuestionQaById(qaId)
|
|
if err != nil || questionQa == nil {
|
|
responses.FailWithMessage("题库不存在", c)
|
|
return
|
|
}
|
|
|
|
if questionQa.QaStatus == 2 {
|
|
responses.FailWithMessage("题库已失效", c)
|
|
return
|
|
}
|
|
|
|
// 检测过期时间
|
|
now := time.Now()
|
|
qaExpireTime := time.Time(questionQa.QaExpireTime)
|
|
if qaExpireTime.Before(now) {
|
|
responses.FailWithMessage("题库已失效", c)
|
|
return
|
|
}
|
|
|
|
g := dto.GetQuestionQaDto(questionQa)
|
|
|
|
responses.OkWithData(g, c)
|
|
}
|
|
|
|
// GetQuestion 获取题目数据
|
|
func (r *Share) GetQuestion(c *gin.Context) {
|
|
// 获取知识问答id
|
|
qaId := c.GetInt64("QaId")
|
|
if qaId == 0 {
|
|
c.JSON(http.StatusUnauthorized, gin.H{
|
|
"message": "请输入密码后查看",
|
|
"code": consts.TokenError,
|
|
"data": "",
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
questionQaDao := dao.QuestionQaDao{}
|
|
questionQaItemDao := dao.QuestionQaItemDao{}
|
|
|
|
// 获取题库数据
|
|
questionQa, err := questionQaDao.GetQuestionQaById(qaId)
|
|
if err != nil || questionQa == nil {
|
|
responses.FailWithMessage("题库不存在", c)
|
|
return
|
|
}
|
|
|
|
if questionQa.QaStatus == 2 {
|
|
responses.FailWithMessage("题库已失效", c)
|
|
return
|
|
}
|
|
|
|
// 检测过期时间
|
|
now := time.Now()
|
|
qaExpireTime := time.Time(questionQa.QaExpireTime)
|
|
if qaExpireTime.Before(now) {
|
|
responses.FailWithMessage("题库已失效", c)
|
|
return
|
|
}
|
|
|
|
g := dto.ShareDto{
|
|
Question: []*dto.QuestionDto{},
|
|
BaseTokenItem: []*dto.BaseTokenItemDto{},
|
|
}
|
|
|
|
// 题目数据
|
|
var questions []*model.QuestionQaItem
|
|
|
|
// 题目数据-必备选中
|
|
maps := make(map[string]interface{})
|
|
maps["qa_id"] = questionQa.QaId
|
|
maps["is_must_select"] = 1
|
|
questionQaItems, err := questionQaItemDao.GetQuestionQaItemPreloadList(maps)
|
|
if err == nil && len(questionQaItems) > 0 {
|
|
for _, item := range questionQaItems {
|
|
questions = append(questions, item)
|
|
}
|
|
}
|
|
|
|
// 题目数据-剩余随机数量
|
|
remainingQuantity := questionQa.QaQuantity - len(questionQaItems)
|
|
if remainingQuantity > 0 {
|
|
// 随机获取剩余题目
|
|
maps = make(map[string]interface{})
|
|
maps["qa_id"] = questionQa.QaId
|
|
maps["is_must_select"] = 0
|
|
questionQaItems, err = questionQaItemDao.GetQuestionQaItemWhereListRand(maps, remainingQuantity)
|
|
if err == nil && len(questionQaItems) > 0 {
|
|
for _, item := range questionQaItems {
|
|
questions = append(questions, item)
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, v := range questions {
|
|
questionOptionDao := dao.QuestionOptionDao{}
|
|
maps = make(map[string]interface{})
|
|
maps["question_id"] = v.QuestionId
|
|
questionOption, err := questionOptionDao.GetQuestionOptionList(maps)
|
|
if err != nil {
|
|
responses.FailWithMessage("内部错误", c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
questionDto := dto.GetShareQuestionDto(v.Question)
|
|
|
|
// 加载选项
|
|
questionDto.LoadQuestionOptionSimplify(questionOption)
|
|
|
|
// 加载图片
|
|
questionDto.LoadQuestionImage(v.Question.QuestionImage)
|
|
|
|
// 加载答案
|
|
questionDto.LoadQuestionAnswer(v.Question)
|
|
|
|
// 将转换后的结构体添加到新切片中
|
|
g.Question = append(g.Question, questionDto)
|
|
}
|
|
|
|
// 按照难度重新排序题目顺序
|
|
//sort.SliceStable(g.Question, func(i, j int) bool {
|
|
// return g.Question[i].Difficulty < g.Question[j].Difficulty
|
|
//})
|
|
|
|
// 加载飞花令
|
|
if questionQa.QaDisplayType == 2 {
|
|
questionQaTokenDao := dao.QuestionQaTokenDao{}
|
|
maps = make(map[string]interface{})
|
|
maps["qa_id"] = questionQa.QaId
|
|
questionQaToken, err := questionQaTokenDao.GetQuestionQaTokenList(maps)
|
|
if err != nil {
|
|
responses.FailWithMessage("内部错误", c)
|
|
return
|
|
}
|
|
|
|
baseTokenItemDao := dao.BaseTokenItemDao{}
|
|
for _, v := range questionQaToken {
|
|
// 加载详细飞花令
|
|
baseTokenItem, err := baseTokenItemDao.GetBaseTokenItemPreloadOrderByTokenId(v.TokenId)
|
|
if err != nil {
|
|
responses.FailWithMessage("内部错误", c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
for _, item := range baseTokenItem {
|
|
if len(g.BaseTokenItem) < questionQa.QaQuantity {
|
|
// 处理返回值
|
|
baseTokenItemDto := dto.GetBaseTokenItemDto(item)
|
|
|
|
// 将转换后的结构体添加到新切片中
|
|
g.BaseTokenItem = append(g.BaseTokenItem, baseTokenItemDto)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 记录问答题库打开次数
|
|
questionQaService := service.QuestionQaService{}
|
|
_ = questionQaService.RecordQuestionQaOpenNum(global.Db, questionQa.QaId)
|
|
|
|
// 返回
|
|
responses.OkWithData(g, c)
|
|
}
|