Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用户中心新增学生类型、绑定关系字段和统一登录接口 #16

Open
wants to merge 6 commits into
base: dev_2022
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions app/controllers/emailController/emailReSetController.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package emailController

import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"math/rand"
"net/http"
"time"
"usercenter/app/services/emailService"
"usercenter/app/services/userService"
"usercenter/app/utility"
)
Expand Down Expand Up @@ -44,11 +40,5 @@ func EmailReset(c *gin.Context) {
return
}

rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
vcode := fmt.Sprintf("%06v", rnd.Int31n(1000000))

emailService.SendEmail(data.Email, vcode)
userService.CreateUserInRedis("", data.Email, "", vcode)

utility.JsonResponse(http.StatusOK, "OK", nil, c)
}
19 changes: 13 additions & 6 deletions app/controllers/userController/activateWithEmail.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package userController

import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"log"
"math/rand"
"time"
Expand All @@ -13,10 +15,12 @@ import (
)

type RegisterData struct {
StudentId string `json:"stu_id"`
Password string `json:"password"`
Iid string `json:"iid"`
Email string `json:"email"`
StudentId string `json:"stu_id"`
Password string `json:"password"`
Iid string `json:"iid"`
Email string `json:"email"`
Type uint8 `json:"type"` // 0: 本科生 1: 研究生
BoundSystem uint8 `json:"bound_system"` // 0:wjh 1:foru
}

