Browse Source

12月24日下午后台查询页面逻辑,token过期时间,将导出方法分离到service

dev_ljk
lijikun 5 months ago
parent
commit
e2a5ee0274
  1. 3
      link_homework/internal/controller/record/manageRecord.go
  2. 2
      link_homework/internal/logic/homework/homework.go
  3. 2
      link_homework/internal/logic/login/login.go
  4. 71
      link_homework/internal/logic/record/record.go
  5. 4
      link_homework/internal/service/record.go

3
link_homework/internal/controller/record/manageRecord.go

@ -1,13 +1,10 @@
package record package record
import ( import (
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/net/ghttp"
"link_homework/api/v1/record" "link_homework/api/v1/record"
"link_homework/internal/model/dto" "link_homework/internal/model/dto"
"link_homework/internal/service" "link_homework/internal/service"
"strconv"
"time"
) )
type ManageRecord struct{} type ManageRecord struct{}

2
link_homework/internal/logic/homework/homework.go

@ -68,7 +68,7 @@ func UpdateHomework(ctx context.Context, homeworkList []*entity.ActivityInteract
if v.Status != 2 { if v.Status != 2 {
if v.StartDate.After(gtime.Now()) { if v.StartDate.After(gtime.Now()) {
v.Status = 0 v.Status = 0
} else if v.EndDate.Before(gtime.Now()) {
} else if v.EndDate.Before(gtime.Now().AddDate(0, 0, -1)) {
v.Status = 2 v.Status = 2
cnt++ cnt++
_, _ = dao.ActivityInteractiveGroup.Ctx(ctx).Data(do.ActivityInteractiveGroup{ _, _ = dao.ActivityInteractiveGroup.Ctx(ctx).Data(do.ActivityInteractiveGroup{

2
link_homework/internal/logic/login/login.go

@ -39,7 +39,7 @@ func (l *sLoginLogic) Login(ctx context.Context, username, password string) (str
// 创建 JWT Token // 创建 JWT Token
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"username": username, "username": username,
"exp": time.Now().Add(time.Hour * 1).Unix(), // 1 小时后过期
"exp": time.Now().Add(time.Hour * 8).Unix(), // 8 小时后过期
}) })
// 签名并获取完整的编码后的字符串 token // 签名并获取完整的编码后的字符串 token

71
link_homework/internal/logic/record/record.go

@ -5,11 +5,15 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
pkgRecord "link_homework/api/v1/record" pkgRecord "link_homework/api/v1/record"
"link_homework/internal/dao" "link_homework/internal/dao"
"link_homework/internal/model/dto" "link_homework/internal/model/dto"
"link_homework/internal/service" "link_homework/internal/service"
"strconv"
"time"
) )
type sRecord struct{} type sRecord struct{}
@ -183,3 +187,70 @@ func (s *sRecord) GetShopInfoByDeptId(ctx context.Context, deptId string) (shops
_, err = g.Redis().Set(ctx, fmt.Sprintf("%s shopinfo", deptId), shopJSON) _, err = g.Redis().Set(ctx, fmt.Sprintf("%s shopinfo", deptId), shopJSON)
return return
} }
// ExportExcel 查询数据,创建excel文件,设置表头,写入数据,设置文件名,保存到缓冲区并返回,设置响应头,指定内容类型为excel文件,指定文件名为fileName,将buffer中的内容写入响应
func (s *sRecord) ExportExcel(r *ghttp.Request, groupId, jwcode int, deptId, shopId string, pageNo, pageSize int) {
// 调用查询服务
records, err := service.Record().GetRecordByCondition(
r.Context(), groupId, jwcode, deptId, shopId, pageNo, pageSize)
if err != nil {
//g.Log().Error(r.Context(), "查询记录失败: ", err)
r.Response.WriteJsonExit(dto.Error("查询记录失败: " + err.Error()))
return
}
if len(records) == 0 {
//g.Log().Warning(r.Context(), "查询结果为空: 参数 = ", req)
r.Response.WriteJsonExit(dto.Error("查询结果为空"))
return
}
// 创建 Excel 文件
excelFile := excelize.NewFile() // 默认会创建一个 Sheet1
sheetName := "Sheet1" // 使用默认的 Sheet1
// 设置表头
headers := []string{"序号", "精网号", "名字", "部门ID", "部门名称", "门店ID", "门店名称", "题目ID", "题目类型", "题目名称", "简答题标题", "作答内容", "提交时间"}
for i, header := range headers {
col := string('A' + i) // 将索引转换为 Excel 列名
excelFile.SetCellValue(sheetName, col+"1", header)
}
/*写入数据*/
// 写入数据
rowIndex := 2 // 从第二行开始写入数据
for _, record := range records {
for _, reply := range record.Reply { // 遍历每个 `Reply`
excelFile.SetCellValue(sheetName, "A"+strconv.Itoa(rowIndex), rowIndex-1)
excelFile.SetCellValue(sheetName, "B"+strconv.Itoa(rowIndex), record.Jwcode)
excelFile.SetCellValue(sheetName, "C"+strconv.Itoa(rowIndex), record.Name)
excelFile.SetCellValue(sheetName, "D"+strconv.Itoa(rowIndex), record.DeptId)
excelFile.SetCellValue(sheetName, "E"+strconv.Itoa(rowIndex), record.DeptName)
excelFile.SetCellValue(sheetName, "F"+strconv.Itoa(rowIndex), record.ShopId)
excelFile.SetCellValue(sheetName, "G"+strconv.Itoa(rowIndex), record.ShopName)
excelFile.SetCellValue(sheetName, "H"+strconv.Itoa(rowIndex), reply.FormId)
excelFile.SetCellValue(sheetName, "I"+strconv.Itoa(rowIndex), reply.Type)
excelFile.SetCellValue(sheetName, "J"+strconv.Itoa(rowIndex), reply.Description)
excelFile.SetCellValue(sheetName, "K"+strconv.Itoa(rowIndex), reply.ContentTitle)
excelFile.SetCellValue(sheetName, "L"+strconv.Itoa(rowIndex), reply.Content)
excelFile.SetCellValue(sheetName, "M"+strconv.Itoa(rowIndex), reply.UpdatedAt.String())
rowIndex++
}
}
/*写入数据*/
// 设置文件名
fileName := "Records_" + time.Now().Format("20060102150405") + ".xlsx"
// 保存到缓冲区并返回
buffer, err := excelFile.WriteToBuffer()
if err != nil {
r.Response.WriteJsonExit(dto.Error("生成 Excel 文件失败: " + err.Error()))
return
}
// 设置响应头,指定内容类型为Excel文件
r.Response.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
// 设置响应头,指定文件名为fileName
r.Response.Header().Set("Content-Disposition", "attachment; filename="+fileName)
// 将buffer中的内容写入响应
r.Response.Write(buffer.Bytes())
}

4
link_homework/internal/service/record.go

@ -8,6 +8,8 @@ package service
import ( import (
"context" "context"
pkgRecord "link_homework/api/v1/record" pkgRecord "link_homework/api/v1/record"
"github.com/gogf/gf/v2/net/ghttp"
) )
type ( type (
@ -20,6 +22,8 @@ type (
GetDeptInfo(ctx context.Context) (depts []pkgRecord.GetDeptInfoRes, err error) GetDeptInfo(ctx context.Context) (depts []pkgRecord.GetDeptInfoRes, err error)
// 根据部门信息查询门店信息 // 根据部门信息查询门店信息
GetShopInfoByDeptId(ctx context.Context, deptId string) (shops []pkgRecord.GetShopInfoByDeptIdRes, err error) GetShopInfoByDeptId(ctx context.Context, deptId string) (shops []pkgRecord.GetShopInfoByDeptIdRes, err error)
// ExportExcel 查询数据,创建excel文件,设置表头,写入数据,设置文件名,保存到缓冲区并返回,设置响应头,指定内容类型为excel文件,指定文件名为fileName,将buffer中的内容写入响应
ExportExcel(r *ghttp.Request, groupId int, jwcode int, deptId string, shopId string, pageNo int, pageSize int)
} }
) )

Loading…
Cancel
Save