diff --git a/cmd/open_im_api/main.go b/cmd/open_im_api/main.go index aa49378b1..1f7f88c09 100644 --- a/cmd/open_im_api/main.go +++ b/cmd/open_im_api/main.go @@ -31,9 +31,9 @@ func main() { friendRouterGroup := r.Group("/friend") { // friendRouterGroup.POST("/get_friends_info", friend.GetFriendsInfo) - friendRouterGroup.POST("/add_friend", friend.AddFriend) + friendRouterGroup.POST("/add_friend", friend.AddFriend) //1 friendRouterGroup.POST("/get_friend_apply_list", friend.GetFriendApplyList) - friendRouterGroup.POST("/get_self_apply_list", friend.GetSelfApplyList) + friendRouterGroup.POST("/get_self_apply_list", friend.GetSelfApplyList) //1 friendRouterGroup.POST("/get_friend_list", friend.GetFriendList) friendRouterGroup.POST("/add_blacklist", friend.AddBlacklist) friendRouterGroup.POST("/get_blacklist", friend.GetBlacklist) diff --git a/internal/api/friend/friend.go b/internal/api/friend/friend.go index 85ec67e5e..040402f28 100644 --- a/internal/api/friend/friend.go +++ b/internal/api/friend/friend.go @@ -21,7 +21,7 @@ func AddBlacklist(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return } - req := &rpc.AddBlacklistReq{} + req := &rpc.AddBlacklistReq{CommID: &rpc.CommID{}} utils.CopyStructFields(req.CommID, ¶ms) var ok bool ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) @@ -84,8 +84,9 @@ func AddFriend(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return } - req := &rpc.AddFriendReq{} - utils.CopyStructFields(req.CommID, params) + req := &rpc.AddFriendReq{CommID: &rpc.CommID{}} + utils.CopyStructFields(req.CommID, ¶ms.ParamsCommFriend) + req.ReqMsg = params.ReqMsg var ok bool ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) if !ok { @@ -93,7 +94,7 @@ func AddFriend(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"}) return } - log.NewInfo("AddFriend args ", req.String()) + log.NewInfo(req.CommID.OperationID, "AddFriend args ", req.String()) etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImFriendName) client := rpc.NewFriendClient(etcdConn) @@ -116,8 +117,8 @@ func AddFriendResponse(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return } - req := &rpc.AddFriendResponseReq{} - utils.CopyStructFields(req.CommID, ¶ms) + req := &rpc.AddFriendResponseReq{CommID: &rpc.CommID{}} + utils.CopyStructFields(req.CommID, ¶ms.ParamsCommFriend) var ok bool ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) if !ok { @@ -125,7 +126,7 @@ func AddFriendResponse(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"}) return } - utils.CopyStructFields(&req, ¶ms) + utils.CopyStructFields(req, ¶ms) log.NewInfo(req.CommID.OperationID, "AddFriendResponse args ", req.String()) etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImFriendName) @@ -149,8 +150,8 @@ func DeleteFriend(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return } - req := &rpc.DeleteFriendReq{} - utils.CopyStructFields(req.CommID, ¶ms) + req := &rpc.DeleteFriendReq{CommID: &rpc.CommID{}} + utils.CopyStructFields(req.CommID, ¶ms.ParamsCommFriend) var ok bool ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) if !ok { @@ -181,7 +182,7 @@ func GetBlacklist(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return } - req := &rpc.GetBlacklistReq{} + req := &rpc.GetBlacklistReq{CommID: &rpc.CommID{}} utils.CopyStructFields(req.CommID, ¶ms) var ok bool ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) @@ -218,8 +219,8 @@ func SetFriendComment(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return } - req := &rpc.SetFriendCommentReq{} - utils.CopyStructFields(req.CommID, params) + req := &rpc.SetFriendCommentReq{CommID: &rpc.CommID{}} + utils.CopyStructFields(req.CommID, ¶ms.ParamsCommFriend) req.Remark = params.Remark var ok bool ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) @@ -251,8 +252,8 @@ func RemoveBlacklist(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return } - req := &rpc.RemoveBlacklistReq{} - utils.CopyStructFields(req.CommID, params) + req := &rpc.RemoveBlacklistReq{CommID: &rpc.CommID{}} + utils.CopyStructFields(req.CommID, ¶ms.ParamsCommFriend) var ok bool ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) if !ok { @@ -271,8 +272,8 @@ func RemoveBlacklist(c *gin.Context) { return } resp := api.RemoveBlackListResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}} - c.JSON(http.StatusOK, resp) log.NewInfo(req.CommID.OperationID, "RemoveBlacklist api return ", resp) + c.JSON(http.StatusOK, resp) } func IsFriend(c *gin.Context) { @@ -282,8 +283,8 @@ func IsFriend(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return } - req := &rpc.IsFriendReq{} - utils.CopyStructFields(req.CommID, params) + req := &rpc.IsFriendReq{CommID: &rpc.CommID{}} + utils.CopyStructFields(req.CommID, ¶ms.ParamsCommFriend) var ok bool ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) if !ok { @@ -302,8 +303,8 @@ func IsFriend(c *gin.Context) { return } resp := api.IsFriendResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}, Response: RpcResp.Response} - c.JSON(http.StatusOK, resp) log.NewInfo(req.CommID.OperationID, "IsFriend api return ", resp) + c.JSON(http.StatusOK, resp) } // @@ -347,8 +348,8 @@ func GetFriendList(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return } - req := &rpc.GetFriendListReq{} - utils.CopyStructFields(req.CommID, params) + req := &rpc.GetFriendListReq{CommID: &rpc.CommID{}} + utils.CopyStructFields(req.CommID, ¶ms.ParamsCommFriend) var ok bool ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) if !ok { @@ -367,9 +368,9 @@ func GetFriendList(c *gin.Context) { return } - resp := api.GetFriendListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}} - utils.CopyStructFields(&resp, RpcResp) + resp := api.GetFriendListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}, FriendInfoList: RpcResp.FriendInfoList} log.NewInfo(req.CommID.OperationID, "GetFriendList api return ", resp) + c.JSON(http.StatusOK, resp) } func GetFriendApplyList(c *gin.Context) { @@ -379,8 +380,8 @@ func GetFriendApplyList(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return } - req := &rpc.GetFriendApplyListReq{} - utils.CopyStructFields(req.CommID, params) + req := &rpc.GetFriendApplyListReq{CommID: &rpc.CommID{}} + utils.CopyStructFields(req.CommID, ¶ms) var ok bool ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) if !ok { @@ -400,10 +401,9 @@ func GetFriendApplyList(c *gin.Context) { return } - resp := api.GetFriendApplyListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}} - utils.CopyStructFields(&resp, RpcResp) + resp := api.GetFriendApplyListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}, FriendRequestList: RpcResp.FriendRequestList} log.NewInfo(req.CommID.OperationID, "GetFriendApplyList api return ", resp) - + c.JSON(http.StatusOK, resp) } func GetSelfApplyList(c *gin.Context) { @@ -413,8 +413,8 @@ func GetSelfApplyList(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return } - req := &rpc.GetSelfApplyListReq{} - utils.CopyStructFields(req.CommID, params) + req := &rpc.GetSelfApplyListReq{CommID: &rpc.CommID{}} + utils.CopyStructFields(req.CommID, ¶ms) var ok bool ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) if !ok { @@ -433,9 +433,7 @@ func GetSelfApplyList(c *gin.Context) { return } - resp := api.GetSelfApplyListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}} - utils.CopyStructFields(resp, RpcResp) - c.JSON(http.StatusOK, resp) + resp := api.GetSelfApplyListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}, FriendRequestList: RpcResp.FriendRequestList} log.NewInfo(req.CommID.OperationID, "GetSelfApplyList api return ", resp) - + c.JSON(http.StatusOK, resp) } diff --git a/internal/rpc/friend/firend.go b/internal/rpc/friend/firend.go index b334cb1e3..e0ba79824 100644 --- a/internal/rpc/friend/firend.go +++ b/internal/rpc/friend/firend.go @@ -144,6 +144,8 @@ func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq //Establish a latest relationship in the friend request table friendRequest := imdb.FriendRequest{ReqMsg: req.ReqMsg} utils.CopyStructFields(&friendRequest, req.CommID) + // {openIM001 openIM002 0 test add friend 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC }] + log.NewDebug(req.CommID.OperationID, "UpdateFriendApplication args ", friendRequest) err := imdb.UpdateFriendApplication(&friendRequest) if err != nil { log.NewError(req.CommID.OperationID, "UpdateFriendApplication failed ", err.Error(), friendRequest) diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index 1192155ea..01192c049 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -94,7 +94,7 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR } //to group member - groupMember := imdb.GroupMember{GroupID: groupId, RoleLevel: 1} + groupMember := imdb.GroupMember{GroupID: groupId, RoleLevel: constant.GroupOwner} utils.CopyStructFields(&groupMember, us) err = im_mysql_model.InsertIntoGroupMember(groupMember) if err != nil { @@ -114,7 +114,7 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR log.NewError(req.OperationID, "FindUserByUID failed ", err.Error(), user.UserID) continue } - if user.RoleLevel == 1 { + if user.RoleLevel == constant.GroupOwner { log.NewError(req.OperationID, "only one owner, failed ", user) continue } @@ -224,7 +224,7 @@ func (s *groupServer) InviteUserToGroup(ctx context.Context, req *pbGroup.Invite var toInsertInfo imdb.GroupMember utils.CopyStructFields(&toInsertInfo, toUserInfo) toInsertInfo.GroupID = req.GroupID - toInsertInfo.RoleLevel = 0 + toInsertInfo.RoleLevel = constant.GroupOrdinaryUsers err = imdb.InsertIntoGroupMember(toInsertInfo) if err != nil { log.NewError(req.OperationID, "InsertIntoGroupMember failed ", req.GroupID, toUserInfo.UserID, toUserInfo.Nickname, toUserInfo.FaceUrl) @@ -331,7 +331,7 @@ func (s *groupServer) KickGroupMember(ctx context.Context, req *pbGroup.KickGrou groupOwnerUserID := "" for _, v := range ownerList { - if v.RoleLevel == 1 { + if v.RoleLevel == constant.GroupOwner { groupOwnerUserID = v.UserID } } @@ -513,7 +513,7 @@ func hasAccess(req *pbGroup.SetGroupInfoReq) bool { return false } - if groupUserInfo.RoleLevel == constant.OrdinaryMember { + if groupUserInfo.RoleLevel == constant.GroupAdmin { return true } return false diff --git a/pkg/base_info/friend_api_struct.go b/pkg/base_info/friend_api_struct.go index b5cee09e5..a45e7ccfa 100644 --- a/pkg/base_info/friend_api_struct.go +++ b/pkg/base_info/friend_api_struct.go @@ -2,14 +2,14 @@ package base_info import open_im_sdk "Open_IM/pkg/proto/sdk_ws" -type paramsCommFriend struct { +type ParamsCommFriend struct { OperationID string `json:"operationID" binding:"required"` ToUserID string `json:"toUserID" binding:"required"` FromUserID string `json:"fromUserID" binding:"required"` } type AddBlacklistReq struct { - paramsCommFriend + ParamsCommFriend } type AddBlacklistResp struct { CommResp @@ -26,7 +26,7 @@ type ImportFriendResp struct { } type AddFriendReq struct { - paramsCommFriend + ParamsCommFriend ReqMsg string `json:"reqMsg"` } type AddFriendResp struct { @@ -34,7 +34,7 @@ type AddFriendResp struct { } type AddFriendResponseReq struct { - paramsCommFriend + ParamsCommFriend Flag int32 `json:"flag" binding:"required"` HandleMsg string `json:"handleMsg"` } @@ -43,14 +43,15 @@ type AddFriendResponseResp struct { } type DeleteFriendReq struct { - paramsCommFriend + ParamsCommFriend } type DeleteFriendResp struct { CommResp } type GetBlackListReq struct { - paramsCommFriend + OperationID string `json:"operationID" binding:"required"` + FromUserID string `json:"fromUserID" binding:"required"` } type GetBlackListResp struct { CommResp @@ -69,7 +70,7 @@ type BlackUserInfo struct { } type SetFriendCommentReq struct { - paramsCommFriend + ParamsCommFriend Remark string `json:"remark" binding:"required"` } type SetFriendCommentResp struct { @@ -77,14 +78,14 @@ type SetFriendCommentResp struct { } type RemoveBlackListReq struct { - paramsCommFriend + ParamsCommFriend } type RemoveBlackListResp struct { CommResp } type IsFriendReq struct { - paramsCommFriend + ParamsCommFriend } type IsFriendResp struct { CommResp @@ -92,7 +93,7 @@ type IsFriendResp struct { } type GetFriendsInfoReq struct { - paramsCommFriend + ParamsCommFriend } type GetFriendsInfoResp struct { CommResp @@ -100,7 +101,7 @@ type GetFriendsInfoResp struct { } type GetFriendListReq struct { - paramsCommFriend + ParamsCommFriend } type GetFriendListResp struct { CommResp @@ -108,17 +109,19 @@ type GetFriendListResp struct { } type GetFriendApplyListReq struct { - paramsCommFriend + OperationID string `json:"operationID" binding:"required"` + FromUserID string `json:"fromUserID" binding:"required"` } type GetFriendApplyListResp struct { CommResp - FriendRequestList open_im_sdk.FriendRequest `json:"data"` + FriendRequestList []*open_im_sdk.FriendRequest `json:"data"` } type GetSelfApplyListReq struct { - paramsCommFriend + OperationID string `json:"operationID" binding:"required"` + FromUserID string `json:"fromUserID" binding:"required"` } type GetSelfApplyListResp struct { CommResp - FriendRequestList open_im_sdk.FriendRequest `json:"data"` + FriendRequestList []*open_im_sdk.FriendRequest `json:"data"` } diff --git a/pkg/common/constant/constant.go b/pkg/common/constant/constant.go index d8aa4a965..aa2f12c45 100644 --- a/pkg/common/constant/constant.go +++ b/pkg/common/constant/constant.go @@ -3,12 +3,12 @@ package constant const ( //group admin - OrdinaryMember = 0 - GroupOwner = 1 - Administrator = 2 + // OrdinaryMember = 0 + // GroupOwner = 1 + // Administrator = 2 //group application - Application = 0 - AgreeApplication = 1 + // Application = 0 + // AgreeApplication = 1 //friend related BlackListFlag = 1 @@ -117,4 +117,16 @@ var ContentType2PushContent = map[int64]string{ Common: "你收到一条新消息", } +const ( + AppOrdinaryUsers = 1 + AppAdmin = 2 + + GroupOrdinaryUsers = 1 + GroupOwner = 2 + GroupAdmin = 3 + + Male = 1 + Female = 2 +) + const FriendAcceptTip = "You have successfully become friends, so start chatting" diff --git a/pkg/common/db/mysql_model/im_mysql_model/friend_request_model.go b/pkg/common/db/mysql_model/im_mysql_model/friend_request_model.go index 891020ec0..a8973c2e2 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/friend_request_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/friend_request_model.go @@ -2,6 +2,7 @@ package im_mysql_model import ( "Open_IM/pkg/common/db" + "Open_IM/pkg/utils" "time" ) @@ -65,11 +66,12 @@ func UpdateFriendApplication(friendRequest *FriendRequest) error { return err } friendRequest.CreateTime = time.Now() - err = dbConn.Table("friend_request").Where("from_user_id=? and to_user_id=?", friendRequest.FromUserID, friendRequest.ToUserID).Update(&friendRequest).Error - if err != nil { - return err + if dbConn.Table("friend_request").Where("from_user_id=? and to_user_id=?", + friendRequest.FromUserID, friendRequest.ToUserID).Update(&friendRequest).RowsAffected == 0 { + return InsertFriendApplication(friendRequest) + } else { + return nil } - return nil } func InsertFriendApplication(friendRequest *FriendRequest) error { @@ -77,6 +79,12 @@ func InsertFriendApplication(friendRequest *FriendRequest) error { if err != nil { return err } + if friendRequest.CreateTime.Unix() < 0 { + friendRequest.CreateTime = time.Now() + } + if friendRequest.HandleTime.Unix() < 0 { + friendRequest.HandleTime = utils.UnixSecondToTime(0) + } err = dbConn.Table("friend_request").Create(friendRequest).Error if err != nil { return err diff --git a/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go b/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go index 84175c0a1..70be7a2bc 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go @@ -1,6 +1,7 @@ package im_mysql_model import ( + "Open_IM/pkg/common/constant" "Open_IM/pkg/common/db" "time" ) @@ -22,7 +23,10 @@ func InsertIntoGroupMember(toInsertInfo GroupMember) error { if err != nil { return err } - toInsertInfo.JoinSource = time.Now() + toInsertInfo.JoinTime = time.Now() + if toInsertInfo.RoleLevel == 0 { + toInsertInfo.RoleLevel = constant.GroupOrdinaryUsers + } err = dbConn.Table("group_member").Create(toInsertInfo).Error if err != nil { return err @@ -138,7 +142,7 @@ func GetGroupOwnerInfoByGroupID(groupID string) (*GroupMember, error) { return nil, err } for _, v := range omList { - if v.RoleLevel == 1 { + if v.RoleLevel == constant.GroupOwner { return &v, nil } } diff --git a/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go b/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go index e8f64208b..cd6cf641f 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go @@ -1,6 +1,7 @@ package im_mysql_model import ( + "Open_IM/pkg/common/constant" "Open_IM/pkg/common/db" "time" ) @@ -88,7 +89,7 @@ func GetGroupApplicationList(userID string) ([]GroupRequest, error) { return nil, err } for _, v := range memberList { - if v.RoleLevel > 0 { + if v.RoleLevel > constant.GroupOrdinaryUsers { list, err := GetGroupRequestByGroupID(v.GroupID) if err != nil { continue diff --git a/pkg/common/db/mysql_model/im_mysql_model/model_struct.go b/pkg/common/db/mysql_model/im_mysql_model/model_struct.go index ae2d9dc43..29125d813 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/model_struct.go +++ b/pkg/common/db/mysql_model/im_mysql_model/model_struct.go @@ -95,7 +95,7 @@ type GroupMember struct { JoinTime time.Time `gorm:"column:join_time"` Nickname string `gorm:"column:nickname"` FaceUrl string `gorm:"user_group_face_url"` - JoinSource time.Time `gorm:"column:join_source"` + JoinSource int32 `gorm:"column:join_source"` OperatorUserID string `gorm:"column:operator_user_id"` Ex string `gorm:"column:ex"` } diff --git a/pkg/common/db/mysql_model/im_mysql_model/user_model.go b/pkg/common/db/mysql_model/im_mysql_model/user_model.go index 01ca6d593..298487c38 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/user_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/user_model.go @@ -2,6 +2,7 @@ package im_mysql_model import ( "Open_IM/pkg/common/config" + "Open_IM/pkg/common/constant" "Open_IM/pkg/common/db" "Open_IM/pkg/utils" "fmt" @@ -15,13 +16,16 @@ func init() { user, err := GetUserByUserID(v) if err != nil { fmt.Println("GetUserByUserID failed ", err.Error(), v, user) + } else { continue } var appMgr User + appMgr.UserID = v appMgr.Nickname = "AppManager" + utils.IntToString(k+1) + appMgr.AppMangerLevel = constant.AppAdmin err = UserRegister(appMgr) if err != nil { - fmt.Println("AppManager insert error", err.Error()) + fmt.Println("AppManager insert error", err.Error(), appMgr, "time: ", appMgr.Birth.Unix()) } } @@ -33,7 +37,12 @@ func UserRegister(user User) error { return err } user.CreateTime = time.Now() - + if user.AppMangerLevel == 0 { + user.AppMangerLevel = constant.AppOrdinaryUsers + } + if user.Birth.Unix() < 0 { + user.Birth = utils.UnixSecondToTime(0) + } err = dbConn.Table("user").Create(&user).Error if err != nil { return err diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index d5dc0254a..224e459e3 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -1,49 +1,10 @@ package utils import ( - "fmt" "github.com/jinzhu/copier" - "reflect" ) // copy a by b b->a func CopyStructFields(a interface{}, b interface{}, fields ...string) (err error) { return copier.Copy(a, b) - - at := reflect.TypeOf(a) - av := reflect.ValueOf(a) - bt := reflect.TypeOf(b) - bv := reflect.ValueOf(b) - - if at.Kind() != reflect.Ptr { - err = fmt.Errorf("a must be a struct pointer") - return err - } - av = reflect.ValueOf(av.Interface()) - - _fields := make([]string, 0) - if len(fields) > 0 { - _fields = fields - } else { - for i := 0; i < bv.NumField(); i++ { - _fields = append(_fields, bt.Field(i).Name) - } - } - - if len(_fields) == 0 { - err = fmt.Errorf("no fields to copy") - return err - } - - for i := 0; i < len(_fields); i++ { - name := _fields[i] - - f := av.Elem().FieldByName(name) - bValue := bv.FieldByName(name) - - if f.IsValid() && f.Kind() == bValue.Kind() { - f.Set(bValue) - } - } - return nil }