Skip to content

Commit

Permalink
Merge pull request #87 from Penryn/main
Browse files Browse the repository at this point in the history
refactor: 重构用户注册、登录和注销功能以适配修改后的用户中心接口
  • Loading branch information
XiMo-210 authored Dec 3, 2024
2 parents c823d5d + a64a608 commit 58fcc5b
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 22 deletions.
10 changes: 6 additions & 4 deletions app/controllers/userController/reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type createStudentUserForm struct {
StudentID string `json:"studentID" binding:"required"`
IDCardNumber string `json:"idCardNumber" binding:"required"`
Email string `json:"email" binding:"required"`
LoginType string `json:"type"`
Type uint `json:"type" ` // 用户类型 0-本科生 1-研究生
}
type createStudentUserWechatForm struct {
Username string `json:"username" binding:"required"`
Expand All @@ -26,7 +26,7 @@ type createStudentUserWechatForm struct {
IDCardNumber string `json:"idCardNumber" binding:"required"`
Email string `json:"email" binding:"required"`
Code string `json:"code" binding:"required"`
LoginType string `json:"type"`
Type uint `json:"type" ` // 用户类型 0-本科生 1-研究生
}

func BindOrCreateStudentUserFromWechat(c *gin.Context) {
Expand All @@ -52,7 +52,8 @@ func BindOrCreateStudentUserFromWechat(c *gin.Context) {
postForm.StudentID,
postForm.IDCardNumber,
postForm.Email,
session.OpenID)
session.OpenID,
postForm.Type)
if err != nil && err != apiException.ReactiveError {
_ = c.AbortWithError(200, err)
return
Expand Down Expand Up @@ -87,7 +88,8 @@ func CreateStudentUser(c *gin.Context) {
postForm.Password,
postForm.StudentID,
postForm.IDCardNumber,
postForm.Email)
postForm.Email,
postForm.Type)
if err != nil && err != apiException.ReactiveError {
_ = c.AbortWithError(200, err)
return
Expand Down
3 changes: 2 additions & 1 deletion app/services/userCenterServices/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ func Login(stu_id string, pass string) error {
}
Url.RawQuery = params.Encode()
urlPath := Url.String()
regMap := make(map[string]string)
regMap := make(map[string]any)
regMap["stu_id"] = stu_id
regMap["password"] = pass
regMap["bound_system"] = 0
resp, err := FetchHandleOfPost(regMap, userCenterApi.UserCenterApi(urlPath))
if err != nil {
return apiException.RequestError
Expand Down
5 changes: 4 additions & 1 deletion app/services/userCenterServices/del.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package userCenterServices

import (
"fmt"
"net/url"
"wejh-go/app/apiException"
"wejh-go/config/api/userCenterApi"
Expand All @@ -15,10 +16,12 @@ func DelAccount(stuID, iid string) error {
}
Url.RawQuery = params.Encode()
urlPath := Url.String()
regMap := make(map[string]string)
regMap := make(map[string]any)
regMap["stuid"] = stuID
regMap["iid"] = iid
regMap["bound_system"] = 0
resp, err := FetchHandleOfPost(regMap, userCenterApi.UserCenterApi(urlPath))
fmt.Println(resp)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions app/services/userCenterServices/reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import (
"wejh-go/config/api/userCenterApi"
)

func RegWithoutVerify(stu_id string, pass string, iid string, email string) error {
func RegWithoutVerify(stu_id string, pass string, iid string, email string, userType uint) error {
params := url.Values{}
Url, err := url.Parse(string(userCenterApi.UCRegWithoutVerify))
if err != nil {
return err
}
Url.RawQuery = params.Encode()
urlPath := Url.String()
regMap := make(map[string]string)
regMap := make(map[string]any)
regMap["stu_id"] = stu_id
regMap["password"] = pass
regMap["iid"] = iid
regMap["email"] = email
regMap["type"] = userType
regMap["bound_system"] = 0
resp, err := FetchHandleOfPost(regMap, userCenterApi.UserCenterApi(urlPath))
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion app/services/userCenterServices/repass.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func ResetPass(stuID, iid, password string) error {
}
Url.RawQuery = params.Encode()
urlPath := Url.String()
regMap := make(map[string]string)
regMap := make(map[string]any)
regMap["stuid"] = stuID
regMap["iid"] = iid
regMap["pwd"] = password
Expand Down
2 changes: 1 addition & 1 deletion app/services/userCenterServices/userCenterService.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type UserCenterResponse struct {
Data interface{} `json:"data"`
}

func FetchHandleOfPost(form map[string]string, url userCenterApi.UserCenterApi) (*UserCenterResponse, error) {
func FetchHandleOfPost(form map[string]any, url userCenterApi.UserCenterApi) (*UserCenterResponse, error) {
f := fetch.Fetch{}
f.Init()
res, err := f.PostJsonForm(userCenterApi.UserCenterHost+string(url), form)
Expand Down
10 changes: 5 additions & 5 deletions app/services/userServices/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"wejh-go/config/database"
)

func CreateStudentUser(username, password, studentID, IDCardNumber, email string) (*models.User, error) {
func CreateStudentUser(username, password, studentID, IDCardNumber, email string, userType uint) (*models.User, error) {
if CheckUsername(username) {
return nil, apiException.UserAlreadyExisted
}
err := userCenterServices.RegWithoutVerify(studentID, password, IDCardNumber, email)
err := userCenterServices.RegWithoutVerify(studentID, password, IDCardNumber, email, userType)
if err != nil && err != apiException.ReactiveError {
return nil, err
}
Expand All @@ -26,7 +26,7 @@ func CreateStudentUser(username, password, studentID, IDCardNumber, email string
user := &models.User{
JHPassword: pass,
Username: username,
Type: models.Undergraduate,
Type: models.UserType(userType),
StudentID: studentID,
LibPassword: "",
PhoneNum: "",
Expand All @@ -41,11 +41,11 @@ func CreateStudentUser(username, password, studentID, IDCardNumber, email string
return user, res.Error
}

func CreateStudentUserWechat(username, password, studentID, IDCardNumber, email, wechatOpenID string) (*models.User, error) {
func CreateStudentUserWechat(username, password, studentID, IDCardNumber, email, wechatOpenID string, userType uint) (*models.User, error) {
if CheckWechatOpenID(wechatOpenID) {
return nil, apiException.OpenIDError
}
user, err := CreateStudentUser(username, password, studentID, IDCardNumber, email)
user, err := CreateStudentUser(username, password, studentID, IDCardNumber, email, userType)
if err != nil && err != apiException.ReactiveError {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion app/services/userServices/del.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func DelAccount(user *models.User, iid string) error {

if err := userCenterServices.DelAccount(user.Username, iid); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions app/services/yxyServices/bindService.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func GetCaptchaImage(deviceId, securityToken string) (*string, error) {
}

func SendVerificationCode(securityToken, deviceId, phoneNum string) error {
form := make(map[string]string)
form := make(map[string]any)
form["phone_num"] = phoneNum
form["security_token"] = securityToken
form["captcha"] = ""
Expand Down Expand Up @@ -95,7 +95,7 @@ func SendVerificationCode(securityToken, deviceId, phoneNum string) error {
}

func LoginByCode(code, deviceId, phoneNum string) (*string, error) {
form := make(map[string]string)
form := make(map[string]any)
form["phone_num"] = phoneNum
form["code"] = code
form["device_id"] = deviceId
Expand All @@ -121,7 +121,7 @@ func LoginByCode(code, deviceId, phoneNum string) (*string, error) {
}

func SilentLogin(deviceId, uid, phoneNum string) error {
form := make(map[string]string)
form := make(map[string]any)
form["uid"] = uid
form["device_id"] = deviceId
form["phone_num"] = phoneNum
Expand Down
2 changes: 1 addition & 1 deletion app/services/yxyServices/yxyService.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type YxyResponse struct {
Data interface{} `json:"data"`
}

func FetchHandleOfPost(form map[string]string, url yxyApi.YxyApi) (*YxyResponse, error) {
func FetchHandleOfPost(form map[string]any, url yxyApi.YxyApi) (*YxyResponse, error) {
f := fetch.Fetch{}
f.Init()
res, err := f.PostJsonForm(yxyApi.YxyHost+string(url), form)
Expand Down
4 changes: 2 additions & 2 deletions app/utils/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (f *Fetch) PostForm(url string, requestData url.Values) ([]byte, error) {
return ioutil.ReadAll(response.Body)
}

func (f *Fetch) PostJsonFormRaw(url string, requestData map[string]string) (*http.Response, error) {
func (f *Fetch) PostJsonFormRaw(url string, requestData map[string]any) (*http.Response, error) {
bytesData, _ := json.Marshal(requestData)
request, _ := http.NewRequest("POST", url, bytes.NewReader(bytesData))
request.Header.Set("Content-Type", "application/json")
Expand All @@ -94,7 +94,7 @@ func (f *Fetch) PostJsonFormRaw(url string, requestData map[string]string) (*htt
return f.client.Do(request)
}

func (f *Fetch) PostJsonForm(url string, requestData map[string]string) ([]byte, error) {
func (f *Fetch) PostJsonForm(url string, requestData map[string]any) ([]byte, error) {
response, err := f.PostJsonFormRaw(url, requestData)
if err != nil {
return nil, err
Expand Down

0 comments on commit 58fcc5b

Please sign in to comment.