Skip to content

Commit

Permalink
Merge pull request #35 from zjutjh/dev
Browse files Browse the repository at this point in the history
init
  • Loading branch information
cbluebird authored Dec 16, 2024
2 parents b0785cc + bb86b60 commit c6f7eba
Show file tree
Hide file tree
Showing 125 changed files with 4,714 additions and 559 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/
32 changes: 32 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Lint

# 定义触发条件:在 `dev` 分支的 push 或 pull request 事件触发工作流
on:
push:
branches:
- dev # 当向 dev 分支推送时触发
pull_request:
branches:
- dev # 当对 dev 分支发起拉取请求时触发

jobs:
lint:
runs-on: ubuntu-latest # 指定工作流运行在最新的 Ubuntu 环境中

steps:
# 第一步:检查出代码
- name: Checkout code
uses: actions/checkout@v4 # 使用 GitHub 提供的 checkout 动作,确保代码在工作流环境中可用

# 第二步:设置 Go 环境
- name: Set up Go
uses: actions/setup-go@v5 # 使用 GitHub 提供的 setup-go 动作,设置 Go 环境
with:
go-version: stable

# 第三步:运行 golangci-lint
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v6 # 使用 golangci-lint-action
with:
version: v1.62.0 # 指定 golangci-lint 版本
args: '--config .golangci.yml' # 使用指定的配置文件
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ go.work.sum
config.yaml

.idea
.vscode
.vscode

logs
37 changes: 28 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

run:
concurrency: 4 # 并行执行的 linter 数量,默认为可用 CPU 数量
deadline: 1m # 分析的最大超时时间,默认为 1 分钟
timeout: 5m # 超时时间
deadline: 5m # 分析的最大超时时间,默认为 5 分钟
timeout: 10m # 超时时间
issues-exit-code: 1 # 如果发现至少一个问题,退出码为 1
tests: true # 包含测试文件进行分析
modules-download-mode: readonly # 防止分析过程中修改 go.mod 文件
Expand All @@ -28,27 +28,46 @@ linters-settings:
# 默认失败置信度
confidence: 0.1
rules:
- name: var-naming # 变量命名规则
- name: var-naming # 变量命名规则
severity: warning
disabled: true
- name: line-length-limit # 行长度限制
- name: line-length-limit # 行长度限制
severity: warning
disabled: false
exclude: [ "" ]
arguments: [ 110 ]
- name: add-constant #使用命名常量而非魔法数字
arguments: [ 120 ]
- name: add-constant # 使用命名常量而非魔法数字
severity: warning
disabled: true
- name: package-comments #包注释
- name: package-comments # 包注释
severity: warning
disabled: true
- name: import-alias-naming #导入别名命名规则
- name: import-alias-naming # 导入别名命名规则
severity: warning
disabled: true
- name: get-return # 使用get必须有返回值
severity: warning
disabled: true
- name: max-public-structs # 最大公共结构体数目
severity: warning
disabled: true
- name: argument-limit # 函数参数限制
severity: warning
disabled: true
- name: function-length # 函数长度限制
severity: warning
disabled: true
- name: cyclomatic # 圈复杂度
severity: warning
disabled: true
- name: cognitive-complexity # 认知复杂度
severity: warning
disabled: false
arguments: [ 10 ]
gofmt:
simplify: true # 使用 gofmt 的 `-s` 选项简化代码
gocyclo:
min-complexity: 5 # 触发报告的最小代码复杂度
min-complexity: 10 # 触发报告的最小代码复杂度
dupl:
threshold: 100 # 触发报告的最小令牌数
goconst:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# 4UOnline-Go
校学生会权益服务系统

点此查看[项目说明](docs/README.md)
73 changes: 53 additions & 20 deletions app/apiException/apiException.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,73 @@
package apiException

