用户数据同步部分
This commit is contained in:
parent
4918c54fc6
commit
29e8151420
|
|
@ -128,3 +128,10 @@ func DeleteUserSync(req proto.UserDelID) uint {
|
||||||
}
|
}
|
||||||
return req.ID
|
return req.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取所有用户
|
||||||
|
func GetAllUser() []proto.User {
|
||||||
|
var users []proto.User
|
||||||
|
DB.Find(&users)
|
||||||
|
return users
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ func SetUpUserGroup(router *gin.Engine) {
|
||||||
userGroup.POST("/search", SearchHandler)
|
userGroup.POST("/search", SearchHandler)
|
||||||
userGroup.POST("/info", GetUserInfo)
|
userGroup.POST("/info", GetUserInfo)
|
||||||
userGroup.POST("/update", UpdateUserInfo)
|
userGroup.POST("/update", UpdateUserInfo)
|
||||||
|
userGroup.POST("/sync", GetSyncUserInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
type RLReq struct {
|
type RLReq struct {
|
||||||
|
|
@ -350,3 +351,44 @@ func registerHandler(c *gin.Context) {
|
||||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": data})
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": data})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SyncUserReq struct {
|
||||||
|
token string `json:"token" form:"token"`
|
||||||
|
types int `json:"type" form:"type"` // 1为全量同步 2为增量同步
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSyncUserInfo(c *gin.Context) {
|
||||||
|
var req_data SyncUserReq
|
||||||
|
if err := c.ShouldBind(&req_data); err == nil {
|
||||||
|
if req_data.token == "" {
|
||||||
|
c.JSON(200, gin.H{"code": proto.ParameterError, "message": "error", "data": "token is empty"})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
if worker.IsContainSet("super_permission_tokens", req_data.token) {
|
||||||
|
if proto.Config.SERVER_USER_TYPE == "master" {
|
||||||
|
if req_data.types == 1 {
|
||||||
|
add_users := dao.GetAllUser()
|
||||||
|
resp := proto.UserSyncResp{}
|
||||||
|
resp.Add = add_users
|
||||||
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": resp})
|
||||||
|
} else if req_data.types == 2 {
|
||||||
|
res := service.GetUserSyncData()
|
||||||
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": res})
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"code": proto.ParameterError, "message": "type is error", "data": proto.UserSyncResp{}})
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"code": proto.NoPermission, "message": "no permission,server is not master", "data": proto.UserSync{}})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"code": proto.NoPermission, "message": "error", "data": "no permission"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "error"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package proto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
"videoplayer/dao"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UpdateUserInfoReq struct {
|
type UpdateUserInfoReq struct {
|
||||||
|
|
@ -33,6 +34,13 @@ type CronInfo struct {
|
||||||
Every int `json:"every" form:"every"` //每隔多少秒执行一次,小于等于0表示不执行,时间粒度为10s
|
Every int `json:"every" form:"every"` //每隔多少秒执行一次,小于等于0表示不执行,时间粒度为10s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 用户数据同步
|
||||||
|
type UserSyncResp struct {
|
||||||
|
Update []dao.User `json:"update" form:"update"` //更新用户
|
||||||
|
Add []dao.User `json:"add" form:"add"` //添加用户
|
||||||
|
Delete []UserDelID `json:"delete" form:"delete"` //删除用户
|
||||||
|
}
|
||||||
|
|
||||||
// 用户数据同步
|
// 用户数据同步
|
||||||
type UserSync struct {
|
type UserSync struct {
|
||||||
Update []UserAddOrUpdate `json:"update" form:"update"` //更新用户
|
Update []UserAddOrUpdate `json:"update" form:"update"` //更新用户
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"videoplayer/dao"
|
"videoplayer/dao"
|
||||||
"videoplayer/proto"
|
"videoplayer/proto"
|
||||||
"videoplayer/worker"
|
"videoplayer/worker"
|
||||||
|
|
@ -90,3 +91,28 @@ func UserSyncDataFromMaster() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 同步数据到主服务器-增删改数据
|
||||||
|
func GetUserSyncData() proto.UserSyncResp {
|
||||||
|
add_user_ids := worker.GetRedisSetMembers("sync_add_user_ids")
|
||||||
|
update_user_ids := worker.GetRedisSetMembers("sync_update_user_ids")
|
||||||
|
delete_user_ids := worker.GetRedisSetMembers("sync_delete_user_ids")
|
||||||
|
add_users := []dao.User{}
|
||||||
|
update_users := []dao.User{}
|
||||||
|
delete_users := []proto.UserDelID{}
|
||||||
|
for _, v := range add_user_ids {
|
||||||
|
id, _ := strconv.Atoi(v)
|
||||||
|
user := dao.FindUserByUserID(id)
|
||||||
|
add_users = append(add_users, user)
|
||||||
|
}
|
||||||
|
for _, v := range update_user_ids {
|
||||||
|
id, _ := strconv.Atoi(v)
|
||||||
|
user := dao.FindUserByUserID(id)
|
||||||
|
update_users = append(update_users, user)
|
||||||
|
}
|
||||||
|
for _, v := range delete_user_ids {
|
||||||
|
id, _ := strconv.Atoi(v)
|
||||||
|
delete_users = append(delete_users, proto.UserDelID{ID: uint(id)})
|
||||||
|
}
|
||||||
|
return proto.UserSyncResp{Add: add_users, Update: update_users, Delete: delete_users}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue