Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

323 рядки
8.9 KiB

  1. package api
  2. import (
  3. "context"
  4. "egg-im/internal/business/app"
  5. comm "egg-im/internal/business/comm"
  6. "egg-im/internal/business/comm/utils"
  7. "egg-im/internal/business/domain/user/model/business"
  8. "egg-im/internal/business/domain/user/repo"
  9. friend2 "egg-im/internal/logic/domain/friend"
  10. "egg-im/internal/logic/domain/group/model"
  11. repo2 "egg-im/internal/logic/domain/group/repo"
  12. "egg-im/internal/logic/domain/message/service"
  13. "egg-im/pkg/db"
  14. "egg-im/pkg/gerrors"
  15. "egg-im/pkg/grpclib"
  16. "egg-im/pkg/pb"
  17. "encoding/json"
  18. "errors"
  19. "github.com/jinzhu/gorm"
  20. "strconv"
  21. "time"
  22. )
  23. type BusinessExtServer struct{}
  24. func (s *BusinessExtServer) ViewGroupNotice(ctx context.Context, req *pb.ViewGroupNoticeReq) (*pb.ViewGroupNoticeResp, error) {
  25. userId, _, err := grpclib.GetCtxData(ctx)
  26. if err != nil {
  27. return nil, err
  28. }
  29. groupNotice, err := repo2.GroupNoticeDao.Get(req.GroupId)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if groupNotice == nil {
  34. return &pb.ViewGroupNoticeResp{
  35. GroupId: 0,
  36. UserId: 0,
  37. Content: "",
  38. LikeNums: 0,
  39. ReadNums: 0,
  40. PublishType: 0,
  41. PublishTime: "",
  42. IsLike: false,
  43. }, nil
  44. }
  45. groupNotice.ReadNums++
  46. err = repo2.GroupNoticeDao.Save(groupNotice, false)
  47. if err != nil {
  48. return nil, err
  49. }
  50. groupNoticeWithLikeRecords, err := repo2.GroupNoticeWithLikeRecordsDao.Get(userId, req.GroupId)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return &pb.ViewGroupNoticeResp{
  55. GroupId: groupNotice.GroupId,
  56. UserId: groupNotice.UserId,
  57. Content: groupNotice.Content,
  58. LikeNums: int64(groupNotice.LikeNums),
  59. ReadNums: int64(groupNotice.ReadNums),
  60. PublishType: pb.GroupNoticePublishType(groupNotice.PublishType),
  61. PublishTime: groupNotice.UpdateTime.Format("2006-01-02 15:04:05"),
  62. IsLike: func() bool {
  63. if groupNoticeWithLikeRecords == nil {
  64. return false
  65. }
  66. return true
  67. }(),
  68. }, nil
  69. }
  70. func (s *BusinessExtServer) LikeGroupNotice(ctx context.Context, req *pb.LikeGroupNoticeReq) (*pb.Empty, error) {
  71. userId, _, err := grpclib.GetCtxData(ctx)
  72. if err != nil {
  73. return nil, err
  74. }
  75. groupNoticeWithLikeRecords, err := repo2.GroupNoticeWithLikeRecordsDao.Get(userId, req.GroupId)
  76. if err != nil {
  77. return nil, err
  78. }
  79. if groupNoticeWithLikeRecords != nil {
  80. return nil, errors.New("请勿重复点赞!")
  81. }
  82. groupNotice, err := repo2.GroupNoticeDao.Get(req.GroupId)
  83. if err != nil {
  84. return nil, err
  85. }
  86. if groupNotice == nil {
  87. return nil, errors.New("群公告记录不存在!")
  88. }
  89. err = repo2.GroupNoticeWithLikeRecordsDao.Save(&model.GroupNoticeWithLikeRecords{
  90. GroupId: req.GroupId,
  91. UserId: userId,
  92. })
  93. if err != nil {
  94. return nil, err
  95. }
  96. groupNotice.LikeNums++
  97. return new(pb.Empty), repo2.GroupNoticeDao.Save(groupNotice, false)
  98. }
  99. func (s *BusinessExtServer) CancelLikeGroupNotice(ctx context.Context, req *pb.CancelLikeGroupNoticeReq) (*pb.Empty, error) {
  100. userId, _, err := grpclib.GetCtxData(ctx)
  101. if err != nil {
  102. return nil, err
  103. }
  104. groupNoticeWithLikeRecords, err := repo2.GroupNoticeWithLikeRecordsDao.Get(userId, req.GroupId)
  105. if err != nil {
  106. return nil, err
  107. }
  108. if groupNoticeWithLikeRecords == nil {
  109. return nil, errors.New("不存在的点赞记录!")
  110. }
  111. groupNotice, err := repo2.GroupNoticeDao.Get(req.GroupId)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if groupNotice == nil {
  116. return nil, errors.New("群公告记录不存在!")
  117. }
  118. err = repo2.GroupNoticeWithLikeRecordsDao.Delete(userId, req.GroupId)
  119. if err != nil {
  120. return nil, err
  121. }
  122. groupNotice.LikeNums--
  123. return new(pb.Empty), repo2.GroupNoticeDao.Save(groupNotice, false)
  124. }
  125. func (s *BusinessExtServer) PublishGroupNotice(ctx context.Context, req *pb.PublishGroupNoticeReq) (*pb.Empty, error) {
  126. now := time.Now()
  127. userId, _, err := grpclib.GetCtxData(ctx)
  128. if err != nil {
  129. return nil, err
  130. }
  131. //1、判断是否可以进行发布群公告
  132. groupUser, err := repo2.GroupUserRepo.Get(req.GroupId, userId)
  133. if groupUser.MemberType == int(pb.MemberType_GMT_MEMBER) {
  134. return nil, errors.New("普通成员不能发布群公告")
  135. }
  136. groupComplain := model.GroupNotice{
  137. GroupId: req.GroupId,
  138. UserId: userId,
  139. PublishType: int32(req.PublishType),
  140. Content: req.Content,
  141. LikeNums: 0,
  142. ReadNums: 0,
  143. CreateTime: now,
  144. UpdateTime: now,
  145. }
  146. return new(pb.Empty), repo2.GroupNoticeDao.Save(&groupComplain, true)
  147. }
  148. func (s *BusinessExtServer) ComplainGroup(ctx context.Context, req *pb.ComplainGroupReq) (*pb.Empty, error) {
  149. now := time.Now()
  150. userId, _, err := grpclib.GetCtxData(ctx)
  151. if err != nil {
  152. return nil, err
  153. }
  154. imageList, _ := json.Marshal(req.ImageList)
  155. groupComplain := model.GroupComplain{
  156. GroupId: req.GroupId,
  157. UserId: userId,
  158. ComplainType: int(req.ComplainType),
  159. Text: req.Text,
  160. ImageList: string(imageList),
  161. Status: 0,
  162. CreateTime: now,
  163. UpdateTime: now,
  164. }
  165. return new(pb.Empty), repo2.GroupComplainDao.Save(&groupComplain)
  166. }
  167. func (s *BusinessExtServer) IsFriends(ctx context.Context, req *pb.IsFriendsReq) (*pb.IsFriendsResp, error) {
  168. user, err := repo.UserRepo.GetByPhoneNumber(req.UserPhone)
  169. if err != nil {
  170. return nil, err
  171. }
  172. if user == nil {
  173. return nil, errors.New("未查询到自身用户信息")
  174. }
  175. userFriend, err := repo.UserRepo.GetByPhoneNumber(req.FriendPhone)
  176. if err != nil {
  177. return nil, err
  178. }
  179. if userFriend == nil {
  180. return nil, errors.New("未查询到好友用户信息")
  181. }
  182. friend, err := friend2.FriendRepo.Get(user.Id, userFriend.Id)
  183. if err != nil {
  184. return nil, err
  185. }
  186. var isFriend = int64(1)
  187. if friend == nil || friend.Status == friend2.FriendStatusApply {
  188. isFriend = 2
  189. }
  190. return &pb.IsFriendsResp{
  191. User: &pb.User{
  192. UserId: userFriend.Id,
  193. Nickname: userFriend.Nickname,
  194. Sex: userFriend.Sex,
  195. AvatarUrl: userFriend.AvatarUrl,
  196. IsAutoAddedFriends: int64(userFriend.IsAutoAddedFriends),
  197. },
  198. IsFriend: isFriend,
  199. }, err
  200. }
  201. func (s *BusinessExtServer) EmoticonList(ctx context.Context, empty *pb.Empty) (*pb.EmoticonListResp, error) {
  202. list, err := service.EmoticonList()
  203. if err != nil {
  204. return nil, err
  205. }
  206. return &pb.EmoticonListResp{Emoticons: list}, err
  207. }
  208. func (s *BusinessExtServer) SignIn(ctx context.Context, req *pb.SignInReq) (*pb.SignInResp, error) {
  209. utils.FilePutContents("sign_in", utils.SerializeStr(map[string]interface{}{
  210. "args": req,
  211. }))
  212. isNew, userId, token, err := app.AuthApp.SignIn(ctx, req.PhoneNumber, req.Code, req.DeviceId, req.PushAlia, req.Nickname, req.AvatarUrl)
  213. if err != nil {
  214. return nil, err
  215. }
  216. return &pb.SignInResp{
  217. IsNew: isNew,
  218. UserId: userId,
  219. Token: token,
  220. }, nil
  221. }
  222. func (s *BusinessExtServer) GetUser(ctx context.Context, req *pb.GetUserReq) (*pb.GetUserResp, error) {
  223. var user *pb.User
  224. if req.Phone != "" {
  225. useInfo, err := repo.UserRepo.GetByPhoneNumber(req.Phone)
  226. if err != nil {
  227. return nil, err
  228. }
  229. if useInfo != nil {
  230. user = &pb.User{
  231. UserId: useInfo.Id,
  232. Nickname: useInfo.Nickname,
  233. Sex: useInfo.Sex,
  234. AvatarUrl: useInfo.AvatarUrl,
  235. IsAutoAddedFriends: int64(useInfo.IsAutoAddedFriends),
  236. PhoneNumber: useInfo.PhoneNumber,
  237. }
  238. }
  239. } else {
  240. userId, _, err := grpclib.GetCtxData(ctx)
  241. if err != nil {
  242. return nil, err
  243. }
  244. if req.UserId != 0 {
  245. userId = req.UserId
  246. }
  247. user, err = app.UserApp.Get(ctx, userId)
  248. }
  249. var businessUser business.User
  250. err := db.BusinessDB.First(&businessUser, "phone = ?", user.PhoneNumber).Error
  251. if err != nil && err != gorm.ErrRecordNotFound {
  252. return nil, gerrors.WrapError(err)
  253. }
  254. var businessUserLevel business.UserLevel
  255. err = db.BusinessDB.First(&businessUserLevel, "id = ?", businessUser.Level).Error
  256. if err != nil && err != gorm.ErrRecordNotFound {
  257. return nil, gerrors.WrapError(err)
  258. }
  259. return &pb.GetUserResp{User: user, BusinessInfo: &pb.BusinessInfo{
  260. Uid: businessUser.Id,
  261. LevelName: businessUserLevel.LevelName,
  262. LevelId: int32(businessUserLevel.Id),
  263. IsRealName: businessUser.IsRealName == 1,
  264. }}, nil
  265. }
  266. func (s *BusinessExtServer) UpdateUser(ctx context.Context, req *pb.UpdateUserReq) (*pb.Empty, error) {
  267. userId, _, err := grpclib.GetCtxData(ctx)
  268. if err != nil {
  269. return nil, err
  270. }
  271. return new(pb.Empty), app.UserApp.Update(ctx, userId, req)
  272. }
  273. func (s *BusinessExtServer) SearchUser(ctx context.Context, req *pb.SearchUserReq) (*pb.SearchUserResp, error) {
  274. users, err := app.UserApp.Search(ctx, req.Key)
  275. return &pb.SearchUserResp{Users: users}, err
  276. }
  277. func (s *BusinessExtServer) CloudUploadFile(ctx context.Context, req *pb.CloudUploadFileReq) (*pb.CloudUploadFileResp, error) {
  278. userId, _, err := grpclib.GetCtxData(ctx)
  279. if err != nil {
  280. return nil, err
  281. }
  282. upload, err := comm.CloudUpload.FileReqUpload(ctx, strconv.FormatInt(userId, 10), req.Dir, req.FileName, utils.StrToInt64(req.FileSize))
  283. data := utils.SerializeStr(upload)
  284. var result map[string]string
  285. utils.Unserialize([]byte(data), &result)
  286. return &pb.CloudUploadFileResp{
  287. Method: result["method"],
  288. Host: result["host"],
  289. Key: result["key"],
  290. Token: result["token"],
  291. }, err
  292. }