Browse Source

12月21日 添加redis(客户端除提交题目,后台部门门店) 修改了出现错误时返回到前端的提示信息

dev_ljk
lijikun 5 months ago
parent
commit
7e9535bf7a
  1. 39
      link_homework/internal/logic/client/client.go
  2. 2
      link_homework/internal/logic/middleware/interceptor.go
  3. 44
      link_homework/internal/logic/record/record.go

39
link_homework/internal/logic/client/client.go

@ -2,6 +2,9 @@ package client
import ( import (
"context" "context"
"encoding/json"
"errors"
"fmt"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/os/gtime"
"link_homework/api/v1/ClientPage" "link_homework/api/v1/ClientPage"
@ -16,9 +19,20 @@ func init() {
} }
func (s *sClient) ClientGetHomeworkList(ctx context.Context, jwcode int) (homeworkList []ClientPage.GetHomeworkListRse, err error) { func (s *sClient) ClientGetHomeworkList(ctx context.Context, jwcode int) (homeworkList []ClientPage.GetHomeworkListRse, err error) {
// 从Redis中获取数据
value, _ := g.Redis().Get(ctx, "ClientHomeworkList")
if value.String() != "" {
err = json.Unmarshal(value.Bytes(), &homeworkList) //JSON -> Go数据结构
if err != nil {
return nil, errors.New("从Redis获取数据,JSON解析失败")
}
return
}
// 如果Redis中没有数据,查询数据库
//var jwcode = 90038794 //需要从token中获取当前的数据 //var jwcode = 90038794 //需要从token中获取当前的数据
err = dao.ActivityInteractiveGroup.Ctx(ctx).Fields("id", "name", "DATE_FORMAT(end_date, '%Y-%m-%d') as end_date"). err = dao.ActivityInteractiveGroup.Ctx(ctx).Fields("id", "name", "DATE_FORMAT(end_date, '%Y-%m-%d') as end_date").
Where("end_date>?", gtime.Now()).OrderDesc("end_date").Scan(&homeworkList)
Where("end_date>?", gtime.Now()).Where("status", 1).OrderDesc("end_date").Scan(&homeworkList)
if err != nil { if err != nil {
//fmt.Println("作业列表查询失败:", err) //fmt.Println("作业列表查询失败:", err)
return return
@ -37,12 +51,35 @@ func (s *sClient) ClientGetHomeworkList(ctx context.Context, jwcode int) (homewo
} }
homeworkList[i].Submit = count / num homeworkList[i].Submit = count / num
} }
// 将查询结果JSON化并存入Redis
ClientHomeworkListJSON, _ := json.Marshal(homeworkList)
_, err = g.Redis().Set(ctx, "ClientHomeworkList", ClientHomeworkListJSON)
if err != nil {
return nil, errors.New("Redis存储失败")
}
return return
} }
func (s *sClient) GetHomeworkQuestion(ctx context.Context, groupId int) (questions []ClientPage.GetHomeworkQuestionRes, err error) { func (s *sClient) GetHomeworkQuestion(ctx context.Context, groupId int) (questions []ClientPage.GetHomeworkQuestionRes, err error) {
// 从Redis中获取数据
value, _ := g.Redis().Get(ctx, fmt.Sprintf("%d HomeworkQuestions", groupId))
if value.String() != "" {
err = json.Unmarshal(value.Bytes(), &questions) //JSON -> Go数据结构
if err != nil {
return nil, errors.New("从Redis获取数据,JSON解析失败")
}
return
}
// 如果Redis中没有数据,查询数据库
err = dao.ActivityInteractiveForm.Ctx(ctx).Fields("id", "name", "content", "status", "type", "description"). err = dao.ActivityInteractiveForm.Ctx(ctx).Fields("id", "name", "content", "status", "type", "description").
Where("group_id", groupId).Scan(&questions) Where("group_id", groupId).Scan(&questions)
// 将查询结果JSON化并存入Redis
HomeworkQuestionsJSON, _ := json.Marshal(questions)
_, err = g.Redis().Set(ctx, fmt.Sprintf("%d HomeworkQuestions", groupId), HomeworkQuestionsJSON)
return return
} }

2
link_homework/internal/logic/middleware/interceptor.go

