package com.yeshi.fanli.util;
|
|
import java.io.IOException;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Calendar;
|
import java.util.Collection;
|
import java.util.Collections;
|
import java.util.Comparator;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Map.Entry;
|
import java.util.TreeMap;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.yeshi.utils.taobao.TbImgUtil;
|
|
import com.yeshi.fanli.entity.admin.GoodsClassAdmin;
|
import com.yeshi.fanli.entity.admin.HotSearchAdmin;
|
import com.yeshi.fanli.entity.admin.RecommendBannerAdmin;
|
import com.yeshi.fanli.entity.admin.RecommendSpecialAdmin;
|
import com.yeshi.fanli.entity.common.AdminUser;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
public class Utils {
|
|
private static final String ANDROID = "ANDROID";
|
private static final String IOS = "IOS";
|
private static final String WEB = "WEB";
|
|
private static final Map<String, String> map = new HashMap<String, String>();
|
|
static {
|
map.put("1", ANDROID);
|
map.put("2", IOS);
|
map.put("3", WEB);
|
}
|
|
public static Map<String, String> getMap() {
|
return map;
|
}
|
|
public static boolean isEmailUrlRight(String email, String sign, long time) {
|
if (StringUtil.Md5(email + time + "WEIJU2016xxx").equalsIgnoreCase(sign)
|
&& Math.abs(System.currentTimeMillis() - time) < 1000 * 60 * 60 * 2) {
|
return true;
|
}
|
return false;
|
}
|
|
public static String getUrlPageTitle(String url) {
|
try {
|
Document doc = Jsoup.connect(url).timeout(1000 * 60).get();
|
return doc.title();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
|
public static AdminUser getAdminUser(HttpServletRequest request) {
|
AdminUser adminUser = (AdminUser) request.getSession().getAttribute("ADMIN_USERINFO");
|
return adminUser;
|
}
|
|
public static boolean signIsRight(String devicename, String imei, String sign) {
|
return sign.equalsIgnoreCase(StringUtil.Md5(devicename + imei + "WEIju2016888xx3"));
|
}
|
|
public static Map<String, Object> sort(Map<String, Integer> map) {
|
// 通过ArrayList构�?函数把map.entrySet()转换成list
|
List<Map.Entry<String, Object>> list = new ArrayList<Map.Entry<String, Object>>((Collection) map.entrySet());
|
// 通过比较器实现比较排�?
|
Collections.sort(list, new Comparator<Map.Entry<String, Object>>() {
|
public int compare(Map.Entry<String, Object> mapping1, Map.Entry<String, Object> mapping2) {
|
return mapping1.getKey().compareTo(mapping2.getKey());
|
}
|
});
|
|
Map<String, Object> newMap = new TreeMap<String, Object>();
|
for (Entry<String, Object> entry : list) {
|
newMap.put(entry.getKey(), entry.getValue());
|
}
|
return newMap;
|
}
|
|
|
@SuppressWarnings("unchecked")
|
public static Map orderBy(Map map) {
|
if (map.size() == 0) {
|
return map;
|
}
|
|
Collection values = map.values();
|
Object obj = null;
|
for (Object object : values) {
|
obj = object;
|
break;
|
}
|
if (obj instanceof RecommendBannerAdmin) {
|
|
// 通过ArrayList构�?函数把map.entrySet()转换成list
|
List<Map.Entry<Long, RecommendBannerAdmin>> list = new ArrayList<Map.Entry<Long, RecommendBannerAdmin>>(
|
(Collection) map.entrySet());
|
// 通过比较器实现比较排�?
|
Collections.sort(list, new Comparator<Map.Entry<Long, RecommendBannerAdmin>>() {
|
public int compare(Map.Entry<Long, RecommendBannerAdmin> mapping1,
|
Map.Entry<Long, RecommendBannerAdmin> mapping2) {
|
int orderby1 = mapping1.getValue().getRecommendBanner().getOrderby();
|
int orderby2 = mapping2.getValue().getRecommendBanner().getOrderby();
|
return orderby1 - orderby2;
|
}
|
});
|
LinkedHashMap<Long, Object> newMap = new LinkedHashMap<Long, Object>();
|
for (Entry<Long, RecommendBannerAdmin> entry : list) {
|
newMap.put(entry.getKey(), entry.getValue());
|
}
|
return newMap;
|
} else if (obj instanceof RecommendSpecialAdmin) {
|
List<Map.Entry<Long, RecommendSpecialAdmin>> list = new ArrayList<Map.Entry<Long, RecommendSpecialAdmin>>(
|
(Collection) map.entrySet());
|
Collections.sort(list, new Comparator<Map.Entry<Long, RecommendSpecialAdmin>>() {
|
public int compare(Map.Entry<Long, RecommendSpecialAdmin> mapping1,
|
Map.Entry<Long, RecommendSpecialAdmin> mapping2) {
|
int orderby1 = mapping1.getValue().getRecommendSpecial().getOrderby();
|
int orderby2 = mapping2.getValue().getRecommendSpecial().getOrderby();
|
return orderby1 - orderby2;
|
}
|
});
|
LinkedHashMap<Long, Object> newMap = new LinkedHashMap<Long, Object>();
|
for (Entry<Long, RecommendSpecialAdmin> entry : list) {
|
newMap.put(entry.getKey(), entry.getValue());
|
}
|
return newMap;
|
} else if (obj instanceof HotSearchAdmin) {
|
List<Map.Entry<Long, HotSearchAdmin>> list = new ArrayList<Map.Entry<Long, HotSearchAdmin>>(
|
(Collection) map.entrySet());
|
Collections.sort(list, new Comparator<Map.Entry<Long, HotSearchAdmin>>() {
|
public int compare(Map.Entry<Long, HotSearchAdmin> mapping1, Map.Entry<Long, HotSearchAdmin> mapping2) {
|
int orderby1 = mapping1.getValue().getHotSearch().getOrderby();
|
int orderby2 = mapping2.getValue().getHotSearch().getOrderby();
|
return orderby1 - orderby2;
|
}
|
});
|
LinkedHashMap<Long, Object> newMap = new LinkedHashMap<Long, Object>();
|
for (Entry<Long, HotSearchAdmin> entry : list) {
|
newMap.put(entry.getKey(), entry.getValue());
|
}
|
return newMap;
|
} else if (obj instanceof GoodsClassAdmin) {
|
List<Map.Entry<Long, GoodsClassAdmin>> list = new ArrayList<Map.Entry<Long, GoodsClassAdmin>>(
|
(Collection) map.entrySet());
|
Collections.sort(list, new Comparator<Map.Entry<Long, GoodsClassAdmin>>() {
|
public int compare(Map.Entry<Long, GoodsClassAdmin> mapping1,
|
Map.Entry<Long, GoodsClassAdmin> mapping2) {
|
int orderby1 = mapping1.getValue().getGoodsClass().getOrderby();
|
int orderby2 = mapping2.getValue().getGoodsClass().getOrderby();
|
return orderby1 - orderby2;
|
}
|
});
|
LinkedHashMap<Long, Object> newMap = new LinkedHashMap<Long, Object>();
|
for (Entry<Long, GoodsClassAdmin> entry : list) {
|
newMap.put(entry.getKey(), entry.getValue());
|
}
|
return newMap;
|
}
|
return map;
|
}
|
|
public static List<String> getDateMonthList(Date beginDate, Date endDate) {
|
Calendar endCalendar = Calendar.getInstance();
|
endCalendar.setTime(endDate);
|
|
Calendar beginCalendar = Calendar.getInstance();
|
beginCalendar.setTime(beginDate);
|
|
int month = beginCalendar.get(Calendar.MONTH) - endCalendar.get(Calendar.MONTH);
|
List<String> list = new ArrayList<String>();
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
|
for (int ii = month; ii < 0; ii++) {
|
Calendar ca = Calendar.getInstance();
|
ca.set(Calendar.DAY_OF_MONTH, 1);
|
ca.set(Calendar.MONTH, (ca.get(Calendar.MONTH)) + ii + 1);
|
String format = dateFormat.format(ca.getTime());
|
list.add(format);
|
}
|
return list;
|
}
|
|
public static List<String> getDateMonthList(int month, Date date) {
|
List<String> list = new ArrayList<String>();
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
|
Calendar ca;
|
for (int ii = -month; ii < 0; ii++) {
|
ca = Calendar.getInstance();
|
ca.setTime(date);
|
ca.set(Calendar.DAY_OF_MONTH, 1);
|
ca.set(Calendar.MONTH, (ca.get(Calendar.MONTH)) + ii + 1);
|
String format = dateFormat.format(ca.getTime());
|
list.add(format);
|
}
|
return list;
|
}
|
|
public static int countMonths(String date1, String date2) throws ParseException {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
Calendar c1 = Calendar.getInstance();
|
Calendar c2 = Calendar.getInstance();
|
|
c1.setTime(sdf.parse(date1));
|
c2.setTime(sdf.parse(date2));
|
|
int year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
|
|
// �?��日期若小月结束日�?
|
if (year < 0) {
|
year = -year;
|
return year * 12 + c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
|
}
|
|
return year * 12 + c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
|
}
|
|
public static int countDay(String date1, String date2) throws ParseException {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
Calendar c1 = Calendar.getInstance();
|
Calendar c2 = Calendar.getInstance();
|
|
c1.setTime(sdf.parse(date1));
|
c2.setTime(sdf.parse(date2));
|
int day = ((Long) ((c2.getTimeInMillis() - c1.getTimeInMillis()) / 86400000)).intValue();
|
return day;
|
}
|
|
public static String imgSize(String jsonStr) {
|
JSONObject json = JSONObject.fromObject(jsonStr);
|
String picUrl = (String) json.opt("pictUrl");
|
if (picUrl.contains("alicdn") || picUrl.contains("tbcdn")) {
|
picUrl = TbImgUtil.getTBSize320Img(picUrl);
|
}
|
json.element("pictUrl", picUrl);
|
return json.toString();
|
}
|
|
public static String imgListSize(String jsonStr) {
|
JSONArray json = JSONArray.fromObject(jsonStr);
|
for (Object object : json) {
|
String picUrl = (String) ((JSONObject) object).opt("pictUrl");
|
if (picUrl.contains("alicdn") || picUrl.contains("tbcdn")) {
|
picUrl = TbImgUtil.getTBSize320Img(picUrl);
|
}
|
((JSONObject) object).element("pictUrl", picUrl);
|
}
|
return json.toString();
|
}
|
|
public static Map<String, String> parseURL(String url) {
|
|
String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
|
Pattern p = Pattern.compile(URL_REGEX);
|
Matcher matcher = p.matcher(url);
|
boolean b = matcher.matches();
|
Map<String, String> map = new HashMap<String, String>();
|
if (!b) {
|
return map;
|
}
|
if (!url.contains("?")) {
|
return map;
|
}
|
|
String params = url.substring(url.indexOf("?") + 1);
|
if (params == null || "".equals(params)) {
|
return map;
|
}
|
|
String[] paramArr = params.split("&");
|
|
for (String arr : paramArr) {
|
String[] kv = arr.split("=");
|
if (kv.length == 1) {
|
kv[1] = "";
|
}
|
map.put(kv[0], kv[1]);
|
}
|
|
return map;
|
}
|
|
public static double random(double mix) {
|
return (Math.random() * 20) + mix;
|
}
|
|
public static String getStarString(String content, int begin, int end) {
|
|
if (begin >= content.length() || begin < 0) {
|
return content;
|
}
|
if (end >= content.length() || end < 0) {
|
return content;
|
}
|
if (begin >= end) {
|
return content;
|
}
|
String starStr = "";
|
for (int i = begin; i < end; i++) {
|
starStr = starStr + "*";
|
}
|
return content.substring(0, begin) + starStr + content.substring(end, content.length());
|
|
}
|
|
public static boolean isNum(String content) {
|
if (content == null) {
|
return false;
|
}
|
String regex = "\\d+";
|
Pattern compile = Pattern.compile(regex);
|
Matcher matcher = compile.matcher(content);
|
return matcher.matches();
|
}
|
|
public static boolean isOrderNum(String order) {
|
if (order == null) {
|
return false;
|
}
|
String regex = "\\d{16,}";
|
Pattern compile = Pattern.compile(regex);
|
Matcher matcher = compile.matcher(order);
|
return matcher.matches();
|
}
|
|
public static boolean isUserOrder(String existOrder, String lostOrder) {
|
if (existOrder.length() < 16 || lostOrder.length() < 16) {
|
return false;
|
}
|
String existNum = existOrder.substring(existOrder.length() - 6);
|
String lostOrderNum = lostOrder.substring(lostOrder.length() - 6);
|
|
if (existNum.equals(lostOrderNum)) {
|
return true;
|
}
|
|
return false;
|
}
|
|
}
|