CourseController.java
8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package com.server.web.controller;
import com.server.web.common.mapper.TKzyUserMapper;
import com.server.web.common.model.TKzyCourse;
import com.server.web.common.model.TKzyUser;
import com.server.web.common.service.TKzyCourseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* 课程相关action
* Created by weiwenfu@163.com on 2018/12/5 上午 11:56.
*/
@SpringBootApplication
@RestController
@RequestMapping(BaseController.WX_NAMESPACE + "/course")
@ControllerAdvice
public class CourseController extends BaseController{
@Autowired
private TKzyCourseService tKzyCourseService;
@Autowired
private TKzyUserMapper tKzyUserMapper;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private RestTemplate restTemplate;
@Value("${wx.http.danmu.url}")
private String danmuUrl;
/**
* 热门课程
* @return
*/
@RequestMapping("/hotCourse")
public Map<String,Object> hotCourse(){
return tKzyCourseService.hotCourse();
}
/**
* 录播课列表
* @return
*/
@RequestMapping("/recordCourse")
public Map<String,Object> recordCourse(@RequestParam(value = "typeId",required = false) Long typeId, @RequestParam(value = "pageNo",required = false) Integer pageNo, @RequestParam(value = "pageSize",required = false) Integer pageSize){
return tKzyCourseService.recordCourse(typeId,pageNo,pageSize);
}
/**
* 直播课列表(全查)
* @return
*/
@RequestMapping("/livingCourse")
public Map<String,Object> livingCourse(){
return tKzyCourseService.livingCourse();
}
/**
* 直播课详情页
* @return
*/
@RequestMapping("/livingCourseDetail")
public Map<String,Object> livingCourseDetail(HttpServletRequest request,@RequestParam(value = "courseId",required = true) Long courseId){
TKzyUser tKzyUser = getLoginUser(request);
Long userId = null;
if(tKzyUser!=null){
userId = tKzyUser.getId();
}
return tKzyCourseService.livingCourseDetail(courseId,userId);
}
/**
* 录播课详情页
* @return
*/
@RequestMapping("/recordCourseDetail")
public Map<String,Object> recordCourseDetail(HttpServletRequest request,@RequestParam(value = "courseId",required = true) Long courseId){
TKzyUser tKzyUser = getLoginUser(request);
Long userId = null;
if(tKzyUser!=null){
userId = tKzyUser.getId();
}
return tKzyCourseService.recordCourseDetail(courseId,userId);
}
/**
* 评论列表
* @param courseId
* @return
*/
@RequestMapping("/commentList")
public Map<String,Object> commentList(@RequestParam(value = "courseId",required = true) Long courseId,@RequestParam(value = "pageNo",required = false) Integer pageNo, @RequestParam(value = "pageSize",required = false) Integer pageSize){
return tKzyCourseService.commentList(courseId,pageNo,pageSize);
}
/**
* 提交评论
* @param courseId
* @param comment
* @return
*/
@RequestMapping("/comment")
public Map<String,Object> comment(HttpServletRequest request,@RequestParam(value = "courseId",required = true) Long courseId, @RequestParam(value = "comment",required = true) String comment){
TKzyUser tKzyUser = getLoginUser(request);
Long userId = null;
if(tKzyUser!=null){
userId = tKzyUser.getId();
}
return tKzyCourseService.comment(courseId,userId,comment);
}
/**
* 课程分类
* @return
*/
@RequestMapping("/courseType")
public Map<String,Object> courseType(){
return tKzyCourseService.courseType();
}
/**
* 送花(此处只是调用前端的服务)
* @return
*/
@RequestMapping("/sendFlower")
public Map<String,Object> sendFlower(HttpServletRequest request,@RequestParam(value = "courseId") Long courseId){
Map<String,Object> resultMap = new HashMap<String,Object>();
resultMap.put("status","0");
resultMap.put("message","");
String giftUrl = "http://cdn.yxvzb.com/WEB/wx-master/images/gift1.png";
TKzyUser tKzyUser = getLoginUser(request);
if(tKzyUser==null){
resultMap.put("message","请先登录~");
resultMap.put("status","-1");
return resultMap;
}
TKzyCourse tKzyCourse = tKzyCourseService.selectByPrimaryKey(courseId);
if(tKzyCourse==null){
resultMap.put("message","课程信息有误,请确认~");
return resultMap;
}
//每分钟可赠送五次礼物,超出则吐丝提示:礼物赠送频繁,请稍后赠送
ValueOperations vv = redisTemplate.opsForValue();
Integer count = (Integer) vv.get("WX_FLOWER_COURSE_"+tKzyUser.getId()+"_"+courseId);
if(count!=null && count>=5){
resultMap.put("message","礼物赠送频繁,请稍后赠送~");
return resultMap;
}
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, Object> valueMap = new LinkedMultiValueMap<>();
valueMap.add("type",2+"");
valueMap.add("courseId", courseId+"");
valueMap.add("giftType", 2+"");
valueMap.add("userId",tKzyUser.getId()+"");
valueMap.add("giftCount",1+"");
valueMap.add("giftImg",giftUrl);
valueMap.add("giftName","鲜花");
valueMap.add("channelId","Kzy"+courseId+"");
if (StringUtils.isEmpty(tKzyUser.getFullName())) {
valueMap.add("userName",tKzyUser.getFullName());
}else {
valueMap.add("userName",tKzyUser.getFullName());
}
Date liveEndTime = tKzyCourse.getEndDt();
valueMap.add("liveEndTime",liveEndTime.getTime()+"");
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(valueMap, headers);
ResponseEntity<String> response = restTemplate.exchange(danmuUrl + "/sendGift", HttpMethod.POST, requestEntity, String.class);
System.out.println(tKzyUser.getId()+"送花返回:"+response);
vv.set("WX_FLOWER_COURSE_"+tKzyUser.getId()+"_"+courseId,(count==null?1:count+1),1, TimeUnit.MINUTES);
} catch (Exception e) {
e.printStackTrace();
}
resultMap.put("status","1");
return resultMap;
}
/**
* 临时设置登录信息
* @param request
* @param userId
* @return
*/
@RequestMapping("/tmpSetSession")
public Map<String,Object> tmpSetSession(HttpServletRequest request,@RequestParam(value = "userId",required = true) Long userId){
Map<String,Object> resultMap = new HashMap<String,Object>();
resultMap.put("status","0");
resultMap.put("message","");
if(userId==null||userId<=0l){
resultMap.put("message","用户ID不能为空~");
return resultMap;
}
TKzyUser tKzyUser = tKzyUserMapper.selectByPrimaryKey(userId);
if(tKzyUser!=null){
request.getSession().setAttribute(WX_USER_SESSION_KEY,tKzyUser);
resultMap.put("message","操作成功~");
return resultMap;
}else{
resultMap.put("message","未查到该用户信息~");
return resultMap;
}
}
}