NeteaseMessageService.java 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.izouma.nineth.service.netease;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.izouma.nineth.domain.User;
  5. import com.izouma.nineth.domain.netease.NeteaseMessage;
  6. import com.izouma.nineth.dto.PageQuery;
  7. import com.izouma.nineth.exception.BusinessException;
  8. import com.izouma.nineth.repo.netease.NeteaseMessageRepo;
  9. import com.izouma.nineth.repo.UserRepo;
  10. import com.izouma.nineth.service.ContentAuditService;
  11. import com.izouma.nineth.utils.JpaUtils;
  12. import lombok.AllArgsConstructor;
  13. import org.springframework.data.domain.Page;
  14. import org.springframework.stereotype.Service;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. @Service
  18. @AllArgsConstructor
  19. public class NeteaseMessageService {
  20. private NeteaseMessageRepo neteaseMessageRepo;
  21. private NeteaseUserService neteaseUserService;
  22. private UserRepo userRepo;
  23. private ContentAuditService contentAuditService;
  24. public Page<NeteaseMessage> all(PageQuery pageQuery) {
  25. return neteaseMessageRepo
  26. .findAll(JpaUtils.toSpecification(pageQuery, NeteaseMessage.class), JpaUtils.toPageRequest(pageQuery));
  27. }
  28. public NeteaseMessage sendMessage(NeteaseMessage msg) {
  29. boolean result1 = contentAuditService.auditText(msg.getBody());
  30. if (!result1) {
  31. throw new BusinessException("包含敏感词!");
  32. }
  33. User from = userRepo.findById(Long.valueOf(msg.getFromId())).orElseThrow(new BusinessException("未找到用户"));
  34. // User to = userRepo.findById(Long.valueOf(msg.getToId())).orElseThrow(new BusinessException("未找到用户"));
  35. msg.setFromAvatar(from.getAvatar());
  36. msg.setFromNickName(from.getNickname());
  37. // msg.setToAvatar(to.getAvatar());
  38. // msg.setToNickName(to.getNickname());
  39. String url = "msg/sendMsg.action";
  40. String contentType = "application/x-www-form-urlencoded;charset=utf-8";
  41. Map<String, Object> params = new HashMap<>();
  42. params.put("from", msg.getFromId());
  43. params.put("to", msg.getToId());
  44. params.put("ope", msg.getOpe());
  45. params.put("type", msg.getType());
  46. params.put("body", msg.getBody());
  47. params.put("markRead", 1);
  48. String result = neteaseUserService.httpPost(url, contentType, params);
  49. JSONObject jsonObject = JSON.parseObject(result);
  50. Integer code = jsonObject.getInteger("code");
  51. if (code != 200) {
  52. throw new BusinessException("发信出错,请检查后重新发送");
  53. }
  54. JSONObject data = jsonObject.getJSONObject("data");
  55. msg.setMsgId(data.getLong("msgid"));
  56. msg.setTimetag(data.getLong("timetag"));
  57. return neteaseMessageRepo.save(msg);
  58. }
  59. }