Skip to content

Commit

Permalink
refactor: 优化注册接口的请求参数设计
Browse files Browse the repository at this point in the history
  • Loading branch information
Penryn committed Dec 3, 2024
1 parent a148f50 commit 87f3fb2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
8 changes: 7 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ linters-settings:
- name: max-public-structs # 最大公共结构体数目
severity: warning
disabled: true
- name: cognitive-complexity
- name: argument-limit # 函数参数限制
severity: warning
disabled: true
- name: function-length # 函数长度限制
severity: warning
disabled: true
- name: cognitive-complexity # 认知复杂度
severity: warning
disabled: false
arguments: [ 10 ]
Expand Down
15 changes: 12 additions & 3 deletions app/controllers/userController/reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ type createStudentUserWechatForm struct {
Password string `json:"password" binding:"required"`
Type uint `json:"type" binding:"required"` // 用户类型 1-本科生 2-研究生
IDCardNumber string `json:"idCardNumber" binding:"required"`
Email string `json:"email" binding:"required"`
Name string `json:"name" binding:"required"`
College string `json:"college" binding:"required"`
Code string `json:"code" binding:"required"`
Email string `json:"email"`
}

// BindOrCreateStudentUserFromWechat 微信创建学生用户
Expand All @@ -42,6 +44,8 @@ func BindOrCreateStudentUserFromWechat(c *gin.Context) {
postForm.Type,
postForm.IDCardNumber,
postForm.Email,
postForm.Name,
postForm.College,
session.OpenID)
if err != nil {
var apiErr *apiException.Error
Expand All @@ -66,7 +70,9 @@ type createStudentUserForm struct {
Password string `json:"password" binding:"required"`
Type uint `json:"type" binding:"required"` // 用户类型 1-本科生 2-研究生
IDCardNumber string `json:"idCardNumber" binding:"required"`
Email string `json:"email" binding:"required"`
Name string `json:"name" binding:"required"`
College string `json:"college" binding:"required"`
Email string `json:"email"`
}

// CreateStudentUser H5创建学生用户
Expand All @@ -84,7 +90,10 @@ func CreateStudentUser(c *gin.Context) {
postForm.Password,
postForm.IDCardNumber,
postForm.Email,
postForm.Type)
postForm.Name,
postForm.College,
postForm.Type,
)
if err != nil {
var apiErr *apiException.Error
if errors.As(err, &apiErr) {
Expand Down
1 change: 0 additions & 1 deletion app/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type User struct {
Password string `json:"password"` // 密码 (只有管理员有密码)
WechatOpenID string `json:"wechat_open_id"` // 微信 OpenID
College string `json:"college"` // 学院
Class string `json:"class"` // 班级
PhoneNum string `json:"phone_num"` // 手机号码
CreatedAt time.Time `json:"created_at"` // 记录创建时间
}
Expand Down
11 changes: 9 additions & 2 deletions app/services/userService/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
)

// CreateStudentUser 创建学生用户
func CreateStudentUser(studentID, password, idCardNumber, email string, usertype uint) (*models.User, error) {
func CreateStudentUser(
studentID, password, idCardNumber, email, name, college string,
usertype uint,
) (*models.User, error) {
_, err := GetUserByStudentID(studentID)
if err == nil {
return nil, apiException.UserAlreadyExisted
Expand All @@ -24,6 +27,8 @@ func CreateStudentUser(studentID, password, idCardNumber, email string, usertype
}

user := &models.User{
Name: name,
College: college,
Type: usertype,
StudentID: studentID,
}
Expand All @@ -41,13 +46,15 @@ func CreateStudentUserWechat(
userType uint,
idCardNumber string,
email string,
name string,
college string,
wechatOpenID string,
) (*models.User, error) {
_, err := GetUserByWechatOpenID(wechatOpenID)
if err == nil {
return nil, apiException.OpenIDError
}
user, err := CreateStudentUser(studentID, password, idCardNumber, email, userType)
user, err := CreateStudentUser(studentID, password, idCardNumber, email, name, college, userType)
if err != nil && !errors.Is(err, apiException.ReactiveError) {
return nil, err
}
Expand Down

0 comments on commit 87f3fb2

Please sign in to comment.