张延森 4 years ago
parent
commit
e7668f2952

+ 7
- 3
src/main/java/com/yunzhi/demo/common/JWTUtils.java View File

@@ -23,8 +23,8 @@ import java.util.Map;
23 23
 @Slf4j
24 24
 public class JWTUtils {
25 25
 
26
-    // 过期时间 5 分钟
27
-    public static final long EXPIRE_TIME = 5 * 60;
26
+    // 过期时间 10 分钟
27
+    public static final long EXPIRE_TIME = 10 * 60;
28 28
 
29 29
     // 私钥
30 30
     static final SecretKey SECRET_KEY = Keys.hmacShaKeyFor(Base64.getEncoder().encode("Yansen is so handsome. He is a good man. Everyone like him !!!".getBytes()));
@@ -43,7 +43,11 @@ public class JWTUtils {
43 43
         Date exp = datePair[1];
44 44
 
45 45
         if (null != claims.get("exp")) {
46
-            exp = (Date) claims.get("exp");
46
+            Integer milliSecond = (Integer) claims.get("exp");
47
+            int oneMinute = 1 * 60 * 1000;
48
+            if (milliSecond - exp.getTime() > oneMinute) {
49
+                exp.setTime(milliSecond);
50
+            }
47 51
         }
48 52
 
49 53
         return Jwts.builder().setIssuer(claims.get("userId").toString()).setIssuedAt(iat).setExpiration(exp).addClaims(claims).signWith(SECRET_KEY).compact();

+ 11
- 0
src/main/java/com/yunzhi/demo/common/MathUtils.java View File

@@ -0,0 +1,11 @@
1
+package com.yunzhi.demo.common;
2
+
3
+import java.util.Random;
4
+
5
+public class MathUtils {
6
+
7
+    public static Integer getRand(int min, int max) {
8
+        Random random = new Random();
9
+        return random.nextInt(max) % (max - min + 1) + min;
10
+    }
11
+}

+ 16
- 1
src/main/java/com/yunzhi/demo/common/ResponseBean.java View File

@@ -1,6 +1,7 @@
1 1
 package com.yunzhi.demo.common;
2 2
 
3 3
 import java.io.Serializable;
4
+import java.util.Map;
4 5
 
5 6
 /**
6 7
  * 接口统一状态返回BEAN.
@@ -19,8 +20,12 @@ public class ResponseBean<T> implements Serializable {
19 20
     private int code;
20 21
     private String message;
21 22
     private T data;
23
+    private String token;
22 24
 
23
-    public ResponseBean() {
25
+    /**
26
+     * 禁止外部 new
27
+     */
28
+    private ResponseBean() {
24 29
         code = ResponseBean.CODE_SUCCESS;
25 30
     }
26 31
 
@@ -28,6 +33,7 @@ public class ResponseBean<T> implements Serializable {
28 33
         ResponseBean responseBean = new ResponseBean();
29 34
         responseBean.code = ResponseBean.CODE_SUCCESS;
30 35
         responseBean.data = data;
36
+        responseBean.token = getContextToken();
31 37
 
32 38
         if (null != msgs && msgs.length > 0) {
33 39
             responseBean.message = msgs[0];
@@ -36,6 +42,11 @@ public class ResponseBean<T> implements Serializable {
36 42
         return responseBean;
37 43
     }
38 44
 
45
+    private static String getContextToken() {
46
+        Map<String, Object> tokenParam = CurrentContext.getTokenParam();
47
+        return null != tokenParam ? (String) tokenParam.get("token") : null;
48
+    }
49
+
39 50
     public static <T> ResponseBean error(String msg, int code, T ...datas) {
40 51
         ResponseBean responseBean = new ResponseBean();
41 52
         responseBean.code = code;
@@ -59,6 +70,10 @@ public class ResponseBean<T> implements Serializable {
59 70
     public T getData() {
60 71
         return data;
61 72
     }
73
+
74
+    public String getToken() {
75
+        return token;
76
+    }
62 77
     
63 78
     @Override
64 79
     public String toString() {

+ 7
- 6
src/main/java/com/yunzhi/demo/controller/SysConfigController.java View File

@@ -12,14 +12,9 @@ import io.swagger.annotations.ApiParam;
12 12
 import org.slf4j.Logger;
13 13
 import org.slf4j.LoggerFactory;
14 14
 import org.springframework.beans.factory.annotation.Autowired;
15
-import org.springframework.web.bind.annotation.PathVariable;
16
-import org.springframework.web.bind.annotation.RequestBody;
17
-import org.springframework.web.bind.annotation.RequestMapping;
18
-import org.springframework.web.bind.annotation.RequestMethod;
19
-import org.springframework.web.bind.annotation.RequestParam;
15
+import org.springframework.web.bind.annotation.*;
20 16
 import com.yunzhi.demo.service.ISysConfigService;
21 17
 import com.yunzhi.demo.entity.SysConfig;
22
-import org.springframework.web.bind.annotation.RestController;
23 18
 
24 19
 /**
25 20
  * <p>
@@ -92,4 +87,10 @@ public class SysConfigController extends BaseController {
92 87
             return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
93 88
         }
94 89
     }
90
+
91
+    @GetMapping("/admin/sys-config/value")
92
+    public ResponseBean getValueByCode(@RequestParam String code) throws Exception {
93
+        String val = iSysConfigService.getValByCode(code);
94
+        return ResponseBean.success(val);
95
+    }
95 96
 }

+ 17
- 13
src/main/java/com/yunzhi/demo/controller/TaPostController.java View File

@@ -3,15 +3,13 @@ package com.yunzhi.demo.controller;
3 3
 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
-import com.yunzhi.demo.common.BaseController;
7
-import com.yunzhi.demo.common.Constants;
8
-import com.yunzhi.demo.common.ResponseBean;
9
-import com.yunzhi.demo.common.StringUtils;
6
+import com.yunzhi.demo.common.*;
10 7
 import com.yunzhi.demo.entity.*;
11 8
 import com.yunzhi.demo.service.*;
12 9
 import io.swagger.annotations.Api;
13 10
 import io.swagger.annotations.ApiOperation;
14 11
 import io.swagger.annotations.ApiParam;
12
+import io.swagger.models.auth.In;
15 13
 import org.slf4j.Logger;
16 14
 import org.slf4j.LoggerFactory;
17 15
 import org.springframework.beans.factory.annotation.Autowired;
@@ -181,7 +179,7 @@ public class TaPostController extends BaseController {
181 179
         // 分享信息
182 180
         TaShareSetting taShareSetting = iTaShareSettingService.getSettingBy(Constants.RESOURCE_TYPE_POST, id);
183 181
         if (!"admin".equals(client)) {
184
-            if (Constants.STATUS_NORMAL.equals(taShareSetting.getStatus())) {
182
+            if (null != taShareSetting && Constants.STATUS_NORMAL.equals(taShareSetting.getStatus())) {
185 183
                 taPost.setShareSetting(taShareSetting);
186 184
             }
187 185
         } else {
@@ -189,10 +187,11 @@ public class TaPostController extends BaseController {
189 187
         }
190 188
 
191 189
         // 获取试题
192
-        List<TaPostTest> postTestList = new ArrayList<>();
193
-        if ("admin".equals(client)) {
194
-            postTestList = iTaPostTestService.getListByPostId(id);
195
-        } else {
190
+        List<TaPostTest> postTestList = iTaPostTestService.getListByPostId(id);
191
+        taPost.setPostTestList(postTestList);
192
+
193
+        // 小程序端
194
+        if (!"admin".equals(client)) {
196 195
             // 当前人员是否收藏过
197 196
             TaPerson taPerson = getCurrentPerson();
198 197
             boolean saved = iTaPostSaveService.checkSaved(taPerson.getPersonId(), id);
@@ -201,16 +200,21 @@ public class TaPostController extends BaseController {
201 200
             // 埋点数据
202 201
             iTaPostDataService.recordBy(id, taPerson);
203 202
 
203
+            iTaPostTestService.getListByPostId(id);
204
+
204 205
             // 随机取题目
205
-            if (null != taPost.getAnswerNum() && taPost.getAnswerNum() > 0) {
206
+            List<TaPostTest> testList = new ArrayList<>();
207
+            if (null != taPost.getAnswerNum() && postTestList.size() >= taPost.getAnswerNum()) {
206 208
                 for (int i = 0; i < taPost.getAnswerNum(); i++) {
207
-                    TaPostTest taPostTest = iTaPostTestService.getRandRowBy(id);
209
+                    int rand = MathUtils.getRand(0, postTestList.size());
210
+                    TaPostTest taPostTest = postTestList.get(rand);
211
+                    postTestList.remove(rand);
208 212
                     taPostTest.setCorrectAnswers(null);
209
-                    postTestList.add(taPostTest);
213
+                    testList.add(taPostTest);
210 214
                 }
211 215
             }
216
+            taPost.setPostTestList(testList);
212 217
         }
213
-        taPost.setPostTestList(postTestList);
214 218
 
215 219
         return ResponseBean.success(taPost);
216 220
     }

+ 3
- 1
src/main/java/com/yunzhi/demo/controller/TaPostTestController.java View File

@@ -1,5 +1,6 @@
1 1
 package com.yunzhi.demo.controller;
2 2
 
3
+import com.alibaba.fastjson.JSONObject;
3 4
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 5
 import com.baomidou.mybatisplus.core.metadata.IPage;
5 6
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -48,7 +49,8 @@ public class TaPostTestController extends BaseController {
48 49
     ITaPointsLogService iTaPointsLogService;
49 50
 
50 51
     @PostMapping("/ma/answer-test")
51
-    public ResponseBean answerPostTest(@ApiParam("答题内容") @RequestBody List<TaPostTest> postTestList) throws Exception {
52
+    public ResponseBean answerPostTest(@ApiParam("答题内容") @RequestBody String paramStr) throws Exception {
53
+        List<TaPostTest> postTestList = JSONObject.parseArray(paramStr, TaPostTest.class);
52 54
         if (null == postTestList || postTestList.size() == 0) {
53 55
             throw new Exception("未发现答题内容");
54 56
         }

+ 13
- 7
src/main/java/com/yunzhi/demo/controller/TaTestLogController.java View File

@@ -4,7 +4,9 @@ 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.yunzhi.demo.common.BaseController;
7
+import com.yunzhi.demo.common.Constants;
7 8
 import com.yunzhi.demo.common.ResponseBean;
9
+import com.yunzhi.demo.entity.TaPerson;
8 10
 import io.swagger.annotations.Api;
9 11
 import io.swagger.annotations.ApiOperation;
10 12
 import io.swagger.annotations.ApiParam;
@@ -46,17 +48,20 @@ public class TaTestLogController extends BaseController {
46 48
      * @param pageSize
47 49
      * @return
48 50
      */
49
-    @RequestMapping(value="/taTestLog",method= RequestMethod.GET)
51
+    @RequestMapping(value="/ma/test-log",method= RequestMethod.GET)
50 52
     @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
51 53
     public ResponseBean taTestLogList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
52
-									 @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception{
54
+                                      @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception{
55
+        TaPerson taPerson = getCurrentPerson();
53 56
 
54
-		    IPage<TaTestLog> pg = new Page<>(pageNum, pageSize);
55
-            QueryWrapper<TaTestLog> queryWrapper = new QueryWrapper<>();
56
-            queryWrapper.orderByDesc("create_date");
57
+        IPage<TaTestLog> pg = new Page<>(pageNum, pageSize);
58
+        QueryWrapper<TaTestLog> queryWrapper = new QueryWrapper<>();
59
+        queryWrapper.eq("person_id", taPerson.getPersonId());
60
+        queryWrapper.eq("status", Constants.STATUS_NORMAL);
61
+        queryWrapper.orderByDesc("create_date");
57 62
 
58
-            IPage<TaTestLog> result = iTaTestLogService.page(pg, queryWrapper);
59
-            return ResponseBean.success(result);
63
+        IPage<TaTestLog> result = iTaTestLogService.page(pg, queryWrapper);
64
+        return ResponseBean.success(result);
60 65
     }
61 66
 
62 67
     /**
@@ -72,6 +77,7 @@ public class TaTestLogController extends BaseController {
72 77
 
73 78
         IPage<TaTestLog> pg = new Page<>(pageNum, pageSize);
74 79
         QueryWrapper<TaTestLog> queryWrapper = new QueryWrapper<>();
80
+        queryWrapper.gt("status", Constants.STATUS_DELETED);
75 81
         queryWrapper.orderByDesc("create_date");
76 82
 
77 83
         IPage<TaTestLog> result = iTaTestLogService.page(pg, queryWrapper);

+ 2
- 2
src/main/java/com/yunzhi/demo/controller/WxMaController.java View File

@@ -79,7 +79,7 @@ public class WxMaController extends BaseController {
79 79
 
80 80
         taPerson.setAvatar(userInfo.getAvatarUrl());
81 81
         taPerson.setNickName(userInfo.getNickName());
82
-        taPerson.setSex("1".equals(userInfo.getGender()) ? 1 : 2);
82
+        taPerson.setSex(Integer.parseInt(userInfo.getGender()));
83 83
 
84 84
         iTaPersonService.updateById(taPerson);
85 85
 
@@ -89,7 +89,7 @@ public class WxMaController extends BaseController {
89 89
     @ApiOperation(value="授权手机", notes = "授权手机", httpMethod = "PUT", response = TaPerson.class)
90 90
     @PutMapping("/auth-phone")
91 91
     public ResponseBean updateUserPhone(@ApiParam("授权手机的参数") @RequestBody WxMaAuthParam params) throws Exception {
92
-        checkAuthParams(params);
92
+//        checkAuthParams(params);
93 93
 
94 94
         // 解密
95 95
         WxMaPhoneNumberInfo phoneNoInfo = wxUtils.getService().getUserService().getPhoneNoInfo(params.getSessionKey(), params.getEncryptedData(), params.getIv());

+ 5
- 7
src/main/java/com/yunzhi/demo/interceptor/PermissionInterceptor.java View File

@@ -37,6 +37,11 @@ public class PermissionInterceptor implements HandlerInterceptor {
37 37
 
38 38
         try {
39 39
             Map<String, Object> tokenMap = JWTUtils.decode(jws);
40
+
41
+            // 基于原始 token 构造新的 token
42
+            String newToken = JWTUtils.refresh(jws);
43
+            tokenMap.put("token", newToken);
44
+
40 45
             CurrentContext.setTokenParam(tokenMap);
41 46
         } catch (Exception e) {
42 47
             log.error("鉴权失败: {}", e.getMessage());
@@ -47,13 +52,6 @@ public class PermissionInterceptor implements HandlerInterceptor {
47 52
         return true;
48 53
     }
49 54
 
50
-    @Override
51
-    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView mv) throws Exception {
52
-        // 刷新 TOKEN
53
-        String jws = JWTUtils.getToken(request);
54
-        JWTUtils.refresh(jws, response);
55
-    }
56
-
57 55
     private void responseTokenError(HttpServletResponse response, String error) throws IOException {
58 56
         response.addHeader("Content-type", "application/json");
59 57
         response.getOutputStream().write(

+ 1
- 0
src/main/java/com/yunzhi/demo/mapper/SysConfigMapper.java View File

@@ -15,4 +15,5 @@ import org.apache.ibatis.annotations.Mapper;
15 15
 @Mapper
16 16
 public interface SysConfigMapper extends BaseMapper<SysConfig> {
17 17
 
18
+    String getValueOf(String paramCode);
18 19
 }

+ 1
- 0
src/main/java/com/yunzhi/demo/service/ISysConfigService.java View File

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

+ 8
- 0
src/main/java/com/yunzhi/demo/service/impl/SysConfigServiceImpl.java View File

@@ -4,6 +4,7 @@ import com.yunzhi.demo.entity.SysConfig;
4 4
 import com.yunzhi.demo.mapper.SysConfigMapper;
5 5
 import com.yunzhi.demo.service.ISysConfigService;
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 SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {
19 20
 
21
+    @Autowired
22
+    SysConfigMapper sysConfigMapper;
23
+
24
+    @Override
25
+    public String getValByCode(String code) {
26
+        return sysConfigMapper.getValueOf(code);
27
+    }
20 28
 }

+ 8
- 4
src/main/java/com/yunzhi/demo/service/impl/TaPointsLogServiceImpl.java View File

@@ -3,9 +3,11 @@ package com.yunzhi.demo.service.impl;
3 3
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 4
 import com.yunzhi.demo.common.Constants;
5 5
 import com.yunzhi.demo.common.StringUtils;
6
+import com.yunzhi.demo.entity.SysConfig;
6 7
 import com.yunzhi.demo.entity.TaPerson;
7 8
 import com.yunzhi.demo.entity.TaPointsLog;
8 9
 import com.yunzhi.demo.entity.TaPost;
10
+import com.yunzhi.demo.mapper.SysConfigMapper;
9 11
 import com.yunzhi.demo.mapper.TaPersonDataMapper;
10 12
 import com.yunzhi.demo.mapper.TaPointsLogMapper;
11 13
 import com.yunzhi.demo.service.ITaPointsLogService;
@@ -30,6 +32,9 @@ public class TaPointsLogServiceImpl extends ServiceImpl<TaPointsLogMapper, TaPoi
30 32
     @Autowired
31 33
     TaPersonDataMapper taPersonDataMapper;
32 34
 
35
+    @Autowired
36
+    SysConfigMapper sysConfigMapper;
37
+
33 38
     @Async
34 39
     @Override
35 40
     public void sendPoints(TaPost taPost, TaPerson taPerson) {
@@ -50,8 +55,7 @@ public class TaPointsLogServiceImpl extends ServiceImpl<TaPointsLogMapper, TaPoi
50 55
         }
51 56
 
52 57
         // 查询积分-学分转换规则
53
-        // TODO 此处需要查询系统参数
54
-        String ratio = "1";
58
+        String ratio = sysConfigMapper.getValueOf(Constants.SYS_PARAM_POINT_CREDIT_RATIO);
55 59
         String creditNum = points.toString();
56 60
 
57 61
         taPointsLog = new TaPointsLog();
@@ -80,10 +84,10 @@ public class TaPointsLogServiceImpl extends ServiceImpl<TaPointsLogMapper, TaPoi
80 84
         }
81 85
     }
82 86
 
83
-    private TaPointsLog getByPersonPost(String personId, String postId) {
87
+    private TaPointsLog getByPersonPost(String personId, String targetId) {
84 88
         QueryWrapper<TaPointsLog> queryWrapper = new QueryWrapper<TaPointsLog>()
85 89
                 .eq("person_id", personId)
86
-                .eq("post_id", postId)
90
+                .eq("target_id", targetId)
87 91
                 .eq("status", Constants.STATUS_NORMAL);
88 92
 
89 93
         return getOne(queryWrapper);

+ 8
- 0
src/main/resources/mapper/SysConfigMapper.xml View File

@@ -2,4 +2,12 @@
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.yunzhi.demo.mapper.SysConfigMapper">
4 4
 
5
+    <select id="getValueOf" resultType="java.lang.String">
6
+        SELECT
7
+            t.`value`
8
+        FROM
9
+            `sys_config` t
10
+        WHERE
11
+            t.`code` = #{paramCode}
12
+    </select>
5 13
 </mapper>