package com.fr.plugin.yt; import com.fanruan.api.log.LogKit; import com.fr.base.Base64; import com.fr.base.FRContext; import com.fr.base.ServerConfig; import com.fr.decision.authority.data.User; import com.fr.decision.system.bean.message.MessageUrlType; import com.fr.decision.webservice.v10.user.UserService; import com.fr.general.IOUtils; import com.fr.json.JSONObject; import com.fr.plugin.beans.YTOutputBean; import com.fr.plugin.dao.MyEntityDao; import com.fr.plugin.dao.MyUserDao; import com.fr.plugin.entitys.YTUserEntity; import com.fr.plugin.entitys.YtOutputMyEntity; import com.fr.plugin.utils.YiTuanMaManager; import com.fr.plugin.utils.YiTuanMaUtils; import com.fr.schedule.base.constant.ScheduleConstants; import com.fr.schedule.feature.output.OutputActionHandler; import com.fr.stable.CodeUtils; import com.fr.stable.StringUtils; import com.fr.stable.db.action.DBAction; import com.fr.stable.db.dao.DAOContext; import com.fr.stable.query.QueryFactory; import com.fr.stable.query.condition.QueryCondition; import com.fr.stable.query.restriction.RestrictionFactory; import java.io.InputStream; import java.util.*; public class MyOutputActionHandler extends OutputActionHandler { //应用消息,群,个人 @Override public void doAction(YTOutputBean myOutputBean, Map map) throws Exception { Object username = map.get("username"); String[] names = new String[0]; if (username != null) { String s = username.toString(); if (StringUtils.isNotBlank(s)) { names = new String[]{ s }; } } if (names.length == 0) { names = (String[]) map.get(ScheduleConstants.USERNAMES); } List userNames = Arrays.asList(names); List userIds = userName2ytUserId(userNames); int todo = myOutputBean.getSendType(); String title = myOutputBean.getSubject(); String content = myOutputBean.getContent(); JSONObject param = JSONObject.create(); //(取值 2:单文本,5:文本链接,6:图文链接) String url = createScheduleEntryUrl(myOutputBean, map); YiTuanMaManager manager = YiTuanMaManager.getInstance(); String mediaId = myOutputBean.getMediaId(); String fromUserId = myOutputBean.getFromUserId(); String imageUrl = ""; if (StringUtils.isNotBlank(mediaId)) { imageUrl = manager.getUrl() + "/url/ytImage?attachId=" + mediaId; } String groupId = manager.getGroupId(); if (todo == 1) {//应用消息 YiTuanMaUtils.sendMsgToUserFromApp(myOutputBean.getClientId(), userIds, title, content, url, imageUrl); } else if (todo == 2) {//聊天群消息 YiTuanMaUtils.sendMsgToGroup(myOutputBean.getClientId(),groupId, fromUserId, title, content, url, imageUrl); } else if (todo == 3) {//公共号 YiTuanMaUtils.sendMsgToUserFromPublicNum(groupId, userIds, title, content, url); } else if (todo == 4) {//个人 YiTuanMaUtils.sendMSGtoUser(myOutputBean.getClientId(),fromUserId, userIds, title, content, url, imageUrl); } LogKit.info("推送日志:" + map); } private List userName2ytUserId(List userNames) { try { List userPhones = new ArrayList<>(userNames.size()); for (String userName : userNames) { User user = UserService.getInstance().getUserByUserName(userName); if (user != null) { String mobile = user.getMobile(); if (StringUtils.isNotBlank(mobile)) { userPhones.add(mobile); } } } List entities = MyCoreDBAccess.getAccessor().runDMLAction(new DBAction>() { public List run(DAOContext content) throws Exception { HashSet phones = new HashSet<>(userPhones); QueryCondition queryCondition = QueryFactory.create().addRestriction(RestrictionFactory.in("phone", phones)); return content.getDAO(MyUserDao.class).find(queryCondition); } }); if (entities != null) { List userIds = new ArrayList<>(); for (YTUserEntity entity : entities) { String id = entity.getId(); userIds.add(id); } return userIds; } } catch (Exception e) { e.printStackTrace(); } return new ArrayList<>(); } public static String createScheduleEntryUrl(YTOutputBean myBean, Map params) throws Exception { String callBack = ""; String servletName = ServerConfig.getInstance().getServletName(); String baseUrl = YiTuanMaManager.getInstance().getUrl(); String url; if (myBean.getLinkOpenType() == MessageUrlType.INNER.toInt()) { String taskName = CodeUtils.cjkEncode(CodeUtils.encodeURIComponent((String) params.get("taskName"))); String savePath = CodeUtils.cjkEncode(CodeUtils.encodeURIComponent((String) params.get("saveDirectory"))); savePath = savePath.replaceAll("\\+", "%20"); String showtype = (String) params.get("showType"); int taskType = (Integer) params.get("taskType"); String username = (String) params.get("username"); callBack = baseUrl + "/url/mobile/schedule/result?taskName=" + taskName + "&username=" + username + "&path=" + savePath + "&showType=" + showtype + "&taskType=" + taskType + "&terminal=H5"; //2022-06-30 更新回调方式为UUID url = baseUrl + "/url/ytlogin?callBack=" + myBean.getId(); updateOutUrl(myBean.getId(),callBack); } else { url = myBean.getCustomizeLink() ; } return url; } public static void updateOutUrl(String id,String url){ try { MyDecisionDBAccess.getAccessor().runDMLAction(new DBAction() { @Override public Object run(DAOContext daoContext) throws Exception { MyEntityDao dao = daoContext.getDAO(MyEntityDao.class); YtOutputMyEntity myEntity = dao.findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("id", id))); myEntity.setGoUrl(url); dao.update(myEntity); return null; } }); } catch (Exception e) { e.printStackTrace(); } return; } public static String toBase64(InputStream paramInputStream) { byte[] arrayOfByte = IOUtils.inputStream2Bytes(paramInputStream); return Base64.encode(arrayOfByte); } private String getResultUrl(Map params) { ServerConfig.getInstance().getReportServletMapping(); String url = ServerConfig.getInstance().getServletName() + "/schedule/result?taskName=%s&username=%s&path=%s&showType=%s&taskType=%s"; String taskName = CodeUtils.encodeURIComponent((String) params.get("taskName")); String saveDirectory = CodeUtils.encodeURIComponent((String) params.get("saveDirectory")); saveDirectory = saveDirectory.replaceAll("\\+", "%20"); String showType = (String) params.get("showType"); int taskType = (Integer) params.get("taskType"); String username = (String) params.get("username"); return String.format(url, taskName, username, saveDirectory, showType, taskType); } }