Quellcode durchsuchen

修复接口返回对象问题

zhuoning vor 4 Jahren
Ursprung
Commit
a187c59c57

+ 83 - 19
huimv-hy-production/huimv.hy.erp.datasource/src/main/java/com/huimv/production/datasource/controller/ErpDataController.java

@@ -1,8 +1,11 @@
 package com.huimv.production.datasource.controller;
 
 import com.alibaba.fastjson.JSONObject;
+import com.huimv.common.result.Result;
+import com.huimv.common.result.ResultCode;
 import com.huimv.production.datasource.service.IErpDataService;
 import com.huimv.production.datasource.utils.TokenUtil;
+import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.repository.query.Param;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -37,12 +40,16 @@ public class ErpDataController {
      * @Time        : 20:57
      */
     @RequestMapping("/getPsy")
-    public String getPsy(@RequestParam(value = "accessToken") String accessToken){
-        JSONObject tokenJo = tokenUtil.verifyToken(accessToken);
-        if(!tokenJo.getBoolean("success")){
-            return tokenJo.toJSONString();
+    public Result getPsy(@RequestParam(value = "accessToken") String accessToken){
+        if(StringUtils.isBlank(accessToken)){
+            return new Result(10001,"token不能为空.",false);
         }
-        return erpDataService.getPsy();
+        // 验证token
+        Result result = tokenUtil.verifyToken(accessToken);
+        if(!result.isSuccess()){
+            return result;
+        }
+        return new Result(ResultCode.SUCCESS,erpDataService.getPsy());
     }
 
     /**
@@ -56,8 +63,16 @@ public class ErpDataController {
      * @Time        : 21:14
      */
     @RequestMapping("/getReEstrusRate")
-    public String getReEstrusRate(@Param(value="startDate") String startDate,@Param(value="endDate") String endDate){
-        return erpDataService.getReEstrusRate(startDate,endDate);
+    public Result getReEstrusRate(@Param(value="startDate") String startDate,@Param(value="endDate") String endDate,@RequestParam(value = "accessToken") String accessToken){
+        if(StringUtils.isBlank(accessToken)){
+            return new Result(10001,"token不能为空.",false);
+        }
+        // 验证token
+        Result result = tokenUtil.verifyToken(accessToken);
+        if(!result.isSuccess()){
+            return result;
+        }
+        return new Result(ResultCode.SUCCESS,erpDataService.getReEstrusRate(startDate,endDate));
     }
 
     /**
@@ -71,8 +86,16 @@ public class ErpDataController {
      * @Time        : 11:02
      */
     @RequestMapping("/getParturitionRate")
-    public String getParturitionRate(@Param(value="startDate") String startDate,@Param(value="endDate") String endDate){
-        return erpDataService.getParturitionRate(startDate,endDate);
+    public Result getParturitionRate(@Param(value="startDate") String startDate,@Param(value="endDate") String endDate,@RequestParam(value = "accessToken",required = true) String accessToken){
+        if(StringUtils.isBlank(accessToken)){
+            return new Result(10001,"token不能为空.",false);
+        }
+        // 验证token
+        Result result = tokenUtil.verifyToken(accessToken);
+        if(!result.isSuccess()){
+            return result;
+        }
+        return new Result(ResultCode.SUCCESS,erpDataService.getParturitionRate(startDate,endDate));
     }
 
     /**
@@ -86,8 +109,16 @@ public class ErpDataController {
      * @Time        : 11:16
      */
     @RequestMapping("/getStorage")
-    public String getStorage(){
-        return erpDataService.getStorage();
+    public Result getStorage(@RequestParam(value = "accessToken") String accessToken){
+        if(StringUtils.isBlank(accessToken)){
+            return new Result(10001,"token不能为空.",false);
+        }
+        // 验证token
+        Result result = tokenUtil.verifyToken(accessToken);
+        if(!result.isSuccess()){
+            return result;
+        }
+        return new Result(ResultCode.SUCCESS,erpDataService.getStorage());
     }
 
     /**
@@ -101,8 +132,16 @@ public class ErpDataController {
      * @Time        : 11:18
      */
     @RequestMapping("/getPigletInfo")
-    public String getPigletInfo(@Param(value="startDate") String startDate,@Param(value="endDate") String endDate){
-        return erpDataService.getPigletInfo(startDate,endDate);
+    public Result getPigletInfo(@Param(value="startDate") String startDate,@Param(value="endDate") String endDate,@RequestParam(value = "accessToken") String accessToken){
+        if(StringUtils.isBlank(accessToken)){
+            return new Result(10001,"token不能为空.",false);
+        }
+        // 验证token
+        Result result = tokenUtil.verifyToken(accessToken);
+        if(!result.isSuccess()){
+            return result;
+        }
+        return new Result(ResultCode.SUCCESS,erpDataService.getPigletInfo(startDate,endDate));
     }
 
     /**
@@ -116,8 +155,17 @@ public class ErpDataController {
      * @Time        : 11:35
      */
     @RequestMapping("/getMateQuantity")
-    public String getMateQuantity(@Param(value="startDate") String startDate,@Param(value="endDate") String endDate){
-        return erpDataService.getMateQuantity(startDate,endDate);
+    public Result getMateQuantity(@Param(value="startDate") String startDate,@Param(value="endDate") String endDate,@RequestParam(value = "accessToken") String accessToken){
+        if(StringUtils.isBlank(accessToken)){
+            return new Result(10001,"token不能为空.",false);
+        }
+        // 验证token
+        Result result = tokenUtil.verifyToken(accessToken);
+        if(!result.isSuccess()){
+            return result;
+        }
+        return new Result(ResultCode.SUCCESS,erpDataService.getMateQuantity(startDate,endDate));
+
     }
 
     /**
@@ -131,8 +179,16 @@ public class ErpDataController {
      * @Time        : 11:38
      */
     @RequestMapping("/getConceptionRate")
-    public String getConceptionRate(@Param(value="startDate") String startDate,@Param(value="endDate") String endDate){
-        return erpDataService.getConceptionRate(startDate,endDate);
+    public Result getConceptionRate(@Param(value="startDate") String startDate,@Param(value="endDate") String endDate,@RequestParam(value = "accessToken") String accessToken){
+        if(StringUtils.isBlank(accessToken)){
+            return new Result(10001,"token不能为空.",false);
+        }
+        // 验证token
+        Result result = tokenUtil.verifyToken(accessToken);
+        if(!result.isSuccess()){
+            return result;
+        }
+        return new Result(ResultCode.SUCCESS,erpDataService.getConceptionRate(startDate,endDate));
     }
 
     /**
@@ -146,7 +202,15 @@ public class ErpDataController {
      * @Time        : 11:39
      */
     @RequestMapping("/getAllStorage")
-    public String getAllStorage(){
-        return erpDataService.getAllStorage();
+    public Result getAllStorage(@RequestParam(value = "accessToken",required = false) String accessToken){
+        if(StringUtils.isBlank(accessToken)){
+            return new Result(10001,"token不能为空.",false);
+        }
+        // 验证token
+        Result result = tokenUtil.verifyToken(accessToken);
+        if(!result.isSuccess()){
+            return result;
+        }
+        return new Result(ResultCode.SUCCESS,erpDataService.getAllStorage());
     }
 }

+ 1 - 1
huimv-hy-production/huimv.hy.erp.datasource/src/main/java/com/huimv/production/datasource/controller/TokenController.java

@@ -71,7 +71,7 @@ public class TokenController {
      * 
      * @Author      : ZhuoNing
      * @Date        : 2021/5/15       
-     * @Time        : 18:56
+     * @Time        : 19:06
      */
     @RequestMapping("/verify")
     public boolean getToken(String verify) {

+ 67 - 0
huimv-hy-production/huimv.hy.erp.datasource/src/main/java/com/huimv/production/datasource/utils/DateUtils.java

@@ -14,8 +14,12 @@ import org.joda.time.LocalDate;
 import org.joda.time.format.DateTimeFormat;
 import org.joda.time.format.DateTimeFormatter;
 
+import java.sql.Timestamp;
+import java.text.DateFormat;
 import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Date;
+import java.util.Locale;
 
 /**
  * 日期处理
@@ -163,4 +167,67 @@ public class DateUtils {
         DateTime dateTime = new DateTime(date);
         return dateTime.plusYears(years).toDate();
     }
+
+    /**
+     *method 将字符串类型的日期转换为一个timestamp(时间戳记java.sql.Timestamp)
+     dateString 需要转换为timestamp的字符串
+     dataTime timestamp
+     */
+    public final static java.sql.Timestamp string2Time(String dateString)
+            throws java.text.ParseException {
+        DateFormat dateFormat;
+        dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS", Locale.ENGLISH);//设定格式
+        //dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);
+        dateFormat.setLenient(false);
+        java.util.Date timeDate = dateFormat.parse(dateString);//util类型
+        java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());//Timestamp类型,timeDate.getTime()返回一个long型
+        return dateTime;
+    }
+    /**
+     *method 将字符串类型的日期转换为一个Date(java.sql.Date)
+     dateString 需要转换为Date的字符串
+     dataTime Date
+     */
+    public final static java.sql.Date string2Date(String dateString)
+            throws java.lang.Exception {
+        DateFormat dateFormat;
+        dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
+        dateFormat.setLenient(false);
+        java.util.Date timeDate = dateFormat.parse(dateString);//util类型
+        java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());//sql类型
+        return dateTime;
+    }
+
+    public static void main(String[] args){
+        Date da = new Date();
+        //注意:这个地方da.getTime()得到的是一个long型的值
+        System.out.println(da.getTime());
+
+        //由日期date转换为timestamp
+
+        //第一种方法:使用new Timestamp(long)
+        Timestamp t = new Timestamp(new Date().getTime());
+        System.out.println(t);
+
+        //第二种方法:使用Timestamp(int year,int month,int date,int hour,int minute,int second,int nano)
+        Timestamp tt = new Timestamp(Calendar.getInstance().get(
+                Calendar.YEAR) - 1900, Calendar.getInstance().get(
+                Calendar.MONTH), Calendar.getInstance().get(
+                Calendar.DATE), Calendar.getInstance().get(
+                Calendar.HOUR), Calendar.getInstance().get(
+                Calendar.MINUTE), Calendar.getInstance().get(
+                Calendar.SECOND), 0);
+        System.out.println(tt);
+
+        try {
+            String sToDate = "2005-8-18";//用于转换成java.sql.Date的字符串
+            String sToTimestamp = "2005-8-18 14:21:12.123";//用于转换成java.sql.Timestamp的字符串
+            Date date1 = string2Date(sToDate);
+            Timestamp date2 = string2Time(sToTimestamp);
+            System.out.println("Date:"+date1.toString());//结果显示
+            System.out.println("Timestamp:"+date2.toString());//结果显示
+        }catch(Exception e) {
+            e.printStackTrace();
+        }
+    }
 }

+ 33 - 0
huimv-hy-production/huimv.hy.erp.datasource/src/main/java/com/huimv/production/datasource/utils/SignUtil.java

@@ -0,0 +1,33 @@
+package com.huimv.production.datasource.utils;
+
+import org.apache.commons.codec.binary.Hex;
+
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+public class SignUtil {
+
+    public String newSign(String random,String timestamp) throws NoSuchAlgorithmException {
+        MessageDigest md = MessageDigest.getInstance("MD5");
+        String data = random + ";" + timestamp;
+        String oneSign = Hex.encodeHexString(md.digest(data.getBytes(StandardCharsets.UTF_8)));
+        oneSign = oneSign + "HuiMv";
+        String twoSign = Hex.encodeHexString(md.digest(oneSign.getBytes(StandardCharsets.UTF_8)));
+        System.out.println(twoSign);
+        return twoSign;
+    }
+
+    public static void main(String[] args) throws NoSuchAlgorithmException {
+        String sign = new SignUtil().newSign("123456","1621076696178");
+        System.out.println("生成sign:"+sign);
+    }
+}

+ 11 - 12
huimv-hy-production/huimv.hy.erp.datasource/src/main/java/com/huimv/production/datasource/utils/TokenUtil.java

@@ -7,6 +7,8 @@ import com.auth0.jwt.JWT;
 import com.auth0.jwt.JWTVerifier;
 import com.auth0.jwt.algorithms.Algorithm;
 import com.auth0.jwt.interfaces.DecodedJWT;
+import com.huimv.common.result.Result;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
 
 import java.util.Date;
@@ -19,10 +21,11 @@ import java.util.Date;
  * @Create : 2021/2/26 0026 9:06
  **/
 @Component
+@Slf4j
 public class TokenUtil {
-
-    private static final long EXPIRE_TIME = 24 * 60 * 60 * 1000;  //有效时长
-    private static final String TOKEN_SECRET = "ben";       // 秘钥
+    // 过期时间设为30分钟
+    private static final long EXPIRE_TIME = 1 * 2 * 60 * 1000;  //有效时长
+    private static final String TOKEN_SECRET = "qinglian";       // 秘钥
 
     /**
      * 签名 生成
@@ -55,7 +58,7 @@ public class TokenUtil {
             JWTVerifier verifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET))
                     .withIssuer("auth0").build();
             DecodedJWT jwt = verifier.verify(token);
-            System.out.println("TOKEN过期时间:" + DateUtil.format(jwt.getExpiresAt(), "yyyy-MM-dd HH:mm:ss"));
+//            log.info("TOKEN过期时间:" + DateUtil.format(jwt.getExpiresAt(), "yyyy-MM-dd HH:mm:ss"));
             return true;
         } catch (Exception e) {
             return false;
@@ -72,19 +75,15 @@ public class TokenUtil {
      * @Date        : 2021/5/15       
      * @Time        : 18:53
      */
-    public JSONObject verifyToken(String token) {
-        JSONObject resultJo = new JSONObject();
+    public Result verifyToken(String token) {
         try {
             JWTVerifier verifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET))
                     .withIssuer("auth0").build();
             DecodedJWT jwt = verifier.verify(token);
-            System.out.println("TOKEN过期时间:" + DateUtil.format(jwt.getExpiresAt(), "yyyy-MM-dd HH:mm:ss"));
-            resultJo.put("success",true);
-            return resultJo;
+            log.info("TOKEN过期时间:" + DateUtil.format(jwt.getExpiresAt(), "yyyy-MM-dd HH:mm:ss"));
+            return new Result(10000,"查询成功.",true);
         } catch (Exception e) {
-            resultJo.put("success",false);
-            resultJo.put("msg",e.getMessage());
-            return resultJo;
+            return new Result(10004,"token获取出错:"+e.getMessage(),false);
         }
     }
 }