package api import ( "context" "egg-im/internal/business/app" comm "egg-im/internal/business/comm" "egg-im/internal/business/comm/utils" "egg-im/internal/business/domain/user/repo" friend2 "egg-im/internal/logic/domain/friend" "egg-im/internal/logic/domain/group/model" repo2 "egg-im/internal/logic/domain/group/repo" "egg-im/internal/logic/domain/message/service" "egg-im/pkg/grpclib" "egg-im/pkg/pb" "encoding/json" "errors" "strconv" "time" ) type BusinessExtServer struct{} func (s *BusinessExtServer) ViewGroupNotice(ctx context.Context, req *pb.ViewGroupNoticeReq) (*pb.ViewGroupNoticeResp, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } groupNotice, err := repo2.GroupNoticeDao.Get(req.GroupId) if err != nil { return nil, err } if groupNotice == nil { return &pb.ViewGroupNoticeResp{ GroupId: 0, UserId: 0, Content: "", LikeNums: 0, ReadNums: 0, PublishType: 0, PublishTime: "", IsLike: false, }, nil } groupNoticeWithLikeRecords, err := repo2.GroupNoticeWithLikeRecordsDao.Get(userId, req.GroupId) if err != nil { return nil, err } return &pb.ViewGroupNoticeResp{ GroupId: groupNotice.GroupId, UserId: groupNotice.UserId, Content: groupNotice.Content, LikeNums: int64(groupNotice.LikeNums), ReadNums: int64(groupNotice.ReadNums), PublishType: pb.GroupNoticePublishType(groupNotice.PublishType), PublishTime: groupNotice.CreateTime.Format("2006-01-02 15:04:05"), IsLike: func() bool { if groupNoticeWithLikeRecords == nil { return false } return true }(), }, nil } func (s *BusinessExtServer) LikeGroupNotice(ctx context.Context, req *pb.LikeGroupNoticeReq) (*pb.Empty, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } groupNoticeWithLikeRecords, err := repo2.GroupNoticeWithLikeRecordsDao.Get(userId, req.GroupId) if err != nil { return nil, err } if groupNoticeWithLikeRecords != nil { return nil, errors.New("请勿重复点赞!") } groupNotice, err := repo2.GroupNoticeDao.Get(req.GroupId) if err != nil { return nil, err } if groupNotice == nil { return nil, errors.New("群公告记录不存在!") } err = repo2.GroupNoticeWithLikeRecordsDao.Save(&model.GroupNoticeWithLikeRecords{ GroupId: req.GroupId, UserId: userId, }) if err != nil { return nil, err } groupNotice.LikeNums++ return new(pb.Empty), repo2.GroupNoticeDao.Save(groupNotice) } func (s *BusinessExtServer) CancelLikeGroupNotice(ctx context.Context, req *pb.CancelLikeGroupNoticeReq) (*pb.Empty, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } groupNoticeWithLikeRecords, err := repo2.GroupNoticeWithLikeRecordsDao.Get(userId, req.GroupId) if err != nil { return nil, err } if groupNoticeWithLikeRecords == nil { return nil, errors.New("不存在的点赞记录!") } groupNotice, err := repo2.GroupNoticeDao.Get(req.GroupId) if err != nil { return nil, err } if groupNotice == nil { return nil, errors.New("群公告记录不存在!") } err = repo2.GroupNoticeWithLikeRecordsDao.Delete(userId, req.GroupId) if err != nil { return nil, err } err = repo2.GroupNoticeWithLikeRecordsDao.Save(&model.GroupNoticeWithLikeRecords{ GroupId: req.GroupId, UserId: userId, }) if err != nil { return nil, err } groupNotice.LikeNums-- return new(pb.Empty), repo2.GroupNoticeDao.Save(groupNotice) } func (s *BusinessExtServer) PublishGroupNotice(ctx context.Context, req *pb.PublishGroupNoticeReq) (*pb.Empty, error) { now := time.Now() userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } //1、判断是否可以进行发布群公告 groupUser, err := repo2.GroupUserRepo.Get(req.GroupId, userId) if groupUser.MemberType == int(pb.MemberType_GMT_MEMBER) { return nil, errors.New("普通成员不能发布群公告") } groupComplain := model.GroupNotice{ GroupId: req.GroupId, UserId: userId, PublishType: int32(req.PublishType), Content: req.Content, LikeNums: 0, ReadNums: 0, CreateTime: now, UpdateTime: now, } return new(pb.Empty), repo2.GroupNoticeDao.Save(&groupComplain) } func (s *BusinessExtServer) ComplainGroup(ctx context.Context, req *pb.ComplainGroupReq) (*pb.Empty, error) { now := time.Now() userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } imageList, _ := json.Marshal(req.ImageList) groupComplain := model.GroupComplain{ GroupId: req.GroupId, UserId: userId, ComplainType: int(req.ComplainType), Text: req.Text, ImageList: string(imageList), Status: 0, CreateTime: now, UpdateTime: now, } return new(pb.Empty), repo2.GroupComplainDao.Save(&groupComplain) } func (s *BusinessExtServer) IsFriends(ctx context.Context, req *pb.IsFriendsReq) (*pb.IsFriendsResp, error) { user, err := repo.UserRepo.GetByPhoneNumber(req.UserPhone) if err != nil { return nil, err } if user == nil { return nil, errors.New("未查询到自身用户信息") } userFriend, err := repo.UserRepo.GetByPhoneNumber(req.FriendPhone) if err != nil { return nil, err } if userFriend == nil { return nil, errors.New("未查询到好友用户信息") } friend, err := friend2.FriendRepo.Get(user.Id, userFriend.Id) if err != nil { return nil, err } var isFriend = int64(1) if friend == nil || friend.Status == friend2.FriendStatusApply { isFriend = 2 } return &pb.IsFriendsResp{ User: &pb.User{ UserId: userFriend.Id, Nickname: userFriend.Nickname, Sex: userFriend.Sex, AvatarUrl: userFriend.AvatarUrl, IsAutoAddedFriends: int64(userFriend.IsAutoAddedFriends), }, IsFriend: isFriend, }, err } func (s *BusinessExtServer) EmoticonList(ctx context.Context, empty *pb.Empty) (*pb.EmoticonListResp, error) { list, err := service.EmoticonList() if err != nil { return nil, err } return &pb.EmoticonListResp{Emoticons: list}, err } func (s *BusinessExtServer) SignIn(ctx context.Context, req *pb.SignInReq) (*pb.SignInResp, error) { utils.FilePutContents("sign_in", utils.SerializeStr(map[string]interface{}{ "args": req, })) isNew, userId, token, err := app.AuthApp.SignIn(ctx, req.PhoneNumber, req.Code, req.DeviceId, req.PushAlia, req.Nickname, req.AvatarUrl) if err != nil { return nil, err } return &pb.SignInResp{ IsNew: isNew, UserId: userId, Token: token, }, nil } func (s *BusinessExtServer) GetUser(ctx context.Context, req *pb.GetUserReq) (*pb.GetUserResp, error) { var user *pb.User if req.Phone != "" { useInfo, err := repo.UserRepo.GetByPhoneNumber(req.Phone) user = &pb.User{ UserId: useInfo.Id, Nickname: useInfo.Nickname, Sex: useInfo.Sex, AvatarUrl: useInfo.AvatarUrl, IsAutoAddedFriends: int64(useInfo.IsAutoAddedFriends), PhoneNumber: useInfo.PhoneNumber, } if err != nil { return nil, err } } else { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } if req.UserId != 0 { userId = req.UserId } user, err = app.UserApp.Get(ctx, userId) } return &pb.GetUserResp{User: user}, nil } func (s *BusinessExtServer) UpdateUser(ctx context.Context, req *pb.UpdateUserReq) (*pb.Empty, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } return new(pb.Empty), app.UserApp.Update(ctx, userId, req) } func (s *BusinessExtServer) SearchUser(ctx context.Context, req *pb.SearchUserReq) (*pb.SearchUserResp, error) { users, err := app.UserApp.Search(ctx, req.Key) return &pb.SearchUserResp{Users: users}, err } func (s *BusinessExtServer) CloudUploadFile(ctx context.Context, req *pb.CloudUploadFileReq) (*pb.CloudUploadFileResp, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } upload, err := comm.CloudUpload.FileReqUpload(ctx, strconv.FormatInt(userId, 10), req.Dir, req.FileName, utils.StrToInt64(req.FileSize)) data := utils.SerializeStr(upload) var result map[string]string utils.Unserialize([]byte(data), &result) return &pb.CloudUploadFileResp{ Method: result["method"], Host: result["host"], Key: result["key"], Token: result["token"], }, err }