增加role表和orm

pull/30/head
taoshihan1991 5 years ago
parent cf05552796
commit 5655facbaa

@ -49,3 +49,11 @@ CREATE TABLE `message` (
KEY `kefu_id` (`kefu_id`),
KEY `visitor_id` (`visitor_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
DROP TABLE IF EXISTS `user_role`;
CREATE TABLE `user_role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL DEFAULT '0',
`role_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

@ -25,12 +25,15 @@ func CheckPass(username string, password string) string {
}
return ""
}
func CheckKefuPass(username string, password string) (models.User,bool) {
func CheckKefuPass(username string, password string) (models.User,models.User_role,bool) {
info:=models.FindUser(username)
var uRole models.User_role
if info.Name==""||info.Password!=tools.Md5(password){
return info,false
return info,uRole,false
}
return info,true
uRole=models.FindRoleByUserId(info.ID)
return info,uRole,true
}
func AuthLocal(username string, password string) string {
account := config.GetAccount()

@ -101,7 +101,6 @@ func sendPingToClient() {
}
go func() {
for {
log.Println("check online users...")
str, _ := json.Marshal(msg)
for uid, user := range clientList {
err := user.conn.WriteMessage(websocket.TextMessage,str)

@ -35,7 +35,7 @@ func LoginCheckPass(c *gin.Context) {
return
}
case "kefulogin":
info,ok:=CheckKefuPass(username, password)
info,uRole,ok:=CheckKefuPass(username, password)
userinfo:= make(map[string]interface{})
if !ok{
c.JSON(200, gin.H{
@ -47,6 +47,11 @@ func LoginCheckPass(c *gin.Context) {
userinfo["name"] = info.Name
userinfo["kefu_id"] = info.ID
userinfo["type"] = "kefu"
if uRole.RoleId!=0 {
userinfo["role_id"] =uRole.RoleId
}else{
userinfo["role_id"]=2
}
userinfo["create_time"] = time.Now().Unix()
token, _ := tools.MakeToken(userinfo)

@ -3,7 +3,6 @@ package middleware
import (
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/tools"
"log"
"time"
)
func JwtPageMiddleware(c *gin.Context){
@ -18,13 +17,13 @@ func JwtPageMiddleware(c *gin.Context){
func JwtApiMiddleware(c *gin.Context){
token := c.GetHeader("token")
userinfo := tools.ParseToken(token)
log.Println(userinfo)
if userinfo == nil||userinfo["name"]==nil {
if userinfo == nil||userinfo["name"]==nil||userinfo["create_time"]==nil {
c.JSON(200, gin.H{
"code": 400,
"msg": "验证失败",
})
c.Abort()
return
}
createTime:=int64(userinfo["create_time"].(float64))
var expire int64=2*60*60
@ -40,5 +39,6 @@ func JwtApiMiddleware(c *gin.Context){
if userinfo["type"]=="kefu"{
c.Set("kefu_id",userinfo["kefu_id"])
c.Set("kefu_name",userinfo["name"])
c.Set("role_id",userinfo["role_id"])
}
}

@ -0,0 +1,10 @@
package models
type User_role struct{
UserId string `json:"user_id"`
RoleId uint `json:"role_id"`
}
func FindRoleByUserId(userId interface{})User_role{
var uRole User_role
DB.Where("user_id = ?", userId).First(&uRole)
return uRole
}
Loading…
Cancel
Save