updata
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
"userNameDup":-4,
|
||||
"userNameNoFund":-41,
|
||||
"userPassIncorrect":-42,
|
||||
"userEmailFormatError":-43,
|
||||
"userCookieError":-44,
|
||||
"userCookieNotFund":-44,
|
||||
"userCookieExpired":-44
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"regexp"
|
||||
|
||||
"time"
|
||||
)
|
||||
@@ -81,3 +82,11 @@ func CheckCookiesAndUpdate(cookie *TabCookie_) bool {
|
||||
}
|
||||
//return false
|
||||
}
|
||||
|
||||
// 判断邮箱是否合法
|
||||
func IsEmailValid(email string) bool {
|
||||
// 正则表达式(覆盖 99% 常见邮箱格式)
|
||||
pattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
|
||||
regex := regexp.MustCompile(pattern)
|
||||
return regex.MatchString(email)
|
||||
}
|
||||
|
||||
@@ -71,6 +71,15 @@ type From_user_updateinfo struct {
|
||||
Birthday string `json:"birthday"`
|
||||
}
|
||||
|
||||
type From_user_changeemail struct {
|
||||
Newemail string `json:"newemail"`
|
||||
}
|
||||
|
||||
type From_user_changepass struct {
|
||||
Oldpass string `json:"oldpass"`
|
||||
Newpass string `json:"newpass"`
|
||||
}
|
||||
|
||||
func AuthenticationAuthority(ctx *gin.Context) (bool, models.TabUser_, map[string]interface{}) {
|
||||
var user models.TabUser_
|
||||
|
||||
@@ -118,6 +127,64 @@ func ApiUser(r *gin.RouterGroup) {
|
||||
r.POST("/test", func(ctx *gin.Context) {
|
||||
ReturnJson(ctx, "apiOK", nil)
|
||||
})
|
||||
|
||||
//修改用户密码
|
||||
r.POST("/changePassword", func(ctx *gin.Context) {
|
||||
isAuth, user, data := AuthenticationAuthority(ctx)
|
||||
if isAuth {
|
||||
var jsonData From_user_changepass
|
||||
if err := mapstructure.Decode(data, &jsonData); err == nil {
|
||||
//验证旧密码
|
||||
fmt.Println(user)
|
||||
//转换旧密码
|
||||
olduser := models.TabUser_{
|
||||
Pass: jsonData.Oldpass,
|
||||
Salt: user.Salt,
|
||||
}
|
||||
models.HashUserPass(&olduser)
|
||||
if olduser.Pass == user.Pass {
|
||||
//旧密码正确,更新新密码
|
||||
var userupdate models.TabUser_
|
||||
userupdate.Pass = jsonData.Newpass
|
||||
userupdate.Salt = models.RandStr32()
|
||||
models.HashUserPass(&userupdate)
|
||||
models.DB.Model(&user).Updates(&userupdate)
|
||||
ReturnJson(ctx, "apiOK", nil)
|
||||
} else {
|
||||
//旧密码错误
|
||||
ReturnJson(ctx, "userPassIncorrect", nil)
|
||||
}
|
||||
|
||||
} else {
|
||||
ReturnJson(ctx, "jsonErr", nil)
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
//更新用户邮箱
|
||||
r.POST("/changeEmail", func(ctx *gin.Context) {
|
||||
isAuth, user, data := AuthenticationAuthority(ctx)
|
||||
if isAuth {
|
||||
var jsonData From_user_changeemail
|
||||
if err := mapstructure.Decode(data, &jsonData); err == nil {
|
||||
//判断新邮箱格式
|
||||
if models.IsEmailValid(jsonData.Newemail) {
|
||||
var userupdate models.TabUser_
|
||||
userupdate.Email = jsonData.Newemail
|
||||
models.DB.Model(&user).Updates(&userupdate)
|
||||
ReturnJson(ctx, "apiOK", nil)
|
||||
} else {
|
||||
ReturnJson(ctx, "userEmailFormatError", nil)
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
ReturnJson(ctx, "jsonErr", nil)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
//更新用户info
|
||||
r.POST("/updateInfo", func(ctx *gin.Context) {
|
||||
isAuth, user, data := AuthenticationAuthority(ctx)
|
||||
|
||||
Reference in New Issue
Block a user