Parcourir la source

bug: fix resttemplate

张延森 il y a 3 ans
Parent
révision
86eda41863

+ 59
- 4
src/main/java/com/yunzhi/training/common/HttpUtils.java Voir le fichier

@@ -4,8 +4,9 @@ import org.springframework.http.*;
4 4
 import org.springframework.stereotype.Component;
5 5
 import org.springframework.web.client.RestTemplate;
6 6
 
7
-import java.util.Arrays;
8
-import java.util.Map;
7
+import java.io.UnsupportedEncodingException;
8
+import java.net.URLDecoder;
9
+import java.util.*;
9 10
 
10 11
 @Component
11 12
 public class HttpUtils {
@@ -14,7 +15,12 @@ public class HttpUtils {
14 15
         HttpHeaders headers = new HttpHeaders();
15 16
         headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
16 17
         HttpEntity<String> entity = new HttpEntity<String>(headers);
17
-        ResponseEntity<String> resp = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
18
+
19
+        Map<String, Object> urlAndParams = parseURL(url);
20
+        String formatURL = (String) urlAndParams.get("url");
21
+        Map<String, String> params = (Map<String, String>) urlAndParams.get("params");
22
+
23
+        ResponseEntity<String> resp = restTemplate.exchange(formatURL, HttpMethod.GET, entity, String.class, params);
18 24
         return resp.getBody();
19 25
     }
20 26
 
@@ -26,7 +32,56 @@ public class HttpUtils {
26 32
         headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
27 33
         headers.setAll(header);
28 34
         HttpEntity<String> entity = new HttpEntity<String>(body, headers);
29
-        ResponseEntity<String> resp = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
35
+
36
+        Map<String, Object> urlAndParams = parseURL(url);
37
+        String formatURL = (String) urlAndParams.get("url");
38
+        Map<String, String> params = (Map<String, String>) urlAndParams.get("params");
39
+
40
+        ResponseEntity<String> resp = restTemplate.exchange(formatURL, HttpMethod.POST, entity, String.class, params);
30 41
         return resp.getBody();
31 42
     }
43
+
44
+    // RestTemplate 不能正常的处理 querystring, 必须按照约定的方式处理
45
+    private Map<String, Object> parseURL(String url) {
46
+        Map<String, Object> res = new HashMap<>();
47
+
48
+        String[] parts = url.split("\\?");
49
+        String baseURL = parts[0];
50
+
51
+        if (parts.length < 2) {
52
+            res.put("url", baseURL);
53
+            res.put("params", new HashMap<>());
54
+            return res;
55
+        }
56
+
57
+
58
+        String[] fields = parts[1].split("&");
59
+        Map<String, String> params = new HashMap<>();
60
+        List<String> searchList = new ArrayList<>();
61
+        for (String field : fields) {
62
+            String[] pairs = field.split("=");
63
+            if (2 != pairs.length) {
64
+                continue;
65
+            }
66
+
67
+            try {
68
+                String key = URLDecoder.decode(pairs[0].trim(), "UTF-8");
69
+                String val = URLDecoder.decode(pairs[1].trim(), "UTF-8");
70
+
71
+                // 转化为 "foo={foo}" 这种字符串
72
+                searchList.add(String.format("%s={%s}", key, key));
73
+                // 把 foo 对应的值存储起来
74
+                params.put(key, val);
75
+            } catch (UnsupportedEncodingException e) {
76
+                e.printStackTrace();
77
+            }
78
+        }
79
+
80
+        // url 格式类似 http://foo.org/bar?name={name}
81
+        String formatURL = baseURL + "?" + String.join("&", searchList);
82
+        res.put("url", formatURL);
83
+        res.put("params", params);
84
+
85
+        return res;
86
+    }
32 87
 }

+ 32
- 0
src/main/java/com/yunzhi/training/controller/StatsController.java Voir le fichier

