123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- package controllers
- import (
- "fmt"
- "net/http"
- "regexp"
- "xugu_license/internal/api"
- "xugu_license/internal/global"
- middlewares "xugu_license/internal/middleware"
- "xugu_license/internal/models"
- "xugu_license/internal/utils"
- tokens "xugu_license/internal/utils/token"
- "github.com/gin-gonic/gin"
- "golang.org/x/crypto/bcrypt"
- )
- // /api/register的请求体
- type ReqRegister struct {
- Username string `json:"username" binding:"required"`
- Account string `json:"Account" binding:"required"`
- Password string `json:"password" binding:"required"`
- TELEPHONE string `json:"TELEPHONE" binding:"required"`
- EMAIL string `json:"EMAIL" binding:"required"`
- }
- func Register(c *gin.Context) {
- var req ReqRegister
- if err := c.ShouldBindBodyWithJSON(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": err.Error(),
- })
- return
- }
- //TODO判断密码,邮箱,电话 合法性
- //定义正则表达式,匹配仅包含字母和数字的字符串
- reg := regexp.MustCompile("^[a-zA-Z0-9]+$")
- if !reg.MatchString(req.Account) {
- global.Logger.Errorln("账户仅能包含字母和数字 ")
- c.JSON(http.StatusBadRequest, gin.H{
- "error": "账户仅能包含字母和数字",
- })
- return
- }
- if len(req.Password) < 6 {
- global.Logger.Errorln("密码长度不能小于6位 ")
- c.JSON(http.StatusBadRequest, gin.H{
- "error": "密码长度不能小于6位",
- })
- return
- }
- if len(req.TELEPHONE) != 11 {
- global.Logger.Errorln("电话号码长度不正确 ")
- c.JSON(http.StatusBadRequest, gin.H{
- "error": "电话号码长度不正确",
- })
- return
- }
- // if len(req.EMAIL) < 6 {
- // c.JSON(http.StatusBadRequest, gin.H{
- // "error": "邮箱长度不能小于6位",
- // })
- // return
- // }
- //TODO判断用户名,邮箱,电话 是否重复
- userTmp, err := models.CheckEmailOrTelphoneOrAccount(req.Username, req.Account, req.EMAIL, req.TELEPHONE)
- if err != nil {
- global.Logger.Errorln("数据库查询重复用户失败 ", err.Error())
- c.JSON(http.StatusBadRequest, gin.H{
- "error": fmt.Sprintln("数据库查询重复用户失败: ", err.Error()),
- })
- return
- }
- if userTmp.Username == req.Username {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": "用户名重复",
- })
- return
- }
- //fmt.Printf("userTmp: %#v ", userTmp)
- if userTmp.Account == req.Account {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": "账号重复",
- })
- return
- }
- if userTmp.Email == req.EMAIL {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": "邮箱重复",
- })
- return
- }
- if userTmp.Telephone == req.TELEPHONE {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": "电话重复",
- })
- return
- }
- uId := utils.GenerateShortIdentifier(req.Account, req.EMAIL, req.TELEPHONE)
- //加密密码
- hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
- if err != nil {
- return
- }
- req.Password = string(hashedPassword)
- _, err = global.XuguDB.Exec("INSERT INTO lic_USER (UNIQUEID,username,Account, password,TELEPHONE,email) VALUES (?, ?,?,?,?,?)", uId, req.Username, req.Account, req.Password, req.TELEPHONE, req.EMAIL)
- if err != nil {
- global.Logger.Errorln("数据库创建新用户插入失败 ", err.Error())
- c.JSON(http.StatusBadRequest, gin.H{
- "error": fmt.Sprintln("创建用户失败: ", err.Error()),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": "register success",
- "data": req,
- })
- }
- // api/login 的请求体
- type ReqLogin struct {
- Account string `json:"Account" binding:"required"`
- Password string `json:"password" binding:"required"`
- }
- func Login(c *gin.Context) {
- var req ReqLogin
- if err := c.ShouldBindBodyWithJSON(&req); err != nil {
- global.Logger.Errorln("解析请求失败 ", err.Error())
- c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintln("解析请求失败: ", err.Error())})
- return
- }
- token, err := models.LoginCheck(req.Account, req.Password)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": "账号或密码不对",
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "token": token,
- })
- }
- func GetUserInfo(c *gin.Context) {
- // 从token中解析出user_id
- userClaims, err := tokens.ExtractTokenID(c)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": err.Error(),
- })
- return
- }
- u, err := models.GetUserInfoByID(userClaims.UserId)
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": err.Error(),
- })
- return
- }
- fmt.Println("sdsad u", u)
- c.JSON(http.StatusOK, gin.H{
- "message": "success",
- "data": u,
- })
- }
- func GetAllUserInfo(c *gin.Context) {
- // 从token中解析出user_id
- u, err := models.GetAllUser()
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": err.Error(),
- })
- return
- }
- fmt.Println("sdsad u", u)
- type allUserInfo struct {
- }
- c.JSON(http.StatusOK, gin.H{
- "message": "success",
- "data": u,
- })
- }
- func UpdateUserInfo(c *gin.Context) {
- //获取用户信息和权限
- userInfo, err := getLoginInfo(c)
- if err != nil {
- c.JSON(http.StatusUnauthorized, gin.H{"error": "用户信息不存在"})
- c.Abort()
- }
- var req api.UserInfoRequest
- if err := c.ShouldBindBodyWithJSON(&req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": err.Error(),
- })
- return
- }
- if userInfo.Permission[middlewares.UpdateRole] != middlewares.UpdateRole {
- req.Role = userInfo.Role
- }
- if req.Password != "" {
- //加密密码
- hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
- if err != nil {
- return
- }
- req.Password = string(hashedPassword)
- if err := models.UpdateUserInfo(req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": err.Error(),
- })
- }
- } else {
- if err := models.UpdateUserInfo(req); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": err.Error(),
- })
- }
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "用户信息更新成功",
- })
- }
- func DeleteUser(c *gin.Context) {
- type ReqDeleteUser struct {
- UniqueID string `json:"UniqueID" `
- }
- var UniqueID ReqDeleteUser
- if err := c.ShouldBindBodyWithJSON(&UniqueID); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": fmt.Sprintln("解析请求失败: ", err.Error()),
- })
- return
- }
- if err := models.DeleteUserInfo(UniqueID.UniqueID); err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": fmt.Sprintln("数据库操作失败: ", err.Error()),
- })
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "已删除用户",
- })
- }
|