-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
470 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package api | ||
|
||
import ( | ||
"cmdb-ops-flow/models" | ||
"cmdb-ops-flow/service" | ||
"cmdb-ops-flow/utils/msg" | ||
"cmdb-ops-flow/utils/result" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
func AddUser(c *gin.Context) { | ||
var data models.User | ||
if err := c.ShouldBindJSON(&data); err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
if data.Username == "" || data.Password == "" { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, nil, msg.GetErrMsg(msg.ERROR_USER_NO_PASSWD))) | ||
return | ||
} | ||
code := models.CheckUser(data.Username) | ||
if code == msg.SUCCSE { | ||
service.AddUser(data) | ||
} | ||
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, data, msg.GetErrMsg(code))) | ||
|
||
} | ||
|
||
func DelUser(c *gin.Context) { | ||
var data models.User | ||
if err := c.ShouldBindJSON(&data); err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
code := models.DelUser(data.Userid) | ||
|
||
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, data, msg.GetErrMsg(code))) | ||
|
||
} | ||
|
||
func GetUser(c *gin.Context) { | ||
var data models.User | ||
if err := c.ShouldBindJSON(&data); err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
list, err := service.GetUserList(data) | ||
if err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
code := 200 | ||
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code))) | ||
|
||
} | ||
func EditUser(c *gin.Context) { | ||
var data models.User | ||
if err := c.ShouldBindJSON(&data); err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
list, err := service.EditUser(data) | ||
if err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR))) | ||
return | ||
} | ||
code := 200 | ||
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code))) | ||
|
||
} | ||
func Login(c *gin.Context) { | ||
var json models.User | ||
if err := c.ShouldBindJSON(&json); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
data, err := service.Login(json) | ||
if err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR_USER_NO_LOGIN, err.Error(), msg.GetErrMsg(msg.ERROR_USER_NO_LOGIN))) | ||
return | ||
} | ||
code := 200 | ||
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, data, msg.GetErrMsg(code))) | ||
} | ||
|
||
func Info(c *gin.Context) { | ||
token := c.MustGet("token").(string) | ||
data, err := service.Info(token) | ||
if err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR_USER_NO_LOGIN, err.Error(), msg.GetErrMsg(msg.ERROR_USER_NO_LOGIN))) | ||
return | ||
} | ||
code := 200 | ||
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, data, msg.GetErrMsg(code))) | ||
} | ||
|
||
func Logout(c *gin.Context) { | ||
token := c.MustGet("token").(string) | ||
err := service.Logout(token) | ||
if err != nil { | ||
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR_USER_NO_LOGOUT, err.Error(), msg.GetErrMsg(msg.ERROR_USER_NO_LOGOUT))) | ||
return | ||
} | ||
code := 200 | ||
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, "退出登录成功", msg.GetErrMsg(code))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package middleware | ||
|
||
import ( | ||
"cmdb-ops-flow/models" | ||
"cmdb-ops-flow/utils/result" | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func Token() gin.HandlerFunc { | ||
return func(context *gin.Context) { | ||
token := context.Request.Header.Get("X-Token") | ||
fmt.Println("Token value:", token) // 在这里打印 Token 的值 | ||
fmt.Println(token) | ||
token_exsits, err := models.TokenInfo(token) | ||
if err != nil { | ||
resospnseWithError(401, "非法请求", context) | ||
return | ||
} | ||
fmt.Println(token_exsits) | ||
if len(token_exsits) != 0 { | ||
//先做时间判断 | ||
target_time, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Time(token_exsits[0].ExpirationAt).Format("2006-01-02 15:04:05"), time.Local) //需要加上time.Local不然会自动加八小时 | ||
if target_time.Unix() <= time.Now().Unix() { | ||
fmt.Println("过期报错") | ||
//过期报错 | ||
resospnseWithError(401, "timeout", context) | ||
return | ||
} | ||
//token没过期,更新到期时间 | ||
now := time.Unix(time.Now().Unix()+7200, 0).Format("2006-01-02 15:04:05") | ||
err = models.UpdateTokenTime(token, now) | ||
context.Set("name", token_exsits[0].Username) | ||
context.Set("avatar", token_exsits[0].Avatar) | ||
context.Set("token", token) | ||
} else { | ||
fmt.Println("没了") | ||
resospnseWithError(401, "已退出", context) | ||
return | ||
} | ||
|
||
context.Next() | ||
} | ||
} | ||
|
||
func resospnseWithError(code int, message string, c *gin.Context) { | ||
var res result.Result | ||
res.Code = code | ||
res.Msg = message | ||
c.JSON(200, res) //前端返回也要返回200才能拦截 | ||
c.JSON(http.StatusOK, res) | ||
c.Abort() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package models | ||
|
||
import ( | ||
"cmdb-ops-flow/utils/msg" | ||
"time" | ||
) | ||
|
||
type User struct { | ||
ID int `gorm:"primary_key"` | ||
Userid int64 `gorm:"type:bigint;not null" json:"userid" validate:"required"` | ||
Username string `json:"username" db:"username" form:"username" ` // Make username required | ||
Password string `json:"password" db:"password" form:"password" ` // Make password required | ||
|
||
Avatar string `json:"avatar" db:"avatar" form:"avatar"` | ||
Token string | ||
Role int `gorm:"type:int;DEFAULT:2" json:"role" validate:"required,gte=2" label:"角色码"` | ||
ExpirationAt time.Time `gorm:"default:CURRENT_TIMESTAMP",json:"created_at"` | ||
} | ||
|
||
func AddUser(user User) (interface{}, error) { | ||
|
||
err := db.Create(&user).Error | ||
return user, err | ||
} | ||
func EditUser(user User) (interface{}, error) { | ||
err := db.Select("id").Where("username = ? AND userid = ?", user.Username, user.Userid).First(&user).Error | ||
if err != nil { | ||
return msg.ERROR_USERNAME_USED, err | ||
} | ||
err = db.Model(&user).Where("username = ? AND userid = ?", user.Username, user.Userid).Updates(user).Error | ||
if err != nil { | ||
return user, err | ||
} | ||
|
||
return user, err | ||
} | ||
|
||
func DelUser(name int64) (code int) { | ||
var user User | ||
db.Select("id").Where("userid = ?", name).First(&user) | ||
if user.ID > 0 { | ||
err = db.Where("userid = ?", name).Delete(&user).Error | ||
if err != nil { | ||
return msg.ERROR_USER_NOT_EXIST | ||
} | ||
return msg.SUCCSE | ||
} else { | ||
return msg.ERROR | ||
} | ||
|
||
} | ||
func GetUserList(id int) ([]User, error) { | ||
var list []User | ||
if id != 0 { | ||
res := db.Debug().Where("id = ?", id).Find(&list) | ||
return list, res.Error | ||
} else { | ||
res := db.Debug().Find(&list) | ||
return list, res.Error | ||
} | ||
} | ||
func CheckUser(name string) (code int) { | ||
var user User | ||
db.Select("id").Where("username = ?", name).First(&user) | ||
if user.ID > 0 { | ||
return msg.ERROR_USERNAME_USED | ||
} | ||
return msg.SUCCSE | ||
} | ||
func Login(name string, password string) (list []User, err error) { | ||
var user []User | ||
db.Debug().Where("username = ? and password = ?", name, password).First(&user) | ||
|
||
return user, nil | ||
} | ||
func LoginCreateToken(name string, password string, token string, expiration_at string) (err error) { | ||
return db.Debug().Table("user").Where("username = ? and password = ?", name, password).Updates(map[string]interface{}{"token": token, "expiration_at": expiration_at}).Error | ||
} | ||
func TokenInfo(token string) (list []User, err error) { | ||
var user []User | ||
db.Debug().Where("token = ? ", token).First(&user) | ||
return user, nil | ||
} | ||
func UpdateTokenTime(token string, expiration_at string) (err error) { | ||
return db.Debug().Table("user").Where("token = ? ", token).Updates(map[string]interface{}{"expiration_at": expiration_at}).Error | ||
} | ||
|
||
func Info(token string) (data interface{}, err error) { | ||
type Result struct { | ||
Username string | ||
Avatar string | ||
} | ||
var result Result | ||
db.Debug().Table("user").Select("username, avatar").Where("token = ? ", token).Scan(&result) | ||
return result, nil | ||
} | ||
func Logout(token string) (err error) { | ||
return db.Debug().Table("user").Where("token = ? ", token).Updates(map[string]interface{}{"token": ""}).Error | ||
} | ||
|
||
//// CheckUser 查询密码是否存在 | ||
//func Check_Login(user User) (code int, err error) { | ||
// err = db.Select("id").Where("username = ? AND password = ?", user.Username, user.Password).First(&user).Error | ||
// if err != nil { | ||
// if errors.Is(err, gorm.ErrRecordNotFound) { | ||
// return 500, err | ||
// } | ||
// return 500, err | ||
// } | ||
// return msg.SUCCSE, err | ||
//} |
Oops, something went wrong.