func ActivateUser(c *gin.Context) {
Expand All @@ -37,11 +41,14 @@ func ActivateUser(c *gin.Context) {
utility.JsonResponse(402, "学号格式不正确,请重新输入", nil, c)
return
}
_, err = userService.GetUserByStudentId(data.StudentId)
_, err = userService.GetUserByStudentIdAndSystem(data.StudentId, data.BoundSystem)
log.Println(err)
if err == nil {
utility.JsonResponse(403, "该通行证已经存在,请重新输入", nil, c)
return
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
_ = c.AbortWithError(200, err)
return
}
flag := studentService.CheckStudentBYSIDAndIID(data.StudentId, data.Iid)
if !flag {
Expand All @@ -56,7 +63,7 @@ func ActivateUser(c *gin.Context) {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
vcode := fmt.Sprintf("%06v", rnd.Int31n(1000000))

err = userService.CreateUserInRedis(data.Password, data.Email, data.StudentId, vcode)
err = userService.CreateUserInRedis(data.Password, data.Email, data.StudentId, vcode, data.Type, data.BoundSystem)
if err != nil {
_ = c.AbortWithError(200, err)
return
Expand Down
14 changes: 11 additions & 3 deletions app/controllers/userController/activateWithoutEmail.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package userController

import (
"errors"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"log"
"usercenter/app/services/studentService"
"usercenter/app/services/userService"
Expand All @@ -17,10 +19,13 @@ func ActiviteWithoutEmail(c *gin.Context) {
return
}

_, err = userService.GetUserByStudentId(data.StudentId)
_, err = userService.GetUserByStudentIdAndSystem(data.StudentId, data.BoundSystem)
if err == nil {
utility.JsonResponse(403, "该通行证已经存在,请重新输入", nil, c)
return
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
_ = c.AbortWithError(200, err)
return
}
flag := studentService.CheckStudentBYSIDAndIID(data.StudentId, data.Iid)
if !flag {
Expand All @@ -31,8 +36,11 @@ func ActiviteWithoutEmail(c *gin.Context) {
utility.JsonResponse(401, "密码长度必须在6~20位之间", nil, c)
return
}
err = userService.CreateUser(data.Password, data.Email, data.StudentId)
if err != nil {
err = userService.CreateUser(data.Password, data.Email, data.StudentId, data.Type, data.BoundSystem)
if errors.Is(err, errors.New("密码错误")) {
utility.JsonResponse(407, "该账号已在其他系统激活,请重新输入正确密码", nil, c)
return
} else if err != nil {
log.Println(err)
utility.JsonResponseInternalServerError(c)
return
Expand Down
18 changes: 13 additions & 5 deletions app/controllers/userController/del.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package userController

import (
"errors"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"usercenter/app/apiExpection"
"usercenter/app/services/studentService"
"usercenter/app/services/userService"
"usercenter/app/utility"
)

type form struct {
IDCard string `json:"iid" binding:"required"`
StudentID string `json:"stuid" binding:"required"`
IDCard string `json:"iid" binding:"required"`
StudentID string `json:"stuid" binding:"required"`
BoundSystem uint8 `json:"bound_system"` // 0:wjh 1:foru
}

func DelAccount(c *gin.Context) {
Expand All @@ -25,12 +28,17 @@ func DelAccount(c *gin.Context) {
return
}

err = userService.DelAccount(postForm.StudentID)
if err != nil {
err = userService.DelAccount(postForm.StudentID, postForm.BoundSystem)
if errors.Is(err, gorm.ErrRecordNotFound) {
utility.JsonResponse(401, "用户不存在", nil, c)
return
} else if !errors.Is(err, errors.New("系统未绑定")) {
utility.JsonResponse(406, err.Error(), nil, c)
return
} else if err != nil {
_ = c.AbortWithError(200, apiExpection.ServerError)
return
}

utility.JsonSuccessResponse(c, nil)

}
27 changes: 25 additions & 2 deletions app/controllers/userController/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"log"
"usercenter/app/services/userService"
"usercenter/app/utility"
"usercenter/app/utility/oauth"
)

type LoginData struct {
StudentId string `json:"stu_id"`
Password string `json:"password"`
StudentId string `json:"stu_id"`
Password string `json:"password"`
BoundSystem uint8 `json:"bound_system"` // 0:wjh 1:foru
}

func AuthPassword(c *gin.Context) {
Expand All @@ -30,5 +32,26 @@ func AuthPassword(c *gin.Context) {
utility.JsonResponse(405, "密码错误", nil, c)
return
}
err = userService.UpdateBoundSystem(data.StudentId, data.BoundSystem)
if err != nil {
utility.JsonResponseInternalServerError(c)
return
}
utility.JsonResponse(200, "OK", nil, c)
}

func OauthPassword(c *gin.Context) {
var data LoginData
err := c.ShouldBindJSON(&data)
if err != nil {
log.Println(err)
utility.JsonResponseInternalServerError(c)
return
}
if sid, err := oauth.CheckByOauth(data.StudentId, data.Password); sid != data.StudentId || err != nil {
log.Println(err)
utility.JsonResponse(405, "密码错误", nil, c)
return
}
utility.JsonResponse(200, "OK", nil, c)
}
16 changes: 16 additions & 0 deletions app/model/systemBinding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package model

import "time"

// 系统绑定信息的结构体,用于存储 JSON 数据
type SystemBinding struct {
SystemName SystemNameEnum `json:"SystemName"`
BoundAt time.Time `json:"BoundAt"`
}

type SystemNameEnum uint

const (
wjh SystemNameEnum = 0
foru SystemNameEnum = 1
)
17 changes: 11 additions & 6 deletions app/model/user.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package model

import "time"
import (
"encoding/json"
"time"
)

type User struct {
StudentId string
UserId int `gorm:"primary_key;AUTO_INCREMENT"`
Password string
Email string
CreateTime time.Time
StudentId string
UserId int `gorm:"primary_key;AUTO_INCREMENT"`
Password string
Email string
Type uint8 // 0: 本科生 1: 研究生
BoundSystems json.RawMessage `gorm:"type:json"` // 绑定的系统
CreateTime time.Time
}
28 changes: 28 additions & 0 deletions app/services/userService/check.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package userService

import (
"encoding/json"
"time"
"usercenter/app/model"
"usercenter/app/utility"
"usercenter/config/database"
Expand All @@ -17,3 +19,29 @@ func CheckUserBYStudentIdAndPassword(studentId, password string) bool {
}).First(&user)
return result.Error == nil
}

func UpdateBoundSystem(studentId string, boundSystem uint8) error {
user, err := GetUserByStudentId(studentId)
if err != nil {
return err
}
var boundSystems []model.SystemBinding
if err := json.Unmarshal(user.BoundSystems, &boundSystems); err != nil {
return err
}
for _, system := range boundSystems {
if system.SystemName == model.SystemNameEnum(boundSystem) {
return nil
}
}
boundSystems = append(boundSystems, model.SystemBinding{
SystemName: model.SystemNameEnum(boundSystem),
BoundAt: time.Now(),
})
user.BoundSystems, err = json.Marshal(boundSystems)
if err != nil {
return err
}
result := database.DB.Save(user)
return result.Error
}
Loading