@ -17,7 +17,7 @@ func MiddlewareIsLogin(r *ghttp.Request) {
if err != nil || !valid { if err != nil || !valid {
r.Response.WriteJsonExit(dto.Result{ r.Response.WriteJsonExit(dto.Result{
Code: 401, Code: 401,
Message: "Token 无效123",
Message: "Token 无效",
}) })
} else { } else {
r.Middleware.Next() r.Middleware.Next()

44
link_homework/internal/logic/record/record.go

@ -2,7 +2,9 @@ package record
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
pkgRecord "link_homework/api/v1/record" pkgRecord "link_homework/api/v1/record"
"link_homework/internal/dao" "link_homework/internal/dao"
@ -43,7 +45,7 @@ func (s *sRecord) GetRecordList(ctx context.Context, groupId, pageNo, pageSize i
} }
total, err1 := dao.ActivityInteractiveRecord.Ctx(ctx).Fields("jwcode").Where("group_id", groupId).Group("jwcode").Count() total, err1 := dao.ActivityInteractiveRecord.Ctx(ctx).Fields("jwcode").Where("group_id", groupId).Group("jwcode").Count()
if err1 != nil { if err1 != nil {
return
return nil, errors.New("无条件查询总数失败")
} }
record[i].Total = total record[i].Total = total
//根据jwcode,groupId在record表中查询最新的提交记录进行存放 //根据jwcode,groupId在record表中查询最新的提交记录进行存放
@ -51,12 +53,12 @@ func (s *sRecord) GetRecordList(ctx context.Context, groupId, pageNo, pageSize i
err = dao.ActivityInteractiveRecord.Ctx(ctx).Fields("content", "content_title", "updated_at", "form_id"). err = dao.ActivityInteractiveRecord.Ctx(ctx).Fields("content", "content_title", "updated_at", "form_id").
Where("jwcode", info.Jwcode).Where("group_id", groupId).Group("form_id").Order("updated_at desc").Scan(&recordInfo) Where("jwcode", info.Jwcode).Where("group_id", groupId).Group("form_id").Order("updated_at desc").Scan(&recordInfo)
if err != nil { if err != nil {
return nil, err
return nil, errors.New("根据jwcode查询提交记录失败")
} }
for i, title := range recordInfo { for i, title := range recordInfo {
err = dao.ActivityInteractiveForm.Ctx(ctx).Fields("description", "type").Where("id", title.FormId).Scan(&recordInfo[i]) err = dao.ActivityInteractiveForm.Ctx(ctx).Fields("description", "type").Where("id", title.FormId).Scan(&recordInfo[i])
if err != nil { if err != nil {
return nil, err
return nil, errors.New("根据jwcode查询作业题目信息失败")
} }
} }
record[i].Reply = recordInfo record[i].Reply = recordInfo
@ -135,13 +137,49 @@ func (s *sRecord) GetRecordByCondition(ctx context.Context, groupId, jwcode int,
// 查询部门信息 // 查询部门信息
func (s *sRecord) GetDeptInfo(ctx context.Context) (depts []pkgRecord.GetDeptInfoRes, err error) { func (s *sRecord) GetDeptInfo(ctx context.Context) (depts []pkgRecord.GetDeptInfoRes, err error) {
// 从Redis中获取数据
value, _ := g.Redis().Get(ctx, "deptinfo")
if value.String() != "" {
err = json.Unmarshal(value.Bytes(), &depts)
if err != nil {
return nil, errors.New("从Redis获取数据,JSON解析失败")
}
return
}
// 如果Redis中没有数据,查询数据库
err = g.DB("cms").Model("member_info").Fields("deptId", "deptName").Group("deptId").Scan(&depts) err = g.DB("cms").Model("member_info").Fields("deptId", "deptName").Group("deptId").Scan(&depts)
if err != nil {
return nil, errors.New("查询部门信息失败")
}
// 将查询结果JSON化并存入Redis
deptJSON, _ := json.Marshal(depts)
_, err = g.Redis().Set(ctx, "deptinfo", deptJSON)
if err != nil {
return nil, errors.New("Redis存储失败")
}
return return
} }
// 根据部门信息查询门店信息 // 根据部门信息查询门店信息
func (s *sRecord) GetShopInfoByDeptId(ctx context.Context, deptId string) (shops []pkgRecord.GetShopInfoByDeptIdRes, err error) { func (s *sRecord) GetShopInfoByDeptId(ctx context.Context, deptId string) (shops []pkgRecord.GetShopInfoByDeptIdRes, err error) {
// 从Redis中获取数据
value, _ := g.Redis().Get(ctx, fmt.Sprintf("%s shopinfo", deptId))
if value.String() != "" {
err = json.Unmarshal(value.Bytes(), &shops)
if err != nil {
return nil, errors.New("从Redis获取数据,JSON解析失败")
}
return
}
// 如果Redis中没有数据,查询数据库
err = g.DB("cms").Model("member_info").Fields("shopId", "shopName"). err = g.DB("cms").Model("member_info").Fields("shopId", "shopName").
Where("deptId", deptId).Group("shopId").Scan(&shops) Where("deptId", deptId).Group("shopId").Scan(&shops)
// 将查询结果JSON化并存入Redis
shopJSON, _ := json.Marshal(shops)
_, err = g.Redis().Set(ctx, fmt.Sprintf("%s shopinfo", deptId), shopJSON)
return return
} }
Loading…
Cancel
Save