소스 검색

Default Changelist

zlisen 4 년 전
부모
커밋
73b98f3879

+ 21
- 0
src/main/java/com/shigongli/common/BaseController.java 파일 보기

@@ -1,4 +1,25 @@
1 1
 package com.shigongli.common;
2 2
 
3
+import com.shigongli.entity.TaPerson;
4
+import com.shigongli.service.ITaPersonService;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+
7
+import javax.servlet.http.HttpServletRequest;
8
+import java.util.Map;
9
+
3 10
 public class BaseController {
11
+    @Autowired
12
+    ITaPersonService iTaPersonService;
13
+
14
+    public TaPerson getPerson(HttpServletRequest request) {
15
+        String token = JWTUtils.getToken(request);
16
+        try {
17
+            Map<String, Object> params = JWTUtils.decode(token);
18
+            String personId = params.get("personId").toString();
19
+            return iTaPersonService.getById(personId);
20
+        } catch (Exception e) {
21
+            e.printStackTrace();
22
+            return null;
23
+        }
24
+    }
4 25
 }

+ 1
- 1
src/main/java/com/shigongli/common/JWTUtils.java 파일 보기

@@ -54,7 +54,7 @@ public class JWTUtils {
54 54
             Claims claims = parse(token);
55 55
             return encode(claims);
56 56
         } catch (Exception e) {
57
-            log.error("解析 JWT Token 失败: {}", e.getMessage());
57
+//            log.error("解析 JWT Token 失败: {}", e.getMessage());
58 58
         }
59 59
         return null;
60 60
     }

+ 1
- 1
src/main/java/com/shigongli/controller/LoginController.java 파일 보기

