pars-h5-2021/src/main/java/com/szxgl/vote2021/controller/PraiseController.java
2021-11-16 09:24:22 +08:00

94 lines
2.6 KiB
Java

package com.szxgl.vote2021.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.szxgl.vote2021.entity.PraiseDao;
import com.szxgl.vote2021.entity.WorksDao;
import com.szxgl.vote2021.mapper.WorksMapper;
import com.szxgl.vote2021.service.PraiseService;
import com.szxgl.vote2021.utils.R;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
/**
* 点赞
*/
@RestController
@RequestMapping("/praise")
@Slf4j
public class PraiseController {
@Autowired
private PraiseService praiseService;
@Value("${spring.profiles.active}")
private String activeProfiles;
@Autowired
public WorksMapper worksMapper;
/**
* 用户点赞
* @param request
* @param worksId
* @return
*/
@PostMapping("/savePraise")
public R savePraise(HttpServletRequest request,Integer worksId){
String openid = getValue(request.getCookies(), "openid");
if("prod".equals(activeProfiles) && StringUtils.isBlank(openid)){
log.error("openid为空,请求参数");
return R.error("openid不能为空");
}
if(worksId == null){
log.info("作品id不能为空!");
return R.error("作品id不能为空!");
}
QueryWrapper<WorksDao> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id",worksId);
WorksDao worksDao = worksMapper.selectOne(queryWrapper);
if(worksDao == null){
log.info("作品不存在!");
return R.error("作品不存在!");
}
praiseService.savePraise(openid,worksId);
return R.ok();
}
// @PostMapping("/saveSharePraise")
// public R saveSharePraise(HttpServletRequest request){
//
// }
/**
* 获取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;
}
}