pars-h5-2021/src/main/java/com/szxgl/pars2021/controller/WorksController.java
2021-11-16 09:30:12 +08:00

161 lines
4.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.szxgl.pars2021.controller;
import com.szxgl.pars2021.entity.UserDO;
import com.szxgl.pars2021.entity.WorksDao;
import com.szxgl.pars2021.service.UserService;
import com.szxgl.pars2021.service.WorksService;
import com.szxgl.pars2021.utils.R;
import com.szxgl.pars2021.utils.RedisUtil;
import com.szxgl.pars2021.utils.UploadFileToOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;
/**
* 作品
*/
@RestController
@RequestMapping("/works")
@Slf4j
public class WorksController {
@Autowired
private WorksService worksService;
@Value("${spring.profiles.active}")
private String activeProfiles;
//redis中作品排序的key
private static final String RANKING = "RANKING";
@Autowired
private UserService userService;
@Autowired
private RedisUtil redisUtil;
//Bucket 域名
@Value("${oss.bucketDomain}")
private String bucketDomain;
@GetMapping("/index")
public String index(){
return "index";
}
/**
* 添加作品
* @param file
* @param request
* @return
*/
@PostMapping("/saveWorks")
public R saveWorks(MultipartFile file, HttpServletRequest request){
String openid = getValue(request.getCookies(), "openid");
if("prod".equals(activeProfiles) && StringUtils.isBlank(openid)){
log.error("openid为空,请求参数");
return R.error("openid不能为空");
}
UserDO userDO = userService.get(openid);
if(!StringUtils.isNoneBlank(userDO.getRealname()) || !StringUtils.isNoneBlank(userDO.getPhone())){
log.error("请先填写真实姓名和手机号!");
return R.error("请先填写真实姓名和手机号!");
}
//查询用户是否已拥有作品
WorksDao work = worksService.getWorks(openid);
if( work != null ){
log.error("您已经上传过作品,请勿重复上传!");
return R.error("您已经上传过作品,请勿重复上传!");
}
try {
//上传文件到oss上
R r = UploadFileToOssUtil.uploadFileToOss(file.getInputStream(), Objects.requireNonNull(file.getOriginalFilename()));
if("-1".equals(r.get("ret"))){
log.error("文件上传失败!");
return R.error("文件上传失败!");
}
//获取oss的文件上传路径
String url = r.get("msg").toString();
//作品信息添加到数据库
WorksDao worksDao = new WorksDao();
worksDao.setUrl(url);
worksDao.setOpenid(openid);
worksDao.setCreate_time(new Date());
worksService.saveWorks(worksDao);
//把openid存入redis 排行榜的value
redisUtil.addZSet(RANKING,openid,0);
} catch (IOException e) {
e.printStackTrace();
}
return R.ok();
}
/**
* 查询用户是否上传作品
* @param request
* @return
*/
@GetMapping("/getUploadFile")
public R getUploadFile(HttpServletRequest request){
String openid = getValue(request.getCookies(), "openid");
if("prod".equals(activeProfiles) && StringUtils.isBlank(openid)){
log.error("openid为空,请求参数");
return R.error("openid不能为空");
}
WorksDao works = worksService.getWorks(openid);
HashMap<String, Object> map = new HashMap<>();
if(works == null){
log.error("用户未上传作品!");
map.put("result",false);
}else {
map.put("result",true);
}
return R.ok(map);
}
/**
* 获取cookie
* @param cookies
* @param key
* @return
*/
protected String getValue(Cookie[] cookies, String key) {
String value = null;
if (null != cookies && cookies.length > 0) {
for (Cookie c : cookies) {
if (key.equals(c.getName())) {
value = c.getValue();
break;
}
}
}
return value;
}
}