MessageServiceImpl.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. package com.uas.ps.message.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.uas.account.entity.User;
  4. import com.uas.account.entity.UserView;
  5. import com.uas.account.util.AccountUtils;
  6. import com.uas.account.util.FlexJsonUtil;
  7. import com.uas.ps.core.page.PageInfo;
  8. import com.uas.ps.core.page.PageParams;
  9. import com.uas.ps.core.page.criteria.LogicalExpression;
  10. import com.uas.ps.core.page.criteria.PredicateUtils;
  11. import com.uas.ps.core.page.criteria.SimpleExpression;
  12. import com.uas.ps.core.util.ArrayUtils;
  13. import com.uas.ps.message.dao.AppDao;
  14. import com.uas.ps.message.dao.MessageDao;
  15. import com.uas.ps.message.domain.App;
  16. import com.uas.ps.message.domain.Message;
  17. import com.uas.ps.message.domain.SmsMessage;
  18. import com.uas.ps.message.exception.IllegalOperatorException;
  19. import com.uas.ps.message.exception.ParameterMissingException;
  20. import com.uas.ps.message.service.MessageService;
  21. import com.uas.ps.message.util.Constant;
  22. import com.uas.ps.message.util.ConsumeType;
  23. import com.uas.ps.message.util.FastjsonUtils;
  24. import com.uas.ps.message.util.SMSType;
  25. import com.uas.ps.message.util.SplitChar;
  26. import com.uas.ps.message.util.account.HttpUtil;
  27. import com.uas.ps.message.util.account.HttpUtil.ResponseWrap;
  28. import java.util.ArrayList;
  29. import java.util.Date;
  30. import java.util.HashMap;
  31. import java.util.HashSet;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.Set;
  35. import javax.persistence.criteria.CriteriaBuilder;
  36. import javax.persistence.criteria.CriteriaQuery;
  37. import javax.persistence.criteria.Predicate;
  38. import javax.persistence.criteria.Root;
  39. import org.apache.log4j.Logger;
  40. import org.springframework.beans.factory.annotation.Autowired;
  41. import org.springframework.data.domain.Page;
  42. import org.springframework.data.jpa.domain.Specification;
  43. import org.springframework.http.HttpHeaders;
  44. import org.springframework.http.MediaType;
  45. import org.springframework.stereotype.Service;
  46. import org.springframework.ui.ModelMap;
  47. import org.springframework.util.CollectionUtils;
  48. import org.springframework.util.StringUtils;
  49. /**
  50. * Created by wangyc on 2018/1/13.
  51. *
  52. * @version 2018/1/13 15:44 wangyc
  53. */
  54. @Service
  55. public class MessageServiceImpl implements MessageService {
  56. private final MessageDao messageDao;
  57. private final AppDao appDao;
  58. private static final Logger logger = Logger.getLogger(Logger.class);
  59. // private RestTemplate restTemplate;
  60. // private static final String EMAIL_REGEX = "\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
  61. // private static final String TEL_REGEXP = "^((\\(\\d{3}\\))|(\\d{3}\\-))?(13|15|18|17)\\d{9}$";
  62. private static final String PROD_URL = "https://mobile.ubtob.com/tigase/baiduPush";
  63. private static final String PUBLIC_INQUIRY_IM_URL= "https://mobile.ubtob.com:8443/openapp/?pagekind=B&id=%s&uu=%s&telephone=%s";
  64. /**
  65. * 邮件服务主机地址
  66. */
  67. private static final String MAIL_CONSOLE_HOST = "http://10.10.100.136:8080";
  68. /**
  69. * 发送邮件给单个人url
  70. */
  71. private static final String MAIL_SEND_URL = "mail/send";
  72. /**
  73. * 发送邮件给多个人 url
  74. */
  75. private static final String MAIL_SEND_MANY_URL = "mail/send/o2m";
  76. /**
  77. * 短信接口
  78. */
  79. private final String messageUrl = "http://10.10.100.136:8080/sms/send";
  80. @Autowired
  81. public MessageServiceImpl(MessageDao messageDao, AppDao appDao) {
  82. this.messageDao = messageDao;
  83. this.appDao = appDao;
  84. }
  85. @Override
  86. public Page<Message> getMessages(String receiverUu, String receiverEnuu, String consumerApp, String isRead,
  87. String keyword, PageParams pageParams) {
  88. if (StringUtils.isEmpty(receiverUu) || StringUtils.isEmpty(receiverEnuu)) {
  89. throw new ParameterMissingException("接收人信息为空");
  90. }
  91. if (StringUtils.isEmpty(consumerApp)) {
  92. throw new ParameterMissingException("接收应用信息为空");
  93. }
  94. App consumerAppExists = appDao.findByName(consumerApp);
  95. if (consumerApp == null) {
  96. throw new IllegalOperatorException("接收应用不存在");
  97. }
  98. final Long consumerAppId = consumerAppExists.getId();
  99. // 消息接收人过滤
  100. SimpleExpression receiverUuExp = PredicateUtils.eq("receiverUu", Long.valueOf(receiverUu), true);
  101. // 消息接收企业过滤
  102. SimpleExpression receiverEnuuExp = PredicateUtils.eq("receiverEnuu", Long.valueOf(receiverEnuu), true);
  103. SimpleExpression[] simpleExpressions = new SimpleExpression[] {receiverUuExp, receiverEnuuExp};
  104. // 读取状态
  105. if (!StringUtils.isEmpty(isRead)) {
  106. SimpleExpression isReadExp = PredicateUtils.eq("isRead", Short.valueOf(isRead), true);
  107. simpleExpressions = ArrayUtils.concat(simpleExpressions, new SimpleExpression[] {isReadExp});
  108. }
  109. // 关键词搜索
  110. if (!StringUtils.isEmpty(keyword)) {
  111. SimpleExpression contentExp = PredicateUtils.like("content", keyword, true);
  112. simpleExpressions = ArrayUtils.concat(simpleExpressions, new SimpleExpression[] {contentExp});
  113. }
  114. final LogicalExpression logicalExpression = PredicateUtils.and(simpleExpressions);
  115. final PageInfo pageInfo = new PageInfo(pageParams);
  116. return messageDao.findAll(new Specification<Message>() {
  117. @Override
  118. public Predicate toPredicate(Root<Message> root, CriteriaQuery<?> criteriaQuery,
  119. CriteriaBuilder criteriaBuilder) {
  120. Predicate messagePredicate = criteriaBuilder.and(logicalExpression.toPredicate(root, criteriaQuery,
  121. criteriaBuilder));
  122. // 消费应用过滤
  123. Predicate consumerAppPredicate = criteriaBuilder.equal(root.join("consumerApp").get("id"),
  124. consumerAppId);
  125. Predicate all = criteriaBuilder.and(messagePredicate, consumerAppPredicate);
  126. criteriaQuery.where(all);
  127. return null;
  128. }
  129. }, pageInfo);
  130. }
  131. @Override
  132. public Map<String, Object> getUnReadMessageCount(String receiverUu, String receiverEnuu, String consumerApp) {
  133. if (StringUtils.isEmpty(receiverUu) || StringUtils.isEmpty(receiverEnuu)) {
  134. throw new ParameterMissingException("接收人信息为空");
  135. }
  136. if (StringUtils.isEmpty(consumerApp)) {
  137. throw new ParameterMissingException("接收应用信息为空");
  138. }
  139. App consumerAppExists = appDao.findByName(consumerApp);
  140. if (consumerApp == null) {
  141. throw new IllegalOperatorException("接收应用不存在");
  142. }
  143. Integer messagesCount = messageDao.findCountByReceiverUuAndReceiverEnuuAndIsReadAndConsumerAppId(Long.valueOf(receiverUu),
  144. Long.valueOf(receiverEnuu), Constant.NO, consumerAppExists.getId());
  145. Map<String, Object> resultMap = new HashMap<>();
  146. resultMap.put("success", "success");
  147. resultMap.put("count", messagesCount);
  148. return resultMap;
  149. }
  150. @Override
  151. public Map<String, Object> getMessageCount(String receiverUu, String receiverEnuu, String consumerApp,
  152. String isRead) {
  153. if (StringUtils.isEmpty(receiverUu) || StringUtils.isEmpty(receiverEnuu)) {
  154. throw new ParameterMissingException("接收人信息为空");
  155. }
  156. if (StringUtils.isEmpty(consumerApp)) {
  157. throw new ParameterMissingException("接收应用信息为空");
  158. }
  159. App consumerAppExists = appDao.findByName(consumerApp);
  160. if (consumerApp == null) {
  161. throw new IllegalOperatorException("接收应用不存在");
  162. }
  163. Integer messagesCount = 0;
  164. if (StringUtils.isEmpty(isRead) || (!isRead.equals(Constant.NO.toString()) && !isRead.equals(Constant.YES.toString()))) {
  165. messagesCount = messageDao.findCountByReceiverUuAndReceiverEnuuAndConsumerAppId(Long.valueOf(receiverUu),
  166. Long.valueOf(receiverEnuu), consumerAppExists.getId());
  167. } else {
  168. messagesCount = messageDao.findCountByReceiverUuAndReceiverEnuuAndIsReadAndConsumerAppId(Long.valueOf(receiverUu),
  169. Long.valueOf(receiverEnuu), Short.valueOf(isRead), consumerAppExists.getId());
  170. }
  171. Map<String, Object> resultMap = new HashMap<>();
  172. resultMap.put("success", "success");
  173. resultMap.put("count", messagesCount);
  174. return resultMap;
  175. }
  176. @Override
  177. public List<Message> saveMessages(String messages) {
  178. List<JSONObject> jsonObjects = FastjsonUtils.fromJsonArray(messages, JSONObject.class);
  179. List<Message> messageList = convertFromJsonObj(jsonObjects);
  180. return messageDao.save(messageList);
  181. }
  182. @Override
  183. public Map<String, Object> sendMessage(String consumerApp) {
  184. JSONObject consumer = FastjsonUtils.parseObject(consumerApp);
  185. Object consumerAppObj = consumer.get("consumerApp");
  186. if (consumerAppObj != null && !StringUtils.isEmpty(String.valueOf(consumerAppObj))) {
  187. App consumerAppExists = appDao.findByName(String.valueOf(consumerAppObj));
  188. if (consumerAppExists == null) {
  189. throw new IllegalOperatorException("接收应用不存在");
  190. }
  191. // 获取改应用应接收的未读、未发送的需要推送的消息(取前500条)
  192. List<Message> messages = messageDao.findByConsumerAppIdAndIsReadAndIsSentNeedToSend(
  193. consumerAppExists.getId(), Constant.NO, Constant.NO);
  194. logger.info("信息数量:" + messages.size());
  195. Map<String, Object> resultMap = sendMessageByAPI(messages, consumerApp);
  196. resultMap.put("success", "success");
  197. return resultMap;
  198. } else {
  199. throw new ParameterMissingException("接收应用信息为空");
  200. }
  201. }
  202. @Override
  203. public ModelMap readMessage(String consumer) {
  204. ModelMap map = new ModelMap();
  205. map.put("success", "false");
  206. if (StringUtils.isEmpty(consumer)) {
  207. throw new ParameterMissingException("接收人信息为空,无法阅读消息");
  208. }
  209. JSONObject jsonObject = FastjsonUtils.parseObject(consumer);
  210. // 消息id
  211. Object messageId = jsonObject.get("messageId");
  212. Object receiverUu = jsonObject.get("receiverUu");
  213. Object receiverEnuu = jsonObject.get("receiverEnuu");
  214. Object consumerApp = jsonObject.get("consumerApp");
  215. if (StringUtils.isEmpty(messageId)) {
  216. map.put("data", "消息id为空,请重新确认消息信息");
  217. return map;
  218. } else {
  219. String[] messageIds = String.valueOf(messageId).split(SplitChar.COMMA);
  220. List<Message> messages = new ArrayList<>();
  221. for (String id : messageIds) {
  222. Message message = messageDao.findOne(Long.valueOf(String.valueOf(id)));
  223. if (message == null) {
  224. map.put("data", "消息不存在,请重新确认消息信息");
  225. return map;
  226. }
  227. if (receiverUu == null || StringUtils.isEmpty(String.valueOf(receiverEnuu)) || receiverEnuu == null
  228. || StringUtils.isEmpty(String.valueOf(receiverUu))
  229. || StringUtils.isEmpty(String.valueOf(consumerApp))) {
  230. map.put("data", "用户信息为空,无法读取消息");
  231. return map;
  232. }
  233. if (!Long.valueOf(String.valueOf(receiverEnuu)).equals(message.getReceiverEnuu())
  234. || !Long.valueOf(String.valueOf(receiverUu)).equals(message.getReceiverUu())) {
  235. map.put("data", "此消息不属于当前用户,请重新确认后读取");
  236. return map;
  237. }
  238. if (Constant.YES.equals(message.getIsRead())) {
  239. map.put("data", "消息已阅读");
  240. return map;
  241. }
  242. App app = appDao.findByName(String.valueOf(consumerApp));
  243. if (app == null) {
  244. map.put("data", "消费应用不存在,请重新确认用户信息");
  245. return map;
  246. }
  247. if (!message.getConsumerApp().contains(app)) {
  248. map.put("data", "消息不属于当前应用");
  249. return map;
  250. }
  251. message.setIsRead(Constant.YES);
  252. messages.add(message);
  253. }
  254. messages = messageDao.save(messages);
  255. map.put("success", "success");
  256. map.put("data", FastjsonUtils.toJson(messages));
  257. return map;
  258. }
  259. }
  260. /**
  261. * jsonObject转换为Message
  262. * @param jsonObjects jsonObjects
  263. * @return messages
  264. */
  265. private List<Message> convertFromJsonObj(List<JSONObject> jsonObjects) {
  266. List<Message> messages = new ArrayList<>();
  267. for (JSONObject jsonObject : jsonObjects) {
  268. Message message = new Message();
  269. // 消息内容
  270. Object content = jsonObject.get("content");
  271. if (StringUtils.isEmpty(content)) {
  272. throw new ParameterMissingException("消息内容为空");
  273. } else {
  274. message.setContent(String.valueOf(content));
  275. }
  276. // 发送人
  277. Object senderUu = jsonObject.get("senderUu");
  278. Object senderEnuu = jsonObject.get("senderEnuu");
  279. if (StringUtils.isEmpty(senderUu) || StringUtils.isEmpty(senderEnuu)) {
  280. throw new ParameterMissingException("发送人信息为空");
  281. } else {
  282. message.setSenderUu(Long.valueOf(String.valueOf(senderUu)));
  283. message.setSenderEnuu(Long.valueOf(String.valueOf(senderEnuu)));
  284. }
  285. // 接收人
  286. Object receiverUu = jsonObject.get("receiverUu");
  287. Object receiverEnuu = jsonObject.get("receiverEnuu");
  288. if (StringUtils.isEmpty(receiverUu) || StringUtils.isEmpty(receiverEnuu)) {
  289. throw new ParameterMissingException("接收人信息为空");
  290. } else {
  291. message.setReceiverUu(Long.valueOf(String.valueOf(receiverUu)));
  292. message.setReceiverEnuu(Long.valueOf(String.valueOf(receiverEnuu)));
  293. }
  294. // 发送应用
  295. Object producerApp = jsonObject.get("producerApp");
  296. if (StringUtils.isEmpty(producerApp)) {
  297. throw new ParameterMissingException("发送应用为空");
  298. } else {
  299. App app = appDao.findByName(String.valueOf(producerApp));
  300. if (app == null) {
  301. throw new IllegalOperatorException("发送应用不存在");
  302. }
  303. message.setProducerApp(app.getId());
  304. }
  305. // 接收应用
  306. Object consumerType = jsonObject.get("consumerType");
  307. if (StringUtils.isEmpty(consumerType)) {
  308. throw new ParameterMissingException("接收应用类型为空");
  309. } else {
  310. String type = String.valueOf(consumerType);
  311. // 消息接收类型为公共,即所有子应用共享
  312. if (ConsumeType.PUBLIC.equals(type)) {
  313. List<App> apps = appDao.findAll();
  314. message.setConsumerApp(new HashSet<>(apps));
  315. message.setConsumeType(type);
  316. } else {
  317. Object consumerApp = jsonObject.get("consumerApp");
  318. if (consumerApp == null) {
  319. throw new ParameterMissingException("接收应用为空");
  320. } else {
  321. // 取出通过逗号拼接的接收应用
  322. String[] consumers = String.valueOf(consumerApp).split(SplitChar.COMMA);
  323. if (consumers.length < 1) {
  324. throw new ParameterMissingException("接收应用为空");
  325. }
  326. Set<App> consumerApps = new HashSet<>();
  327. for (String consumer : consumers) {
  328. App app = appDao.findByName(consumer);
  329. if (app == null) {
  330. throw new IllegalOperatorException("接收应用不存在");
  331. }
  332. consumerApps.add(app);
  333. }
  334. message.setConsumerApp(consumerApps);
  335. message.setConsumeType(consumers.length > 1 ? ConsumeType.MULTI : ConsumeType.SINGLE);
  336. }
  337. }
  338. }
  339. // 消息类型(暂时未定)
  340. Object type = jsonObject.get("type");
  341. if (!StringUtils.isEmpty(type)) {
  342. message.setType(String.valueOf(type));
  343. }
  344. // 推送类型
  345. Object smsType = jsonObject.get("smsType");
  346. if (StringUtils.isEmpty(smsType)) {
  347. message.setSmsType(SMSType.DONT_SEND);
  348. } else {
  349. message.setSmsType(String.valueOf(smsType));
  350. }
  351. // 备注
  352. Object remark = jsonObject.get("remark");
  353. if (!StringUtils.isEmpty(remark)) {
  354. message.setRemark(String.valueOf(remark));
  355. }
  356. // 来源id
  357. Object sourceId = jsonObject.get("sourceId");
  358. if (!StringUtils.isEmpty(sourceId)) {
  359. message.setSourceId(Long.valueOf(String.valueOf(sourceId)));
  360. }
  361. // 邮件模板
  362. Object mailTemplate = jsonObject.get("mailTemplate");
  363. if (!StringUtils.isEmpty(mailTemplate)) {
  364. message.setMailTemplate(String.valueOf(mailTemplate));
  365. }
  366. // 短信模板
  367. Object smTemplate = jsonObject.get("smTemplate");
  368. if (!StringUtils.isEmpty(smTemplate)) {
  369. message.setSmTemplate(String.valueOf(smTemplate));
  370. }
  371. message.setIsRead(Constant.NO);
  372. message.setCreateTime(new Date());
  373. message.setIsSent(Constant.NO);
  374. messages.add(message);
  375. }
  376. return messages;
  377. }
  378. /**
  379. * 推送消息(调用邮件、短信、IM接口)
  380. * @param messages 消息
  381. * @param consumerApp 消费app
  382. * @return resultMap
  383. */
  384. private Map<String, Object> sendMessageByAPI(List<Message> messages, String consumerApp) {
  385. Map<String, Object> resultMap = new HashMap<>();
  386. if (!CollectionUtils.isEmpty(messages)) {
  387. for (Message message : messages) {
  388. try {
  389. UserView receiver = AccountUtils.getImUserByUserUU(message.getReceiverUu(),
  390. message.getReceiverEnuu());
  391. if (receiver.getName() != null) {
  392. logger.info("接收人:" + receiver.getName());
  393. UserView sender = AccountUtils.getImUserByUserUU(message.getSenderUu(),
  394. message.getSenderEnuu());
  395. if (sender != null && sender.getName() != null && sender.getSpaceName() != null) {
  396. logger.info("发送人:" + sender.getName());
  397. boolean sendSuccess = false;
  398. // 发送邮件
  399. // if (message.getSmsType().contains(SMSType.MAIL)) {
  400. // sendMail(message, receiver);
  401. // }
  402. // 发送短息
  403. if (message.getSmsType().contains(SMSType.SM)) {
  404. logger.info("开始推送短信");
  405. sendSuccess = sendSM(message, receiver, sender);
  406. }
  407. // 发送IM
  408. if (message.getSmsType().contains(SMSType.IM)) {
  409. logger.info("开始推送IM");
  410. sendSuccess = sendIM(message, receiver, consumerApp);
  411. }
  412. // 推送成功
  413. if (sendSuccess) {
  414. pushSuccess(message, receiver, sender);
  415. }
  416. } else {
  417. logger.info("发送人信息为空,uu:" + message.getSenderUu() + ",enuu:" + message.getSenderEnuu());
  418. }
  419. } else {
  420. logger.info("接收人姓名为空,uu:" + message.getReceiverUu() + ",enuu:" + message.getReceiverEnuu());
  421. }
  422. } catch (Exception e) {
  423. message.setIsSent(Constant.ERROR);
  424. logger.info(e.getMessage());
  425. }
  426. }
  427. messageDao.save(messages);
  428. }
  429. return resultMap;
  430. }
  431. /**
  432. * 发送邮件
  433. * @param message 消息
  434. * @param receiver 接收人
  435. */
  436. private void sendMail(Message message, User receiver) {
  437. if (message.getMailTemplate() != null && receiver.getSecondUID() != null) {
  438. HttpHeaders headers = new HttpHeaders();
  439. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  440. headers.setContentType(type);
  441. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  442. JSONObject object = new JSONObject();
  443. object.put("templateId", message.getMailTemplate());
  444. object.put("receiver", receiver.getSecondUID());
  445. object.put("params", message.getContent());
  446. // HttpEntity<String> formEntity = new HttpEntity<String>(, headers);
  447. try {
  448. ResponseWrap responseWrap = HttpUtil.doPost(MAIL_CONSOLE_HOST + MAIL_SEND_URL,
  449. FlexJsonUtil.toJson(object));
  450. } catch (Exception e) {
  451. e.printStackTrace();
  452. }
  453. }
  454. }
  455. /**
  456. * 发送短信
  457. * @param message 消息
  458. * @param receiver 接收人
  459. * @param sender 发送人
  460. * @return 推送成功状态
  461. */
  462. private boolean sendSM(Message message, UserView receiver, UserView sender) {
  463. if (!StringUtils.isEmpty(message.getSmTemplate()) && !StringUtils.isEmpty(receiver.getUid())) {
  464. try {
  465. SmsMessage sms = new SmsMessage();
  466. List<Object> obj = new ArrayList<Object>();
  467. switch (message.getSmTemplate()) {
  468. // 公共询价单,普通短信
  469. case "e14d502a-25b8-40a0-8acf-5c61ae38d763" :
  470. obj.add(receiver.getName());
  471. obj.add(sender.getSpaceName());
  472. obj.add(message.getRemark());
  473. pushSm(sms, message, obj, receiver, sender);
  474. break;
  475. // 公共询价单,UU互联
  476. case "6e094dcf-f2c7-462a-9686-b2d32d684a78" :
  477. obj.add(message.getSourceId());
  478. obj.add(message.getReceiverEnuu());
  479. obj.add(receiver.getUid());
  480. pushSm(sms, message, obj, receiver, sender);
  481. break;
  482. // 公共询价统计
  483. case "e6320a3c-89ac-4c77-a75f-62a727bce654" :
  484. obj.add(message.getRemark());
  485. pushSm(sms, message, obj, receiver, sender);
  486. default:
  487. break;
  488. }
  489. return true;
  490. } catch (Exception e) {
  491. logger.info("短信推送异常:" + e.getMessage());
  492. e.printStackTrace();
  493. return false;
  494. }
  495. }
  496. return false;
  497. }
  498. /**
  499. * 调用短信接口
  500. * @param sms 短信model
  501. * @param message 消息
  502. * @param param 短信参数
  503. * @param receiver 接收人
  504. * @param sender 发送人
  505. */
  506. private void pushSm(SmsMessage sms, Message message, List<Object> param, UserView receiver, UserView sender) {
  507. sms.setParams(param);
  508. sms.setReceiver(receiver.getUid());
  509. sms.setTemplateId(message.getSmTemplate());
  510. String response = com.uas.ps.message.util.HttpUtil
  511. .sendPost(messageUrl, FastjsonUtils.toJson(sms));
  512. logger.info("短信推送成功:" + response);
  513. }
  514. public static void main(String[] args) {
  515. SmsMessage smsMessage = new SmsMessage();
  516. List<Object> param = new ArrayList<>();
  517. param.add("测试公司");
  518. param.add("测试型号");
  519. smsMessage.setParams(param);
  520. smsMessage.setReceiver("13632823241");
  521. smsMessage.setTemplateId("7879bfda-f871-4a95-9430-54d1ea00b198");
  522. String response = com.uas.ps.message.util.HttpUtil
  523. .sendPost("http://10.10.100.136:8080/sms/send", FastjsonUtils.toJson(smsMessage));
  524. System.out.println(response);
  525. }
  526. /**
  527. * 设置推送送成功
  528. * @param message 消息
  529. * @param receiver 接收人
  530. * @param sender 发送人
  531. */
  532. private void pushSuccess(Message message, UserView receiver, UserView sender) {
  533. message.setIsSent(Constant.YES);
  534. message.setReceiver(FastjsonUtils.toJson(receiver));
  535. message.setSender(FastjsonUtils.toJson(sender));
  536. }
  537. /**
  538. * 发送IM
  539. * @param message 消息
  540. * @param receiver 接收人
  541. * @param consumerApp 消费app
  542. * @return 推送成功状态
  543. */
  544. private boolean sendIM(Message message, UserView receiver, String consumerApp) {
  545. Map<String, Object> params = new HashMap<>();
  546. if (!StringUtils.isEmpty(receiver.getDialectUID())) {
  547. params.put("master", receiver.getSpaceName()); // 账套 公司名称
  548. params.put("userid", receiver.getDialectUID()); // 推送目标用户
  549. params.put("title", message.getType() == null ? "" : message.getType()); // 推送标题
  550. params.put("content", message.getContent()); // 正文
  551. params.put("enUU", message.getReceiverEnuu()); // UU号
  552. params.put("masterId", null);
  553. params.put("url", String.format(PUBLIC_INQUIRY_IM_URL, message.getSourceId(),
  554. message.getReceiverEnuu(), receiver.getUid())); // 跳转链接地址
  555. params.put("pageTitle", message.getType() == null ? "" : message.getType());
  556. params.put("platform", "B2B"); // 系统名称,ERP或B2B
  557. try {
  558. ResponseWrap res = HttpUtil.doPost(PROD_URL, params);
  559. if (res.isSuccess()) {
  560. logger.info("IM推送成功");
  561. return true;
  562. } else {
  563. logger.info("IM推送失败:" + res.getContent());
  564. }
  565. } catch (Exception e) {
  566. e.printStackTrace();
  567. return false;
  568. }
  569. }
  570. return false;
  571. }
  572. }