Skip to content

Commit

Permalink
Merge branch 'dev' into Tim3088/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Penryn authored Dec 10, 2024
2 parents d789166 + 29d6d10 commit b479440
Show file tree
Hide file tree
Showing 42 changed files with 890 additions and 293 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Build

on:
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.22

- name: Build
run: |
go install src.techknowlogick.com/xgo@latest
xgo -out 4uonline --targets=*/amd64 .
- name: Archive Output
run: |
mkdir -p artifacts
mv 4uonline* artifacts/
cp README.md artifacts/
cp config.example.yaml artifacts/config.yaml
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: build-output
path: artifacts/
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ linters-settings:
- name: function-length # 函数长度限制
severity: warning
disabled: true
- name: cyclomatic # 圈复杂度
severity: warning
disabled: true
- name: cognitive-complexity # 认知复杂度
severity: warning
disabled: false
Expand Down
2 changes: 1 addition & 1 deletion app/apiException/apiException.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
ResourceNotFound = NewError(200511, log.LevelInfo, "访问的资源不存在")
AdminKeyError = NewError(200512, log.LevelInfo, "管理员注册密钥错误")
AdminAlreadyExisted = NewError(200513, log.LevelInfo, "管理员账号已存在")
UploadFileError = NewError(200514, log.LevelError, "上传文件失败")
UploadFileError = NewError(200514, log.LevelInfo, "上传文件失败")
FileSizeExceedError = NewError(200515, log.LevelInfo, "文件大小超限")
FileNotImageError = NewError(200516, log.LevelInfo, "上传的文件不是图片")

Expand Down
12 changes: 12 additions & 0 deletions app/common/counterName/counterName.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package counterName

// 定义要统计的字段

const (
// QrcodeScan 权益码扫描次数
QrcodeScan string = "qrcode_scan"
// Feedback 问题反馈数量
Feedback string = "feedback_scan"
// FeedbackHandle 处理问题反馈数量
FeedbackHandle string = "feedback_handle"
)
17 changes: 17 additions & 0 deletions app/common/feedbackType/feedbackType.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package feedbackType

// FeedbackType
const (
Activities uint = iota // 校园活动
DiningAndShops // 食堂及商铺
Dormitories // 宿舍
Academic // 教学服务(选课、转专业等)
Facilities // 校园设施
Classrooms // 教室
Library // 图书馆
Transportation // 交通
Security // 安保
HealthCare // 医疗服务
Policies // 学院相关政策(如综测等)
Others // 其他服务
)
69 changes: 0 additions & 69 deletions app/config/config.go

This file was deleted.

24 changes: 0 additions & 24 deletions app/config/encrypt.go

This file was deleted.

19 changes: 0 additions & 19 deletions app/config/init.go

This file was deleted.

38 changes: 23 additions & 15 deletions app/controllers/objectController/upload.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint:all
package objectController

import (
Expand Down Expand Up @@ -26,22 +27,21 @@ func UploadFile(c *gin.Context) {
}

uploadType := data.UploadType
fileHeader := data.File
// 获取文件流
fileSize := data.File.Size
file, err := data.File.Open()
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
apiException.AbortWithException(c, apiException.UploadFileError, err)
return
}
defer func(file multipart.File) {
err := file.Close()
if err != nil {
zap.L().Error("文件流关闭错误", zap.Error(err))
zap.L().Warn("文件关闭错误", zap.Error(err))
}
}(file)

// 获取文件信息
contentType, fileExt, err := objectService.GetFileInfo(file, fileHeader, uploadType)
contentType, fileExt, err := objectService.GetFileInfo(file, fileSize, uploadType)
if errors.Is(err, objectService.ErrSizeExceeded) {
apiException.AbortWithException(c, apiException.FileSizeExceedError, err)
return
Expand All @@ -50,25 +50,33 @@ func UploadFile(c *gin.Context) {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}
if errors.Is(err, objectService.ErrNotImage) {
apiException.AbortWithException(c, apiException.FileNotImageError, err)
return
}
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}
_, err = file.Seek(0, io.SeekStart)
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return

var fileReader io.Reader = file
if uploadType == objectService.TypeImage {
reader, size, err := objectService.ConvertToWebP(file)
if err != nil {
if errors.Is(err, objectService.ErrNotImage) {
apiException.AbortWithException(c, apiException.FileNotImageError, err)
return
}
zap.L().Error("转换图片到 WebP 失败", zap.Error(err))
} else { // 若转换成功则替代原文件
fileReader = reader
fileSize = size
fileExt = ".webp"
contentType = "image/webp"
}
}

// 上传文件
objectKey := objectService.GenerateObjectKey(uploadType, fileExt)
objectUrl, err := objectService.PutObject(objectKey, file, fileHeader.Size, contentType)
objectUrl, err := objectService.PutObject(objectKey, fileReader, fileSize, contentType)
if err != nil {
apiException.AbortWithException(c, apiException.UploadFileError, err)
apiException.AbortWithException(c, apiException.ServerError, err)
return
}

Expand Down
33 changes: 33 additions & 0 deletions app/controllers/qrcodeController/count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package qrcodeController

import (
"errors"

"4u-go/app/apiException"
"4u-go/app/services/qrcodeService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

type scanCountData struct {
ID uint `form:"id" binding:"required"`
}

// ScanCount 更新权益码扫码次数
func ScanCount(c *gin.Context) {
var data scanCountData
if err := c.ShouldBind(&data); err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}
if err := qrcodeService.AddScanCount(data.ID); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
apiException.AbortWithException(c, apiException.ResourceNotFound, nil)
} else {
apiException.AbortWithException(c, apiException.ServerError, err)
}
return
}
utils.JsonSuccessResponse(c, nil)
}
43 changes: 43 additions & 0 deletions app/controllers/qrcodeController/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package qrcodeController

import (
"4u-go/app/apiException"
"4u-go/app/models"
"4u-go/app/services/qrcodeService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
)

type createQrcodeData struct {
College uint `json:"college"`
Department string `json:"department" binding:"required"`
Description string `json:"description"`
FeedbackType uint `json:"feedback_type"`
Location string `json:"location" binding:"required"`
}

// CreateQrcode 创建一个权益码
func CreateQrcode(c *gin.Context) {
var data createQrcodeData
err := c.ShouldBindJSON(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

err = qrcodeService.SaveQrcode(models.Qrcode{
Status: true,
College: data.College,
Department: data.Department,
Description: data.Description,
FeedbackType: data.FeedbackType,
Location: data.Location,
})

if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}

utils.JsonSuccessResponse(c, nil)
}
Loading

0 comments on commit b479440

Please sign in to comment.