@@ -0,0 +1,32 @@
1
+package com.yunzhi.training.controller;
2
+
3
+import com.yunzhi.training.common.BaseController;
4
+import com.yunzhi.training.common.ResponseBean;
5
+import com.yunzhi.training.service.ITaStudentService;
6
+import io.swagger.annotations.Api;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.web.bind.annotation.RestController;
9
+
10
+import java.util.HashMap;
11
+import java.util.Map;
12
+
13
+
14
+@Api(tags = "数据统计")
15
+@RestController
16
+public class StatsController  extends BaseController {
17
+
18
+    @Autowired
19
+    ITaStudentService iTaStudentService;
20
+
21
+    public ResponseBean totalNum() throws Exception {
22
+        // 学生总数
23
+        long totalStudents = iTaStudentService.countALL();
24
+        //
25
+
26
+
27
+        Map<String, Object> res = new HashMap<>();
28
+        res.put("totalStudents", totalStudents);
29
+
30
+        return ResponseBean.success(res);
31
+    }
32
+}

+ 21
- 2
src/main/java/com/yunzhi/training/controller/TaCourseController.java Voir le fichier

@@ -3,6 +3,9 @@ package com.yunzhi.training.controller;
3 3
 import com.baomidou.mybatisplus.core.metadata.IPage;
4 4
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
5 5
 import com.yunzhi.training.common.*;
6
+import com.yunzhi.training.entity.TaFavorites;
7
+import com.yunzhi.training.entity.TaPerson;
8
+import com.yunzhi.training.service.ITaFavoritesService;
6 9
 import io.swagger.annotations.Api;
7 10
 import io.swagger.annotations.ApiOperation;
8 11
 import io.swagger.annotations.ApiParam;
@@ -18,6 +21,7 @@ import com.yunzhi.training.service.ITaCourseService;
18 21
 import com.yunzhi.training.entity.TaCourse;
19 22
 import org.springframework.web.bind.annotation.RestController;
20 23
 
24
+import javax.servlet.http.HttpServletRequest;
21 25
 import java.time.LocalDateTime;
22 26
 
23 27
 /**
@@ -39,6 +43,9 @@ public class TaCourseController extends BaseController {
39 43
     @Autowired
40 44
     public ITaCourseService iTaCourseService;
41 45
 
46
+    @Autowired
47
+    public ITaFavoritesService iTaFavoritesService;
48
+
42 49
 
43 50
     /**
44 51
      * 分页查询列表
@@ -151,7 +158,19 @@ public class TaCourseController extends BaseController {
151 158
     @RequestMapping(value="/{client}/course/{id}",method= RequestMethod.GET)
152 159
     @ApiOperation(value="详情", notes = "详情", httpMethod = "GET", response = ResponseBean.class)
153 160
     public ResponseBean taCourseGet(@ApiParam(value = "客户端",allowableValues = "admin,wx") @PathVariable String client,
154
-                                    @ApiParam("对象ID") @PathVariable String id) throws Exception{
155
-        return ResponseBean.success(iTaCourseService.getExistBy("course_id", id, "wx".equals(client), true));
161
+                                    @ApiParam("对象ID") @PathVariable String id,
162
+                                    HttpServletRequest request) throws Exception{
163
+        TaCourse taCourse = iTaCourseService.getExistBy("course_id", id, "wx".equals(client), true);
164
+
165
+        // 微信的查询当然人是否收藏
166
+        if ("wx".equals(client)) {
167
+            TaPerson taPerson = getCurrentPerson(request);
168
+            TaFavorites taFavor = iTaFavoritesService.getByPerson("course", id, taPerson.getPersonId());
169
+            if (null != taFavor) {
170
+                taCourse.setIsFavored(true);
171
+            }
172
+        }
173
+
174
+        return ResponseBean.success(taCourse);
156 175
     }
157 176
 }

+ 6
- 10
src/main/java/com/yunzhi/training/controller/TaFavoritesController.java Voir le fichier

@@ -98,23 +98,19 @@ public class TaFavoritesController extends BaseController {
98 98
 
99 99
     /**
100 100
      * 根据id删除对象
101
-     * @param id  实体ID
101
+     * @param taFavorites  收藏内容
102 102
      */
103
-    @RequestMapping(value="/wx/favorite/{id}", method= RequestMethod.DELETE)
103
+    @RequestMapping(value="/wx/favorite", method= RequestMethod.DELETE)
104 104
     @ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
