1 Star 0 Fork 0

dabolau / lakaigo

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
account.go 5.71 KB
Copy Edit Raw Blame History
dabolau authored 2020-10-20 15:48 . 添加授权方式
package controller
import (
"fmt"
"net/http"
"gitee.com/dabolau/lakaigo/common"
"gitee.com/dabolau/lakaigo/database"
"gitee.com/dabolau/lakaigo/model"
"github.com/gin-gonic/gin"
)
// RequestAccount 结构体
type RequestAccount struct {
model.User
NewPassword string `form:"newpassword" json:"newpassword"`
ConfirmPassword string `form:"confirmpassword" json:"confirmpassword"`
}
// AccountHandler 帐号信息
// https://gorm.io/zh_CN/docs/query.html
func AccountHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusOK,
"Message": "查询成功",
})
}
// LoginHandler 帐号登录
// https://gorm.io/zh_CN/docs/query.html
func LoginHandler(c *gin.Context) {
var account model.User
var requestAccount model.User
// 绑定前端数据
c.Bind(&requestAccount)
// 参数错误
if requestAccount.Username == "" || requestAccount.Password == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 查询数据
database.DB.Where(&model.User{
Username: requestAccount.Username,
Password: requestAccount.Password,
}).Or(&model.User{
Email: requestAccount.Username,
Password: requestAccount.Password,
}).Or(&model.User{
Phone: requestAccount.Username,
Password: requestAccount.Password,
}).First(&account)
// 登录成功
if account.ID > 0 {
// 获取令牌
token, _ := common.GenerateToken(account.Username)
c.JSON(http.StatusOK, gin.H{
"ID": account.ID,
"BearerToken": token,
"StatusCode": http.StatusOK,
"Message": "登录成功",
})
return
}
// 登录失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusNotFound,
"Message": "登录失败",
})
}
// RegisterHandler 帐号注册
// https://gorm.io/zh_CN/docs/query.html
func RegisterHandler(c *gin.Context) {
var account model.User
var requestAccount model.User
// 绑定前端数据
c.Bind(&requestAccount)
// 参数错误
if requestAccount.Username == "" || requestAccount.Email == "" || requestAccount.Phone == "" || requestAccount.Password == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 查询数据
database.DB.Where(&model.User{
Username: requestAccount.Username,
}).Or(&model.User{
Email: requestAccount.Email,
}).Or(&model.User{
Phone: requestAccount.Phone,
}).First(&account)
// 注册成功
if account.ID < 1 {
// 添加数据
database.DB.Create(&requestAccount)
c.JSON(http.StatusOK, gin.H{
"ID": requestAccount.ID,
"StatusCode": http.StatusOK,
"Message": "注册成功",
})
return
}
// 注册失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusNotFound,
"Message": "注册失败",
})
}
// ChangePasswordHandler 修改密码
// https://gorm.io/zh_CN/docs/query.html
func ChangePasswordHandler(c *gin.Context) {
var account model.User
var requestAccount RequestAccount
// 绑定前端数据
c.Bind(&requestAccount)
// 参数错误
if requestAccount.Username == "" || requestAccount.Password == "" || requestAccount.NewPassword == "" || requestAccount.ConfirmPassword == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 两次密码验证
if requestAccount.NewPassword != requestAccount.ConfirmPassword {
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusNotFound,
"Message": "两次密码不相同",
})
return
}
// 查询数据
database.DB.Where(&model.User{
Username: requestAccount.Username,
Password: requestAccount.Password,
}).Or(&model.User{
Email: requestAccount.Username,
Password: requestAccount.Password,
}).Or(&model.User{
Phone: requestAccount.Username,
Password: requestAccount.Password,
}).First(&account)
// 修改成功
if account.ID > 0 {
// 更新数据
account.Password = requestAccount.NewPassword
database.DB.Save(&account)
c.JSON(http.StatusOK, gin.H{
"ID": account.ID,
"StatusCode": http.StatusOK,
"Message": "修改成功",
})
return
}
// 修改失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusNotFound,
"Message": "修改失败",
})
}
// GetPasswordHandler 找回密码
// https://gorm.io/zh_CN/docs/query.html
func GetPasswordHandler(c *gin.Context) {
var account model.User
var requestAccount model.User
// 绑定前端数据
c.Bind(&requestAccount)
// 参数错误
if requestAccount.Email == "" {
c.JSON(http.StatusForbidden, gin.H{
"StatusCode": http.StatusForbidden,
"Message": "参数错误",
})
return
}
// 查询数据
database.DB.Where(&model.User{
Email: requestAccount.Email,
}).First(&account)
// 找回成功
if account.ID > 0 {
// 发送文本邮件
// err := common.SendTextEMail(account.Email, "找到您的信息", fmt.Sprintf("您的账号:[%v],密码:[%v],请及时修改。", account.Username, account.Password))
// 发送超文本标记邮件
err := common.SendHTMLEMail(account.Email, "找到您的信息", fmt.Sprintf(common.HTMLEMailTemplate, account.Username, account.Password))
if err != nil {
c.JSON(http.StatusOK, gin.H{
"Error": err,
"StatusCode": http.StatusNotFound,
"Message": "发送失败",
})
return
}
c.JSON(http.StatusOK, gin.H{
"ID": account.ID,
"StatusCode": http.StatusOK,
"Message": "找回成功",
})
return
}
// 找回失败
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusNotFound,
"Message": "找回失败",
})
}
// LogoutHandler 帐号注销
// https://gorm.io/zh_CN/docs/query.html
func LogoutHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"StatusCode": http.StatusOK,
"Message": "注销成功",
})
}
Go
1
https://gitee.com/dabolau/lakaigo.git
git@gitee.com:dabolau/lakaigo.git
dabolau
lakaigo
lakaigo
4b3cbd6f3e69

Search

53164aa7 5694891 3bd8fe86 5694891