处理分享2
This commit is contained in:
+178
-56
@@ -1,22 +1,69 @@
|
||||
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/consts"
|
||||
"knowledge/global"
|
||||
"knowledge/utils"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Share struct{}
|
||||
|
||||
// GetShare 获取分享数据
|
||||
func (r *Share) GetShare(c *gin.Context) {
|
||||
// GetBackgroundImage 获取背景图
|
||||
func (r *Share) GetBackgroundImage(c *gin.Context) {
|
||||
shareRequest := requests.ShareRequest{}
|
||||
req := shareRequest.GetShare
|
||||
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
|
||||
@@ -28,9 +75,7 @@ func (r *Share) GetShare(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 业务处理
|
||||
questionQaDao := dao.QuestionQaDao{}
|
||||
questionQaItemDao := dao.QuestionQaItemDao{}
|
||||
|
||||
// 获取题库数据
|
||||
maps := make(map[string]interface{})
|
||||
@@ -59,47 +104,120 @@ func (r *Share) GetShare(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
//g := dto.ShareDto{}
|
||||
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{
|
||||
QuestionQa: &dto.QuestionQaDto{},
|
||||
Question: []*dto.QuestionDto{},
|
||||
BaseTokenItem: []*dto.BaseTokenItemDto{},
|
||||
}
|
||||
|
||||
// 加载题库数据
|
||||
g.QuestionQa = dto.GetQuestionQaDto(questionQa)
|
||||
// 题目数据
|
||||
var questions []*model.QuestionQaItem
|
||||
|
||||
// 题目数据-必备选中
|
||||
maps = make(map[string]interface{})
|
||||
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 {
|
||||
questionOptionDao := dao.QuestionOptionDao{}
|
||||
maps = make(map[string]interface{})
|
||||
maps["question_id"] = item.QuestionId
|
||||
questionOption, err := questionOptionDao.GetQuestionOptionList(maps)
|
||||
if err != nil {
|
||||
responses.FailWithMessage("内部错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理返回值
|
||||
questionDto := dto.GetShareQuestionDto(item.Question)
|
||||
|
||||
// 加载选项
|
||||
questionDto.LoadQuestionOption(questionOption)
|
||||
|
||||
// 加载图片
|
||||
questionDto.LoadQuestionImage(item.Question.QuestionImage)
|
||||
|
||||
// 加载答案
|
||||
questionDto.LoadQuestionAnswer(item.Question.QuestionAnswer)
|
||||
|
||||
// 将转换后的结构体添加到新切片中
|
||||
g.Question = append(g.Question, questionDto)
|
||||
questions = append(questions, item)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,33 +231,37 @@ func (r *Share) GetShare(c *gin.Context) {
|
||||
questionQaItems, err = questionQaItemDao.GetQuestionQaItemWhereListRand(maps, remainingQuantity)
|
||||
if err == nil && len(questionQaItems) > 0 {
|
||||
for _, item := range questionQaItems {
|
||||
questionOptionDao := dao.QuestionOptionDao{}
|
||||
maps = make(map[string]interface{})
|
||||
maps["question_id"] = item.QuestionId
|
||||
questionOption, err := questionOptionDao.GetQuestionOptionList(maps)
|
||||
if err != nil {
|
||||
responses.FailWithMessage("内部错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理返回值
|
||||
questionDto := dto.GetShareQuestionDto(item.Question)
|
||||
|
||||
// 加载选项
|
||||
questionDto.LoadQuestionOption(questionOption)
|
||||
|
||||
// 加载图片
|
||||
questionDto.LoadQuestionImage(item.Question.QuestionImage)
|
||||
|
||||
// 加载答案
|
||||
questionDto.LoadQuestionAnswer(item.Question.QuestionAnswer)
|
||||
|
||||
// 将转换后的结构体添加到新切片中
|
||||
g.Question = append(g.Question, questionDto)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user