| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package com.izouma.nineth.service.netease;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.izouma.nineth.domain.User;
- import com.izouma.nineth.domain.netease.NeteaseMessage;
- import com.izouma.nineth.dto.PageQuery;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.netease.NeteaseMessageRepo;
- import com.izouma.nineth.repo.UserRepo;
- import com.izouma.nineth.utils.JpaUtils;
- import lombok.AllArgsConstructor;
- import org.springframework.data.domain.Page;
- import org.springframework.stereotype.Service;
- import java.util.HashMap;
- import java.util.Map;
- @Service
- @AllArgsConstructor
- public class NeteaseMessageService {
- private NeteaseMessageRepo neteaseMessageRepo;
- private NeteaseUserService neteaseUserService;
- private UserRepo userRepo;
- public Page<NeteaseMessage> all(PageQuery pageQuery) {
- return neteaseMessageRepo
- .findAll(JpaUtils.toSpecification(pageQuery, NeteaseMessage.class), JpaUtils.toPageRequest(pageQuery));
- }
- public NeteaseMessage sendMessage(NeteaseMessage msg) {
- User from = userRepo.findById(Long.valueOf(msg.getFromId())).orElseThrow(new BusinessException("未找到用户"));
- // User to = userRepo.findById(Long.valueOf(msg.getToId())).orElseThrow(new BusinessException("未找到用户"));
- msg.setFromAvatar(from.getAvatar());
- msg.setFromNickName(from.getNickname());
- // msg.setToAvatar(to.getAvatar());
- // msg.setToNickName(to.getNickname());
- String url = "msg/sendMsg.action";
- String contentType = "application/x-www-form-urlencoded;charset=utf-8";
- Map<String, Object> params = new HashMap<>();
- params.put("from", msg.getFromId());
- params.put("to", msg.getToId());
- params.put("ope", msg.getOpe());
- params.put("type", msg.getType());
- params.put("body", msg.getBody());
- params.put("markRead", 1);
- String result = neteaseUserService.httpPost(url, contentType, params);
- JSONObject jsonObject = JSON.parseObject(result);
- Integer code = jsonObject.getInteger("code");
- if (code != 200) {
- throw new BusinessException("发信出错,请检查后重新发送");
- }
- JSONObject data = jsonObject.getJSONObject("data");
- msg.setMsgId(data.getLong("msgid"));
- msg.setTimetag(data.getLong("timetag"));
- return neteaseMessageRepo.save(msg);
- }
- }
|