Sfoglia il codice sorgente

接收边缘端数据,传给三维数据,token

gaodd 4 anni fa
parent
commit
60a8adcb00

+ 3 - 1
src/main/java/com/unissoft/parameter/controller/HardwareParameterController.java

@@ -10,7 +10,9 @@ import com.unissoft.parameter.service.HardwareParameterService;
10 10
 
11 11
 import io.swagger.annotations.ApiOperation;
12 12
 
13
+import java.math.BigDecimal;
13 14
 import java.util.Date;
15
+import java.util.Random;
14 16
 
15 17
 import org.springframework.beans.factory.annotation.Autowired;
16 18
 import org.springframework.web.bind.annotation.*;
@@ -96,5 +98,5 @@ public class HardwareParameterController {
96 98
         else
97 99
             return ResultView.error();
98 100
     }
99
-
101
+    
100 102
 }

+ 23 - 0
src/main/java/com/unissoft/systemManage/controller/NBasicEdgeController.java

@@ -9,15 +9,26 @@ import com.unissoft.common.SystemLog;
9 9
 import com.unissoft.systemManage.model.LogOperation;
10 10
 import com.unissoft.systemManage.model.NBasicEdge;
11 11
 import com.unissoft.systemManage.model.RoleInfo;
12
+import com.unissoft.systemManage.model.SyncV2xyz;
12 13
 import com.unissoft.systemManage.service.NBasicEdgeService;
13 14
 import com.unissoft.systemManage.service.RoleInfoService;
15
+
16
+import ch.qos.logback.classic.Logger;
14 17
 import io.swagger.annotations.Api;
15 18
 import io.swagger.annotations.ApiOperation;
19
+
16 20
 import org.springframework.beans.factory.annotation.Autowired;
21
+import org.springframework.stereotype.Controller;
17 22
 import org.springframework.web.bind.annotation.*;
18 23
 
24
+import java.net.URLDecoder;
19 25
 import java.text.ParseException;
26
+import java.util.Date;
20 27
 import java.util.List;
28
+import java.util.Map;
29
+
30
+import javax.servlet.http.HttpServletRequest;
31
+import javax.servlet.http.HttpServletResponse;
21 32
 
22 33
 /**
23 34
  * <p>
@@ -39,6 +50,18 @@ public class NBasicEdgeController {
39 50
         this.nBasicEdgeService = nBasicEdgeService;
40 51
     }
41 52
 
53
+    
54
+    @PostMapping(value ="/getEdgeData")
55
+    public String getEdgeData(@RequestBody SyncV2xyz syncV2xyz) throws Exception{
56
+    	/*SyncV2xyz syncV2xyz = new SyncV2xyz();
57
+    	syncV2xyz.setStartTime(new Date());
58
+    	syncV2xyz.setVolume(21.28);
59
+    	syncV2xyz.setXyz("%5B%5B1%2C1%2C0.61%5D%2C%5B1%2C2%2C0.61%5D%2C%5B-1%2C2%2C0.61%5D%2C%5B1%2C1%2C0.61%5D%5D%5B%5B1%2C1%2C0.37%5D%2C%5B1%2C2%2C0.37%5D%2C%5B-1%2C2%2C0.37%5D%2C%5B1%2C1%2C0.37%5D%2C%5B1%2C1%2C0.37%5D%2C%5B1%2C2%2C0.37%5D%2C%5B-1%2C2%2C0.37%5D%2C%5B1%2C1%2C0.37%5D%5D");
60
+    	*/
61
+    	nBasicEdgeService.saveEdgeData(syncV2xyz);
62
+		return "202";
63
+    }
64
+    
42 65
     @ApiOperation(value = "根据仓房id查询分页", notes = "")
43 66
     @PostMapping("/basic/getCornByHouseId")
