| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977 |
- package com.telerobot.fs.utils;
- import com.alibaba.fastjson.JSONObject;
- import com.auth0.jwt.JWT;
- import com.auth0.jwt.algorithms.Algorithm;
- import com.auth0.jwt.interfaces.DecodedJWT;
- import com.auth0.jwt.interfaces.JWTVerifier;
- import com.telerobot.fs.config.SystemConfig;
- import com.telerobot.fs.entity.bo.InboundDetail;
- import com.telerobot.fs.entity.po.FunAsrResultEntity;
- import com.telerobot.fs.entity.po.HangupCause;
- import com.telerobot.fs.wshandle.MessageResponse;
- import com.telerobot.fs.wshandle.RespStatus;
- import link.thingscloud.freeswitch.esl.EslConnectionUtil;
- import link.thingscloud.freeswitch.esl.transport.message.EslMessage;
- import org.apache.commons.lang.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.*;
- import java.math.BigDecimal;
- import java.math.BigInteger;
- import java.net.URLDecoder;
- import java.util.*;
- import java.util.concurrent.TimeUnit;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipOutputStream;
- /**
- * 工具类
- */
- public class CommonUtils<T> {
- private static final Logger logger = LoggerFactory.getLogger(CommonUtils.class);
- public static String execSystemCommand(String command) {
- Runtime runtime = Runtime.getRuntime();
- StringBuilder output = new StringBuilder();
- try {
- Process p = runtime.exec(command);
- // 启动另一个进程来执行命令
- BufferedInputStream in = new BufferedInputStream(p.getInputStream());
- BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
- String lineStr;
- //获得命令执行后在控制台的输出信息
- while ((lineStr = inBr.readLine()) != null) {
- output.append(lineStr).append("\n");
- }
- if (p.waitFor(11, TimeUnit.SECONDS)) {
- // p.exitValue()==0表示正常结束,1:非正常结束
- if (p.exitValue() == 1) {
- logger.error("exec system command error: {}", command);
- }
- }
- } catch (Throwable e) {
- logger.error("exec system command error: {} {} {}", command, e.toString(), CommonUtils.getStackTraceString(e.getStackTrace()));
- }
- return output.toString();
- }
- public static String getIpFromFullAddress(String clientFullAddr){
- String ip = clientFullAddr;
- if(clientFullAddr.contains(":")){
- ip = clientFullAddr.substring(0, clientFullAddr.indexOf(":")) ;
- };
- return ip.replace("/","");
- }
- public static void hangupCallSession(String uuid, String reason){
- EslConnectionUtil.sendExecuteCommand("hangup", reason, uuid);
- }
- public static void setHangupCauseDetail(InboundDetail callDetail, HangupCause cause, String details){
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("code", cause.getCode());
- jsonObject.put("details", details);
- String json = jsonObject.toJSONString();
- if(callDetail.getOutboundPhoneInfo() != null) {
- if(com.telerobot.fs.utils.StringUtils.isNullOrEmpty(callDetail.getOutboundPhoneInfo().getHangupCause())) {
- callDetail.getOutboundPhoneInfo().setHangupCause(json);
- }
- }
- if(com.telerobot.fs.utils.StringUtils.isNullOrEmpty(callDetail.getHangupCause())) {
- callDetail.setHangupCause(json);
- }
- }
- public static void setHangupCauseDetail(InboundDetail callDetail, String cause, String details){
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("code", cause);
- jsonObject.put("details", details);
- String json = jsonObject.toJSONString();
- if(callDetail.getOutboundPhoneInfo() != null) {
- callDetail.getOutboundPhoneInfo().setHangupCause(json);
- }
- callDetail.setHangupCause(json);
- }
- /**
- * 查询反向注册分机的 Contact(ip:port),默认先查 internal。
- */
- public static String getDynamicGatewayAddr(String extensionNumber, String traceId){
- return getDynamicGatewayAddr(extensionNumber, traceId, "internal");
- }
- /**
- * 按指定 sofia profile 查询已注册分机的 Contact(ip:port)。
- * 反向注册线路通常在 external;查不到时再回退 external / internal。
- */
- public static String getDynamicGatewayAddr(String extensionNumber, String traceId, String profileName){
- if(StringUtils.isBlank(extensionNumber)){
- logger.warn("{} 反向注册 authUsername 为空,无法查询 Contact", traceId);
- return "";
- }
- List<String> profiles = new ArrayList<String>(3);
- if(StringUtils.isNotBlank(profileName)){
- profiles.add(profileName.trim());
- }
- if(!profiles.contains("external")){
- profiles.add("external");
- }
- if(!profiles.contains("internal")){
- profiles.add("internal");
- }
- for(String profile : profiles){
- EslMessage response = EslConnectionUtil.sendSyncApiCommand(
- "sofia", "xmlstatus profile " + profile + " reg");
- if(response == null || response.getBodyLines() == null){
- logger.warn("{} 查询反向注册 Contact:profile={} 无响应,authUsername={}",
- traceId, profile, extensionNumber);
- continue;
- }
- StringBuilder xml = new StringBuilder();
- for(String s : response.getBodyLines()){
- xml.append(s);
- }
- logger.info("{} 查询反向注册 Contact:sofia xmlstatus profile {} reg,响应长度={}, authUsername={}",
- traceId, profile, xml.length(), extensionNumber);
- String contact = XmlUtils.parseFsOnlineUserListXml(xml.toString(), extensionNumber);
- if(StringUtils.isNotBlank(contact)){
- logger.info("{} 已找到反向注册 Contact:authUsername={}, profile={}, contact={}",
- traceId, extensionNumber, profile, contact);
- return contact;
- }
- logger.info("{} 当前 profile 未找到 Contact:profile={}, authUsername={}",
- traceId, profile, extensionNumber);
- }
- logger.warn("{} 反向注册 Contact 为空,authUsername={},已查 profiles={},不再 originate 空地址",
- traceId, extensionNumber, profiles);
- return "";
- }
- /**
- * Randomly obtain a outbound display number
- * @return
- */
- public static String getCallerNumberRandomly(String callers){
- if(callers.contains("\n")){
- String[] array = callers.split("\\n");
- int index = RandomUtils.getRandomByRange(0, array.length - 1);
- return array[index].trim();
- }else{
- return callers.trim();
- }
- }
- public static boolean safeCreateDirectory(String dir){
- File directory = new File(dir);
- if(!directory.exists()){
- synchronized (dir.intern()){
- if(!directory.exists()){
- return directory.mkdirs();
- }
- }
- }
- return true;
- }
- /**
- * 校验客户端 http 请求的 token
- * @param request
- * @return
- */
- public static String validateHttpHeaderToken(HttpServletRequest request, HttpServletResponse response) {
- String sysToken = SystemConfig.getValue("call-center-api-token", "");
- String token = request.getHeader("Authorization");
- // remove start string: "Bearer "
- if (!StringUtils.isEmpty(token) && token.length() > 7) {
- token = token.substring(7);
- }
- if (!sysToken.equals(token)) {
- response.setStatus(400);
- return "{ \"code\": 400, \"msg\" : \"validate token error.\" }";
- }
- return "";
- }
- public static Map<String, String> parseUrlQueryString(String queryString) throws UnsupportedEncodingException {
- Map<String, String> queryPairs = new HashMap<>(16);
- String[] pairs = queryString.split("&");
- for (String pair : pairs) {
- int idx = pair.indexOf("=");
- String key = URLDecoder.decode(pair.substring(0, idx), "UTF-8");
- String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8") : "";
- queryPairs.put(key, value);
- }
- return queryPairs;
- }
- private static final String FUN_ASR_MODE_ONLINE = "2pass-online";
- private static final String FUN_ASR_MODE_OFFLINE = "2pass-offline";
- public static FunAsrResultEntity parseFunAsrResponse(String msg){
- FunAsrResultEntity resultEntity = new FunAsrResultEntity();
- JSONObject jsonObject = JSONObject.parseObject(msg);
- boolean isFinal = jsonObject.getBoolean("is_final");
- resultEntity.setFinal_flag(isFinal);
- String mode = jsonObject.getString("mode");
- String text = jsonObject.getString("text");
- String vadType = "";
- if(!StringUtils.isEmpty(text)){
- if(mode.equals(FUN_ASR_MODE_ONLINE)) {
- vadType = "middle";
- }else if(mode.equals(FUN_ASR_MODE_OFFLINE)) {
- vadType = "vad";
- }
- resultEntity.setVad_type(vadType);
- resultEntity.setText(text);
- return resultEntity;
- }
- return null;
- }
- public static String joinTtsFiles(String traceId, String ttsFiles, boolean with_prefix, boolean checkExists){
- StringBuilder ttsFileUnion = new StringBuilder();
- if(!StringUtils.isEmpty(ttsFiles)){
- if(ttsFiles.contains(";")) {
- String[] fileArrs = ttsFiles.split(";");
- if(with_prefix) {
- ttsFileUnion.append("file_string://");
- }
- for (int i = 0; i <= fileArrs.length - 1; i++) {
- if (checkExists && !new File(fileArrs[i]).exists()) {
- logger.error("{} 录音文件不存在,跳过放音: {} ", traceId, fileArrs[i]);
- }else{
- if(i != fileArrs.length - 1){
- ttsFileUnion.append(fileArrs[i]).append("!");
- }else{
- ttsFileUnion.append(fileArrs[i]);
- }
- }
- }
- }else{
- // 只有一个wav文件;
- ttsFileUnion.append(ttsFiles);
- }
- }
- return ttsFileUnion.toString();
- }
- public static String getStackTraceString(StackTraceElement[] stackTraceElements){
- StringBuilder stringBuilder = new StringBuilder();
- for (int i = 0; i < stackTraceElements.length; i++) {
- stringBuilder.append("ClassName:");
- stringBuilder.append(stackTraceElements[i].getClassName());
- stringBuilder.append("\n FileName:");
- stringBuilder.append(stackTraceElements[i].getFileName());
- stringBuilder.append("\n LineNumber:");
- stringBuilder.append(stackTraceElements[i].getLineNumber());
- stringBuilder.append("\n MethodName:");
- stringBuilder.append(stackTraceElements[i].getMethodName());
- }
- return stringBuilder.toString();
- }
- /**
- * 已知的拨号错误
- */
- public static String[] KNOWN_DIAL_FAIL_CASE_TABLES = new String[]{
- "DESTINATION_OUT_OF_ORDER",
- "NO_ANSWER",
- "USER_BUSY",
- "NO_USER_RESPONSE",
- "RECOVERY_ON_TIMER_EXPIRE",
- "INCOMPATIBLE_DESTINATION"
- };
- /**
- * 检测拨号错误是否命中已知的错误类型
- * @param callResponseStr
- * @return 命中返回true,否则false
- */
- public static boolean checkTransferFailCase(String callResponseStr){
- boolean hitCase = false;
- for (String caseStr : KNOWN_DIAL_FAIL_CASE_TABLES) {
- if(callResponseStr.contains(caseStr)){
- hitCase = true;
- break;
- }
- }
- return hitCase;
- }
- /**
- * 解析外呼时的分机错误;
- * @param callResponseStr
- * @param extnum
- * @return
- */
- public static MessageResponse sendExtensionErrorInfo(String callResponseStr, String extnum) {
- MessageResponse response = null;
- if (callResponseStr.contains("USER_BUSY")) {
- response = (new MessageResponse(RespStatus.CALLER_BUSY, "呼叫失败,分机忙,请先挂断上一通电话。"));
- } else if (callResponseStr.contains("USER_NOT_REGISTERED")) {
- response = (new MessageResponse(RespStatus.CALLER_NOT_LOGIN, "分机没有登录,请打开软电话,确保软电话号码是" + extnum));
- } else if (callResponseStr.contains("SUBSCRIBER_ABSENT")) {
- response = (new MessageResponse(RespStatus.CALLER_NOT_LOGIN, "分机没有登录,请打开软电话,确保软电话号码是" + extnum));
- } else if (callResponseStr.contains("NO_USER_RESPONSE")) {
- response = (new MessageResponse(RespStatus.CALLER_RESPOND_TIMEOUT, "分机无响应,请重新打开电话或者刷新页面后重试。"));
- } else if (callResponseStr.contains("RECOVERY_ON_TIMER_EXPIRE")) {
- response = (new MessageResponse(RespStatus.CALLER_RESPOND_TIMEOUT, "操作超时,请检查分机是否已经登录,稍后重试。"));
- } else if (callResponseStr.contains("NO_ANSWER")) {
- response = (new MessageResponse(RespStatus.CALLER_RESPOND_TIMEOUT, "外呼超时"));
- } else if (callResponseStr.contains("INCOMPATIBLE_DESTINATION")) {
- response = (new MessageResponse(RespStatus.SERVER_ERROR_AUDIO_CODEC_NOT_MATCH, "外呼失败,可能是语音编码不匹配。 INCOMPATIBLE_DESTINATION"));
- } else if (callResponseStr.trim().length() != 0) {
- response = (new MessageResponse(RespStatus.SERVER_ERROR, "操作失败,请稍后重试,详情:" + callResponseStr));
- }
- return response;
- }
- public static Map<String,String> validateToken(String token, String traceId){
- try {
- //创建验证对象,这里使用的加密算法和密钥必须与生成TOKEN时的相同否则无法验证
- JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(
- SystemConfig.getValue("ws-server-auth-token-secret").trim()
- )).build();
- //验证JWT
- DecodedJWT decodedJwt = jwtVerifier.verify(token);
- Map<String,String> map = new HashMap<>(10);
- map.put("extnum", decodedJwt.getClaim("extnum").asString());
- map.put("opnum", decodedJwt.getClaim("opnum").asString());
- map.put("groupId", decodedJwt.getClaim("groupId").asString());
- map.put("skillLevel", decodedJwt.getClaim("skillLevel").asString());
- map.put("calleePrefix", decodedJwt.getClaim("calleePrefix").asString());
- map.put("callerNumber", decodedJwt.getClaim("callerNumber").asString());
- map.put("gatewayAddress", decodedJwt.getClaim("gatewayAddress").asString());
- map.put("projectId", decodedJwt.getClaim("projectId").asString());
- map.put("sipProfile", decodedJwt.getClaim("sipProfile").asString());
- return map;
- //获取JWT中的数据,注意数据类型一定要与添加进去的数据类型一致,否则取不到数据
- // System.out.println(decodedJwt.getExpiresAt());
- } catch (Throwable err) {
- logger.warn("{} token 校验失败: {}", traceId, err.toString());
- return null;
- }
- }
- /**
- * String Scramble Tool Class
- * @param input
- * @return
- */
- public static String shuffleString(String input) {
- // 将字符串转换为字符数组以便随机交换
- char[] chars = input.toCharArray();
- Random random = new Random();
- // Fisher-Yates洗牌算法
- for (int i = chars.length - 1; i > 0; i--) {
- int j = random.nextInt(i + 1); // 生成0到i之间的随机索引
- // 交换字符
- char temp = chars[i];
- chars[i] = chars[j];
- chars[j] = temp;
- }
- return new String(chars);
- }
- /**
- * 禁止jsp页面被客户端浏览器缓存
- * @param response
- */
- public static void setPageNoCache(HttpServletResponse response){
- response.setHeader("Pragma","no-cache");
- response.setHeader("Cache-Control","no-cache");
- response.setDateHeader("Expires", 0);
- }
- public static String hiddenPhoneNumber(String phone) {
- if (phone == null || phone.length() == 0) {
- return "";
- }
- if (phone.length() < 10) {
- return phone;
- }
- return phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
- }
- /**
- * 设置页面缓存为指定的分钟数后过期
- * @param response
- * @param minutes
- */
- public static void setPageCacheByTime(HttpServletResponse response, int minutes){
- Date d = new Date();
- String modDate = d.toGMTString();
- String expireDate = (new Date(d.getTime() + minutes * 60 * 1000)).toGMTString();
- response.setHeader("Last-Modified", modDate);
- response.setHeader("Expires", expireDate);
- response.setHeader("Cache-Control", "public");
- }
-
-
- /**
- * 过滤数组中重复的元素
- *
- * @param array 输入需要去重的数组
- * @return 返回去重后数组
- */
- public List<T> uniqueArray(T[] array) {
- if (array.length == 0) {
- return null;
- }
- List<T> list = new ArrayList<T>();;
- int length = array.length;
- for (int i = 0, len = length; i < len; i++) {
- if (!list.contains(array[i])) {
- list.add(array[i]);
- }
- }
- return list;
- }
-
- public static void main(String[] args) {
- }
- public static Boolean createZipFile(String sourceFile, String zipFilePath) {
- try{
- File file = new File(zipFilePath);
- if(!file.exists())
- file.createNewFile();
- String string= FileUtils.ReadFile(sourceFile, "utf-8");
- byte[] buffer =string.getBytes();
- FileOutputStream fOutputStream = new FileOutputStream(file);
- ZipOutputStream zoutput = new ZipOutputStream(fOutputStream);
- ZipEntry zEntry = new ZipEntry(new File(sourceFile).getName());
- zoutput.putNextEntry(zEntry);
- zoutput.write(buffer);
- zoutput.closeEntry();
- zoutput.close();
- return true;
- }
- catch(Throwable e){
- return false;
- }
- }
- /**
- * 格式化double类型为2位小数
- *
- * @return
- */
- public static Double formatDoubleWithTwo(Double f) {
- BigDecimal bg = new BigDecimal(f);
- return bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
- }
- /**把http请求参数转换为Map
- *
- * @param request
- * @return
- */
- public static Map<String, String> getHttpRequestHeaders(HttpServletRequest request) {
- Map<String, String> map = new HashMap<String, String>();
- @SuppressWarnings("rawtypes")
- Enumeration headerNames = request.getHeaderNames();
- while (headerNames.hasMoreElements()) {
- String key = (String) headerNames.nextElement();
- String value = request.getHeader(key);
- map.put(key, value);
- }
- return map;
- }
- public static Map<String, String> processRequestParameter(String data) {
- Map<String, String> params = new HashMap<String, String>();
- String[] keyValues;
- if (data.indexOf("&") != -1) {
- keyValues = data.split("&");
- } else {
- keyValues = new String[] { data };
- }
- for (String item : keyValues) {
- if (item.indexOf("=") != -1) {
- String[] tmp = item.split("=");
- if (tmp.length != 0) {
- if (tmp.length == 1) {
- params.put(tmp[0], "");
- }
- if (tmp.length == 2) {
- params.put(tmp[0], tmp[1]);
- }
- }
- }
- }
- return params;
- }
- /**
- * Integer转为long
- *
- * @author: easycallcenter365@126.com
- * @param data
- * @return
- * @date: 2016年12月17日 上午10:17:26
- */
- public static Long integer2long(Object data) {
- try {
- return ((Integer) (data == null ? new Integer(0) : data)).longValue();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * BigInteger转为long
- *
- * @author: easycallcenter365@126.com
- * @param data
- * @return
- * @date: 2016年12月16日 上午10:50:31
- */
- public static Long biginteger2long(Object data) {
- try {
- return ((BigInteger) (data == null ? new BigInteger("0") : data)).longValue();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * BigInteger转为int
- *
- * @author: easycallcenter365@126.com
- * @param data
- * @return
- * @date: 2016年12月19日 下午1:41:19
- */
- public static Integer biginteger2int(Object data) {
- try {
- return ((BigInteger) (data == null ? new BigInteger("0") : data)).intValue();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * BigDecimal转为Double
- *
- * @author: easycallcenter365@126.com
- * @param data
- * @return
- * @date: 2016年12月16日 上午10:31:47
- */
- public static Double bigdecimal2double(Object data) {
- try {
- return ((BigDecimal) (data == null ? BigDecimal.valueOf(0.0) : data)).doubleValue();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * BigDecimal转为long
- *
- * @author: easycallcenter365@126.com
- * @param data
- * @return
- * @date: 2016年12月17日 上午10:25:27
- */
- public static Long bigdecimal2long(Object data) {
- try {
- return ((BigDecimal) (data == null ? BigDecimal.valueOf(0.0) : data)).longValue();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * BigDecimal转为int
- *
- * @author: easycallcenter365@126.com
- * @param data
- * @return
- * @date: 2016年12月19日 下午1:44:58
- */
- public static Integer bigdecimal2int(Object data) {
- try {
- return ((BigDecimal) (data == null ? BigDecimal.valueOf(0.0) : data)).intValue();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * String转为Long
- *
- * @author: easycallcenter365@126.com
- * @param data
- * @return
- * @date: 2016年12月29日 下午4:11:58
- */
- public static Long string2long(String data) {
- try {
- return (data == null || "".equals(data)) ? null : Long.parseLong(data);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * String转为Double
- *
- * @author: easycallcenter365@126.com
- * @param data
- * @return
- * @date: 2016年12月29日 下午4:11:58
- */
- public static Double string2double(String data) {
- try {
- return (data == null || "".equals(data)) ? 0.0 : Double.parseDouble(data);
- } catch (Exception e) {
- e.printStackTrace();
- return 0.0;
- }
- }
- public static boolean string2boolean(String data) {
- try {
- return (data == null || "".equals(data)) ? false : Boolean.valueOf(data);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 转为Date
- *
- * @author: easycallcenter365@126.com
- * @param data
- * @return
- * @date: 2017年2月4日 下午1:34:53
- */
- public static Date obj2date(Object data) {
- try {
- return (data == null || "".equals(data)) ? null : (Date) (data);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 转为int
- *
- * @author: easycallcenter365@126.com
- * @param data
- * @return
- * @date: 2017年2月8日 下午4:24:46
- */
- public static Integer obj2int(Object data, int defalutInt) {
- try {
- return (data == null || "".equals(data)) ? defalutInt : (Integer) (data);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 转为double
- *
- * @author: easycallcenter365@126.com
- * @param data
- * @return
- * @date: 2017年2月8日 下午4:24:46
- */
- public static Double obj2double(Object data, double defalutDouble) {
- try {
- return (data == null || "".equals(data)) ? defalutDouble : (Double) (data);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 转为string
- *
- * @author: easycallcenter365@126.com
- * @param data
- * @param defalutStr
- * @return
- * @date: 2017年2月8日 下午4:30:19
- */
- public static String obj2string(Object data, String defalutStr) {
- try {
- return (data == null || "".equals(data)) ? defalutStr : (String) (data);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 转为bigdecimal
- *
- * @author: easycallcenter365@126.com
- * @param value
- * @return
- * @date: 2017年2月26日 下午3:05:13
- */
- public static BigDecimal obj2bigdecimal(Object value) {
- BigDecimal ret = null;
- if (value != null) {
- if (value instanceof BigDecimal) {
- ret = (BigDecimal) value;
- } else if (value instanceof String) {
- ret = new BigDecimal((String) value);
- } else if (value instanceof BigInteger) {
- ret = new BigDecimal((BigInteger) value);
- } else if (value instanceof Number) {
- ret = BigDecimal.valueOf(((Number) value).doubleValue());
- } else {
- throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass()
- + " into a BigDecimal.");
- }
- }
- return ret;
- }
- /**
- * 获取挂断类型
- *
- * @author: easycallcenter365@126.com
- * @param typeId
- * @return
- * @date: 2016年12月4日 下午3:11:10
- */
- public static String getHangupType(Integer typeId) {
- if (null == typeId || typeId < 0) {
- return "";
- } else if (typeId == 0) {
- return "振铃挂断";
- } else if (typeId == 1) {
- return "坐席挂断";
- } else if (typeId == 2) {
- return "客户挂断";
- } else {
- return "";
- }
- }
- /**
- * 将秒转为时分秒
- *
- * @author: easycallcenter365@126.com
- * @param t
- * 单位秒
- * @return hh:mm:ss
- * @date: 2017年1月24日 上午9:42:46
- */
- public static String formatTime(Long t) {
- String str = "";
- if (null == t || t <= 0) {
- return "0:0:0";
- }
- str = t / 3600 + ":" + (t % 3600) / 60 + ":" + t % 60;
- return str;
- }
- /**
- * 将秒转为时分秒
- *
- * @author: easycallcenter365@126.com
- * @param t
- * 单位秒
- * @return hh:mm:ss
- * @date: 2017年1月24日 上午9:42:46
- */
- public static String formatTime2(Long t) {
- String str = "";
- if (null == t || t <= 0) {
- return "0:0:0";
- }
- str += t / 3600 + ":";
- if ((t % 3600) / 60 < 10) {
- str += "0" + (t % 3600) / 60 + ":";
- } else {
- str += (t % 3600) / 60 + ":";
- }
- if (t % 60 < 10) {
- str += "0" + t % 60;
- } else {
- str += t % 60;
- }
- return str;
- }
- /**
- * 电话号码隐藏中间部分
- *
- * @author: easycallcenter365@126.com
- * @param num
- * @return
- * @date: 2017年3月16日 下午2:43:53
- */
- public static String formatPhoneNum(String num) {
- int sub_start = 3;
- int sub_end = num.length() - 4;
- String hide_str = "";
- for (int i = 0; i < (sub_end - sub_start); i++) {
- hide_str += "*";
- }
- if (sub_start > sub_end) {
- return num;
- } else {
- return num.substring(0, sub_start) + hide_str + num.substring(sub_end, num.length());
- }
- }
- /**
- * 比较2个list是否相同(逗号隔开)
- *
- * @author: easycallcenter365@126.com
- * @time 下午2:35:10
- * @param list1
- * @param list2
- * @return
- */
- public static boolean equalsList(String list1, String list2) {
- if (null == list1 || "".equals(list1)) {
- return false;
- }
- if (null == list2 || "".equals(list2)) {
- return false;
- }
- if (list1.split(",").length != list2.split(",").length) {
- return false;
- }
- for (String obj : list1.split(",")) {
- if (list2.indexOf(obj) < 0) {
- return false;
- }
- }
- return true;
- }
- /**
- * 从HttpServletRequest参数中获取用户请求参数值
- *
- * @param request
- * @param key
- * @return
- */
- public static int getRequestParameterInt(HttpServletRequest request, String key) {
- String idValue = request.getParameter(key);
- if (idValue != null && idValue.trim() != "") {
- return Integer.valueOf(idValue);
- }
- return 0;
- }
- /**
- * 从HttpServletRequest参数中获取用户请求参数值
- *
- * @param request
- * @param key
- * @return
- */
- public static String getRequestParameterStr(HttpServletRequest request, String key) {
- String idValue = request.getParameter(key);
- if (idValue != null && idValue.trim() != "") {
- return String.valueOf(idValue);
- }
- return "";
- }
- /**
- * 格式化idList为List<Integer> 类型的对象;
- *
- * @param idListStr
- * 例如"521,352,523"
- * @return
- */
- public static List<Integer> parseIdList(String idListStr) {
- if (!StringUtils.isNotBlank(idListStr))
- return null;
- List<Integer> list = new ArrayList<Integer>(10);
- String[] array = idListStr.split(",");
- for (String ele : array) {
- list.add(Integer.valueOf(ele));
- }
- return list;
- }
- /**
- * uuid的正则表达式
- */
- public static final String uuidRegExpPattern = "[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}";
- public static String ListToString(List<?> objectList) {
- if(objectList.size() == 0) {
- return "";
- }
- return ListToString(objectList, true);
- }
- /**
- * ListToString,是否使用逗号分隔符
- ***/
- public static String ListToString(List<?> objectList, boolean useSpe) {
- StringBuilder sb = new StringBuilder("");
- for (Object ele : objectList) {
- sb.append(ele);
- if (useSpe) {
- sb.append(",");
- }
- }
- String result = sb.toString();
- if (useSpe) {
- result = result.substring(0, result.length() - 1);
- }
- return result;
- }
- /* 把计费周期转换为对应的秒数
- * @param period
- * @return
- */
- public static Long calcRentTime(Integer rentType){
- Long secs = 0L;
- switch(String.valueOf(rentType)){
- case "0":
- secs = 24L * 3600L; //天
- break;
- case "1":
- secs = 30L* 24L * 3600L; //月
- break;
- case "2":
- secs = 360L * 30L* 24L * 3600L; //年
- break;
-
- }
- return secs;
- }
- }
|