@@ -24,7 +24,7 @@ public class LoginController extends BaseController {
24 24
 //    @Autowired
25 25
 //    ISysUserService iSysUserService;
26 26
 
27
-    @PostMapping("/login")
27
+    @PostMapping("/mp/login")
28 28
     @ApiOperation(value="登录", notes = "登录", httpMethod = "POST", response = ResponseBean.class)
29 29
     public ResponseBean login(@ApiParam("登录参数") @RequestBody LoginParam loginParam) throws Exception {
30 30
 //        if (null == loginParam) {

+ 33
- 7
src/main/java/com/shigongli/controller/TaPersonController.java 파일 보기

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 4
 import com.baomidou.mybatisplus.core.metadata.IPage;
5 5
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
6 6
 import com.shigongli.common.BaseController;
7
+import com.shigongli.common.JWTUtils;
7 8
 import com.shigongli.common.ResponseBean;
8 9
 import io.swagger.annotations.Api;
9 10
 import io.swagger.annotations.ApiOperation;
@@ -11,14 +12,13 @@ import io.swagger.annotations.ApiParam;
11 12
 import org.slf4j.Logger;
12 13
 import org.slf4j.LoggerFactory;
13 14
 import org.springframework.beans.factory.annotation.Autowired;
14
-import org.springframework.web.bind.annotation.PathVariable;
15
-import org.springframework.web.bind.annotation.RequestBody;
16
-import org.springframework.web.bind.annotation.RequestMapping;
17
-import org.springframework.web.bind.annotation.RequestMethod;
18
-import org.springframework.web.bind.annotation.RequestParam;
15
+import org.springframework.web.bind.annotation.*;
19 16
 import com.shigongli.service.ITaPersonService;
20 17
 import com.shigongli.entity.TaPerson;
21
-import org.springframework.web.bind.annotation.RestController;
18
+
19
+import javax.servlet.http.HttpServletRequest;
20
+import java.util.HashMap;
21
+import java.util.Map;
22 22
 
23 23
 /**
24 24
  * <p>
@@ -39,6 +39,31 @@ public class TaPersonController extends BaseController {
39 39
     @Autowired
40 40
     public ITaPersonService iTaPersonService;
41 41
 
42
+    @PostMapping("/ma/login")
43
+    public ResponseBean login(String code) {
44
+        // 用 code 去换 openid
45
+        String openid = "";
46
+
47
+        // 先查询人员是否存在
48
+        TaPerson taPerson = iTaPersonService.getByOpenId(openid);
49
+
50
+        // 如果人员不存在, 则新增
51
+        if (null == taPerson) {
52
+            taPerson = new TaPerson();
53
+            taPerson.setOpenid(openid);
54
+            iTaPersonService.save(taPerson);
55
+//            taPerson = iTaPersonService.getById(taPerson.getPersonId());
56
+        }
57
+
58
+        // 生成 token
59
+        Map<String, Object> claims = new HashMap<>();
60
+        claims.put("personId", taPerson.getPersonId());
61
+        String token = JWTUtils.encode(claims);
62
+        taPerson.setToken(token);
63
+
64
+        return ResponseBean.success(taPerson);
65
+    }
66
+
42 67
 
43 68
     /**
44 69
      * 分页查询列表
@@ -49,7 +74,8 @@ public class TaPersonController extends BaseController {
49 74
     @RequestMapping(value="/taPerson",method= RequestMethod.GET)
50 75
     @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
51 76
     public ResponseBean taPersonList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
52
-									 @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception{
77
+                                     @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
78
+                                     HttpServletRequest request) throws Exception{
53 79
 
54 80
 		    IPage<TaPerson> pg = new Page<>(pageNum, pageSize);
55 81
             QueryWrapper<TaPerson> queryWrapper = new QueryWrapper<>();

+ 5
- 2
src/main/java/com/shigongli/entity/TaPerson.java 파일 보기

@@ -2,6 +2,8 @@ package com.shigongli.entity;
2 2
 
3 3
 import com.baomidou.mybatisplus.annotation.IdType;
4 4
 import java.time.LocalDateTime;
5
+
6
+import com.baomidou.mybatisplus.annotation.TableField;
5 7
 import com.baomidou.mybatisplus.annotation.TableId;
6 8
 import java.io.Serializable;
7 9
 import io.swagger.annotations.ApiModel;
@@ -27,7 +29,7 @@ public class TaPerson implements Serializable {
27 29
     private static final long serialVersionUID = 1L;
28 30
 
29 31
     @ApiModelProperty(value = "人员ID")
30
-    @TableId(value = "person_id", type = IdType.INPUT)
32
+    @TableId(value = "person_id", type = IdType.UUID)
31 33
     private String personId;
32 34
 
33 35
     @ApiModelProperty(value = "手机")
@@ -51,5 +53,6 @@ public class TaPerson implements Serializable {
51 53
     @ApiModelProperty(value = "创建时间")
52 54
     private LocalDateTime createDate;
53 55
 
54
-
56
+    @TableField(exist = false)
57
+    private String token;
55 58
 }

+ 12
- 12
src/main/java/com/shigongli/interceptor/PermissionInterceptor.java 파일 보기

@@ -24,18 +24,18 @@ public class PermissionInterceptor implements HandlerInterceptor {
24 24
 
25 25
         // JWT
26 26
         String jws = JWTUtils.getToken(request);
27
-        if (StringUtils.isEmpty(jws)) {
28
-            responseTokenError(response, "鉴权失败, 请先进行登录操作");
29
-            return false;
30
-        }
31
-
32
-        try {
33
-            JWTUtils.verify(jws);
34
-        } catch (Exception e) {
35
-            log.error("鉴权失败: {}", e.getMessage());
36
-            responseTokenError(response, "鉴权失败, 请重新登录");
37
-            return false;
38
-        }
27
+//        if (StringUtils.isEmpty(jws)) {
28
+//            responseTokenError(response, "鉴权失败, 请先进行登录操作");
29
+//            return false;
30
+//        }
31
+
32
+//        try {
33
+//            JWTUtils.verify(jws);
34
+//        } catch (Exception e) {
35
+//            log.error("鉴权失败: {}", e.getMessage());
36
+//            responseTokenError(response, "鉴权失败, 请重新登录");
37
+//            return false;
38
+//        }
39 39
 
40 40
         return true;
41 41
     }

+ 2
- 0
src/main/java/com/shigongli/mapper/TaPersonMapper.java 파일 보기

@@ -3,6 +3,7 @@ package com.shigongli.mapper;
3 3
 import com.shigongli.entity.TaPerson;
4 4
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
5 5
 import org.apache.ibatis.annotations.Mapper;
6
+import org.apache.ibatis.annotations.Param;
6 7
 
7 8
 /**
8 9
  * <p>
@@ -15,4 +16,5 @@ import org.apache.ibatis.annotations.Mapper;
15 16
 @Mapper
16 17
 public interface TaPersonMapper extends BaseMapper<TaPerson> {
17 18
 
19
+    TaPerson getByOpenId(@Param("openid") String openid);
18 20
 }

+ 1
- 0
src/main/java/com/shigongli/service/ITaPersonService.java 파일 보기

@@ -13,4 +13,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
13 13
  */
14 14
 public interface ITaPersonService extends IService<TaPerson> {
15 15
 
16
+    TaPerson getByOpenId(String openid);
16 17
 }

+ 8
- 0
src/main/java/com/shigongli/service/impl/TaPersonServiceImpl.java 파일 보기

@@ -4,6 +4,7 @@ import com.shigongli.entity.TaPerson;
4 4
 import com.shigongli.mapper.TaPersonMapper;
5 5
 import com.shigongli.service.ITaPersonService;
6 6
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
7
+import org.springframework.beans.factory.annotation.Autowired;
7 8
 import org.springframework.stereotype.Service;
8 9
 
9 10
 /**
@@ -17,4 +18,11 @@ import org.springframework.stereotype.Service;
17 18
 @Service
18 19
 public class TaPersonServiceImpl extends ServiceImpl<TaPersonMapper, TaPerson> implements ITaPersonService {
19 20
 
21
+    @Autowired
22
+    TaPersonMapper taPersonMapper;
23
+
24
+    @Override
25
+    public TaPerson getByOpenId(String openid) {
26
+        return taPersonMapper.getByOpenId(openid);
27
+    }
20 28
 }

+ 3
- 0
src/main/resources/mapper/TaPersonMapper.xml 파일 보기

@@ -2,4 +2,7 @@
2 2
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 3
 <mapper namespace="com.shigongli.mapper.TaPersonMapper">
4 4
 
5
+    <select id="getByOpenId" resultType="com.shigongli.entity.TaPerson">
6
+        select * from ta_person where openid = #{openid}
7
+    </select>
5 8
 </mapper>