|
@@ -4,6 +4,7 @@ package com.gree.mall.miniapp.utils;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.gree.mall.miniapp.constant.Constant;
|
|
|
import com.gree.mall.miniapp.exception.RemoteServiceException;
|
|
|
+import com.gree.mall.miniapp.helper.ResponseHelper;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.jdom.Document;
|
|
|
import org.jdom.Element;
|
|
@@ -25,77 +26,77 @@ import java.util.Map;
|
|
|
public class CommonUtils {
|
|
|
|
|
|
|
|
|
- public static String getAPPID(HttpServletRequest request){
|
|
|
+ public static String getAPPID(HttpServletRequest request) {
|
|
|
String header = request.getHeader(Constant.APPID);
|
|
|
- if(StringUtils.isBlank(header)){
|
|
|
+ if (StringUtils.isBlank(header)) {
|
|
|
header = Constant.DEF_APPID;
|
|
|
}
|
|
|
- // header = Constant.DEF_APPID;
|
|
|
+ // header = Constant.DEF_APPID;
|
|
|
return header;
|
|
|
}
|
|
|
|
|
|
- public static List<String> initList(List<String> list,int size){
|
|
|
+ public static List<String> initList(List<String> list, int size) {
|
|
|
int listSize = list.size();
|
|
|
- if(listSize < size){
|
|
|
- for(int i = listSize;i < size;i++){
|
|
|
+ if (listSize < size) {
|
|
|
+ for (int i = listSize; i < size; i++) {
|
|
|
list.add("");
|
|
|
}
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
- public static String getStatusNameOfStudentExam(int status){
|
|
|
- if(status == 1){
|
|
|
+ public static String getStatusNameOfStudentExam(int status) {
|
|
|
+ if (status == 1) {
|
|
|
return "未完成";
|
|
|
- }else if(status == 2){
|
|
|
+ } else if (status == 2) {
|
|
|
return "已完成";
|
|
|
- }else if(status == 0){
|
|
|
+ } else if (status == 0) {
|
|
|
return "作废";
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
//(1.未考试 2.已及格,3.未及格)
|
|
|
- public static String getExamStatusNameOfStudentExam(int examStatus){
|
|
|
- if(examStatus == 1){
|
|
|
+ public static String getExamStatusNameOfStudentExam(int examStatus) {
|
|
|
+ if (examStatus == 1) {
|
|
|
return "未考试";
|
|
|
- }else if(examStatus == 2){
|
|
|
+ } else if (examStatus == 2) {
|
|
|
return "已及格";
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
return "未及格";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static String getSourceName(int source){
|
|
|
- if(source == 1){
|
|
|
+ public static String getSourceName(int source) {
|
|
|
+ if (source == 1) {
|
|
|
return "后台新增";
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
return "线上报名";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static Boolean checkNotEmpty(Object... str){
|
|
|
- for(Object s : str){
|
|
|
- if(s instanceof String) {
|
|
|
- String value = (String)s;
|
|
|
+ public static Boolean checkNotEmpty(Object... str) {
|
|
|
+ for (Object s : str) {
|
|
|
+ if (s instanceof String) {
|
|
|
+ String value = (String) s;
|
|
|
if (StringUtils.isBlank(value)) {
|
|
|
return false;
|
|
|
}
|
|
|
- }else if(s == null) {
|
|
|
+ } else if (s == null) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- public static Boolean checkDate(String dateStr,String dateFormat){
|
|
|
- if(StringUtils.isBlank(dateStr)){
|
|
|
+ public static Boolean checkDate(String dateStr, String dateFormat) {
|
|
|
+ if (StringUtils.isBlank(dateStr)) {
|
|
|
return true;
|
|
|
}
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
|
|
- try{
|
|
|
+ try {
|
|
|
sdf.parse(dateStr);
|
|
|
- }catch(Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
@@ -112,7 +113,7 @@ public class CommonUtils {
|
|
|
/**
|
|
|
* 从token获取userId
|
|
|
*/
|
|
|
- public static String getUserId(HttpServletRequest request){
|
|
|
+ public static String getUserId(HttpServletRequest request) {
|
|
|
try {
|
|
|
String token = getToken(request);
|
|
|
if (StringUtils.isBlank(token)) {
|
|
@@ -121,27 +122,27 @@ public class CommonUtils {
|
|
|
if (StringUtils.isBlank(token)) {
|
|
|
token = request.getParameter("x-token");
|
|
|
}
|
|
|
- if(StringUtils.isBlank(token)){
|
|
|
+ if (StringUtils.isBlank(token)) {
|
|
|
return null;
|
|
|
}
|
|
|
return JwtUtils.validateJWT(token).getClaims().getId();
|
|
|
- }catch(Exception e){
|
|
|
- return "";
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RemoteServiceException(ResponseHelper.ResponseCode_AUTH_ERROR, "token过期");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static String getNickName(){
|
|
|
+ public static String getNickName() {
|
|
|
HttpServletRequest request = RequestContextHolder.getRequestAttributes() != null ? ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest() : null;
|
|
|
return getNickName(request);
|
|
|
}
|
|
|
|
|
|
- public static String getNickName(HttpServletRequest request){
|
|
|
- try{
|
|
|
+ public static String getNickName(HttpServletRequest request) {
|
|
|
+ try {
|
|
|
String token = getToken(request);
|
|
|
- if(StringUtils.isBlank(token))
|
|
|
+ if (StringUtils.isBlank(token))
|
|
|
return null;
|
|
|
return JwtUtils.validateJWT(token).getClaims().getSubject();
|
|
|
- }catch(Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
return "";
|
|
|
}
|
|
|
}
|
|
@@ -149,7 +150,7 @@ public class CommonUtils {
|
|
|
/**
|
|
|
* 获取token
|
|
|
*/
|
|
|
- public static String getToken(HttpServletRequest request){
|
|
|
+ public static String getToken(HttpServletRequest request) {
|
|
|
String token = request.getHeader("x-token");
|
|
|
return token;
|
|
|
}
|
|
@@ -158,20 +159,21 @@ public class CommonUtils {
|
|
|
/**
|
|
|
* 获取来源
|
|
|
*/
|
|
|
- public static String getSource(HttpServletRequest request){
|
|
|
+ public static String getSource(HttpServletRequest request) {
|
|
|
String token = request.getHeader("source");
|
|
|
return token;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取性别code
|
|
|
+ *
|
|
|
* @param gender
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String getGenderCode(String gender){
|
|
|
- if(org.apache.commons.lang3.StringUtils.equals(gender,"男")){
|
|
|
+ public static String getGenderCode(String gender) {
|
|
|
+ if (org.apache.commons.lang3.StringUtils.equals(gender, "男")) {
|
|
|
return "1";
|
|
|
- }else if(org.apache.commons.lang3.StringUtils.equals(gender,"女")){
|
|
|
+ } else if (org.apache.commons.lang3.StringUtils.equals(gender, "女")) {
|
|
|
return "2";
|
|
|
}
|
|
|
//未知
|
|
@@ -179,7 +181,7 @@ public class CommonUtils {
|
|
|
}
|
|
|
|
|
|
public static void downloadFile(String fileName, HttpServletResponse response) throws IOException {
|
|
|
- InputStream inputStream = CommonUtils.class.getResourceAsStream("/mapper/" + fileName);
|
|
|
+ InputStream inputStream = CommonUtils.class.getResourceAsStream("/mapper/" + fileName);
|
|
|
response.setContentType("content-type:octet-stream");
|
|
|
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
|
OutputStream outputStream = response.getOutputStream();
|
|
@@ -195,13 +197,14 @@ public class CommonUtils {
|
|
|
|
|
|
/**
|
|
|
* 检查下标对应的值不能为空
|
|
|
+ *
|
|
|
* @param row
|
|
|
* @param index
|
|
|
* @return
|
|
|
*/
|
|
|
- public static Integer checkRowEmpty(List<String> row, int[] index){
|
|
|
- for(int i : index){
|
|
|
- if(StringUtils.isBlank(row.get(i))){
|
|
|
+ public static Integer checkRowEmpty(List<String> row, int[] index) {
|
|
|
+ for (int i : index) {
|
|
|
+ if (StringUtils.isBlank(row.get(i))) {
|
|
|
return i;
|
|
|
}
|
|
|
}
|
|
@@ -261,12 +264,13 @@ public class CommonUtils {
|
|
|
|
|
|
/**
|
|
|
* 根据URL地址获取文件
|
|
|
- * @param path URL网络地址
|
|
|
+ *
|
|
|
+ * @param path URL网络地址
|
|
|
* @param checkStr
|
|
|
* @param replacePrefix
|
|
|
* @return File
|
|
|
*/
|
|
|
- public static File getFileByHttpURL(String path, String address, String checkStr, String replacePrefix){
|
|
|
+ public static File getFileByHttpURL(String path, String address, String checkStr, String replacePrefix) {
|
|
|
String newUrl = path.split("[?]")[0];
|
|
|
String[] suffix = newUrl.split("/");
|
|
|
//得到最后一个分隔符后的名字
|
|
@@ -279,20 +283,20 @@ public class CommonUtils {
|
|
|
File file = null;
|
|
|
InputStream inputStream = null;
|
|
|
OutputStream outputStream = null;
|
|
|
- try{
|
|
|
- file = new File(address+"/"+fileName);//创建临时文件
|
|
|
+ try {
|
|
|
+ file = new File(address + "/" + fileName);//创建临时文件
|
|
|
URL urlFile = new URL(reqUrl);
|
|
|
inputStream = urlFile.openStream();
|
|
|
outputStream = new FileOutputStream(file);
|
|
|
|
|
|
int bytesRead = 0;
|
|
|
byte[] buffer = new byte[8192];
|
|
|
- while ((bytesRead=inputStream.read(buffer,0,8192))!=-1) {
|
|
|
+ while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
|
|
|
outputStream.write(buffer, 0, bytesRead);
|
|
|
}
|
|
|
- }catch (Exception e) {
|
|
|
+ } catch (Exception e) {
|
|
|
throw new RemoteServiceException(e.getMessage());
|
|
|
- }finally {
|
|
|
+ } finally {
|
|
|
try {
|
|
|
if (null != outputStream) {
|
|
|
outputStream.close();
|
|
@@ -311,30 +315,31 @@ public class CommonUtils {
|
|
|
|
|
|
/**
|
|
|
* 根据URL地址获取文件
|
|
|
+ *
|
|
|
* @param path URL网络地址
|
|
|
* @return File
|
|
|
*/
|
|
|
- public static File getFileByHttpURL(String path, String address, String fileName){
|
|
|
+ public static File getFileByHttpURL(String path, String address, String fileName) {
|
|
|
String newUrl = path.split("[?]")[0];
|
|
|
String[] suffix = newUrl.split("/");
|
|
|
//得到最后一个分隔符后的名字
|
|
|
File file = null;
|
|
|
InputStream inputStream = null;
|
|
|
OutputStream outputStream = null;
|
|
|
- try{
|
|
|
- file = new File(address+"/"+fileName);//创建临时文件
|
|
|
+ try {
|
|
|
+ file = new File(address + "/" + fileName);//创建临时文件
|
|
|
URL urlFile = new URL(path);
|
|
|
inputStream = urlFile.openStream();
|
|
|
outputStream = new FileOutputStream(file);
|
|
|
|
|
|
int bytesRead = 0;
|
|
|
byte[] buffer = new byte[8192];
|
|
|
- while ((bytesRead=inputStream.read(buffer,0,8192))!=-1) {
|
|
|
+ while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
|
|
|
outputStream.write(buffer, 0, bytesRead);
|
|
|
}
|
|
|
- }catch (Exception e) {
|
|
|
+ } catch (Exception e) {
|
|
|
throw new RemoteServiceException(e.getMessage());
|
|
|
- }finally {
|
|
|
+ } finally {
|
|
|
try {
|
|
|
if (null != outputStream) {
|
|
|
outputStream.close();
|
|
@@ -351,5 +356,4 @@ public class CommonUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
}
|