44 67
     public ResultView getCornByHouseId(@RequestBody PageParam pageParam) {

+ 1 - 3
src/main/java/com/unissoft/systemManage/controller/PointDataController.java

@@ -14,9 +14,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
14 14
 import org.springframework.web.bind.annotation.RestController;
15 15
 
16 16
 import com.unissoft.common.ResultView;
17
-import com.unissoft.systemManage.model.OrgInfo;
18 17
 import com.unissoft.systemManage.model.PointData;
19
-import com.unissoft.systemManage.service.OrgInfoService;
20 18
 import com.unissoft.systemManage.service.PointDataService;
21 19
 
22 20
 /**
@@ -25,7 +23,7 @@ import com.unissoft.systemManage.service.PointDataService;
25 23
  * </p>
26 24
  *
27 25
  * @author root
28
- * @since 2021-12-02
26
+ * @since 2021-12-06
29 27
  */
30 28
 @RestController
31 29
 @RequestMapping("/point-data")

+ 15 - 1
src/main/java/com/unissoft/systemManage/controller/UserInfoController.java

@@ -6,6 +6,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
6 6
 import com.unissoft.common.*;
7 7
 import com.unissoft.systemManage.model.UserInfo;
8 8
 import com.unissoft.systemManage.service.UserInfoService;
9
+import com.unissoft.utils.JWTUtil;
10
+
9 11
 import io.swagger.annotations.Api;
10 12
 import io.swagger.annotations.ApiOperation;
11 13
 import org.springframework.beans.factory.annotation.Autowired;
@@ -14,7 +16,10 @@ import org.springframework.web.bind.annotation.*;
14 16
 
15 17
 import javax.servlet.http.HttpServletRequest;
16 18
 import javax.websocket.server.PathParam;
19
+
20
+import java.util.HashMap;
17 21
 import java.util.List;
22
+import java.util.Map;
18 23
 
19 24
 /**
20 25
  * <p>
@@ -105,7 +110,16 @@ public class UserInfoController {
105 110
                     String md5 = MD5Util.getMD5(userPassword);
106 111
                     if (one.getUserPassword().equals(md5)) {
107 112
                         request.getSession().setAttribute("userLogin", one);
108
-                        return ResultView.success(one);
113
+                        
114
+                        Map<String, String> map = new HashMap<>(); //用来存放payload信息
115
+
116
+                        map.put("id",one.getId().toString());
117
+                        map.put("username",one.getUserName());
118
+
119
+                        // 生成token令牌
120
+                        Map<String, Object> token = JWTUtil.generateToken(map,one);
121
+                        
122
+                        return ResultView.success(token);
109 123
                     }
110 124
                 } else {
111 125
                     return ResultView.error(ResultEnum.CODE_5.getMsg());

+ 1 - 1
src/main/java/com/unissoft/systemManage/mapper/PointDataMapper.java

@@ -9,7 +9,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
9 9
  * </p>
10 10
  *
11 11
  * @author root
12
- * @since 2021-12-02
12
+ * @since 2021-12-06
13 13
  */
14 14
 public interface PointDataMapper extends BaseMapper<PointData> {
15 15
 

+ 4 - 2
src/main/java/com/unissoft/systemManage/mapper/PointDataMapper.xml

@@ -6,16 +6,18 @@
6 6
     <resultMap id="BaseResultMap" type="com.unissoft.systemManage.model.PointData">
7 7
         <id column="id" property="id" />
8 8
         <result column="house_id" property="houseId" />
9
-        <result column="high" property="high" />
9
+        <result column="lk_height" property="lkHeight" />
10 10
         <result column="radius" property="radius" />
11 11
         <result column="points" property="points" />
12 12
         <result column="min_heigth" property="minHeigth" />
13
+        <result column="vertical_max_heigth" property="verticalMaxHeigth" />
14
+        <result column="vertical_min_heigth" property="verticalMinHeigth" />
13 15
         <result column="create_time" property="createTime" />
14 16
     </resultMap>
15 17
 
16 18
     <!-- 通用查询结果列 -->
17 19
     <sql id="Base_Column_List">
18
-        id, house_id, high, radius, points, min_heigth, create_time
20
+        id, house_id, lk_height, radius, points, min_heigth, vertical_max_heigth, vertical_min_heigth, create_time
19 21
     </sql>
20 22
 
21 23
 </mapper>

+ 17 - 8
src/main/java/com/unissoft/systemManage/model/PointData.java

@@ -1,12 +1,9 @@
1 1
 package com.unissoft.systemManage.model;
2 2
 
3 3
 import com.baomidou.mybatisplus.annotation.TableName;
4
+import com.fasterxml.jackson.annotation.JsonProperty;
4 5
 import com.baomidou.mybatisplus.annotation.IdType;
5 6
 import java.util.Date;
6
-import java.util.List;
7
-
8
-import org.assertj.core.util.Lists;
9
-
10 7
 import com.baomidou.mybatisplus.annotation.TableId;
11 8
 import com.baomidou.mybatisplus.annotation.TableField;
12 9
 import java.io.Serializable;
@@ -21,7 +18,7 @@ import lombok.EqualsAndHashCode;
21 18
  * </p>
22 19
  *
23 20
  * @author root
24
- * @since 2021-12-02
21
+ * @since 2021-12-06
25 22
  */
26 23
 @Data
27 24
 @EqualsAndHashCode(callSuper = false)
@@ -39,24 +36,36 @@ public class PointData implements Serializable {
39 36
     private Integer houseId;
40 37
 
41 38
     @ApiModelProperty(value = "高")
42
-    @TableField("high")
43
-    private String high;
39
+    @JsonProperty("LKHeight")
40
+    @TableField("lk_height")
41
+    private String lkHeight;
44 42
 
45 43
     @ApiModelProperty(value = "b半径")
44
+    @JsonProperty("Radius")
46 45
     @TableField("radius")
47 46
     private String radius;
48 47
 
49 48
     @ApiModelProperty(value = "所有的点的坐标")
49
+    @JsonProperty("Points")
50 50
     @TableField("points")
51 51
     private String points;
52 52
 
53 53
     @ApiModelProperty(value = "最低点的高")
54
+    @JsonProperty("MinHeigth")
54 55
     @TableField("min_heigth")
55 56
     private String minHeigth;
56 57
 
58
+    @ApiModelProperty(value = "垂直最大高度")
59
+    @TableField("vertical_max_heigth")
60
+    private String verticalMaxHeigth;
61
+
62
+    @ApiModelProperty(value = "垂直最小高度")
63
+    @TableField("vertical_min_heigth")
64
+    private String verticalMinHeigth;
65
+
57 66
     @ApiModelProperty(value = "创建时间")
58 67
     @TableField("create_time")
59 68
     private Date createTime;
60
-    
69
+
61 70
 
62 71
 }

+ 30 - 0
src/main/java/com/unissoft/systemManage/model/SyncV2xyz.java

@@ -0,0 +1,30 @@
1
+package com.unissoft.systemManage.model;
2
+
3
+import java.util.Date;
4
+
5
+public class SyncV2xyz {
6
+
7
+	private double volume;
8
+	private String xyz;
9
+	private Date startTime;
10
+	public double getVolume() {
11
+		return volume;
12
+	}
13
+	public void setVolume(double volume) {
14
+		this.volume = volume;
15
+	}
16
+	public String getXyz() {
17
+		return xyz;
18
+	}
19
+	public void setXyz(String xyz) {
20
+		this.xyz = xyz;
21
+	}
22
+	public Date getStartTime() {
23
+		return startTime;
24
+	}
25
+	public void setStartTime(Date startTime) {
26
+		this.startTime = startTime;
27
+	}
28
+	
29
+	
30
+}

+ 3 - 0
src/main/java/com/unissoft/systemManage/service/NBasicEdgeService.java

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
5 5
 import com.unissoft.common.PageParam;
6 6
 import com.unissoft.systemManage.model.BasicRulePolice;
7 7
 import com.unissoft.systemManage.model.NBasicEdge;
8
+import com.unissoft.systemManage.model.SyncV2xyz;
8 9
 
9 10
 import java.text.ParseException;
10 11
 import java.util.List;
@@ -24,4 +25,6 @@ public interface NBasicEdgeService extends IService<NBasicEdge> {
24 25
     List<NBasicEdge> getListByOrgId(PageParam pageParam);
25 26
 
26 27
 	int manualSaveNBasicEdge (String nBasicEdgeStr) throws ParseException;
28
+
29
+	void saveEdgeData(SyncV2xyz syncV2xyz) throws Exception;
27 30
 }

+ 1 - 1
src/main/java/com/unissoft/systemManage/service/PointDataService.java

@@ -9,7 +9,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
9 9
  * </p>
10 10
  *
11 11
  * @author root
12
- * @since 2021-12-02
12
+ * @since 2021-12-06
13 13
  */
14 14
 public interface PointDataService extends IService<PointData> {
15 15
 

+ 97 - 0
src/main/java/com/unissoft/systemManage/service/impl/NBasicEdgeServiceImpl.java

@@ -1,6 +1,7 @@
1 1
 package com.unissoft.systemManage.service.impl;
2 2
 
3 3
 import com.alibaba.fastjson.JSON;
4
+import com.alibaba.fastjson.JSONArray;
4 5
 import com.alibaba.fastjson.JSONObject;
5 6
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
6 7
 import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -8,8 +9,11 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
8 9
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
9 10
 import com.unissoft.common.PageParam;
10 11
 import com.unissoft.systemManage.mapper.NBasicEdgeMapper;
12
+import com.unissoft.systemManage.mapper.PointDataMapper;
11 13
 import com.unissoft.systemManage.model.LogOperation;
12 14
 import com.unissoft.systemManage.model.NBasicEdge;
15
+import com.unissoft.systemManage.model.PointData;
16
+import com.unissoft.systemManage.model.SyncV2xyz;
13 17
 import com.unissoft.systemManage.service.NBasicEdgeService;
14 18
 import org.springframework.stereotype.Service;
15 19
 import org.springframework.util.StringUtils;
@@ -17,8 +21,12 @@ import org.springframework.util.StringUtils;
17 21
 import javax.annotation.Resource;
18 22
 
19 23
 import java.math.BigDecimal;
24
+import java.net.URLDecoder;
20 25
 import java.text.ParseException;
21 26
 import java.text.SimpleDateFormat;
27
+import java.util.ArrayList;
28
+import java.util.Collections;
29
+import java.util.Date;
22 30
 import java.util.List;
23 31
 
24 32
 /**
@@ -34,6 +42,8 @@ public class NBasicEdgeServiceImpl extends ServiceImpl<NBasicEdgeMapper, NBasicE
34 42
 
35 43
     @Resource
36 44
     private NBasicEdgeMapper nBasicEdgeMapper;
45
+    @Resource
46
+    private PointDataMapper pointDataMapper;
37 47
 /*
38 48
     @Override
39 49
     public List<NBasicEdge> getTreeList(String houseId) {
@@ -134,4 +144,91 @@ public class NBasicEdgeServiceImpl extends ServiceImpl<NBasicEdgeMapper, NBasicE
134 144
 		
135 145
 		return nBasicEdgeMapper.insert(nBasicEdge);
136 146
 	}
147
+
148
+
149
+	@Override
150
+	public void saveEdgeData(SyncV2xyz syncV2xyz) throws Exception {
151
+		
152
+		System.out.println("volume----"+syncV2xyz.getVolume());
153
+    	System.out.println("xyz-----"+syncV2xyz.getXyz());
154
+    	System.out.println("startTime----"+syncV2xyz.getStartTime());
155
+    	
156
+		Date startTime = syncV2xyz.getStartTime();
157
+		double volume = syncV2xyz.getVolume();
158
+		String xyz_old = URLDecoder.decode(syncV2xyz.getXyz(), "UTF-8");
159
+		String xyz = xyz_old.replace("][", ",");
160
+		
161
+		System.out.println("xyz后-----"+xyz);
162
+		System.out.println("volume后----"+syncV2xyz.getVolume());
163
+    	System.out.println("startTime后-----"+startTime);
164
+		     
165
+		// TODO Auto-generated method stub
166
+		NBasicEdge nBasicEdge = new NBasicEdge(); 
167
+		//体积
168
+		BigDecimal big_volume = new BigDecimal(volume);
169
+		double lkVolume = big_volume.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
170
+		nBasicEdge.setLkVolume(Math.abs(lkVolume)+"");
171
+		
172
+		String density = "500"; //密度 千克/立方米
173
+		//质量
174
+	    BigDecimal big_density = new BigDecimal(density); 
175
+	    BigDecimal big_lkVolume = new BigDecimal(lkVolume); //保留两位小数的体积
176
+	    BigDecimal quality = big_density.multiply(big_lkVolume).divide(new BigDecimal(1000));
177
+		nBasicEdge.setLkWeight(Double.parseDouble(quality+""));
178
+		
179
+		/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
180
+		Date beginTime = sdf.parse(startTime);*/
181
+		nBasicEdge.setBeginTime(startTime);//开始时间
182
+		Date date = new Date();
183
+		nBasicEdge.setEndTime(date);//结束时间
184
+		nBasicEdge.setZsk("泉州福海粮油工业有限公司");
185
+		nBasicEdge.setFk("43");
186
+		nBasicEdge.setHouseId("44");
187
+		nBasicEdge.setCaPolic("2");//不报警
188
+		nBasicEdge.setEquiName("3号稻谷仓");
189
+		
190
+		
191
+		//取上次的数据 如果大于上次的数据 则入库 否则出库
192
+		QueryWrapper<NBasicEdge> queryWrapper = new QueryWrapper<>();
193
+        queryWrapper.eq("fk", 43);
194
+        queryWrapper.orderByDesc("id");
195
+        // 分页对象
196
+        List<NBasicEdge> nList = nBasicEdgeMapper.selectList(queryWrapper);
197
+        double v = 0 ;
198
+        if(nList.size()>0){
199
+             v =Double.parseDouble(nList.get(0).getLkVolume());
200
+        }
201
+        
202
+		if(v - lkVolume >0){ //出库
203
+			nBasicEdge.setHStatus("4");//仓房状态1,空仓2,入库3,库存4,出库
204
+		}else if (v - lkVolume <0){//入库
205
+			nBasicEdge.setHStatus("2");
206
+		}
207
+		 nBasicEdgeMapper.insert(nBasicEdge);
208
+		
209
+		PointData ponitData = new PointData();
210
+		ponitData.setHouseId(44);
211
+		ponitData.setCreateTime(date);
212
+		ponitData.setPoints(xyz);
213
+		
214
+		//String jsonString = "[[1,2,3],[4,5,62.33],[1,5,2.3]]";
215
+		JSONArray jsonArray=JSONArray.parseArray(xyz);
216
+		List<Double> list = new ArrayList<>();
217
+		
218
+		for(int i=0;i<jsonArray.size();i++){
219
+		    double z = Double.parseDouble(jsonArray.get(i).toString().split(",")[2].split("]")[0]);
220
+		    list.add(z);
221
+		    //System.out.println(jsonArray.get(i)+"*******************"+jsonArray.get(i).toString().split(",")[2].split("]")[0]);
222
+		}
223
+		Double max = Collections.max(list);
224
+	    Double min = Collections.min(list);
225
+	    Double minHeigth = (max+min)/2;
226
+	    ponitData.setVerticalMaxHeigth(max+"");
227
+	    ponitData.setVerticalMinHeigth(min+"");
228
+	    ponitData.setMinHeigth(minHeigth+"");
229
+	    ponitData.setRadius("6");
230
+	    ponitData.setLkHeight("31");
231
+		pointDataMapper.insert(ponitData);
232
+		
233
+	}
137 234
 }

+ 1 - 1
src/main/java/com/unissoft/systemManage/service/impl/PointDataServiceImpl.java

@@ -12,7 +12,7 @@ import org.springframework.stereotype.Service;
12 12
  * </p>
13 13
  *
14 14
  * @author root
15
- * @since 2021-12-02
15
+ * @since 2021-12-06
16 16
  */
17 17
 @Service
18 18
 public class PointDataServiceImpl extends ServiceImpl<PointDataMapper, PointData> implements PointDataService {

+ 31 - 3
src/main/java/com/unissoft/systemManage/service/impl/QuantityStatisticsServiceImpl.java

@@ -118,6 +118,10 @@ public class QuantityStatisticsServiceImpl extends ServiceImpl<QuantityStatistic
118 118
         		Map<String, Object> parm_map = new HashMap<>();
119 119
         	    parm_map.put("main_id", houseId);
120 120
         	    List<StorehouseClxx> list = storehouseClxxMapper.selectByMap(parm_map);
121
+        	    
122
+        	    List dayList = new ArrayList<>();
123
+        	    List wightList = new ArrayList<>();
124
+        	    
121 125
         	    if(list.size()>0){
122 126
         	    	BasicEnum basicEnum = basicEnumMapper.selectById(list.get(0).getPz());
123 127
             	    list.get(0).setCfName(orgInfo.getOrgName());//仓房名称
@@ -142,6 +146,8 @@ public class QuantityStatisticsServiceImpl extends ServiceImpl<QuantityStatistic
142 146
             		
143 147
             	    if(nBasicEdgeList.size()>0){
144 148
             	    	list.get(0).setLsNumber(nBasicEdgeList.get(0).getLkWeight().toString());
149
+            	    }else{
150
+            	    	list.get(0).setLsNumber("0");
145 151
             	    }
146 152
             	    if(nBasicEdgeList.size()>1){
147 153
             	    	BigDecimal thisNumber = new BigDecimal(nBasicEdgeList.get(0).getLkWeight());
@@ -187,8 +193,6 @@ public class QuantityStatisticsServiceImpl extends ServiceImpl<QuantityStatistic
187 193
             	    
188 194
             	    ew.orderByDesc("id");
189 195
             	    List<NBasicEdge> nBasicEdgeList1 = nBasicEdgeMapper.selectList(ew);
190
-            	    List dayList = new ArrayList<>();
191
-            	    List wightList = new ArrayList<>();
192 196
             	    
193 197
             		
194 198
                     Long startT;
@@ -240,7 +244,31 @@ public class QuantityStatisticsServiceImpl extends ServiceImpl<QuantityStatistic
240 244
             	    map.put("wightList", wightList);
241 245
             	    
242 246
         	    }else{
243
-        	    	map.put("clxx", "无储粮信息");
247
+        	    	StorehouseClxx clxx = new StorehouseClxx();
248
+        	    	clxx.setLsNumber("0");
249
+        	    	clxx.setPzName("");
250
+        	    	clxx.setCfName("");
251
+        	    	map.put("clxx", clxx);
252
+        	    	
253
+        	    	
254
+        	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
255
+            	    Calendar calendar = Calendar.getInstance();
256
+            	    Date date = new Date();
257
+            		calendar.setTime(date);
258
+            		
259
+            		//System.out.println(sdf.format(calendar.getTime()));
260
+            		for (int i = 0; i < 7; i++) {//默认展示七天的数据
261
+            			wightList.add(0);
262
+            			calendar.add(Calendar.DAY_OF_MONTH, -i);  // 在当前日基础上-1
263
+            			dayList.add(sdf.format(calendar.getTime()));
264
+            			calendar.setTime(date);
265
+					}
266
+            		
267
+        	    	map.put("dayList", dayList);
268
+            	    map.put("wightList", wightList);
269
+            	    map.put("startTime", dayList.get(dayList.size()-1));
270
+             		map.put("endTime", dayList.get(0));
271
+        	    	
244 272
         	    }
245 273
         	    
246 274
         	}

+ 51 - 0
src/main/java/com/unissoft/utils/JWTInterceptor.java

@@ -0,0 +1,51 @@
1
+package com.unissoft.utils;
2
+
3
+import java.util.HashMap;
4
+import java.util.Map;
5
+
6
+import javax.servlet.http.HttpServletRequest;
7
+import javax.servlet.http.HttpServletResponse;
8
+
9
+import org.springframework.web.servlet.HandlerInterceptor;
10
+import org.springframework.web.servlet.ModelAndView;
11
+
12
+import com.auth0.jwt.exceptions.AlgorithmMismatchException;
13
+import com.auth0.jwt.exceptions.SignatureVerificationException;
14
+import com.auth0.jwt.exceptions.TokenExpiredException;
15
+import com.fasterxml.jackson.databind.ObjectMapper;
16
+
17
+public class JWTInterceptor implements HandlerInterceptor{
18
+
19
+	@Override
20
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
21
+
22
+        Map map = new HashMap();
23
+
24
+        //获取请求头中的token令牌
25
+        String token = request.getHeader("token");
26
+        try {
27
+            JWTUtil.verify(token);//验证令牌
28
+            return true;//放行请求
29
+        } catch (SignatureVerificationException e) {
30
+            e.printStackTrace();
31
+            map.put("msg", "无效签名!");
32
+        } catch (TokenExpiredException e) {
33
+            e.printStackTrace();
34
+            map.put("msg", "token过期!");
35
+        } catch (AlgorithmMismatchException e) {
36
+            e.printStackTrace();
37
+            map.put("msg", "token算法不一致!");
38
+        } catch (Exception e) {
39
+            e.printStackTrace();
40
+            map.put("msg", "token无效!!");
41
+        }
42
+        map.put("code", 403);//设置状态
43
+        //将 map 转为json  jackson
44
+        String json = new ObjectMapper().writeValueAsString(map);
45
+        response.setContentType("application/json;charset=UTF-8");
46
+        response.getWriter().println(json);
47
+        return false;
48
+    }
49
+	
50
+	
51
+}

+ 57 - 0
src/main/java/com/unissoft/utils/JWTUtil.java

@@ -0,0 +1,57 @@
1
+package com.unissoft.utils;
2
+
3
+
4
+import java.util.Calendar;
5
+import java.util.HashMap;
6
+import java.util.Map;
7
+
8
+import com.auth0.jwt.JWT;
9
+import com.auth0.jwt.JWTCreator;
10
+import com.auth0.jwt.algorithms.Algorithm;
11
+import com.auth0.jwt.interfaces.DecodedJWT;
12
+
13
+public class JWTUtil {
14
+
15
+	// 用于JWT进行签名加密的秘钥
16
+    private static String SECRET = "code-duck-*%#@*!&";
17
+
18
+    /**
19
+     * @Param: 传入需要设置的payload信息
20
+     * @return: 返回token
21
+     */
22
+    public static Map<String,Object> generateToken(Map<String,String> map,Object object) {
23
+        JWTCreator.Builder builder = JWT.create();
24
+
25
+        // 将map内的信息传入JWT的payload中
26
+        map.forEach((k, v) -> {
27
+            builder.withClaim(k, v);
28
+        });
29
+
30
+        // 设置JWT令牌的过期时间为24小时
31
+        Calendar instance = Calendar.getInstance();
32
+        instance.add(Calendar.HOUR, 24);
33
+        builder.withExpiresAt(instance.getTime());
34
+
35
+        Map<String, Object> mp = new HashMap<>(); //用来存放payload信息
36
+        mp.put("token", builder.sign(Algorithm.HMAC256(SECRET)).toString());
37
+        mp.put("object", object);
38
+        // 设置签名并返回token
39
+        return mp;
40
+    }
41
+
42
+    /**
43
+     * @Param: 传入token
44
+     * @return:
45
+     */
46
+    public static void verify(String token) {
47
+        JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token);
48
+    }
49
+
50
+    /**
51
+     * @Param: 传入token
52
+     * @return: 解密的token信息
53
+     */
54
+    public static DecodedJWT getTokenInfo(String token) {
55
+        return JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token);
56
+    }
57
+}

+ 17 - 0
src/main/java/com/unissoft/utils/MyWebMvcConfigurer.java

@@ -0,0 +1,17 @@
1
+package com.unissoft.utils;
2
+
3
+import org.springframework.context.annotation.Configuration;
4
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
5
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
6
+
7
+@Configuration
8
+public class MyWebMvcConfigurer implements WebMvcConfigurer{
9
+
10
+	@Override
11
+    public void addInterceptors(InterceptorRegistry registry) {
12
+        registry.addInterceptor(new JWTInterceptor())
13
+                .addPathPatterns("/**") // 其他非登录接口都需要进行token验证
14
+                .excludePathPatterns("/userInfo/login","/n-basic-edge/getEdgeData","/point-data/getPoints/{houseId}"); // 登录接口、接收边缘端数据、三维获取点的数据不用于token验证
15
+                
16
+    }
17
+}