From e9a95012bd8123379da2f4100fc267fd05923438 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sun, 15 Dec 2024 15:43:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=83=A8=E5=88=86=E9=94=99?= =?UTF-8?q?=E8=AF=AF=EF=BC=8C=E6=B7=BB=E5=8A=A0=E8=AF=B7=E6=B1=82=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=8F=8A=E7=A1=AE=E8=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/user.go | 9 +------- proto/conf.go | 3 ++- proto/user_req.go | 8 +++++++ service/userService.go | 37 ++++++++++++++++++++++++++++-- worker/req.go | 52 +++++++++++++++++++++++++++++++++++++++++- 5 files changed, 97 insertions(+), 12 deletions(-) diff --git a/handler/user.go b/handler/user.go index b54d2a3..776f86c 100644 --- a/handler/user.go +++ b/handler/user.go @@ -370,15 +370,8 @@ func registerHandler(c *gin.Context) { return } -type SyncUserReq struct { - Token string `json:"token" form:"token"` - Types int `json:"type" form:"type"` // 1为全量同步 2为增量同步 - Device string `json:"device" form:"device"` - Confirm proto.UserSyncConfirm `json:"confirm" form:"confirm"` -} - func GetSyncUserInfo(c *gin.Context) { - var req_data SyncUserReq + var req_data proto.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"}) diff --git a/proto/conf.go b/proto/conf.go index 359020f..e6d5670 100644 --- a/proto/conf.go +++ b/proto/conf.go @@ -9,7 +9,7 @@ import ( var Config ConfigStruct var SigningKey = []byte{} -var Url_map = map[string]bool{"/login": true, "/register": true, "/uuid": true, "/gqr": true, "/cid/callback": true, "/tool/monitor": true} // 不需要token验证的url +var Url_map = map[string]bool{"/login": true, "/register": true, "/uuid": true, "/gqr": true, "/cid/callback": true, "/tool/monitor": true, "/user/sync": true} // 不需要token验证的url var Per_menu_map = map[string]int{"/video/": 1, "/device/": 2, "/cid/": 3} const ( @@ -80,6 +80,7 @@ type ConfigStruct struct { SERVER_USER_TYPE string `json:"user_type"` // 服务器用户类型,master: 主服务器,slave: 从服务器,从服务器会定时同步数据 MASTER_SERVER_DOMAIN string `json:"master_server_domain"` // 主服务器域名 USER_SYNC_TIME int `json:"user_sync_time"` // 用户数据同步时间,单位秒 + SERVER_NAME string `json:"server_name"` // 服务器名称,用于区分不同服务器 } // 读取配置文件 diff --git a/proto/user_req.go b/proto/user_req.go index f3b9435..9d06f03 100644 --- a/proto/user_req.go +++ b/proto/user_req.go @@ -76,3 +76,11 @@ type UserAddOrUpdate struct { CreateTime string `json:"CreateTime" form:"CreateTime"` UpdateTime string `json:"UpdateTime" form:"UpdateTime"` } + +// 数据同步请求 +type SyncUserReq struct { + Token string `json:"token" form:"token"` + Types int `json:"type" form:"type"` // 1为全量同步 2为增量同步 + Device string `json:"device" form:"device"` + Confirm UserSyncConfirm `json:"confirm" form:"confirm"` +} diff --git a/service/userService.go b/service/userService.go index b45ea04..c696753 100644 --- a/service/userService.go +++ b/service/userService.go @@ -96,9 +96,20 @@ func DeleteUserService(id, user_id int) int { } func UserSyncDataFromMaster() { //从接口获取数据 - url := "http://" + proto.Config.MASTER_SERVER_DOMAIN + "/user/sync" + url := "https://" + proto.Config.MASTER_SERVER_DOMAIN + "/user/sync" tokens := worker.GetRedisSetMembers("super_permission_tokens") - user_sync_data := worker.SyncDataFromMasterReq(url, tokens[0]) + var req proto.SyncUserReq + req.Token = tokens[0] + req.Device = proto.Config.SERVER_NAME + all := worker.GetRedis("user_sync_all") + if all == "" || all == "1" { + req.Types = 1 + } else { + worker.SetRedis("user_sync_all", "2") + req.Types = 2 + } + + user_sync_data := worker.SyncDataFromMasterReq2(url, req) add_users := user_sync_data.Add update_users := user_sync_data.Update delete_users := user_sync_data.Delete @@ -106,26 +117,48 @@ func UserSyncDataFromMaster() { var fail_ids []uint //添加用户 + var add_confirm []proto.UserConfirmID for _, v := range add_users { res := dao.AddUserSync(v) if res == 0 { fail_ids = append(fail_ids, v.ID) + } else { + add_confirm = append(add_confirm, proto.UserConfirmID{ID: v.ID}) } } //更新用户 + var update_confirm []proto.UserConfirmID for _, v := range update_users { res := dao.UpdateUserSync(v) if res == 0 { fail_ids = append(fail_ids, v.ID) + } else { + update_confirm = append(update_confirm, proto.UserConfirmID{ID: v.ID}) } } //删除用户 + var delete_confirm []proto.UserConfirmID for _, v := range delete_users { res := dao.DeleteUserSync(v) if res == 0 { fail_ids = append(fail_ids, v.ID) + } else { + delete_confirm = append(delete_confirm, proto.UserConfirmID{ID: v.ID}) } } + + //确认同步数据 + var data proto.UserSyncConfirm + data.Add = add_confirm + data.Update = update_confirm + data.Delete = delete_confirm + //确认同步数据请求 + var confirm_req proto.SyncUserReq + confirm_req.Token = tokens[0] + confirm_req.Device = proto.Config.SERVER_NAME + confirm_req.Types = 3 + confirm_req.Confirm = data + worker.SyncDataFromMasterReq2(url, confirm_req) } // 同步数据到主服务器-增删改数据 diff --git a/worker/req.go b/worker/req.go index 0b42692..fcbefd4 100644 --- a/worker/req.go +++ b/worker/req.go @@ -91,7 +91,17 @@ func SyncDataFromMasterReq(url string, token string) proto.UserSync { return proto.UserSync{} } req.Header.Set("token", token) - client := &http.Client{} + //json负载 + req.Header.Set("Content-Type", "application/json") + //传输数据 + m := make(map[string]interface{}) + m["token"] = token + m["device"] = "" + + if client == nil { + client = &http.Client{} + } + client = &http.Client{} //获取数据 resp, err := client.Do(req) if err != nil { @@ -118,3 +128,43 @@ func SyncDataFromMasterReq(url string, token string) proto.UserSync { } return userSync } + +// 获取数据,全量及增量 +func SyncDataFromMasterReq2(url string, data proto.SyncUserReq) proto.UserSync { + var res proto.UserSync + //从接口获取数据 + json_data, err := json.Marshal(data) + if err != nil { + return res + } + req, err := http.NewRequest("POST", url, bytes.NewBuffer(json_data)) + if err != nil { + return res + } + req.Header.Set("Content-Type", "application/json") + //传输数据 + if client == nil { + client = &http.Client{} + } + //获取数据 + resp, err := client.Do(req) + if err != nil { + return res + } + defer resp.Body.Close() + //解析数据 + var m map[string]interface{} + err = json.NewDecoder(resp.Body).Decode(&m) + if err != nil { + return res + } + if m["code"].(float64) != 0 { + return res + } + err = json.Unmarshal([]byte(m["data"].(string)), &res) + if err != nil { + fmt.Println("SyncDataFromMasterReq2 error decode data:", err) + return res + } + return res +}