105
-    public ResponseBean taFavoritesDelete(@ApiParam("对象ID") @PathVariable String id,
105
+    public ResponseBean taFavoritesDelete(@ApiParam("收藏内容") @RequestBody TaFavorites taFavorites,
106 106
                                           HttpServletRequest request) throws Exception{
107
-        TaFavorites taFavorites = iTaFavoritesService.getById(id);
107
+        TaPerson taPerson = getCurrentPerson(request);
108
+        TaFavorites origin = iTaFavoritesService.getByPerson(taFavorites.getTargetType(), taFavorites.getTargetId(), taPerson.getPersonId());
108 109
         if (null == taFavorites) {
109 110
             ResponseBean.success("success");
110 111
         }
111 112
 
112
-        TaPerson taPerson = getCurrentPerson(request);
113
-        if (!taFavorites.getPersonId().equals(taPerson.getPersonId())) {
114
-            return ResponseBean.error("您无权操作", ResponseBean.ERROR_ILLEGAL_PARAMS);
115
-        }
116
-
117
-        if(iTaFavoritesService.removeById(id)){
113
+        if(iTaFavoritesService.removeById(origin.getFavId())){
118 114
             return ResponseBean.success("success");
119 115
         }else {
120 116
             return ResponseBean.error("删除失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);

+ 8
- 0
src/main/java/com/yunzhi/training/controller/TaQuestionController.java Voir le fichier

@@ -81,6 +81,14 @@ public class TaQuestionController extends BaseController {
81 81
         queryWrapper.orderByAsc("sort_no");
82 82
 
83 83
         IPage<TaQuestion> result = iTaQuestionService.page(pg, queryWrapper);
84
+
85
+        // 如果是微信端请求
86
+        if ("wx".equals(client) && result.getTotal() > 0) {
87
+            for(TaQuestion question : result.getRecords()) {
88
+                question.setAnswerList(iTaAnswerService.getListByQuestion(question, true));
89
+            }
90
+        }
91
+
84 92
         return ResponseBean.success(result);
85 93
     }
86 94
 

+ 8
- 1
src/main/java/com/yunzhi/training/entity/TaCourse.java Voir le fichier

@@ -2,6 +2,8 @@ package com.yunzhi.training.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;
@@ -36,6 +38,9 @@ public class TaCourse implements Serializable {
36 38
     @ApiModelProperty(value = "课程简介")
37 39
     private String subTitle;
38 40
 
41
+    @ApiModelProperty(value = "封面图")
42
+    private String thumb;
43
+
39 44
     @ApiModelProperty(value = "开始时间")
40 45
     private LocalDateTime startDate;
41 46
 
@@ -51,5 +56,7 @@ public class TaCourse implements Serializable {
51 56
     @ApiModelProperty(value = "创建时间")
52 57
     private LocalDateTime createDate;
53 58
 
54
-
59
+    @TableField(exist = false)
60
+    @ApiModelProperty(value = "是否收藏")
61
+    private Boolean isFavored;
55 62
 }

+ 2
- 0
src/main/java/com/yunzhi/training/service/ITaStudentService.java Voir le fichier

@@ -14,4 +14,6 @@ import com.yunzhi.training.entity.TaStudent;
14 14
 public interface ITaStudentService extends IBaseService<TaStudent> {
15 15
 
16 16
     IPage<TaStudent> getPageBy(IPage<TaStudent> pg, String termId, String classId, String name, String phone);
17
+
18
+    long countALL();
17 19
 }

+ 10
- 0
src/main/java/com/yunzhi/training/service/impl/TaStudentServiceImpl.java Voir le fichier

@@ -1,6 +1,8 @@
1 1
 package com.yunzhi.training.service.impl;
2 2
 
3
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
3 4
 import com.baomidou.mybatisplus.core.metadata.IPage;
5
+import com.yunzhi.training.common.Constants;
4 6
 import com.yunzhi.training.entity.TaStudent;
5 7
 import com.yunzhi.training.mapper.TaStudentMapper;
6 8
 import com.yunzhi.training.service.ITaStudentService;
@@ -21,4 +23,12 @@ public class TaStudentServiceImpl extends BaseServiceImpl<TaStudentMapper, TaStu
21 23
     public IPage<TaStudent> getPageBy(IPage<TaStudent> pg, String termId, String classId, String name, String phone) {
22 24
         return baseMapper.getPageBy(pg, termId, classId, name, phone);
23 25
     }
26
+
27
+    @Override
28
+    public long countALL() {
29
+        QueryWrapper<TaStudent> queryWrapper = new QueryWrapper<>();
30
+        queryWrapper.gt("state", Constants.STATUS_DELETED);
31
+
32
+        return count(queryWrapper);
33
+    }
24 34
 }