import "net/http"
import (
"net/http"

// Error 表示自定义错误,包括状态码、代码和消息。
"4u-go/app/utils/log"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

// Error 表示自定义错误,包括状态码、消息和日志级别。
type Error struct {
StatusCode int `json:"-"`
Code int `json:"code"`
Msg string `json:"msg"`
Code int
Msg string
Level log.Level
}

// Error 表示自定义的错误类型
var (
ServerError = NewError(http.StatusInternalServerError, 200500, "系统异常,请稍后重试!")
OpenIDError = NewError(http.StatusInternalServerError, 200500, "系统异常,请稍后重试!")
ParamError = NewError(http.StatusInternalServerError, 200501, "参数错误")
ServerError = NewError(200500, log.LevelError, "系统异常,请稍后重试!")
OpenIDError = NewError(200500, log.LevelInfo, "系统异常,请稍后重试!")
ParamError = NewError(200501, log.LevelInfo, "参数错误")
ReactiveError = NewError(200502, log.LevelInfo, "该通行证已经存在,请重新输入")
UserAlreadyExisted = NewError(200503, log.LevelInfo, "该用户已激活")
RequestError = NewError(200504, log.LevelInfo, "系统异常,请稍后重试!")
StudentNumAndIidError = NewError(200505, log.LevelInfo, "该学号或身份证不存在或者不匹配,请重新输入")
PwdError = NewError(200506, log.LevelInfo, "密码长度必须在6~20位之间")
UserNotFound = NewError(200507, log.LevelInfo, "该用户不存在")
NoThatPasswordOrWrong = NewError(200508, log.LevelInfo, "密码错误")
NotLogin = NewError(200509, log.LevelInfo, "未登录")
NotPermission = NewError(200510, log.LevelInfo, "该用户无权限")
ResourceNotFound = NewError(200511, log.LevelInfo, "访问的资源不存在")
AdminKeyError = NewError(200512, log.LevelInfo, "管理员注册密钥错误")
AdminAlreadyExisted = NewError(200513, log.LevelInfo, "管理员账号已存在")
UploadFileError = NewError(200514, log.LevelInfo, "上传文件失败")
FileSizeExceedError = NewError(200515, log.LevelInfo, "文件大小超限")
FileNotImageError = NewError(200516, log.LevelInfo, "上传的文件不是图片")

NotInit = NewError(http.StatusNotFound, 200404, http.StatusText(http.StatusNotFound))
NotFound = NewError(http.StatusNotFound, 200404, http.StatusText(http.StatusNotFound))
Unknown = NewError(http.StatusInternalServerError, 300500, "系统异常,请稍后重试!")
NotFound = NewError(200404, log.LevelWarn, http.StatusText(http.StatusNotFound))
)

// OtherError 返回一个表示其他未定义错误的自定义错误消息
func OtherError(message string) *Error {
return NewError(http.StatusForbidden, 100403, message)
}

// Error 方法实现了 error 接口,返回错误的消息内容
func (e *Error) Error() string {
return e.Msg
}

// NewError 创建并返回一个新的自定义错误实例
func NewError(statusCode, code int, msg string) *Error {
func NewError(code int, level log.Level, msg string) *Error {
return &Error{
StatusCode: statusCode,
Code: code,
Msg: msg,
Code: code,
Msg: msg,
Level: level,
}
}

// AbortWithException 用于返回自定义错误信息
func AbortWithException(c *gin.Context, apiError *Error, err error) {
logError(c, apiError, err)
_ = c.AbortWithError(200, apiError) //nolint:errcheck
}

// logError 记录错误日志
func logError(c *gin.Context, apiErr *Error, err error) {
// 构建日志字段
logFields := []zap.Field{
zap.Int("error_code", apiErr.Code),
zap.String("path", c.Request.URL.Path),
zap.String("method", c.Request.Method),
zap.String("ip", c.ClientIP()),
zap.Error(err), // 记录原始错误信息
}
log.GetLogFunc(apiErr.Level)(apiErr.Msg, logFields...)
}
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.

Loading

0 comments on commit c6f7eba

Please sign in to comment.