AppApiController.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package com.goafanti.app.controller;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.UUID;
  7. import javax.annotation.Resource;
  8. import javax.servlet.http.HttpServletRequest;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.springframework.http.HttpMethod;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestMethod;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import com.alibaba.fastjson.JSONObject;
  15. import com.goafanti.common.bo.Result;
  16. import com.goafanti.common.constant.AFTConstants;
  17. import com.goafanti.common.constant.ErrorConstants;
  18. import com.goafanti.common.controller.BaseApiController;
  19. import com.goafanti.common.model.JpushEasemobAccount;
  20. import com.goafanti.common.model.MessageConsumer;
  21. import com.goafanti.common.model.MessageFromSystem;
  22. import com.goafanti.common.model.MessageProducer;
  23. import com.goafanti.common.model.User;
  24. import com.goafanti.common.utils.DateUtils;
  25. import com.goafanti.core.shiro.token.TokenManager;
  26. import com.goafanti.easemob.EasemobUtils;
  27. import com.goafanti.easemob.bo.EasemobInfo;
  28. import com.goafanti.message.bo.MessageBo;
  29. import com.goafanti.message.bo.MessageListBo;
  30. import com.goafanti.message.service.MessageService;
  31. import com.goafanti.user.service.UserService;
  32. @RestController
  33. @RequestMapping(path = "/api/user/app", method = RequestMethod.GET)
  34. public class AppApiController extends BaseApiController {
  35. @Resource
  36. private UserService userServiceImpl;
  37. @Resource
  38. private MessageService messageService;
  39. @Resource
  40. private EasemobUtils easemobUtils;
  41. @RequestMapping(value = "/uploadImg",method = RequestMethod.POST)
  42. public Result uploadAvatar(HttpServletRequest req){
  43. Result res = new Result();
  44. String headPortraitUrl = handleFile(res, "/avatar/", false, req, "");
  45. if(TokenManager.getToken() instanceof User){
  46. User u = (User)TokenManager.getToken();
  47. u.setHeadPortraitUrl(headPortraitUrl);
  48. userServiceImpl.updateByPrimaryKeySelective(u);
  49. }
  50. res.setData(headPortraitUrl);
  51. return res;
  52. }
  53. @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
  54. public Result updateUser(User u){
  55. Result res = new Result();
  56. if(StringUtils.isBlank(u.getNickname())){
  57. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"用户昵称不能为空"));
  58. return res;
  59. }
  60. List<User> checkData = userServiceImpl.checkExistUser(null, null, null, null, AFTConstants.USER_SOURCE_REGISTER,
  61. u.getNickname());
  62. boolean exist = false;
  63. boolean needChange = false;
  64. if (null != checkData && checkData.size() > 0) {
  65. for (User item : checkData) {
  66. if (!item.getId().equals(TokenManager.getUserId()))
  67. exist = true;
  68. }
  69. } else {
  70. needChange = true;
  71. }
  72. if (exist) {
  73. res.getError().add(buildError(ErrorConstants.USER_NICKNAME_EXIST));
  74. return res;
  75. } else if (needChange) { // 修改环信昵称
  76. JpushEasemobAccount jea = messageService.selectSynAccByUid(TokenManager.getUserId());
  77. if (null != jea && StringUtils.isNotBlank(jea.getEasemobName())) {
  78. JSONObject resultObj = new JSONObject();
  79. resultObj.put("username", jea.getEasemobName());
  80. resultObj.put("nickname", u.getNickname());
  81. easemobUtils.sendLater(
  82. new EasemobInfo().uri("/users/").data(resultObj.toJSONString()).method(HttpMethod.POST));
  83. }
  84. }
  85. u.setId(TokenManager.getUserId());
  86. userServiceImpl.updateByPrimaryKeySelective(u);
  87. return res;
  88. }
  89. @RequestMapping(value = "/logout",method = RequestMethod.GET)
  90. public Result logout(HttpServletRequest request){
  91. Result res = new Result();
  92. TokenManager.logout();
  93. return res;
  94. }
  95. @RequestMapping(value = "/userInfo",method = RequestMethod.GET)
  96. public Result userInfo(HttpServletRequest request){
  97. Result res = new Result();
  98. res.setData(userServiceImpl.selectBaseInfo());
  99. return res;
  100. }
  101. @RequestMapping(value = "/index", method = RequestMethod.GET)
  102. public Result index(){
  103. Result res = new Result();
  104. res.setData(messageService.selectMessageWithGroup());
  105. return res;
  106. }
  107. @RequestMapping(value = "/listMessage", method = RequestMethod.POST)
  108. public Result listMessage(Integer subject,Integer sourceType,Integer pageNo,Integer pageSize){
  109. Result res = new Result();
  110. if(null == subject){
  111. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "必须指定消息类型"));
  112. return res;
  113. }
  114. User u = TokenManager.getUserToken();
  115. if(u != null){
  116. List<MessageBo> boList = messageService.selectSendMessage(u.getId());
  117. if(boList != null && boList.size()>0){
  118. List<MessageProducer> producers = new ArrayList<MessageProducer>();
  119. List<MessageConsumer> consumers = new ArrayList<MessageConsumer>();
  120. MessageProducer mp = null;
  121. MessageConsumer mc = null;
  122. Date date = new Date();
  123. for(MessageBo bo : boList){
  124. mp = new MessageProducer();
  125. mp.setId(bo.getId());
  126. mp.setSendTime(date);
  127. producers.add(mp);
  128. mc = new MessageConsumer();
  129. mc.setId(UUID.randomUUID().toString());
  130. mc.setConsumerId(TokenManager.getUserId());
  131. mc.setMessageId(bo.getMessageId());
  132. consumers.add(mc);
  133. }
  134. if(producers.size()>0)messageService.updateMessageProducer(producers);
  135. //新增 message_consumer
  136. if(consumers.size()>0)messageService.insertBatchConsumer(consumers);
  137. }
  138. }
  139. res.setData(messageService.listPersonalMessage(subject,sourceType,pageNo,pageSize));
  140. return res;
  141. }
  142. /**
  143. * 同步绑定账号
  144. * @param uuid
  145. * @param registrationId
  146. * @param userName
  147. */
  148. @RequestMapping(value = "/synBindingAccount",method = RequestMethod.POST)
  149. private Result synBindingAccount(String uuid,String registrationId,String easemobName,String easemobPass){
  150. Result res = new Result();
  151. //更新 jpush_easemob_account
  152. messageService.updateJpushEasemobAccount(uuid, registrationId, easemobName,easemobPass);
  153. return res;
  154. }
  155. /**
  156. * 读取消息
  157. * @param messageId
  158. * @throws IllegalAccessException
  159. * @throws InvocationTargetException
  160. */
  161. @RequestMapping(value = "/readMessage",method = RequestMethod.GET)
  162. private Result readMessage(String messageId){
  163. Result res = new Result();
  164. if(StringUtils.isBlank(messageId)){
  165. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息编号为空"));
  166. return res;
  167. }
  168. //读取消息详情
  169. MessageFromSystem mfs = messageService.selectSystemMessageDetail(messageId);
  170. MessageListBo bo = new MessageListBo();
  171. bo.setCreateTime(mfs.getCreateTime()==null?"":DateUtils.formatDate(mfs.getCreateTime(), AFTConstants.YYYYMMDDHHMMSS));
  172. bo.setResourceType(String.valueOf(mfs.getResourceType()));
  173. bo.setSubject(String.valueOf(mfs.getSubject()));
  174. bo.setMessageId(mfs.getId());
  175. bo.setTitle(mfs.getTitle());
  176. bo.setBody(mfs.getBody());
  177. res.setData(bo);
  178. //更新 message_consumer
  179. messageService.updateMessageConsumer(messageId);
  180. return res;
  181. }
  182. }