diff --git a/contact-center/app/src/main/java/com/chatopera/cc/app/basic/MainUtils.java b/contact-center/app/src/main/java/com/chatopera/cc/app/basic/MainUtils.java index decdc172..ce592ddd 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/app/basic/MainUtils.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/app/basic/MainUtils.java @@ -16,47 +16,34 @@ */ package com.chatopera.cc.app.basic; -import java.beans.BeanInfo; -import java.beans.Introspector; -import java.beans.PropertyDescriptor; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.StringWriter; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.security.NoSuchAlgorithmException; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Date; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.UUID; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import com.chatopera.cc.util.*; -import com.chatopera.cc.util.asr.AsrResult; +import com.aliyuncs.DefaultAcsClient; +import com.aliyuncs.IAcsClient; +import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; +import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; +import com.aliyuncs.http.MethodType; +import com.aliyuncs.profile.DefaultProfile; +import com.aliyuncs.profile.IClientProfile; +import com.chatopera.cc.app.cache.CacheHelper; +import com.chatopera.cc.app.model.*; +import com.chatopera.cc.app.persistence.repository.*; import com.chatopera.cc.concurrent.chatbot.ChatbotEvent; import com.chatopera.cc.concurrent.multiupdate.MultiUpdateEvent; import com.chatopera.cc.concurrent.user.UserDataEvent; import com.chatopera.cc.exchange.UserEvent; +import com.chatopera.cc.util.*; +import com.chatopera.cc.util.WebIMReport; +import com.chatopera.cc.util.asr.AsrResult; import com.chatopera.cc.util.mail.MailSender; -import com.chatopera.cc.app.cache.CacheHelper; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.googlecode.aviator.AviatorEvaluator; +import com.lmax.disruptor.dsl.Disruptor; +import freemarker.template.Configuration; +import freemarker.template.TemplateException; +import io.netty.handler.codec.http.HttpHeaders; +import net.coobird.thumbnailator.Thumbnails; import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.beanutils.ConversionException; import org.apache.commons.beanutils.ConvertUtils; @@ -76,1176 +63,1159 @@ import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.web.multipart.MultipartFile; -import com.aliyuncs.DefaultAcsClient; -import com.aliyuncs.IAcsClient; -import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; -import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; -import com.aliyuncs.http.MethodType; -import com.aliyuncs.profile.DefaultProfile; -import com.aliyuncs.profile.IClientProfile; -import com.fasterxml.jackson.annotation.JsonInclude.Include; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.googlecode.aviator.AviatorEvaluator; -import com.lmax.disruptor.dsl.Disruptor; -import com.chatopera.cc.app.persistence.repository.AdTypeRepository; -import com.chatopera.cc.app.persistence.repository.AreaTypeRepository; -import com.chatopera.cc.app.persistence.repository.AttachmentRepository; -import com.chatopera.cc.app.persistence.repository.SecretRepository; -import com.chatopera.cc.app.persistence.repository.SystemConfigRepository; -import com.chatopera.cc.app.persistence.repository.SystemMessageRepository; -import com.chatopera.cc.app.persistence.repository.TablePropertiesRepository; -import com.chatopera.cc.app.persistence.repository.TemplateRepository; -import com.chatopera.cc.app.model.AdType; -import com.chatopera.cc.app.model.AttachmentFile; -import com.chatopera.cc.app.model.JobDetail; -import com.chatopera.cc.app.model.JobTask; -import com.chatopera.cc.app.model.Secret; -import com.chatopera.cc.app.model.SysDic; -import com.chatopera.cc.app.model.SystemConfig; -import com.chatopera.cc.app.model.SystemMessage; -import com.chatopera.cc.app.model.TableProperties; -import com.chatopera.cc.app.model.Template; -import com.chatopera.cc.app.model.UKeFuDic; -import com.chatopera.cc.app.model.User; -import com.chatopera.cc.app.model.WorkOrders; -import com.chatopera.cc.app.model.WorkSession; - -import freemarker.template.Configuration; -import freemarker.template.TemplateException; -import io.netty.handler.codec.http.HttpHeaders; -import net.coobird.thumbnailator.Thumbnails; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.beans.BeanInfo; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.io.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.security.NoSuchAlgorithmException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class MainUtils { - private static MD5 md5 = new MD5(); - - public static SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ; - - public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd") ; - - public static SimpleDateFormat timeRangeDateFormat = new SimpleDateFormat("HH:mm"); - - /** - * 当前时间+已过随机生成的 长整形数字 - * @return - */ - public static String genID(){ - return Base62.encode(getUUID()).toLowerCase() ; - } - - public static String genIDByKey(String key){ - return Base62.encode(key).toLowerCase() ; - } - - public static String getUUID(){ - return UUID.randomUUID().toString().replace("-", "") ; - } - - public static String getContextID(String session){ - return session.replaceAll("-", "") ; - } - - public static String md5(String str) { - return md5.getMD5ofStr(md5.getMD5ofStr(str)); - } - - public static String md5(byte[] bytes) { - return md5.getMD5ofByte(bytes); - } - - public static void copyProperties(Object source, Object target,String... ignoreProperties) - throws BeansException { - - Assert.notNull(source, "Source must not be null"); - Assert.notNull(target, "Target must not be null"); - - Class actualEditable = target.getClass(); - PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable); - List ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null; - - for (PropertyDescriptor targetPd : targetPds) { - Method writeMethod = targetPd.getWriteMethod(); - if (writeMethod != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) { - PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName()); - if (sourcePd != null && !targetPd.getName().equalsIgnoreCase("id")) { - Method readMethod = sourcePd.getReadMethod(); - if (readMethod != null && - ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) { - try { - if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { - readMethod.setAccessible(true); - } - Object value = readMethod.invoke(source); - if(value != null){ //只拷贝不为null的属性 by zhao - if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { - writeMethod.setAccessible(true); - } - writeMethod.invoke(target, value); - } - } - catch (Throwable ex) { - throw new FatalBeanException( - "Could not copy property '" + targetPd.getName() + "' from source to target", ex); - } - } - } - } - } - } - - public static long ipToLong(String ipAddress) { - long result = 0; - String[] ipAddressInArray = ipAddress.split("\\."); - if(ipAddressInArray!=null && ipAddressInArray.length == 4){ - for (int i = 3; i >= 0; i--) { - long ip = Long.parseLong(ipAddressInArray[3 - i]); - - // left shifting 24,16,8,0 and bitwise OR - - // 1. 192 << 24 - // 1. 168 << 16 - // 1. 1 << 8 - // 1. 2 << 0 - result |= ip << (i * 8); - - } - } - return result; - } + private static MD5 md5 = new MD5(); - public static String longToIp2(long ip) { + public static SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - return ((ip >> 24) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." - + ((ip >> 8) & 0xFF) + "." + (ip & 0xFF); - } - /*** - * ID编码 , 发送对话的时候使用 - * @param id - * @param nid - * @return - */ - public static String genNewID(String id , String nid){ - StringBuffer strb = new StringBuffer(); - if(id!=null && nid!=null){ - int length = Math.max(id.length(), nid.length()); - for(int i=0 ; i i && id.length() > i){ - int cur = (id.charAt(i) + nid.charAt(i)) / 2 ; - strb.append((char)cur) ; - }else if(nid.length() > i){ - strb.append(nid.charAt(i)) ; - }else{ - strb.append(id.charAt(i)) ; - } - } - } - return strb.toString() ; - } - - public static void published(UserEvent event){ - @SuppressWarnings("unchecked") - Disruptor disruptor = (Disruptor) MainContext.getContext().getBean("disruptor") ; - long seq = disruptor.getRingBuffer().next(); - disruptor.getRingBuffer().get(seq).setEvent(event); ; - disruptor.getRingBuffer().publish(seq); - } - /** - * - * @param request - * @return - */ - public static Map getRequestParam(HttpServletRequest request){ - Map values = new HashMap(); - Enumeration enums = request.getParameterNames() ; - while(enums.hasMoreElements()){ - String param = enums.nextElement() ; - values.put(param,request.getParameter(param)) ; - } - return values; - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - public static void multiupdate(MultiUpdateEvent event){ - Disruptor disruptor = (Disruptor) MainContext.getContext().getBean("multiupdate") ; - long seq = disruptor.getRingBuffer().next(); - disruptor.getRingBuffer().get(seq).setEvent(event); - disruptor.getRingBuffer().publish(seq); - } - - @SuppressWarnings({ "unchecked"}) - public static void chatbot(ChatbotEvent event){ - Disruptor disruptor = (Disruptor) MainContext.getContext().getBean("chatbot") ; - long seq = disruptor.getRingBuffer().next(); - disruptor.getRingBuffer().get(seq).setEvent(event); - disruptor.getRingBuffer().publish(seq); - } + public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + + public static SimpleDateFormat timeRangeDateFormat = new SimpleDateFormat("HH:mm"); + + /** + * 当前时间+已过随机生成的 长整形数字 + * + * @return + */ + public static String genID() { + return Base62.encode(getUUID()).toLowerCase(); + } + + public static String genIDByKey(String key) { + return Base62.encode(key).toLowerCase(); + } + + public static String getUUID() { + return UUID.randomUUID().toString().replace("-", ""); + } + + public static String getContextID(String session) { + return session.replaceAll("-", ""); + } + + public static String md5(String str) { + return md5.getMD5ofStr(md5.getMD5ofStr(str)); + } + + public static String md5(byte[] bytes) { + return md5.getMD5ofByte(bytes); + } + + public static void copyProperties(Object source, Object target, String... ignoreProperties) + throws BeansException { + + Assert.notNull(source, "Source must not be null"); + Assert.notNull(target, "Target must not be null"); + + Class actualEditable = target.getClass(); + PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable); + List ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null; + + for (PropertyDescriptor targetPd : targetPds) { + Method writeMethod = targetPd.getWriteMethod(); + if (writeMethod != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) { + PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName()); + if (sourcePd != null && !targetPd.getName().equalsIgnoreCase("id")) { + Method readMethod = sourcePd.getReadMethod(); + if (readMethod != null && + ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) { + try { + if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { + readMethod.setAccessible(true); + } + Object value = readMethod.invoke(source); + if (value != null) { //只拷贝不为null的属性 by zhao + if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { + writeMethod.setAccessible(true); + } + writeMethod.invoke(target, value); + } + } catch (Throwable ex) { + throw new FatalBeanException( + "Could not copy property '" + targetPd.getName() + "' from source to target", ex); + } + } + } + } + } + } + + public static long ipToLong(String ipAddress) { + long result = 0; + String[] ipAddressInArray = ipAddress.split("\\."); + if (ipAddressInArray != null && ipAddressInArray.length == 4) { + for (int i = 3; i >= 0; i--) { + long ip = Long.parseLong(ipAddressInArray[3 - i]); + + // left shifting 24,16,8,0 and bitwise OR + + // 1. 192 << 24 + // 1. 168 << 16 + // 1. 1 << 8 + // 1. 2 << 0 + result |= ip << (i * 8); + + } + } + return result; + } + + public static String longToIp2(long ip) { + + return ((ip >> 24) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + + ((ip >> 8) & 0xFF) + "." + (ip & 0xFF); + } + + /*** + * ID编码 , 发送对话的时候使用 + * @param id + * @param nid + * @return + */ + public static String genNewID(String id, String nid) { + StringBuffer strb = new StringBuffer(); + if (id != null && nid != null) { + int length = Math.max(id.length(), nid.length()); + for (int i = 0; i < length; i++) { + if (nid.length() > i && id.length() > i) { + int cur = (id.charAt(i) + nid.charAt(i)) / 2; + strb.append((char) cur); + } else if (nid.length() > i) { + strb.append(nid.charAt(i)); + } else { + strb.append(id.charAt(i)); + } + } + } + return strb.toString(); + } + + public static void published(UserEvent event) { + @SuppressWarnings("unchecked") + Disruptor disruptor = (Disruptor) MainContext.getContext().getBean("disruptor"); + long seq = disruptor.getRingBuffer().next(); + disruptor.getRingBuffer().get(seq).setEvent(event); + ; + disruptor.getRingBuffer().publish(seq); + } + + /** + * @param request + * @return + */ + public static Map getRequestParam(HttpServletRequest request) { + Map values = new HashMap(); + Enumeration enums = request.getParameterNames(); + while (enums.hasMoreElements()) { + String param = enums.nextElement(); + values.put(param, request.getParameter(param)); + } + return values; + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + public static void multiupdate(MultiUpdateEvent event) { + Disruptor disruptor = (Disruptor) MainContext.getContext().getBean("multiupdate"); + long seq = disruptor.getRingBuffer().next(); + disruptor.getRingBuffer().get(seq).setEvent(event); + disruptor.getRingBuffer().publish(seq); + } + + @SuppressWarnings({"unchecked"}) + public static void chatbot(ChatbotEvent event) { + Disruptor disruptor = (Disruptor) MainContext.getContext().getBean("chatbot"); + long seq = disruptor.getRingBuffer().next(); + disruptor.getRingBuffer().get(seq).setEvent(event); + disruptor.getRingBuffer().publish(seq); + } + + /** + * @param request + * @return + */ + public static String getParameter(HttpServletRequest request) { + Enumeration names = request.getParameterNames(); + StringBuffer strb = new StringBuffer(); + while (names.hasMoreElements()) { + String name = names.nextElement(); + if (name.indexOf("password") < 0) { //不记录 任何包含 password 的参数内容 + if (strb.length() > 0) { + strb.append(","); + } + strb.append(name).append("=").append(request.getParameter(name)); + } + } + return strb.toString(); + + } + + /** + * 获取一天的开始时间 + * + * @return + */ + public static Date getStartTime() { + Calendar todayStart = Calendar.getInstance(); + todayStart.set(Calendar.HOUR_OF_DAY, 0); + todayStart.set(Calendar.MINUTE, 0); + todayStart.set(Calendar.SECOND, 0); + todayStart.set(Calendar.MILLISECOND, 0); + return todayStart.getTime(); + } + + /** + * 获取一天的开始时间 + * + * @return + */ + public static Date getWeekStartTime() { + Calendar weekStart = Calendar.getInstance(); + weekStart.set(weekStart.get(Calendar.YEAR), weekStart.get(Calendar.MONDAY), weekStart.get(Calendar.DAY_OF_MONTH), 0, 0, 0); + weekStart.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); + return weekStart.getTime(); + } + + /** + * 获取一天的开始时间 + * + * @return + */ + public static Date getLast30Day() { + Calendar todayStart = Calendar.getInstance(); + todayStart.set(Calendar.DAY_OF_MONTH, -30); + todayStart.set(Calendar.HOUR_OF_DAY, 0); + todayStart.set(Calendar.MINUTE, 0); + todayStart.set(Calendar.SECOND, 0); + todayStart.set(Calendar.MILLISECOND, 0); + return todayStart.getTime(); + } + + /** + * 获取一天的开始时间 + * + * @return + */ + public static Date getLastDay(int days) { + Calendar todayStart = Calendar.getInstance(); + todayStart.set(Calendar.DAY_OF_MONTH, -days); + todayStart.set(Calendar.HOUR_OF_DAY, 0); + todayStart.set(Calendar.MINUTE, 0); + todayStart.set(Calendar.SECOND, 0); + todayStart.set(Calendar.MILLISECOND, 0); + return todayStart.getTime(); + } + + /** + * 获取一天的结束时间 + * + * @return + */ + public static Date getEndTime() { + Calendar todayEnd = Calendar.getInstance(); + todayEnd.set(Calendar.HOUR_OF_DAY, 23); + todayEnd.set(Calendar.MINUTE, 59); + todayEnd.set(Calendar.SECOND, 59); + todayEnd.set(Calendar.MILLISECOND, 999); + return todayEnd.getTime(); + } + + /** + * 获取一天的结束时间 + * + * @return + */ + public static Date getLastTime(int secs) { + Calendar todayEnd = Calendar.getInstance(); + todayEnd.add(Calendar.SECOND, secs * -1); + return todayEnd.getTime(); + } + + public static void noCacheResponse(HttpServletResponse response) { + response.setDateHeader("Expires", 0); + response.setHeader("Buffer", "True"); + response.setHeader("Cache-Control", "no-cache"); + response.setHeader("Cache-Control", "no-store"); + response.setHeader("Expires", "0"); + response.setHeader("ETag", String.valueOf(System.currentTimeMillis())); + response.setHeader("Pragma", "no-cache"); + response.setHeader("Date", String.valueOf(new Date())); + response.setHeader("Last-Modified", String.valueOf(new Date())); + } + + public static BrowserClient parseClient(HttpServletRequest request) { + BrowserClient client = new BrowserClient(); + String browserDetails = request.getHeader("User-Agent"); + String userAgent = browserDetails; + String user = userAgent.toLowerCase(); + String os = ""; + String browser = "", version = ""; - /** - * - * @param request - * @return - */ - public static String getParameter(HttpServletRequest request){ - Enumeration names = request.getParameterNames() ; - StringBuffer strb = new StringBuffer(); - while(names.hasMoreElements()){ - String name = names.nextElement() ; - if(name.indexOf("password") < 0){ //不记录 任何包含 password 的参数内容 - if(strb.length() > 0){ - strb.append(",") ; - } - strb.append(name).append("=").append(request.getParameter(name)) ; - } - } - return strb.toString() ; - - } - - /** - * 获取一天的开始时间 - * @return - */ - public static Date getStartTime(){ - Calendar todayStart = Calendar.getInstance(); - todayStart.set(Calendar.HOUR_OF_DAY, 0); - todayStart.set(Calendar.MINUTE, 0); - todayStart.set(Calendar.SECOND, 0); - todayStart.set(Calendar.MILLISECOND, 0); - return todayStart.getTime(); - } - - /** - * 获取一天的开始时间 - * @return - */ - public static Date getWeekStartTime(){ - Calendar weekStart = Calendar.getInstance(); - weekStart.set(weekStart.get(Calendar.YEAR), weekStart.get(Calendar.MONDAY), weekStart.get(Calendar.DAY_OF_MONTH), 0, 0, 0); - weekStart.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); - return weekStart.getTime(); - } - - /** - * 获取一天的开始时间 - * @return - */ - public static Date getLast30Day(){ - Calendar todayStart = Calendar.getInstance(); - todayStart.set(Calendar.DAY_OF_MONTH, -30); - todayStart.set(Calendar.HOUR_OF_DAY, 0); - todayStart.set(Calendar.MINUTE, 0); - todayStart.set(Calendar.SECOND, 0); - todayStart.set(Calendar.MILLISECOND, 0); - return todayStart.getTime(); - } - - /** - * 获取一天的开始时间 - * @return - */ - public static Date getLastDay(int days){ - Calendar todayStart = Calendar.getInstance(); - todayStart.set(Calendar.DAY_OF_MONTH, -days); - todayStart.set(Calendar.HOUR_OF_DAY, 0); - todayStart.set(Calendar.MINUTE, 0); - todayStart.set(Calendar.SECOND, 0); - todayStart.set(Calendar.MILLISECOND, 0); - return todayStart.getTime(); - } - - /** - * 获取一天的结束时间 - * @return - */ - public static Date getEndTime(){ - Calendar todayEnd = Calendar.getInstance(); - todayEnd.set(Calendar.HOUR_OF_DAY, 23); - todayEnd.set(Calendar.MINUTE, 59); - todayEnd.set(Calendar.SECOND, 59); - todayEnd.set(Calendar.MILLISECOND, 999); - return todayEnd.getTime(); - } - - /** - * 获取一天的结束时间 - * @return - */ - public static Date getLastTime(int secs){ - Calendar todayEnd = Calendar.getInstance(); - todayEnd.add(Calendar.SECOND, secs*-1); - return todayEnd.getTime(); - } - - public static void noCacheResponse(HttpServletResponse response){ - response.setDateHeader("Expires",0); - response.setHeader("Buffer","True"); - response.setHeader("Cache-Control","no-cache"); - response.setHeader("Cache-Control","no-store"); - response.setHeader("Expires","0"); - response.setHeader("ETag",String.valueOf(System.currentTimeMillis())); - response.setHeader("Pragma","no-cache"); - response.setHeader("Date",String.valueOf(new Date())); - response.setHeader("Last-Modified",String.valueOf(new Date())); - } - - public static BrowserClient parseClient(HttpServletRequest request){ - BrowserClient client = new BrowserClient() ; - String browserDetails = request.getHeader("User-Agent"); - String userAgent = browserDetails; - String user = userAgent.toLowerCase(); - String os = ""; - String browser = "" , version = ""; - //=================OS======================= - if (userAgent.toLowerCase().indexOf("windows") >= 0 ) - { - os = "windows"; - } else if(userAgent.toLowerCase().indexOf("mac") >= 0) - { - os = "mac"; - } else if(userAgent.toLowerCase().indexOf("x11") >= 0) - { - os = "unix"; - } else if(userAgent.toLowerCase().indexOf("android") >= 0) - { - os = "android"; - } else if(userAgent.toLowerCase().indexOf("iphone") >= 0) - { - os = "iphone"; - }else{ - os = "UnKnown"; - } - //===============Browser=========================== - if(user.contains("qqbrowser")) - { - browser = "QQBrowser" ; - }else if (user.contains("msie") || user.indexOf("rv:11") > -1) - { - if(user.indexOf("rv:11") >= 0){ - browser = "IE11" ; - }else{ - String substring=userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0]; - browser=substring.split(" ")[0].replace("MSIE", "IE")+substring.split(" ")[1]; - } - }else if (user.contains("trident")) - { - browser= "IE 11" ; - }else if (user.contains("edge")) - { - browser= "Edge" ; - } else if (user.contains("safari") && user.contains("version")) - { + if (userAgent.toLowerCase().indexOf("windows") >= 0) { + os = "windows"; + } else if (userAgent.toLowerCase().indexOf("mac") >= 0) { + os = "mac"; + } else if (userAgent.toLowerCase().indexOf("x11") >= 0) { + os = "unix"; + } else if (userAgent.toLowerCase().indexOf("android") >= 0) { + os = "android"; + } else if (userAgent.toLowerCase().indexOf("iphone") >= 0) { + os = "iphone"; + } else { + os = "UnKnown"; + } + //===============Browser=========================== + if (user.contains("qqbrowser")) { + browser = "QQBrowser"; + } else if (user.contains("msie") || user.indexOf("rv:11") > -1) { + if (user.indexOf("rv:11") >= 0) { + browser = "IE11"; + } else { + String substring = userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0]; + browser = substring.split(" ")[0].replace("MSIE", "IE") + substring.split(" ")[1]; + } + } else if (user.contains("trident")) { + browser = "IE 11"; + } else if (user.contains("edge")) { + browser = "Edge"; + } else if (user.contains("safari") && user.contains("version")) { browser = (userAgent.substring(userAgent.indexOf("Safari")).split(" ")[0]).split("/")[0]; - version = (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1] ; - } else if ( user.contains("opr") || user.contains("opera")) - { - if(user.contains("opera")) - browser=(userAgent.substring(userAgent.indexOf("Opera")).split(" ")[0]).split("/")[0]+"-"+(userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1]; - else if(user.contains("opr")) - browser=((userAgent.substring(userAgent.indexOf("OPR")).split(" ")[0]).replace("/", "-")).replace("OPR", "Opera"); - } else if (user.contains("chrome")) - { + version = (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1]; + } else if (user.contains("opr") || user.contains("opera")) { + if (user.contains("opera")) + browser = (userAgent.substring(userAgent.indexOf("Opera")).split(" ")[0]).split("/")[0] + "-" + (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1]; + else if (user.contains("opr")) + browser = ((userAgent.substring(userAgent.indexOf("OPR")).split(" ")[0]).replace("/", "-")).replace("OPR", "Opera"); + } else if (user.contains("chrome")) { browser = "Chrome"; - } else if ((user.indexOf("mozilla/7.0") > -1) || (user.indexOf("netscape6") != -1) || (user.indexOf("mozilla/4.7") != -1) || (user.indexOf("mozilla/4.78") != -1) || (user.indexOf("mozilla/4.08") != -1) || (user.indexOf("mozilla/3") != -1) ) - { + } else if ((user.indexOf("mozilla/7.0") > -1) || (user.indexOf("netscape6") != -1) || (user.indexOf("mozilla/4.7") != -1) || (user.indexOf("mozilla/4.78") != -1) || (user.indexOf("mozilla/4.08") != -1) || (user.indexOf("mozilla/3") != -1)) { //browser=(userAgent.substring(userAgent.indexOf("MSIE")).split(" ")[0]).replace("/", "-"); browser = "Netscape-?"; - }else if ((user.indexOf("mozilla") > -1)) - { + } else if ((user.indexOf("mozilla") > -1)) { //browser=(userAgent.substring(userAgent.indexOf("MSIE")).split(" ")[0]).replace("/", "-"); - if(browserDetails.indexOf(" ") > 0){ - browser = browserDetails.substring(0 , browserDetails.indexOf(" ")); - }else{ - browser = "Mozilla" ; - } + if (browserDetails.indexOf(" ") > 0) { + browser = browserDetails.substring(0, browserDetails.indexOf(" ")); + } else { + browser = "Mozilla"; + } - } else if (user.contains("firefox")) - { - browser=(userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0]).replace("/", "-"); - } else if(user.contains("rv")) - { - browser="ie"; - } else - { + } else if (user.contains("firefox")) { + browser = (userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0]).replace("/", "-"); + } else if (user.contains("rv")) { + browser = "ie"; + } else { browser = "UnKnown"; } client.setUseragent(browserDetails); client.setOs(os); client.setBrowser(browser); client.setVersion(version); - - return client ; - } - /** - * 活动JPA统计结果 - * @param values - * @return - */ - public static WebIMReport getWebIMReport(List values){ - WebIMReport report = new WebIMReport(); - if(values!=null && values.size()>0){ - Object[] value = (Object[]) values.get(0) ; - if(value.length>=2){ - report.setIpnums((long) value[0]); - report.setPvnums((long) value[1]); - } - } - return report ; - } - - /** - * 活动JPA统计结果 - * @param values - * @return - */ - public static WebIMReport getWebIMInviteStatus(List values){ - WebIMReport report = new WebIMReport(); - if(values!=null && values.size()>0){ - - for(int i= 0 ; i=2){ - String invitestatus = (String) value[0] ; - if(MainContext.OnlineUserInviteStatus.DEFAULT.toString().equals(invitestatus) || invitestatus == null){ - report.setUsers((long) value[1]); - }else if(MainContext.OnlineUserInviteStatus.INVITE.toString().equals(invitestatus)){ - report.setInviteusers((long) value[1]); - }else if(MainContext.OnlineUserInviteStatus.REFUSE.toString().equals(invitestatus)){ - report.setRefuseusers((long) value[1]); - } - } - } - } - return report ; - } - - /** - * 活动JPA统计结果 - * @param values - * @return - */ - public static List getWebIMInviteAgg(List values){ - List webIMReportList = new ArrayList() ; - if(values!=null && values.size()>0){ - for(int i= 0 ; i getWebIMDataAgg(List values){ - List webIMReportList = new ArrayList() ; - if(values!=null && values.size()>0){ - for(int i= 0 ; i values){ - WebIMReport report = new WebIMReport(); - if(values!=null && values.size()>0){ - - for(int i= 0 ; i=2){ - String invitestatus = (String) value[0] ; - if(MainContext.OnlineUserInviteStatus.DEFAULT.toString().equals(invitestatus) || invitestatus == null){ - report.setUsers((long) value[1]); - }else if(MainContext.OnlineUserInviteStatus.ACCEPT.toString().equals(invitestatus)){ - report.setInviteusers((long) value[1]); - }else if(MainContext.OnlineUserInviteStatus.REFUSE.toString().equals(invitestatus)){ - report.setRefuseusers((long) value[1]); - } - } - } - } - return report ; - } - - /** - * 活动JPA统计结果 - * @param values - * @return - */ - public static WeiXinReport getWeiXinReportResult(List values){ - WeiXinReport report = new WeiXinReport(); - if(values!=null && values.size()>0){ - for(int i= 0 ; i=2){ - String event = (String) value[0] ; - if(MainContext.WeiXinEventTypeEnum.SUB.toString().equals(event)){ - report.setSubs((long) value[1]); - }else if(MainContext.WeiXinEventTypeEnum.UNSUB.toString().equals(event)){ - report.setUnsubs((long) value[1]); - } - } - } - } - return report ; - } - - public static Map transBean2Map(Object obj) { - - if(obj == null){ - return null; - } - Map map = new HashMap(); - try { - BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); - PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); - for (PropertyDescriptor property : propertyDescriptors) { - String key = property.getName(); - - // 过滤class属性 - if (!key.equals("class")) { - // 得到property对应的getter方法 - - Method readMethod = property.getReadMethod(); - - if (readMethod != null) { - Object value = readMethod.invoke(obj); - if(value instanceof Date){ - value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) value) ; - } - map.put(key, value); - } - } - } - } catch (Exception e) { - System.out.println("transBean2Map Error " + e); - } - - return map; - + + return client; + } + + /** + * 活动JPA统计结果 + * + * @param values + * @return + */ + public static WebIMReport getWebIMReport(List values) { + WebIMReport report = new WebIMReport(); + if (values != null && values.size() > 0) { + Object[] value = (Object[]) values.get(0); + if (value.length >= 2) { + report.setIpnums((long) value[0]); + report.setPvnums((long) value[1]); + } + } + return report; + } + + /** + * 活动JPA统计结果 + * + * @param values + * @return + */ + public static WebIMReport getWebIMInviteStatus(List values) { + WebIMReport report = new WebIMReport(); + if (values != null && values.size() > 0) { + + for (int i = 0; i < values.size(); i++) { + Object[] value = (Object[]) values.get(i); + if (value.length >= 2) { + String invitestatus = (String) value[0]; + if (MainContext.OnlineUserInviteStatus.DEFAULT.toString().equals(invitestatus) || invitestatus == null) { + report.setUsers((long) value[1]); + } else if (MainContext.OnlineUserInviteStatus.INVITE.toString().equals(invitestatus)) { + report.setInviteusers((long) value[1]); + } else if (MainContext.OnlineUserInviteStatus.REFUSE.toString().equals(invitestatus)) { + report.setRefuseusers((long) value[1]); + } + } + } + } + return report; + } + + /** + * 活动JPA统计结果 + * + * @param values + * @return + */ + public static List getWebIMInviteAgg(List values) { + List webIMReportList = new ArrayList(); + if (values != null && values.size() > 0) { + for (int i = 0; i < values.size(); i++) { + Object[] value = (Object[]) values.get(i); + WebIMReport report = new WebIMReport(); + if (value.length == 3) { + report.setData((String) value[0]); + report.setIpnums((long) value[1]); + report.setPvnums((long) value[2]); + } + webIMReportList.add(report); + } + } + return webIMReportList; + } + + /** + * 活动JPA统计结果 + * + * @param values + * @return + */ + public static List getWebIMDataAgg(List values) { + List webIMReportList = new ArrayList(); + if (values != null && values.size() > 0) { + for (int i = 0; i < values.size(); i++) { + Object[] value = (Object[]) values.get(i); + WebIMReport report = new WebIMReport(); + if (value.length == 2) { + if (value[0] == null || value[0].toString().equalsIgnoreCase("null")) { + report.setData("未知"); + } else { + report.setData((String) value[0]); + } + report.setUsers((long) value[1]); + } + webIMReportList.add(report); + } + } + return webIMReportList; + } + + /** + * 活动JPA统计结果 + * + * @param values + * @return + */ + public static WebIMReport getWebIMInviteResult(List values) { + WebIMReport report = new WebIMReport(); + if (values != null && values.size() > 0) { + + for (int i = 0; i < values.size(); i++) { + Object[] value = (Object[]) values.get(i); + if (value.length >= 2) { + String invitestatus = (String) value[0]; + if (MainContext.OnlineUserInviteStatus.DEFAULT.toString().equals(invitestatus) || invitestatus == null) { + report.setUsers((long) value[1]); + } else if (MainContext.OnlineUserInviteStatus.ACCEPT.toString().equals(invitestatus)) { + report.setInviteusers((long) value[1]); + } else if (MainContext.OnlineUserInviteStatus.REFUSE.toString().equals(invitestatus)) { + report.setRefuseusers((long) value[1]); + } + } + } + } + return report; + } + + /** + * 活动JPA统计结果 + * + * @param values + * @return + */ + public static WeiXinReport getWeiXinReportResult(List values) { + WeiXinReport report = new WeiXinReport(); + if (values != null && values.size() > 0) { + for (int i = 0; i < values.size(); i++) { + Object[] value = (Object[]) values.get(i); + if (value.length >= 2) { + String event = (String) value[0]; + if (MainContext.WeiXinEventTypeEnum.SUB.toString().equals(event)) { + report.setSubs((long) value[1]); + } else if (MainContext.WeiXinEventTypeEnum.UNSUB.toString().equals(event)) { + report.setUnsubs((long) value[1]); + } + } + } + } + return report; + } + + public static Map transBean2Map(Object obj) { + + if (obj == null) { + return null; + } + Map map = new HashMap(); + try { + BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + for (PropertyDescriptor property : propertyDescriptors) { + String key = property.getName(); + + // 过滤class属性 + if (!key.equals("class")) { + // 得到property对应的getter方法 + + Method readMethod = property.getReadMethod(); + + if (readMethod != null) { + Object value = readMethod.invoke(obj); + if (value instanceof Date) { + value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) value); + } + map.put(key, value); + } + } + } + } catch (Exception e) { + System.out.println("transBean2Map Error " + e); + } + + return map; + + } + + public static void populate(Object bean, Map properties) throws IllegalAccessException, InvocationTargetException { + ConvertUtils.register(new Converter() { + @SuppressWarnings("rawtypes") + @Override + public Object convert(Class arg0, Object arg1) { + if (arg1 == null) { + return null; + } + if (arg1 instanceof Date) { + return arg1; + } else if (!(arg1 instanceof String)) { + throw new ConversionException("只支持字符串转换 !"); + } + String str = (String) arg1; + if (str.trim().equals("")) { + return null; + } + + SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + try { + return sd.parse(str); + } catch (Exception e) { + throw new RuntimeException(e); + } + + } + + }, java.util.Date.class); + if (properties == null || bean == null) { + return; + } + try { + BeanUtilsBean.getInstance().populate(bean, properties); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static byte[] toBytes(Object object) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ObjectOutputStream objectOutput = new ObjectOutputStream(out); + objectOutput.writeObject(object); + return out.toByteArray(); + } + + public static Object toObject(byte[] data) throws Exception { + ByteArrayInputStream input = new ByteArrayInputStream(data); + ObjectInputStream objectInput = new ObjectInputStream(input); + return objectInput.readObject(); } - - public static void populate(Object bean , Map properties) throws IllegalAccessException, InvocationTargetException{ - ConvertUtils.register(new Converter() - { - @SuppressWarnings("rawtypes") - @Override - public Object convert(Class arg0, Object arg1) - { - if(arg1 == null) - { - return null; - } - if(arg1 instanceof Date){ - return arg1 ; - }else if(!(arg1 instanceof String)){ - throw new ConversionException("只支持字符串转换 !"); - } - String str = (String)arg1; - if(str.trim().equals("")) - { - return null; - } - SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - - try{ - return sd.parse(str); - } catch(Exception e) - { - throw new RuntimeException(e); - } - - } - - }, java.util.Date.class); - if (properties == null || bean == null) { - return; - } - try { - BeanUtilsBean.getInstance().populate(bean, properties); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public static byte[] toBytes(Object object) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ObjectOutputStream objectOutput = new ObjectOutputStream(out); - objectOutput.writeObject(object); - return out.toByteArray(); - } - - public static Object toObject(byte[] data) throws Exception { - ByteArrayInputStream input = new ByteArrayInputStream(data); - ObjectInputStream objectInput = new ObjectInputStream(input); - return objectInput.readObject(); - } - - /** - * - * @param str - * @return - * @throws NoSuchAlgorithmException - */ - public static String encryption(String str) throws NoSuchAlgorithmException{ - BasicTextEncryptor textEncryptor = new BasicTextEncryptor (); - textEncryptor.setPassword(MainContext.getSystemSecrityPassword()); - return textEncryptor.encrypt(str); - } - /** - * * @param str * @return - * @throws NoSuchAlgorithmException + * @throws NoSuchAlgorithmException */ - public static String decryption(String str) throws NoSuchAlgorithmException{ - BasicTextEncryptor textEncryptor = new BasicTextEncryptor (); - textEncryptor.setPassword(MainContext.getSystemSecrityPassword()); - return textEncryptor.decrypt(str); + public static String encryption(String str) throws NoSuchAlgorithmException { + BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); + textEncryptor.setPassword(MainContext.getSystemSecrityPassword()); + return textEncryptor.encrypt(str); } - - public static String getTopic(String snsid ,String msgtype , String eventype , String eventkey , String msg){ - StringBuffer strb = new StringBuffer() ; - strb.append(snsid) ; - strb.append(".").append(msgtype) ; - if(msgtype.equals("text")){ - strb.append(".").append(msg) ; - }else if(msgtype.equals("exchange")){ - strb.append(".").append(eventype.toLowerCase()) ; - if(!StringUtils.isBlank(eventkey)){ - strb.append(".").append(eventkey) ; - } - }else{ - strb.append(".").append(msgtype) ; - } - return strb.toString() ; + + /** + * @param str + * @return + * @throws NoSuchAlgorithmException + */ + public static String decryption(String str) throws NoSuchAlgorithmException { + BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); + textEncryptor.setPassword(MainContext.getSystemSecrityPassword()); + return textEncryptor.decrypt(str); } - - public static String getTopic(String snsid ,String msgtype , String eventype){ - StringBuffer strb = new StringBuffer() ; - strb.append(snsid) ; - strb.append(".").append(msgtype) ; - if(msgtype.equals("text")){ - strb.append(".").append(msgtype) ; - }else if(msgtype.equals("exchange")){ - strb.append(".").append(eventype.toLowerCase()) ; - }else{ - strb.append(".").append(msgtype) ; - } - return strb.toString() ; + + public static String getTopic(String snsid, String msgtype, String eventype, String eventkey, String msg) { + StringBuffer strb = new StringBuffer(); + strb.append(snsid); + strb.append(".").append(msgtype); + if (msgtype.equals("text")) { + strb.append(".").append(msg); + } else if (msgtype.equals("exchange")) { + strb.append(".").append(eventype.toLowerCase()); + if (!StringUtils.isBlank(eventkey)) { + strb.append(".").append(eventkey); + } + } else { + strb.append(".").append(msgtype); + } + return strb.toString(); } - /** - * 处理 对话消息中的图片 - * @param message - * @return - */ - public static String filterChatMessage(String message){ - Document document = Jsoup.parse(message) ; - Elements pngs = document.select("img[src]"); - for (Element element : pngs) { - String imgUrl = element.attr("src"); - if(imgUrl.indexOf("/res/image") >= 0){ - element.attr("class", "ukefu-media-image") ; - } - } - return document.html() ; + + public static String getTopic(String snsid, String msgtype, String eventype) { + StringBuffer strb = new StringBuffer(); + strb.append(snsid); + strb.append(".").append(msgtype); + if (msgtype.equals("text")) { + strb.append(".").append(msgtype); + } else if (msgtype.equals("exchange")) { + strb.append(".").append(eventype.toLowerCase()); + } else { + strb.append(".").append(msgtype); + } + return strb.toString(); } - + + /** + * 处理 对话消息中的图片 + * + * @param message + * @return + */ + public static String filterChatMessage(String message) { + Document document = Jsoup.parse(message); + Elements pngs = document.select("img[src]"); + for (Element element : pngs) { + String imgUrl = element.attr("src"); + if (imgUrl.indexOf("/res/image") >= 0) { + element.attr("class", "ukefu-media-image"); + } + } + return document.html(); + } + /** * 检查当前时间是否是在 时间范围内 ,时间范围的格式为 : 08:30~11:30,13:30~17:30 + * * @param timeRanges * @return */ - public static boolean isInWorkingHours(String timeRanges){ - boolean workintTime = true ; - String timeStr = timeRangeDateFormat.format(new Date()) ; - if(!StringUtils.isBlank(timeRanges)){ //设置了 工作时间段 - workintTime = false ; //将 检查结果设置为 False , 如果当前时间是在 时间范围内,则 置为 True - String[] timeRange = timeRanges.split(",") ; - for(String tr : timeRange){ - String[] timeGroup = tr.split("~") ; - if(timeGroup.length == 2){ - if(timeGroup[0].compareTo(timeGroup[1]) >= 0){ - if(timeStr.compareTo(timeGroup[0]) >= 0 || timeStr.compareTo(timeGroup[1]) <= 0){ - workintTime = true ; - } - }else{ - if(timeStr.compareTo(timeGroup[0]) >= 0 && timeStr.compareTo(timeGroup[1]) <= 0){ - workintTime = true ; - } - } - } - } - } - return workintTime ; - } - - public static File processImage(File destFile,File imageFile) throws FileNotFoundException, IOException{ - if(imageFile != null && imageFile.exists()){ - Thumbnails.of(imageFile).width(460).keepAspectRatio(true).toFile(destFile); - } - return destFile ; - } - - public static File scaleImage(File destFile,File imageFile, float quality) throws FileNotFoundException, IOException{ - if(imageFile != null && imageFile.exists()){ - Thumbnails.of(imageFile).scale(1f).outputQuality(quality).toFile(destFile); - } - return destFile ; + public static boolean isInWorkingHours(String timeRanges) { + boolean workintTime = true; + String timeStr = timeRangeDateFormat.format(new Date()); + if (!StringUtils.isBlank(timeRanges)) { //设置了 工作时间段 + workintTime = false; //将 检查结果设置为 False , 如果当前时间是在 时间范围内,则 置为 True + String[] timeRange = timeRanges.split(","); + for (String tr : timeRange) { + String[] timeGroup = tr.split("~"); + if (timeGroup.length == 2) { + if (timeGroup[0].compareTo(timeGroup[1]) >= 0) { + if (timeStr.compareTo(timeGroup[0]) >= 0 || timeStr.compareTo(timeGroup[1]) <= 0) { + workintTime = true; + } + } else { + if (timeStr.compareTo(timeGroup[0]) >= 0 && timeStr.compareTo(timeGroup[1]) <= 0) { + workintTime = true; + } + } + } + } + } + return workintTime; } - public static String processEmoti(String message) { - Pattern pattern = Pattern.compile("\\[([\\d]*?)\\]"); - SystemConfig systemConfig = (SystemConfig) CacheHelper.getSystemCacheBean().getCacheObject("systemConfig", MainContext.SYSTEM_ORGI) ; - Matcher matcher = pattern.matcher(message); - StringBuffer strb = new StringBuffer(); - while(matcher.find()) { - if(systemConfig!=null && !StringUtils.isBlank(systemConfig.getIconstr())) { - matcher.appendReplacement(strb,""); - }else { - matcher.appendReplacement(strb,""); - } - } - matcher.appendTail(strb) ; - if(strb.length() == 0){ - strb.append(message) ; - } - return strb.toString().replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "[表情]") ; - } - - public static String getIpAddr(HttpServletRequest request) { - String ip = request.getHeader("x-forwarded-for"); - if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getHeader("Proxy-Client-IP"); - } - if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getHeader("WL-Proxy-Client-IP"); - } - if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = request.getRemoteAddr(); - } - return ip; - } - - public static String getIpAddr(HttpHeaders headers , String remoteAddr) { - String ip = headers.get("x-forwarded-for"); - if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = headers.get("Proxy-Client-IP"); - } - if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = headers.get("WL-Proxy-Client-IP"); - } - if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { - ip = remoteAddr; - } - return ip; - } - - public static boolean secConfirm(SecretRepository secRes , String orgi , String confirm){ - /** - * 先调用 IMServer - */ - boolean execute = false ; - List secretConfig = secRes.findByOrgi(orgi) ; - if(!StringUtils.isBlank(confirm)){ - if(secretConfig!=null && secretConfig.size() > 0){ - Secret secret = secretConfig.get( 0) ; - if(MainUtils.md5(confirm).equals(secret.getPassword())){ - execute = true ; - } - } - }else if(secretConfig.size() == 0){ - execute = true ; - } - return execute ; - } - - public static void processAttachmentFile(MultipartFile[] files, AttachmentRepository attachementRes , String path, User user , String orgi, WorkOrders workOrders, HttpServletRequest request , String dataid , String modelid) throws IOException{ - if(files!=null && files.length > 0){ - workOrders.setAnonymous(true);//变更用途为是否有 附件 - //保存附件 - for(MultipartFile file : files){ - if(file.getSize() > 0){ //文件尺寸 限制 ?在 启动 配置中 设置 的最大值,其他地方不做限制 - String fileid = MainUtils.md5(file.getBytes()) ; //使用 文件的 MD5作为 ID,避免重复上传大文件 - if(!StringUtils.isBlank(fileid)){ - AttachmentFile attachmentFile = new AttachmentFile() ; - attachmentFile.setCreater(user.getId()); - attachmentFile.setOrgi(orgi); - attachmentFile.setOrgan(user.getOrgan()); - attachmentFile.setDataid(dataid); - attachmentFile.setModelid(modelid); - attachmentFile.setModel(MainContext.ModelType.WORKORDERS.toString()); - attachmentFile.setFilelength((int) file.getSize()); - if(file.getContentType()!=null && file.getContentType().length() > 255){ - attachmentFile.setFiletype(file.getContentType().substring(0 , 255)); - }else{ - attachmentFile.setFiletype(file.getContentType()); - } - if(file.getOriginalFilename()!=null && file.getOriginalFilename().length() > 255){ - attachmentFile.setTitle(file.getOriginalFilename().substring(0 , 255)); - }else{ - attachmentFile.setTitle(file.getOriginalFilename()); - } - if(!StringUtils.isBlank(attachmentFile.getFiletype()) && attachmentFile.getFiletype().indexOf("image") >= 0){ - attachmentFile.setImage(true); - } - attachmentFile.setFileid(fileid); - attachementRes.save(attachmentFile) ; - FileUtils.writeByteArrayToFile(new File(path , "app/workorders/"+fileid), file.getBytes()); - } - } - } - - } + public static File processImage(File destFile, File imageFile) throws FileNotFoundException, IOException { + if (imageFile != null && imageFile.exists()) { + Thumbnails.of(imageFile).width(460).keepAspectRatio(true).toFile(destFile); + } + return destFile; } - /** - * 获取系统配置 - * @return - */ - public static SystemConfig getSystemConfig(){ - SystemConfig systemConfig = (SystemConfig) CacheHelper.getSystemCacheBean().getCacheObject("systemConfig", MainContext.SYSTEM_ORGI) ; - if(systemConfig == null){ - SystemConfigRepository systemConfigRes = MainContext.getContext().getBean(SystemConfigRepository.class) ; - systemConfig = systemConfigRes.findByOrgi(MainContext.SYSTEM_ORGI) ; - } - return systemConfig; - } - /** - * 初始化呼叫中心功能里需要隐藏号码的字段 - * @param tpRes - */ - public static void initSystemSecField(TablePropertiesRepository tpRes) { - if(tpRes!= null) { - List tpList = tpRes.findBySecfield(true) ; - CacheHelper.getSystemCacheBean().put(MainContext.UKEFU_SYSTEM_SECFIELD, tpList, MainContext.SYSTEM_ORGI) ; - } - } - /** - * 获取系统地区配置 - * @return - */ - public static void initSystemArea(){ - CacheHelper.getSystemCacheBean().delete(MainContext.UKEFU_SYSTEM_AREA, MainContext.SYSTEM_ORGI) ; - AreaTypeRepository areaTypeRes = MainContext.getContext().getBean(AreaTypeRepository.class) ; - CacheHelper.getSystemCacheBean().put(MainContext.UKEFU_SYSTEM_AREA, areaTypeRes.findAll(), MainContext.SYSTEM_ORGI); - } - - /** - * 缓存 广告位 - * @return - */ - public static void initAdv(String orgi){ - CacheHelper.getSystemCacheBean().delete(MainContext.UKEFU_SYSTEM_ADV+"_"+orgi, orgi) ; - AdTypeRepository adRes = MainContext.getContext().getBean(AdTypeRepository.class) ; - CacheHelper.getSystemCacheBean().put(MainContext.UKEFU_SYSTEM_ADV+"_"+orgi, adRes.findByOrgi(orgi),orgi); - } - - public static Template getTemplate(String id){ - Template templet = null ; - if((templet = (Template) CacheHelper.getSystemCacheBean().getCacheObject(id, MainContext.SYSTEM_ORGI)) == null) { - TemplateRepository templateRes = MainContext.getContext().getBean(TemplateRepository.class) ; - templet = templateRes.findByIdAndOrgi(id, MainContext.SYSTEM_ORGI); - CacheHelper.getSystemCacheBean().put(id, templet, MainContext.SYSTEM_ORGI); - } - return templet; - } - /** - * 按照权重获取广告 - * @param adpos - * @return - */ - @SuppressWarnings("unchecked") - public static AdType getPointAdv(String adpos,String orgi){ - List adTypeList = new ArrayList(); - List cacheAdTypeList = (List) CacheHelper.getSystemCacheBean().getCacheObject(MainContext.UKEFU_SYSTEM_ADV+"_"+orgi,orgi); - if(cacheAdTypeList == null) { - AdTypeRepository adRes = MainContext.getContext().getBean(AdTypeRepository.class) ; - cacheAdTypeList = adRes.findByOrgi(orgi); - CacheHelper.getSystemCacheBean().put(MainContext.UKEFU_SYSTEM_ADV+"_"+orgi,cacheAdTypeList,orgi); - } - List sysDicList = UKeFuDic.getInstance().getDic(MainContext.UKEFU_SYSTEM_ADPOS_DIC) ; - SysDic sysDic = null ; - if(sysDicList!=null){ - for(SysDic dic : sysDicList){ - if(dic.getCode().equals(adpos)){ - sysDic = dic ; break ; - } - } - } - if(adTypeList!=null && sysDic!=null){ - for(AdType adType : cacheAdTypeList){ - if(adType.getAdpos().equals(sysDic.getId())){ - adTypeList.add(adType) ; - } - } - } - return weitht(adTypeList) ; - } - private static Random random = new Random(); - /** - * - * 按照权重,获取广告内容 - * @param adList - * @return - */ - private static AdType weitht(List adList){ - AdType adType = null; - int weight = 0 ; - if(adList!=null && adList.size() > 0){ - for (AdType ad : adList) { - weight += ad.getWeight() ; - } - int n = random.nextInt(weight) , m = 0 ; - for (AdType ad : adList) { - if (m <= n && n < m + ad.getWeight()) { - adType = ad ; break; - } - m += ad.getWeight(); - } - } - return adType ; - } - - /** - * 16进制字符串转换为字符串 - * + + public static File scaleImage(File destFile, File imageFile, float quality) throws FileNotFoundException, IOException { + if (imageFile != null && imageFile.exists()) { + Thumbnails.of(imageFile).scale(1f).outputQuality(quality).toFile(destFile); + } + return destFile; + } + + public static String processEmoti(String message) { + Pattern pattern = Pattern.compile("\\[([\\d]*?)\\]"); + SystemConfig systemConfig = (SystemConfig) CacheHelper.getSystemCacheBean().getCacheObject("systemConfig", MainContext.SYSTEM_ORGI); + Matcher matcher = pattern.matcher(message); + StringBuffer strb = new StringBuffer(); + while (matcher.find()) { + if (systemConfig != null && !StringUtils.isBlank(systemConfig.getIconstr())) { + matcher.appendReplacement(strb, ""); + } else { + matcher.appendReplacement(strb, ""); + } + } + matcher.appendTail(strb); + if (strb.length() == 0) { + strb.append(message); + } + return strb.toString().replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "[表情]"); + } + + public static String getIpAddr(HttpServletRequest request) { + String ip = request.getHeader("x-forwarded-for"); + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("WL-Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getRemoteAddr(); + } + return ip; + } + + public static String getIpAddr(HttpHeaders headers, String remoteAddr) { + String ip = headers.get("x-forwarded-for"); + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = headers.get("Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = headers.get("WL-Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = remoteAddr; + } + return ip; + } + + public static boolean secConfirm(SecretRepository secRes, String orgi, String confirm) { + /** + * 先调用 IMServer + */ + boolean execute = false; + List secretConfig = secRes.findByOrgi(orgi); + if (!StringUtils.isBlank(confirm)) { + if (secretConfig != null && secretConfig.size() > 0) { + Secret secret = secretConfig.get(0); + if (MainUtils.md5(confirm).equals(secret.getPassword())) { + execute = true; + } + } + } else if (secretConfig.size() == 0) { + execute = true; + } + return execute; + } + + public static void processAttachmentFile(MultipartFile[] files, AttachmentRepository attachementRes, String path, User user, String orgi, WorkOrders workOrders, HttpServletRequest request, String dataid, String modelid) throws IOException { + if (files != null && files.length > 0) { + workOrders.setAnonymous(true);//变更用途为是否有 附件 + //保存附件 + for (MultipartFile file : files) { + if (file.getSize() > 0) { //文件尺寸 限制 ?在 启动 配置中 设置 的最大值,其他地方不做限制 + String fileid = MainUtils.md5(file.getBytes()); //使用 文件的 MD5作为 ID,避免重复上传大文件 + if (!StringUtils.isBlank(fileid)) { + AttachmentFile attachmentFile = new AttachmentFile(); + attachmentFile.setCreater(user.getId()); + attachmentFile.setOrgi(orgi); + attachmentFile.setOrgan(user.getOrgan()); + attachmentFile.setDataid(dataid); + attachmentFile.setModelid(modelid); + attachmentFile.setModel(MainContext.ModelType.WORKORDERS.toString()); + attachmentFile.setFilelength((int) file.getSize()); + if (file.getContentType() != null && file.getContentType().length() > 255) { + attachmentFile.setFiletype(file.getContentType().substring(0, 255)); + } else { + attachmentFile.setFiletype(file.getContentType()); + } + if (file.getOriginalFilename() != null && file.getOriginalFilename().length() > 255) { + attachmentFile.setTitle(file.getOriginalFilename().substring(0, 255)); + } else { + attachmentFile.setTitle(file.getOriginalFilename()); + } + if (!StringUtils.isBlank(attachmentFile.getFiletype()) && attachmentFile.getFiletype().indexOf("image") >= 0) { + attachmentFile.setImage(true); + } + attachmentFile.setFileid(fileid); + attachementRes.save(attachmentFile); + FileUtils.writeByteArrayToFile(new File(path, "app/workorders/" + fileid), file.getBytes()); + } + } + } + + } + } + + /** + * 获取系统配置 + * * @return - */ - public static String string2HexString(String strPart) { - StringBuffer hexString = new StringBuffer(); - for (int i = 0; i < strPart.length(); i++) { - int ch = (int) strPart.charAt(i); - String strHex = Integer.toHexString(ch); - hexString.append(strHex); - } - return hexString.toString(); + */ + public static SystemConfig getSystemConfig() { + SystemConfig systemConfig = (SystemConfig) CacheHelper.getSystemCacheBean().getCacheObject("systemConfig", MainContext.SYSTEM_ORGI); + if (systemConfig == null) { + SystemConfigRepository systemConfigRes = MainContext.getContext().getBean(SystemConfigRepository.class); + systemConfig = systemConfigRes.findByOrgi(MainContext.SYSTEM_ORGI); + } + return systemConfig; } - - /** - * + + /** + * 初始化呼叫中心功能里需要隐藏号码的字段 + * + * @param tpRes + */ + public static void initSystemSecField(TablePropertiesRepository tpRes) { + if (tpRes != null) { + List tpList = tpRes.findBySecfield(true); + CacheHelper.getSystemCacheBean().put(MainContext.UKEFU_SYSTEM_SECFIELD, tpList, MainContext.SYSTEM_ORGI); + } + } + + /** + * 获取系统地区配置 + * + * @return + */ + public static void initSystemArea() { + CacheHelper.getSystemCacheBean().delete(MainContext.UKEFU_SYSTEM_AREA, MainContext.SYSTEM_ORGI); + AreaTypeRepository areaTypeRes = MainContext.getContext().getBean(AreaTypeRepository.class); + CacheHelper.getSystemCacheBean().put(MainContext.UKEFU_SYSTEM_AREA, areaTypeRes.findAll(), MainContext.SYSTEM_ORGI); + } + + /** + * 缓存 广告位 + * + * @return + */ + public static void initAdv(String orgi) { + CacheHelper.getSystemCacheBean().delete(MainContext.UKEFU_SYSTEM_ADV + "_" + orgi, orgi); + AdTypeRepository adRes = MainContext.getContext().getBean(AdTypeRepository.class); + CacheHelper.getSystemCacheBean().put(MainContext.UKEFU_SYSTEM_ADV + "_" + orgi, adRes.findByOrgi(orgi), orgi); + } + + public static Template getTemplate(String id) { + Template templet = null; + if ((templet = (Template) CacheHelper.getSystemCacheBean().getCacheObject(id, MainContext.SYSTEM_ORGI)) == null) { + TemplateRepository templateRes = MainContext.getContext().getBean(TemplateRepository.class); + templet = templateRes.findByIdAndOrgi(id, MainContext.SYSTEM_ORGI); + CacheHelper.getSystemCacheBean().put(id, templet, MainContext.SYSTEM_ORGI); + } + return templet; + } + + /** + * 按照权重获取广告 + * + * @param adpos + * @return + */ + @SuppressWarnings("unchecked") + public static AdType getPointAdv(String adpos, String orgi) { + List adTypeList = new ArrayList(); + List cacheAdTypeList = (List) CacheHelper.getSystemCacheBean().getCacheObject(MainContext.UKEFU_SYSTEM_ADV + "_" + orgi, orgi); + if (cacheAdTypeList == null) { + AdTypeRepository adRes = MainContext.getContext().getBean(AdTypeRepository.class); + cacheAdTypeList = adRes.findByOrgi(orgi); + CacheHelper.getSystemCacheBean().put(MainContext.UKEFU_SYSTEM_ADV + "_" + orgi, cacheAdTypeList, orgi); + } + List sysDicList = UKeFuDic.getInstance().getDic(MainContext.UKEFU_SYSTEM_ADPOS_DIC); + SysDic sysDic = null; + if (sysDicList != null) { + for (SysDic dic : sysDicList) { + if (dic.getCode().equals(adpos)) { + sysDic = dic; + break; + } + } + } + if (adTypeList != null && sysDic != null) { + for (AdType adType : cacheAdTypeList) { + if (adType.getAdpos().equals(sysDic.getId())) { + adTypeList.add(adType); + } + } + } + return weitht(adTypeList); + } + + private static Random random = new Random(); + + /** + * 按照权重,获取广告内容 + * + * @param adList + * @return + */ + private static AdType weitht(List adList) { + AdType adType = null; + int weight = 0; + if (adList != null && adList.size() > 0) { + for (AdType ad : adList) { + weight += ad.getWeight(); + } + int n = random.nextInt(weight), m = 0; + for (AdType ad : adList) { + if (m <= n && n < m + ad.getWeight()) { + adType = ad; + break; + } + m += ad.getWeight(); + } + } + return adType; + } + + /** + * 16进制字符串转换为字符串 + * + * @return + */ + public static String string2HexString(String strPart) { + StringBuffer hexString = new StringBuffer(); + for (int i = 0; i < strPart.length(); i++) { + int ch = (int) strPart.charAt(i); + String strHex = Integer.toHexString(ch); + hexString.append(strHex); + } + return hexString.toString(); + } + + /** * @throws IOException - * @throws TemplateException + * @throws TemplateException */ @SuppressWarnings("deprecation") - public static String getTemplet(String templet , Map values) throws IOException, TemplateException{ - StringWriter writer = new StringWriter(); - Configuration cfg = null; - freemarker.template.Template template = null ; - String retValue = templet ; - if(templet!=null && templet.length()>0 && templet.indexOf("$")>=0){ - cfg = new Configuration(); - TempletLoader loader = new TempletLoader(templet) ; - cfg.setTemplateLoader(loader); - cfg.setDefaultEncoding("UTF-8"); - template = cfg.getTemplate(""); - template.process(values, writer); - retValue = writer.toString() ; - } - return retValue; + public static String getTemplet(String templet, Map values) throws IOException, TemplateException { + StringWriter writer = new StringWriter(); + Configuration cfg = null; + freemarker.template.Template template = null; + String retValue = templet; + if (templet != null && templet.length() > 0 && templet.indexOf("$") >= 0) { + cfg = new Configuration(); + TempletLoader loader = new TempletLoader(templet); + cfg.setTemplateLoader(loader); + cfg.setDefaultEncoding("UTF-8"); + template = cfg.getTemplate(""); + template.process(values, writer); + retValue = writer.toString(); + } + return retValue; } - + /** - * 发送邮件 - * @param email - * @param cc - * @param subject - * @param content - * @throws Exception - */ - public static void sendMail(String email , String cc , String subject , String content ,List filenames) throws Exception{ - SystemConfig config = MainUtils.getSystemConfig() ; - if(config!=null && config.isEnablemail() && config.getEmailid()!=null) { - SystemMessage systemMessage = MainContext.getContext().getBean(SystemMessageRepository.class).findByIdAndOrgi(config.getEmailid(),config.getOrgi()) ; - MailSender sender = new MailSender(systemMessage.getSmtpserver(),systemMessage.getMailfrom(),systemMessage.getSmtpuser(), decryption(systemMessage.getSmtppassword()),systemMessage.getSeclev(),systemMessage.getSslport()); - if(email!=null){ - sender.send(email,cc, subject, content,filenames); - } - } - } - - public static String encode(Object obj) { - Base64 base64 = new Base64(); - try { - return base64.encodeToString(MainUtils.toBytes(obj)) ; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - @SuppressWarnings("unchecked") - public static T decode(String str,Class clazz) { - Base64 base64 = new Base64(); - try { - return (T) MainUtils.toObject(base64.decode(str)) ; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - public static String processContentEncode(String str) throws Exception{ - return Base64.encodeBase64String(str.getBytes("UTF-8")).replaceAll("\\+", "-"); - } - public static String processContentDecode(String str) throws Exception{ - return new String(Base64.decodeBase64(str.replaceAll("-", "\\+").getBytes()) , "UTF-8"); - } - - /** - * - * @param defaultFormatValue - * @param text - * @return - */ - public static String processParam(String defaultFormatValue, String text){ - String formatValue = "yyyy-MM-dd" ; - if(text.matches("[ ]{0,}([Yy]{1,})[ ]{0,}[+-]{0,1}([\\d]{0,})")){ - formatValue = "yyyy" ; - }else if(text.matches("[ ]{0,}([Mm]{1,})[ ]{0,}[+-]{0,1}([\\d]{0,})")){ - formatValue = "yyyy-MM" ; - } - - return getDays(text, defaultFormatValue!=null && defaultFormatValue.length()>0 ? defaultFormatValue : formatValue) ; - } - /*** - * 计算T+1 - * @param text - * @param format - * @return - */ - public static String getDays(String text , String format){ - String retDateFormat = text ; - Pattern pattern = Pattern.compile("[ ]{0,}([TtMmYy]{1,})[ ]{0,}[+-]{0,1}([\\d]{0,})") ; - Matcher matcher = pattern.matcher(text) ; - if(matcher.find() && matcher.groupCount()>=1){ - try { - if(matcher.group(1) .equalsIgnoreCase("T")){ - retDateFormat = formatDateValue(format, getDaysParam(text)) ; - }else if(matcher.group(1) .equalsIgnoreCase("M")){ - retDateFormat = formatMonthValue(format, getDaysParam(text)) ; - }else if(matcher.group(1) .equalsIgnoreCase("Y")){ - retDateFormat = String.valueOf((int)Double.parseDouble(String.valueOf(getDaysParam(text)))); - } - } catch (ParseException e) { - e.printStackTrace(); - } - } - return retDateFormat ; - } - - /*** - * 计算T+1 - * @param text - * @return - */ - public static Object getDaysParam(String text){ - Map context = new HashMap(); - context.put("T", processDays()) ; - context.put("t", processDays()) ; - context.put("M", processMonth()) ; - context.put("m", processMonth()) ; - context.put("Y", processYear()) ; - context.put("y", processYear()) ; - - return AviatorEvaluator.execute(text , context) ; - } - /** - * - * @param value - * @return - * @throws ParseException - * @throws Exception - */ - public static String formatDateValue(String format , Object value) throws ParseException{ - if(value!=null && value.toString().matches("[\\d.]{5,}")){ - value = new SimpleDateFormat(format).format(new Date((long)(Double.parseDouble(value.toString())*24*60*60*1000))) ; - } - return value!=null ? value.toString() : "0" ; - } - /** - * - * @param value - * @return - * @throws ParseException - * @throws Exception - */ - public static String formatMonthValue(String formatValue , Object value) throws ParseException{ - if(value!=null && value.toString().matches("[\\d.]{3,}")){ - int months = (int)Double.parseDouble(String.valueOf(value)); - int year = 0 ; - int month = 0 ; - if(months%12==0){ - year = months/12 - 1 ; - month = 12 ; - }else{ - year = months/12 ; - month = months % 12 ; - } - if(month<10){ - value = String.valueOf(year)+"0"+String.valueOf(month) ; - }else{ - value = String.valueOf(year)+ String.valueOf(month) ; - } - value = new SimpleDateFormat(formatValue).format(new SimpleDateFormat("yyyyMM").parse(String.valueOf(value))); - } - return value!=null ? value.toString() : "0" ; - } - /** - * - * @return - */ - public static double processDays(){ - return System.currentTimeMillis()*1.0f/(1000*60*60*24); - } - - /** - * - * @return - */ - public static double processMonth(){ - Calendar calendar = Calendar.getInstance() ; - int month = calendar.get(Calendar.YEAR)*12 + calendar.get(Calendar.MONTH )+1 ; - return month; - } - - /** - * - * @return - */ - public static double processYear(){ - return Calendar.getInstance() .get(Calendar.YEAR); - } - - private static final ObjectMapper JSON = new ObjectMapper(); + * 发送邮件 + * + * @param email + * @param cc + * @param subject + * @param content + * @throws Exception + */ + public static void sendMail(String email, String cc, String subject, String content, List filenames) throws Exception { + SystemConfig config = MainUtils.getSystemConfig(); + if (config != null && config.isEnablemail() && config.getEmailid() != null) { + SystemMessage systemMessage = MainContext.getContext().getBean(SystemMessageRepository.class).findByIdAndOrgi(config.getEmailid(), config.getOrgi()); + MailSender sender = new MailSender(systemMessage.getSmtpserver(), systemMessage.getMailfrom(), systemMessage.getSmtpuser(), decryption(systemMessage.getSmtppassword()), systemMessage.getSeclev(), systemMessage.getSslport()); + if (email != null) { + sender.send(email, cc, subject, content, filenames); + } + } + } + + public static String encode(Object obj) { + Base64 base64 = new Base64(); + try { + return base64.encodeToString(MainUtils.toBytes(obj)); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @SuppressWarnings("unchecked") + public static T decode(String str, Class clazz) { + Base64 base64 = new Base64(); + try { + return (T) MainUtils.toObject(base64.decode(str)); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static String processContentEncode(String str) throws Exception { + return Base64.encodeBase64String(str.getBytes("UTF-8")).replaceAll("\\+", "-"); + } + + public static String processContentDecode(String str) throws Exception { + return new String(Base64.decodeBase64(str.replaceAll("-", "\\+").getBytes()), "UTF-8"); + } + + /** + * @param defaultFormatValue + * @param text + * @return + */ + public static String processParam(String defaultFormatValue, String text) { + String formatValue = "yyyy-MM-dd"; + if (text.matches("[ ]{0,}([Yy]{1,})[ ]{0,}[+-]{0,1}([\\d]{0,})")) { + formatValue = "yyyy"; + } else if (text.matches("[ ]{0,}([Mm]{1,})[ ]{0,}[+-]{0,1}([\\d]{0,})")) { + formatValue = "yyyy-MM"; + } + + return getDays(text, defaultFormatValue != null && defaultFormatValue.length() > 0 ? defaultFormatValue : formatValue); + } + + /*** + * 计算T+1 + * @param text + * @param format + * @return + */ + public static String getDays(String text, String format) { + String retDateFormat = text; + Pattern pattern = Pattern.compile("[ ]{0,}([TtMmYy]{1,})[ ]{0,}[+-]{0,1}([\\d]{0,})"); + Matcher matcher = pattern.matcher(text); + if (matcher.find() && matcher.groupCount() >= 1) { + try { + if (matcher.group(1).equalsIgnoreCase("T")) { + retDateFormat = formatDateValue(format, getDaysParam(text)); + } else if (matcher.group(1).equalsIgnoreCase("M")) { + retDateFormat = formatMonthValue(format, getDaysParam(text)); + } else if (matcher.group(1).equalsIgnoreCase("Y")) { + retDateFormat = String.valueOf((int) Double.parseDouble(String.valueOf(getDaysParam(text)))); + } + } catch (ParseException e) { + e.printStackTrace(); + } + } + return retDateFormat; + } + + /*** + * 计算T+1 + * @param text + * @return + */ + public static Object getDaysParam(String text) { + Map context = new HashMap(); + context.put("T", processDays()); + context.put("t", processDays()); + context.put("M", processMonth()); + context.put("m", processMonth()); + context.put("Y", processYear()); + context.put("y", processYear()); + + return AviatorEvaluator.execute(text, context); + } + + /** + * @param value + * @return + * @throws ParseException + * @throws Exception + */ + public static String formatDateValue(String format, Object value) throws ParseException { + if (value != null && value.toString().matches("[\\d.]{5,}")) { + value = new SimpleDateFormat(format).format(new Date((long) (Double.parseDouble(value.toString()) * 24 * 60 * 60 * 1000))); + } + return value != null ? value.toString() : "0"; + } + + /** + * @param value + * @return + * @throws ParseException + * @throws Exception + */ + public static String formatMonthValue(String formatValue, Object value) throws ParseException { + if (value != null && value.toString().matches("[\\d.]{3,}")) { + int months = (int) Double.parseDouble(String.valueOf(value)); + int year = 0; + int month = 0; + if (months % 12 == 0) { + year = months / 12 - 1; + month = 12; + } else { + year = months / 12; + month = months % 12; + } + if (month < 10) { + value = String.valueOf(year) + "0" + String.valueOf(month); + } else { + value = String.valueOf(year) + String.valueOf(month); + } + value = new SimpleDateFormat(formatValue).format(new SimpleDateFormat("yyyyMM").parse(String.valueOf(value))); + } + return value != null ? value.toString() : "0"; + } + + /** + * @return + */ + public static double processDays() { + return System.currentTimeMillis() * 1.0f / (1000 * 60 * 60 * 24); + } + + /** + * @return + */ + public static double processMonth() { + Calendar calendar = Calendar.getInstance(); + int month = calendar.get(Calendar.YEAR) * 12 + calendar.get(Calendar.MONTH) + 1; + return month; + } + + /** + * @return + */ + public static double processYear() { + return Calendar.getInstance().get(Calendar.YEAR); + } + + private static final ObjectMapper JSON = new ObjectMapper(); + static { JSON.setSerializationInclusion(Include.NON_NULL); JSON.configure(SerializationFeature.INDENT_OUTPUT, Boolean.TRUE); @@ -1260,28 +1230,29 @@ public class MainUtils { return null; } + /** - * * @param message */ - public static AsrResult parseAsrResult(String id, String message , int speakms) { - AsrResult asrResult = null ; - Pattern pattern = Pattern.compile("([\\d]{1,})[\\.]{1}([\\s\\S]*);"); - Matcher matcher = pattern.matcher(message); - if(matcher.find() && matcher.groupCount() == 2) { - asrResult = new AsrResult(id , matcher.group(2) , matcher.group(1)); - if(asrResult.getMessage().endsWith("。")) { - asrResult.setMessage(asrResult.getMessage().substring(0 , asrResult.getMessage().length() - 1)); - } - } - if(speakms > 0 && asrResult!=null) { - asrResult.setSpeakms(speakms); - } - return asrResult; + public static AsrResult parseAsrResult(String id, String message, int speakms) { + AsrResult asrResult = null; + Pattern pattern = Pattern.compile("([\\d]{1,})[\\.]{1}([\\s\\S]*);"); + Matcher matcher = pattern.matcher(message); + if (matcher.find() && matcher.groupCount() == 2) { + asrResult = new AsrResult(id, matcher.group(2), matcher.group(1)); + if (asrResult.getMessage().endsWith("。")) { + asrResult.setMessage(asrResult.getMessage().substring(0, asrResult.getMessage().length() - 1)); + } + } + if (speakms > 0 && asrResult != null) { + asrResult.setSpeakms(speakms); + } + return asrResult; } /** * 发送短信 + * * @param phone * @param id * @param tpId @@ -1289,169 +1260,167 @@ public class MainUtils { * @return * @throws Exception */ - public static boolean sendSms(String phone,String id ,String tpId, Map tplValuesMap) throws Exception{ - SystemConfig config = MainUtils.getSystemConfig() ; - if(config!=null) { - SystemMessage systemMessage = MainContext.getContext().getBean(SystemMessageRepository.class).findByIdAndOrgi(id,config.getOrgi()) ; - if(systemMessage==null) { - return false; - } - Template tp = MainUtils.getTemplate(tpId) ; - if(tp==null) { - return false; - } - String params = MainUtils.getTemplet(tp.getTemplettext(),tplValuesMap) ; - - SysDic sysDic= UKeFuDic.getInstance().getDicItem(systemMessage.getSmstype()); - //阿里大于 - if(sysDic!=null && "dysms".equals(sysDic.getCode())) { - //设置超时时间-可自行调整 - System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); - System.setProperty("sun.net.client.defaultReadTimeout", "10000"); - //初始化ascClient需要的几个参数 - final String product ="Dysmsapi";//短信API产品名称(短信产品名固定,无需修改) - final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名(接口地址固定,无需修改) - //替换成你的AK - final String accessKeyId = systemMessage.getAppkey();//你的accessKeyId,参考本文档步骤2 - final String accessKeySecret = systemMessage.getAppsec();//你的accessKeySecret,参考本文档步骤2 - //初始化ascClient,暂时不支持多region(请勿修改) - IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, - accessKeySecret); - DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); - IAcsClient acsClient = new DefaultAcsClient(profile); - //组装请求对象 - SendSmsRequest request = new SendSmsRequest(); - //使用post提交 - request.setMethod(MethodType.POST); - //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式 - request.setPhoneNumbers(phone); - //必填:短信签名-可在短信控制台中找到 - request.setSignName(systemMessage.getSign()); - //必填:短信模板-可在短信控制台中找到 - request.setTemplateCode(systemMessage.getTpcode()); - //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 - //友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败 - request.setTemplateParam(params); - //可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段) - //request.setSmsUpExtendCode("90997"); - //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 - request.setOutId("yourOutId"); - //请求失败这里会抛ClientException异常 - SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); - if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) { - return true; - }else if(!StringUtils.isBlank(sendSmsResponse.getMessage())){ - try { - throw new Exception("短信发送失败,原因:"+sendSmsResponse.getMessage()); - }catch(Exception e) { - e.printStackTrace(); - } - } - } - } - return false; - } - - /** - * - * @param userid - * @param client - * @param session - * @param orgi - * @param ipaddr - * @param hostname - * @return - */ - public static WorkSession createWorkSession(String userid , String client, String session ,String orgi , String ipaddr , String hostname , String admin , boolean first) { - WorkSession workSession = new WorkSession(); - workSession.setCreatetime(new Date()); - workSession.setBegintime(new Date()); - workSession.setAgent(userid); - workSession.setAgentno(userid); - workSession.setAgentno(userid); - if(!StringUtils.isBlank(admin) && admin.equalsIgnoreCase("true")) { - workSession.setAdmin(true); - } - - workSession.setFirsttime(first); - - workSession.setIpaddr(ipaddr); - workSession.setHostname(hostname); - workSession.setUserid(userid); - workSession.setClientid(client); - workSession.setSessionid(session); - workSession.setOrgi(orgi); - - workSession.setDatestr(MainUtils.simpleDateFormat.format(new Date())); - - return workSession ; - } - - /** - * - * @param plan - * @return - */ - public static String convertCrond(JobTask plan){ - StringBuffer strb = new StringBuffer() ; - if("day".equals(plan.getRunCycle())){ - strb.append(plan.getRunBeginSecond()).append(" ").append(plan.getRunBeginMinute()).append(plan.getIsRepeat() && plan.getRepeatSpace()!=null && plan.getRepeatSpace()<60 ? "/"+ plan.getRepeatSpace() : "").append(" ").append(plan.getRunBeginHour()).append(plan.getIsRepeat() && plan.getRepeatSpace()!=null && plan.getRepeatSpace()>60 ? "/"+ plan.getRepeatSpace()/60 : (plan.getRepeatJustTime()!=null && plan.getRepeatJustTime() > 0 ? "-" + (plan.getRunBeginHour() + plan.getRepeatJustTime()):"")).append(" ").append("*").append(plan.getRunSpace() != null && plan.getRunSpace()>0 ? "/"+plan.getRunSpace():"").append(" ").append(" * ?") ; - } - if("week".equals(plan.getRunCycle())){ - strb.append(plan.getRunBeginSecond()).append(" ").append(plan.getRunBeginMinute()).append(plan.getIsRepeat() && plan.getRepeatSpace()!=null && plan.getRepeatSpace()<60 ? "/"+ plan.getRepeatSpace() : "").append(" ").append(plan.getRunBeginHour()).append(plan.getIsRepeat() && plan.getRepeatSpace()!=null && plan.getRepeatSpace()>60 ? "/"+ plan.getRepeatSpace()/60 : (plan.getRepeatJustTime()!=null && plan.getRepeatJustTime() > 0 ? "-" + (plan.getRunBeginHour() + plan.getRepeatJustTime()):"")).append(" ").append(plan.getRunDates()==null || plan.getRunDates().length==0 ? "*":"?").append(" * ").append(plan.getRunDates()==null || plan.getRunDates().length==0? "?" : StringUtils.join(plan.getRunDates() , ",")).append(plan.getRunSpace()!=null && plan.getRunSpace()>0 ? "/"+plan.getRunSpace(): "") ; - } - if("month".equals(plan.getRunCycle())){ - strb.append(plan.getRunBeginSecond()).append(" ").append(plan.getRunBeginMinute()).append(plan.getIsRepeat() && plan.getRepeatSpace()!=null && plan.getRepeatSpace()<60 ? "/"+ plan.getRepeatSpace() : "").append(" ").append(plan.getRunBeginHour()).append(plan.getIsRepeat() && plan.getRepeatSpace()!=null && plan.getRepeatSpace()>60 ? "/"+ plan.getRepeatSpace()/60 : (plan.getRepeatJustTime()!=null && plan.getRepeatJustTime() > 0 ? "-" + (plan.getRunBeginHour() + plan.getRepeatJustTime()):"")).append(" ").append(plan.getRunBeginDate()).append(" ").append(plan.getRunDates()==null || plan.getRunDates().length==0? "*" : StringUtils.join(plan.getRunDates() , ",")).append(" ").append(" ?") ; - } - return strb.toString() ; - } - - public static Date updateTaskNextFireTime(JobDetail jobDetail) throws Exception{ - Date nextFireDate = new Date(); - Date date = new Date(); - if(jobDetail!=null && jobDetail.getCronexp()!=null && jobDetail.getCronexp().length()>0){ - try { - nextFireDate = (CronTools.getFinalFireTime(jobDetail.getCronexp(), jobDetail.getNextfiretime()!=null ? jobDetail.getNextfiretime() : date)) ; - } catch (ParseException e) { - nextFireDate = new Date(System.currentTimeMillis() + 1000*60*60*24) ; //一旦任务的 Cron表达式错误,将下次执行时间自动设置为一天后,避免出现任务永远无法终止的情况 - e.printStackTrace(); - } - } - return nextFireDate ; - } + public static boolean sendSms(String phone, String id, String tpId, Map tplValuesMap) throws Exception { + SystemConfig config = MainUtils.getSystemConfig(); + if (config != null) { + SystemMessage systemMessage = MainContext.getContext().getBean(SystemMessageRepository.class).findByIdAndOrgi(id, config.getOrgi()); + if (systemMessage == null) { + return false; + } + Template tp = MainUtils.getTemplate(tpId); + if (tp == null) { + return false; + } + String params = MainUtils.getTemplet(tp.getTemplettext(), tplValuesMap); + + SysDic sysDic = UKeFuDic.getInstance().getDicItem(systemMessage.getSmstype()); + //阿里大于 + if (sysDic != null && "dysms".equals(sysDic.getCode())) { + //设置超时时间-可自行调整 + System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); + System.setProperty("sun.net.client.defaultReadTimeout", "10000"); + //初始化ascClient需要的几个参数 + final String product = "Dysmsapi";//短信API产品名称(短信产品名固定,无需修改) + final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名(接口地址固定,无需修改) + //替换成你的AK + final String accessKeyId = systemMessage.getAppkey();//你的accessKeyId,参考本文档步骤2 + final String accessKeySecret = systemMessage.getAppsec();//你的accessKeySecret,参考本文档步骤2 + //初始化ascClient,暂时不支持多region(请勿修改) + IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, + accessKeySecret); + DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); + IAcsClient acsClient = new DefaultAcsClient(profile); + //组装请求对象 + SendSmsRequest request = new SendSmsRequest(); + //使用post提交 + request.setMethod(MethodType.POST); + //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式 + request.setPhoneNumbers(phone); + //必填:短信签名-可在短信控制台中找到 + request.setSignName(systemMessage.getSign()); + //必填:短信模板-可在短信控制台中找到 + request.setTemplateCode(systemMessage.getTpcode()); + //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 + //友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败 + request.setTemplateParam(params); + //可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段) + //request.setSmsUpExtendCode("90997"); + //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 + request.setOutId("yourOutId"); + //请求失败这里会抛ClientException异常 + SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); + if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) { + return true; + } else if (!StringUtils.isBlank(sendSmsResponse.getMessage())) { + try { + throw new Exception("短信发送失败,原因:" + sendSmsResponse.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + return false; + } + + /** + * @param userid + * @param client + * @param session + * @param orgi + * @param ipaddr + * @param hostname + * @return + */ + public static WorkSession createWorkSession(String userid, String client, String session, String orgi, String ipaddr, String hostname, String admin, boolean first) { + WorkSession workSession = new WorkSession(); + workSession.setCreatetime(new Date()); + workSession.setBegintime(new Date()); + workSession.setAgent(userid); + workSession.setAgentno(userid); + workSession.setAgentno(userid); + if (!StringUtils.isBlank(admin) && admin.equalsIgnoreCase("true")) { + workSession.setAdmin(true); + } + + workSession.setFirsttime(first); + + workSession.setIpaddr(ipaddr); + workSession.setHostname(hostname); + workSession.setUserid(userid); + workSession.setClientid(client); + workSession.setSessionid(session); + workSession.setOrgi(orgi); + + workSession.setDatestr(MainUtils.simpleDateFormat.format(new Date())); + + return workSession; + } + + /** + * @param plan + * @return + */ + public static String convertCrond(JobTask plan) { + StringBuffer strb = new StringBuffer(); + if ("day".equals(plan.getRunCycle())) { + strb.append(plan.getRunBeginSecond()).append(" ").append(plan.getRunBeginMinute()).append(plan.getIsRepeat() && plan.getRepeatSpace() != null && plan.getRepeatSpace() < 60 ? "/" + plan.getRepeatSpace() : "").append(" ").append(plan.getRunBeginHour()).append(plan.getIsRepeat() && plan.getRepeatSpace() != null && plan.getRepeatSpace() > 60 ? "/" + plan.getRepeatSpace() / 60 : (plan.getRepeatJustTime() != null && plan.getRepeatJustTime() > 0 ? "-" + (plan.getRunBeginHour() + plan.getRepeatJustTime()) : "")).append(" ").append("*").append(plan.getRunSpace() != null && plan.getRunSpace() > 0 ? "/" + plan.getRunSpace() : "").append(" ").append(" * ?"); + } + if ("week".equals(plan.getRunCycle())) { + strb.append(plan.getRunBeginSecond()).append(" ").append(plan.getRunBeginMinute()).append(plan.getIsRepeat() && plan.getRepeatSpace() != null && plan.getRepeatSpace() < 60 ? "/" + plan.getRepeatSpace() : "").append(" ").append(plan.getRunBeginHour()).append(plan.getIsRepeat() && plan.getRepeatSpace() != null && plan.getRepeatSpace() > 60 ? "/" + plan.getRepeatSpace() / 60 : (plan.getRepeatJustTime() != null && plan.getRepeatJustTime() > 0 ? "-" + (plan.getRunBeginHour() + plan.getRepeatJustTime()) : "")).append(" ").append(plan.getRunDates() == null || plan.getRunDates().length == 0 ? "*" : "?").append(" * ").append(plan.getRunDates() == null || plan.getRunDates().length == 0 ? "?" : StringUtils.join(plan.getRunDates(), ",")).append(plan.getRunSpace() != null && plan.getRunSpace() > 0 ? "/" + plan.getRunSpace() : ""); + } + if ("month".equals(plan.getRunCycle())) { + strb.append(plan.getRunBeginSecond()).append(" ").append(plan.getRunBeginMinute()).append(plan.getIsRepeat() && plan.getRepeatSpace() != null && plan.getRepeatSpace() < 60 ? "/" + plan.getRepeatSpace() : "").append(" ").append(plan.getRunBeginHour()).append(plan.getIsRepeat() && plan.getRepeatSpace() != null && plan.getRepeatSpace() > 60 ? "/" + plan.getRepeatSpace() / 60 : (plan.getRepeatJustTime() != null && plan.getRepeatJustTime() > 0 ? "-" + (plan.getRunBeginHour() + plan.getRepeatJustTime()) : "")).append(" ").append(plan.getRunBeginDate()).append(" ").append(plan.getRunDates() == null || plan.getRunDates().length == 0 ? "*" : StringUtils.join(plan.getRunDates(), ",")).append(" ").append(" ?"); + } + return strb.toString(); + } + + public static Date updateTaskNextFireTime(JobDetail jobDetail) throws Exception { + Date nextFireDate = new Date(); + Date date = new Date(); + if (jobDetail != null && jobDetail.getCronexp() != null && jobDetail.getCronexp().length() > 0) { + try { + nextFireDate = (CronTools.getFinalFireTime(jobDetail.getCronexp(), jobDetail.getNextfiretime() != null ? jobDetail.getNextfiretime() : date)); + } catch (ParseException e) { + nextFireDate = new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24); //一旦任务的 Cron表达式错误,将下次执行时间自动设置为一天后,避免出现任务永远无法终止的情况 + e.printStackTrace(); + } + } + return nextFireDate; + } + /** - * * @param dialNum * @param distype * @return */ - public static String processSecField(String dialNum , String distype) { - StringBuilder strb = new StringBuilder(dialNum) ; - if(distype.equals("01")) { - if(strb.length() > 4) { - strb.replace(strb.length()/2 - 2, strb.length()/2 + 2, "****") ; - }else { - strb.replace(0, strb.length(), "****") ; - } - }else if(distype.equals("02")) { - if(strb.length() > 4) { - strb.replace(strb.length()-4 , strb.length(), "****") ; - }else { - strb.replace(0, strb.length(), "****") ; - } - }else if(distype.equals("03")) { - if(strb.length() > 4) { - strb.replace(0 , 4, "****") ; - }else { - strb.replace(0, strb.length(), "****") ; - } - }else if(distype.equals("04")) { - int length = strb.length() ; - strb.setLength(0); - for(int i=0 ; i 4) { + strb.replace(strb.length() / 2 - 2, strb.length() / 2 + 2, "****"); + } else { + strb.replace(0, strb.length(), "****"); + } + } else if (distype.equals("02")) { + if (strb.length() > 4) { + strb.replace(strb.length() - 4, strb.length(), "****"); + } else { + strb.replace(0, strb.length(), "****"); + } + } else if (distype.equals("03")) { + if (strb.length() > 4) { + strb.replace(0, 4, "****"); + } else { + strb.replace(0, strb.length(), "****"); + } + } else if (distype.equals("04")) { + int length = strb.length(); + strb.setLength(0); + for (int i = 0; i < length; i++) { + strb.append("*"); + } + } + return strb.toString(); } } diff --git a/contact-center/app/src/main/java/com/chatopera/cc/app/config/DisruptorConfigure.java b/contact-center/app/src/main/java/com/chatopera/cc/app/config/DisruptorConfigure.java index 91b65a10..4b86ef49 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/app/config/DisruptorConfigure.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/app/config/DisruptorConfigure.java @@ -21,6 +21,7 @@ import java.util.concurrent.Executors; import com.chatopera.cc.concurrent.chatbot.ChatbotDisruptorExceptionHandler; import com.chatopera.cc.concurrent.chatbot.ChatbotEventFactory; +import com.chatopera.cc.concurrent.chatbot.ChatbotEventHandler; import com.chatopera.cc.concurrent.multiupdate.MultiUpdateEventFactory; import com.chatopera.cc.concurrent.multiupdate.MultiUpdateEventHandler; import org.springframework.context.annotation.Bean; @@ -65,7 +66,7 @@ public class DisruptorConfigure { Executor executor = Executors.newCachedThreadPool(); ChatbotEventFactory factory = new ChatbotEventFactory(); Disruptor disruptor = new Disruptor(factory, 1024, executor, ProducerType.SINGLE , new SleepingWaitStrategy()); - disruptor.handleEventsWith(new MultiUpdateEventHandler()); + disruptor.handleEventsWith(new ChatbotEventHandler()); disruptor.setDefaultExceptionHandler(new ChatbotDisruptorExceptionHandler()); disruptor.start(); return disruptor; diff --git a/contact-center/app/src/main/java/com/chatopera/cc/app/im/handler/ChatbotEventHandler.java b/contact-center/app/src/main/java/com/chatopera/cc/app/im/handler/ChatbotEventHandler.java index c7f206d0..3e2ecd4e 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/app/im/handler/ChatbotEventHandler.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/app/im/handler/ChatbotEventHandler.java @@ -17,21 +17,23 @@ package com.chatopera.cc.app.im.handler; import com.chatopera.cc.app.algorithm.AutomaticServiceDist; import com.chatopera.cc.app.basic.MainContext; -import com.chatopera.cc.app.im.util.ChatbotUtils; -import com.chatopera.cc.util.IP; -import com.chatopera.cc.util.IPTools; import com.chatopera.cc.app.basic.MainUtils; -import com.chatopera.cc.app.im.client.NettyClients; -import com.chatopera.cc.app.model.*; import com.chatopera.cc.app.cache.CacheHelper; -import com.chatopera.cc.app.persistence.repository.AgentUserRepository; -import com.chatopera.cc.app.persistence.repository.ConsultInviteRepository; -import com.chatopera.cc.app.persistence.repository.OnlineUserRepository; -import com.chatopera.cc.app.im.router.OutMessageRouter; -import com.chatopera.cc.util.OnlineUserUtils; +import com.chatopera.cc.app.im.client.NettyClients; import com.chatopera.cc.app.im.message.AgentStatusMessage; import com.chatopera.cc.app.im.message.ChatMessage; import com.chatopera.cc.app.im.message.NewRequestMessage; +import com.chatopera.cc.app.im.util.ChatbotUtils; +import com.chatopera.cc.app.model.*; +import com.chatopera.cc.app.persistence.repository.AgentUserRepository; +import com.chatopera.cc.app.persistence.repository.ChatbotRepository; +import com.chatopera.cc.app.persistence.repository.ConsultInviteRepository; +import com.chatopera.cc.app.persistence.repository.OnlineUserRepository; +import com.chatopera.cc.concurrent.chatbot.ChatbotEvent; +import com.chatopera.cc.util.Constants; +import com.chatopera.cc.util.IP; +import com.chatopera.cc.util.IPTools; +import com.chatopera.cc.util.OnlineUserUtils; import com.corundumstudio.socketio.AckRequest; import com.corundumstudio.socketio.SocketIOClient; import com.corundumstudio.socketio.SocketIOServer; @@ -53,6 +55,7 @@ public class ChatbotEventHandler { private AgentUserRepository agentUserRes; private OnlineUserRepository onlineUserRes; + private ChatbotRepository chatbotRes; @Autowired public ChatbotEventHandler(SocketIOServer server) { @@ -73,9 +76,9 @@ public class ChatbotEventHandler { Date now = new Date(); if (StringUtils.isNotBlank(user)) { -// /** -// * 加入到 缓存列表 -// */ + /** + * 加入到 缓存列表 + */ NettyClients.getInstance().putChatbotEventClient(user, client); MessageOutContent outMessage = new MessageOutContent(); CousultInvite invite = OnlineUserUtils.cousult(appid, orgi, MainContext.getContext().getBean(ConsultInviteRepository.class)); @@ -195,62 +198,71 @@ public class ChatbotEventHandler { String orgi = client.getHandshakeData().getSingleUrlParam("orgi"); String aiid = client.getHandshakeData().getSingleUrlParam("aiid"); String user = client.getHandshakeData().getSingleUrlParam("userid"); - if (data.getType() == null) { - data.setType("message"); + logger.info("[chatbot] onEvent message: orgi {}, aiid {}, userid {}, dataType {}", orgi, aiid, user, data.getType()); + // ignore event if dataType is not message. + if (!StringUtils.equals(data.getType(), Constants.IM_MESSAGE_TYPE_MESSAGE)) { + return; } + + AgentUser agentUser = (AgentUser) CacheHelper.getAgentUserCacheBean().getCacheObject(user, orgi); + + // ignore event if no agentUser found. + if (agentUser == null) + return; + /** * 以下代码主要用于检查 访客端的字数限制 */ CousultInvite invite = OnlineUserUtils.cousult(data.getAppid(), data.getOrgi(), MainContext.getContext().getBean(ConsultInviteRepository.class)); - if (invite != null && invite.getMaxwordsnum() > 0) { - if (!StringUtils.isBlank(data.getMessage()) && data.getMessage().length() > invite.getMaxwordsnum()) { + // ignore event if no invite found. + if (invite == null) + return; + + // ignore if Chatbot is turnoff. + if (!invite.isAi()) + return; + + Date now = new Date(); + if (invite.getMaxwordsnum() > 0) { + if (StringUtils.isNotBlank(data.getMessage()) && data.getMessage().length() > invite.getMaxwordsnum()) { data.setMessage(data.getMessage().substring(0, invite.getMaxwordsnum())); } - } else if (!StringUtils.isBlank(data.getMessage()) && data.getMessage().length() > 300) { + } else if (StringUtils.isNotBlank(data.getMessage()) && data.getMessage().length() > 300) { data.setMessage(data.getMessage().substring(0, 300)); } + data.setSessionid(MainUtils.getContextID(client.getSessionId().toString())); - /** - * 处理表情 - */ - data.setMessage(MainUtils.processEmoti(data.getMessage())); + data.setMessage(MainUtils.processEmoti(data.getMessage())); // 处理表情 data.setTousername(invite.getAiname()); - data.setAiid(aiid); + data.setAgentserviceid(agentUser.getAgentserviceid()); + data.setChannel(agentUser.getChannel()); + data.setContextid(agentUser.getAgentserviceid()); // 一定要设置 ContextID + data.setCalltype(MainContext.CallTypeEnum.IN.toString()); - AgentUser agentUser = (AgentUser) CacheHelper.getAgentUserCacheBean().getCacheObject(user, orgi); - if (agentUser != null) { - data.setAgentserviceid(agentUser.getAgentserviceid()); - data.setChannel(agentUser.getChannel()); - /** - * 一定要设置 ContextID - */ - data.setContextid(agentUser.getAgentserviceid()); - } - MessageOutContent outMessage = ChatbotUtils.createTextMessage(data, data.getAppid(), data.getChannel(), MainContext.CallTypeEnum.IN.toString(), MainContext.ChatbotItemType.USERINPUT.toString(), data.getUserid()); - if (StringUtils.isNotBlank(data.getUserid()) && MainContext.MessageTypeEnum.MESSAGE.toString().equals(data.getType())) { - if (!StringUtils.isBlank(data.getTouser())) { - OutMessageRouter router = null; - router = (OutMessageRouter) MainContext.getContext().getBean(data.getChannel()); - if (router != null) { - router.handler(data.getTouser(), MainContext.MessageTypeEnum.MESSAGE.toString(), data.getAppid(), outMessage); - } - } - if (agentUser != null) { - Date now = new Date(); - agentUser.setUpdatetime(now); - agentUser.setLastmessage(now); - agentUser.setLastmsg(data.getMessage()); - CacheHelper.getAgentUserCacheBean().put(user, agentUser, MainContext.SYSTEM_ORGI); - } - } + ChatbotUtils.createTextMessage(data, + MainContext.CallTypeEnum.IN.toString(), + MainContext.ChatbotItemType.USERINPUT.toString()); + + // 更新访客咨询记录 + agentUser.setUpdatetime(now); + agentUser.setLastmessage(now); + agentUser.setLastmsg(data.getMessage()); + CacheHelper.getAgentUserCacheBean().put(user, agentUser, orgi); + getAgentUserRes().save(agentUser); + + // 发送消息给Bot + MainUtils.chatbot(new ChatbotEvent(data, + getChatbotRes(), + Constants.CHATBOT_EVENT_TYPE_CHAT)); } /** * Lazy load + * * @return */ - public AgentUserRepository getAgentUserRes() { + private AgentUserRepository getAgentUserRes() { if (agentUserRes == null) agentUserRes = MainContext.getContext().getBean(AgentUserRepository.class); return agentUserRes; @@ -258,11 +270,19 @@ public class ChatbotEventHandler { /** * Lazy load + * * @return */ - public OnlineUserRepository getOnlineUserRes() { + private OnlineUserRepository getOnlineUserRes() { if (onlineUserRes == null) onlineUserRes = MainContext.getContext().getBean(OnlineUserRepository.class); return onlineUserRes; } + + private ChatbotRepository getChatbotRes() { + if (chatbotRes == null) + chatbotRes = MainContext.getContext().getBean(ChatbotRepository.class); + return chatbotRes; + } + } \ No newline at end of file diff --git a/contact-center/app/src/main/java/com/chatopera/cc/app/im/util/ChatbotUtils.java b/contact-center/app/src/main/java/com/chatopera/cc/app/im/util/ChatbotUtils.java index 36fc97e7..9e41aeab 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/app/im/util/ChatbotUtils.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/app/im/util/ChatbotUtils.java @@ -16,18 +16,23 @@ package com.chatopera.cc.app.im.util; import com.chatopera.cc.app.basic.MainContext; +import com.chatopera.cc.app.cache.CacheHelper; import com.chatopera.cc.app.im.client.NettyClients; import com.chatopera.cc.app.im.message.ChatMessage; import com.chatopera.cc.app.model.AgentUser; +import com.chatopera.cc.app.model.Chatbot; import com.chatopera.cc.app.model.MessageOutContent; -import com.chatopera.cc.app.cache.CacheHelper; import com.chatopera.cc.app.persistence.repository.ChatMessageRepository; +import com.chatopera.cc.app.persistence.repository.ChatbotRepository; import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Arrays; import java.util.HashSet; public class ChatbotUtils { + private final static Logger logger = LoggerFactory.getLogger(ChatbotUtils.class); public static final HashSet VALID_LANGS = new HashSet(Arrays.asList(new String[]{"zh_CN", "en_US"})); public static final String CHATBOT_FIRST = "机器人客服优先"; public static final String HUMAN_FIRST = "人工客服优先"; @@ -35,6 +40,9 @@ public class ChatbotUtils { public static final String SNS_TYPE_WEBIM = "app"; + private static ChatbotRepository chatbotRes; + + private static ChatMessageRepository chatMessageRes; /** * 使用snsid得到ChatbotID @@ -56,6 +64,48 @@ public class ChatbotUtils { return StringUtils.remove(chatbotID, clientId.toLowerCase() + "_"); } + /** + * @param data + * @param direction + * @param chatype + * @param msgtype + * @return + */ + private static MessageOutContent createMessage(ChatMessage data, String direction, String chatype, String msgtype) { + AgentUser agentUser = (AgentUser) CacheHelper.getAgentUserCacheBean().getCacheObject(data.getUserid(), data.getOrgi()); + if (agentUser == null) + return null; + + // 设置发送消息体 + MessageOutContent outMessage = new MessageOutContent(); + outMessage.setMessage(data.getMessage()); + outMessage.setMessageType(msgtype); + outMessage.setCalltype(direction); + outMessage.setAgentUser(null); + outMessage.setSnsAccount(null); + if (StringUtils.isNotBlank(data.getSuggestmsg())) { + outMessage.setSuggest(data.getSuggest()); + } + + outMessage.setContextid(data.getUserid()); + outMessage.setFromUser(data.getUserid()); + outMessage.setToUser(data.getTouser()); + outMessage.setChannelMessage(data); + outMessage.setNickName(data.getUsername()); + outMessage.setCreatetime(data.getCreatetime()); + + /** + * 保存消息 + */ + getChatMessageRes().save(data); + + //将消息发送给 访客 + NettyClients.getInstance().sendChatbotEventMessage(data.getUserid(), MainContext.MessageTypeEnum.MESSAGE.toString(), data); + + return outMessage; + } + + /** * 发送聊天机器人消息 * @@ -68,81 +118,53 @@ public class ChatbotUtils { * @param userid * @return */ - protected static MessageOutContent createMessage(ChatMessage data, String appid, String channel, String direction, String chatype, String msgtype, String userid) { - MessageOutContent outMessage = new MessageOutContent(); + protected static MessageOutContent createMessage(final ChatMessage data, + final String appid, + final String channel, + final String direction, + final String chatype, + final String msgtype, + final String userid, + final String orgi) { + Chatbot c = getChatbotRes().findBySnsAccountIdentifierAndOrgi(appid, orgi); + if (c == null) // ignore event if chatbot not exist. + return null; - outMessage.setMessage(data.getMessage()); - outMessage.setMessageType(msgtype); - outMessage.setCalltype(direction); - outMessage.setAgentUser(null); - outMessage.setSnsAccount(null); - - { - data.setUserid(userid); - data.setUsername(data.getUsername()); - data.setTouser(userid); - - data.setAgentuser(userid); - - - data.setAgentserviceid(data.getContextid()); - data.setChatype(chatype); - - data.setChannel(channel); - - data.setAppid(data.getAppid()); - data.setOrgi(data.getOrgi()); - - data.setMsgtype(msgtype); - - data.setUsername(data.getUsername()); - data.setUsession(data.getUserid()); //agentUser作为 session id - data.setContextid(data.getContextid()); - data.setCalltype(direction); - - outMessage.setContextid(data.getContextid()); - outMessage.setFromUser(data.getUserid()); - outMessage.setToUser(data.getTouser()); - outMessage.setChannelMessage(data); - outMessage.setNickName(data.getUsername()); - outMessage.setCreatetime(data.getCreatetime()); - - if (!StringUtils.isBlank(data.getSuggestmsg())) { - outMessage.setSuggest(data.getSuggest()); - } - - data.setUpdatetime(System.currentTimeMillis()); - - /** - * 保存消息 - */ - if (MainContext.MessageTypeEnum.MESSAGE.toString().equals(data.getType())) { - MainContext.getContext().getBean(ChatMessageRepository.class).save(data); - } - AgentUser agentUser = (AgentUser) CacheHelper.getAgentUserCacheBean().getCacheObject(userid, MainContext.SYSTEM_ORGI); - if (agentUser != null && !StringUtils.isBlank(agentUser.getAgentno())) { - //将消息发送给 坐席 - - if (MainContext.CallTypeEnum.OUT.toString().equals(direction)) { - data.setUserid(agentUser.getAgentno()); - } - NettyClients.getInstance().sendAgentEventMessage(agentUser.getAgentno(), MainContext.MessageTypeEnum.MESSAGE.toString(), data); - } - } - return outMessage; + data.setAiid(c.getId()); + data.setOrgi(orgi); + data.setUserid(userid); + data.setAgentserviceid(data.getContextid()); + data.setChatype(chatype); + data.setChannel(channel); + data.setMsgtype(msgtype); + data.setUsession(data.getUserid()); //agentUser作为 session id + data.setCalltype(direction); + data.setUpdatetime(System.currentTimeMillis()); + return createMessage(data, direction, chatype, msgtype); } /** * 发送文字消息 + * * @param data - * @param appid - * @param channel * @param direction * @param chatype - * @param userid * @return */ - public static MessageOutContent createTextMessage(ChatMessage data, String appid, String channel, String direction, String chatype, String userid) { - return createMessage(data, appid, channel, direction, chatype, MainContext.MediaTypeEnum.TEXT.toString(), userid); + public static MessageOutContent createTextMessage(ChatMessage data, String direction, String chatype) { + return createMessage(data, direction, chatype, MainContext.MediaTypeEnum.TEXT.toString()); } + + private static ChatbotRepository getChatbotRes() { + if (chatbotRes == null) + chatbotRes = MainContext.getContext().getBean(ChatbotRepository.class); + return chatbotRes; + } + + private static ChatMessageRepository getChatMessageRes() { + if (chatMessageRes == null) + chatMessageRes = MainContext.getContext().getBean(ChatMessageRepository.class); + return chatMessageRes; + } + } diff --git a/contact-center/app/src/main/java/com/chatopera/cc/app/im/util/RichMediaUtils.java b/contact-center/app/src/main/java/com/chatopera/cc/app/im/util/RichMediaUtils.java index 1ea16837..97ff51e4 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/app/im/util/RichMediaUtils.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/app/im/util/RichMediaUtils.java @@ -147,7 +147,7 @@ public class RichMediaUtils { if (StringUtils.isNotBlank(userid)) { AgentUser agentUser = (AgentUser) CacheHelper.getAgentUserCacheBean().getCacheObject(userid, MainContext.SYSTEM_ORGI); if ((agentUser != null) && StringUtils.equals(agentUser.getOpttype(), MainContext.OptTypeEnum.CHATBOT.toString())) { - ChatbotUtils.createMessage(data, appid, channel, MainContext.CallTypeEnum.IN.toString(), MainContext.ChatbotItemType.USERINPUT.toString(), msgtype, data.getUserid()); + ChatbotUtils.createMessage(data, appid, channel, MainContext.CallTypeEnum.IN.toString(), MainContext.ChatbotItemType.USERINPUT.toString(), msgtype, data.getUserid(), orgi); } else { HumanUtils.createMessage(data, msgtype, userid); } diff --git a/contact-center/app/src/main/java/com/chatopera/cc/app/persistence/repository/ChatbotRepository.java b/contact-center/app/src/main/java/com/chatopera/cc/app/persistence/repository/ChatbotRepository.java index bb7ab7ed..ffc2212d 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/app/persistence/repository/ChatbotRepository.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/app/persistence/repository/ChatbotRepository.java @@ -17,6 +17,8 @@ public abstract interface ChatbotRepository extends JpaRepository findByIdAndOrgi(String id, String orgi); + public abstract Chatbot findBySnsAccountIdentifierAndOrgi(String snsid, String orgi); + @Query(value = "select c from Chatbot c where " + "(:myorgans is null or c.organ IN :myorgans)") public Page findByOrgans(@Param("myorgans") List myorgans, Pageable pageRequest); diff --git a/contact-center/app/src/main/java/com/chatopera/cc/concurrent/chatbot/ChatbotEvent.java b/contact-center/app/src/main/java/com/chatopera/cc/concurrent/chatbot/ChatbotEvent.java index 8ad995f9..db30ae7e 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/concurrent/chatbot/ChatbotEvent.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/concurrent/chatbot/ChatbotEvent.java @@ -16,17 +16,18 @@ package com.chatopera.cc.concurrent.chatbot; import com.chatopera.cc.app.persistence.hibernate.BaseService; +import com.chatopera.cc.app.persistence.repository.ChatbotRepository; import com.chatopera.cc.exchange.UserEvent; public class ChatbotEvent implements UserEvent { private S data; - private BaseService crudRes; + private ChatbotRepository chatbotRes; private String eventype; - public ChatbotEvent(S data, BaseService crudRes, String eventype) { + public ChatbotEvent(S data, ChatbotRepository chatbotRes, String eventype) { this.data = data; - this.crudRes = crudRes; + this.chatbotRes = chatbotRes; this.eventype = eventype; } @@ -38,12 +39,12 @@ public class ChatbotEvent implements UserEvent { this.data = data; } - public BaseService getCrudRes() { - return crudRes; + public ChatbotRepository getChatbotRes() { + return chatbotRes; } - public void setCrudRes(BaseService crudRes) { - this.crudRes = crudRes; + public void setChatbotRes(ChatbotRepository chatbotRes) { + this.chatbotRes = chatbotRes; } public String getEventype() { diff --git a/contact-center/app/src/main/java/com/chatopera/cc/concurrent/chatbot/ChatbotEventHandler.java b/contact-center/app/src/main/java/com/chatopera/cc/concurrent/chatbot/ChatbotEventHandler.java index 1986e5c1..66a7ce39 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/concurrent/chatbot/ChatbotEventHandler.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/concurrent/chatbot/ChatbotEventHandler.java @@ -15,22 +15,69 @@ */ package com.chatopera.cc.concurrent.chatbot; +import com.chatopera.cc.app.basic.MainContext; +import com.chatopera.cc.app.handler.api.request.RestUtils; +import com.chatopera.cc.app.im.client.NettyClients; +import com.chatopera.cc.app.im.message.ChatMessage; +import com.chatopera.cc.app.model.Chatbot; import com.chatopera.cc.concurrent.user.UserDataEvent; +import com.chatopera.cc.util.Constants; +import com.chatopera.chatbot.ChatbotAPIRuntimeException; import com.lmax.disruptor.EventHandler; +import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.net.MalformedURLException; + @SuppressWarnings("rawtypes") public class ChatbotEventHandler implements EventHandler { private final static Logger logger = LoggerFactory.getLogger(ChatbotEventHandler.class); + private void chat(final ChatbotEvent payload) throws MalformedURLException, ChatbotAPIRuntimeException { + ChatMessage request = (ChatMessage) payload.getData(); + Chatbot c = payload.getChatbotRes() + .findOne(request.getAiid()); + + logger.info("[chatbot disruptor] chat request baseUrl {}, chatbotID {}, fromUserId {}, textMessage {}", c.getBaseUrl(), c.getChatbotID(), request.getUserid(), request.getMessage()); + // Get response from Conversational Engine. + JSONObject result = c.getApi() + .conversation(c.getChatbotID(), request.getUserid(), request.getMessage(), false); + + // parse response + logger.info("[chatbot disruptor] chat response {}", result.toString()); + if(result.getInt(RestUtils.RESP_KEY_RC) == 0){ + // reply + ChatMessage resp = new ChatMessage(); + resp.setCalltype(MainContext.CallTypeEnum.OUT.toString()); + resp.setOrgi(request.getOrgi()); + resp.setAiid(request.getAiid()); + resp.setMessage(result.getJSONObject("data").getString("string")); + resp.setTouser(request.getUsername()); + resp.setMsgtype(request.getMsgtype()); + resp.setUserid(request.getUserid()); + resp.setChannel(request.getChannel()); + resp.setContextid(resp.getContextid()); + resp.setSessionid(resp.getSessionid()); + resp.setUsername(c.getName()); + NettyClients.getInstance().sendChatbotEventMessage(request.getUserid(), MainContext.MessageTypeEnum.MESSAGE.toString(), resp); + } else { + // TODO handle exceptions + } + } + + @Override public void onEvent(UserDataEvent event, long arg1, boolean arg2) throws Exception { - if (event.getEvent() != null) { - ChatbotEvent x = (ChatbotEvent) event.getEvent(); - // TODO #75 - logger.info("onEvent"); + ChatbotEvent payload = (ChatbotEvent) event.getEvent(); + switch (payload.getEventype()) { + case Constants + .CHATBOT_EVENT_TYPE_CHAT: + chat(payload); + break; + default: + logger.warn("[chatbot disruptor] onEvent unknown."); } } diff --git a/contact-center/app/src/main/java/com/chatopera/cc/util/Constants.java b/contact-center/app/src/main/java/com/chatopera/cc/util/Constants.java index 7efd6f7b..53004f80 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/util/Constants.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/util/Constants.java @@ -27,6 +27,9 @@ public class Constants { public final static String MINIO_BUCKET = "chatopera"; + public final static String IM_MESSAGE_TYPE_MESSAGE = "message"; + public final static String CHATBOT_EVENT_TYPE_CHAT = "chat"; + /** * Formatter */ diff --git a/contact-center/app/src/main/resources/templates/apps/im/chatbot/index.html b/contact-center/app/src/main/resources/templates/apps/im/chatbot/index.html index d3fdeee9..2c1a5fc4 100644 --- a/contact-center/app/src/main/resources/templates/apps/im/chatbot/index.html +++ b/contact-center/app/src/main/resources/templates/apps/im/chatbot/index.html @@ -62,16 +62,17 @@ if(this.count("text") == 0){ strValue= "" ; } - if(words != this.count("text")){ - socket.emit('message', { - appid : "${appid!''}", - userid:"${userid!''}", - type:"writing", - session:"${sessionid!''}", - orgi:"${orgi!''}", - message : strValue - }); - } + // ignore writting in Chatbot Service + // if(words != this.count("text")){ + // socket.emit('message', { + // appid : "${appid!''}", + // userid:"${userid!''}", + // type:"writing", + // session:"${sessionid!''}", + // orgi:"${orgi!''}", + // message : strValue + // }); + // } words = this.count("text") ; document.getElementById('surplus').innerHTML = count+"/"+limitNum+" , " + pattern; //输入显示 //////// @@ -488,6 +489,7 @@ } }) socket.on('message', function(data) { + console.log("[chatbot io] message ", data); var chat=document.getElementsByClassName('chatting-left').innerText; chat = data.message; if(data.messageType == "image"){ @@ -496,9 +498,9 @@ chat = "" ; } if(data.calltype == "呼入"){ - output('
'+chat+'
' , "chat-block"); + output('
'+chat+'
' , "chat-block"); }else if(data.calltype == "呼出"){ - output('
'+chat+'
' , "chat-block"); + output('
'+chat+'
' , "chat-block"); R3Ajax.audioplayer('audioplane', newmessage, false); // 播放 } });