From 4391729f956c57d83aec6507e1e709a87643eac0 Mon Sep 17 00:00:00 2001 From: huangdeliang Date: Thu, 17 Jun 2021 19:12:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=91=E9=A1=B5=E9=9D=A2=E5=BC=80?= =?UTF-8?q?=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/custom/SysWapController.java | 45 +++++++++++++- .../custom/VideoWebInterfaceController.java | 49 +++++++++++---- .../custom/dto/response/VideoResponse.java | 11 ++++ .../com/stdiet/custom/utils/CookieUtils.java | 55 +++++++++++++++++ .../mapper/custom/SysVideoClassifyMapper.xml | 60 +++++++++---------- 5 files changed, 177 insertions(+), 43 deletions(-) create mode 100644 stdiet-custom/src/main/java/com/stdiet/custom/utils/CookieUtils.java diff --git a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysWapController.java b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysWapController.java index 1f162a8ef..41f125b4f 100644 --- a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysWapController.java +++ b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/SysWapController.java @@ -1,18 +1,25 @@ package com.stdiet.web.controller.custom; +import com.alibaba.fastjson.JSONObject; import com.stdiet.common.core.controller.BaseController; import com.stdiet.common.core.domain.AjaxResult; import com.stdiet.common.utils.DateUtils; import com.stdiet.common.utils.StringUtils; +import com.stdiet.common.utils.sign.AesUtils; import com.stdiet.custom.domain.SysOrderPause; import com.stdiet.custom.domain.SysWxAdLog; import com.stdiet.custom.service.*; +import com.stdiet.custom.utils.CookieUtils; import com.stdiet.custom.utils.HttpRequestUtils; import com.stdiet.system.service.ISysDictTypeService; +import org.apache.commons.lang3.RandomStringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -38,6 +45,7 @@ public class SysWapController extends BaseController { @Autowired ISysSmsConfirmServie iSysSmsConfirmServie; + /** * 客户食谱详情 * @@ -140,6 +148,29 @@ public class SysWapController extends BaseController { return toAjax(iSysWxSaleAccountService.logWxAd(sysWxAdLog)); } + @GetMapping(value = "/getCookie") + public AjaxResult getCookie(@RequestParam String phone, HttpServletResponse response) { + String tokenStr = phone + "_" + new Date().getTime() + "_" + RandomStringUtils.random(8); + Cookie cookie = new Cookie("token", AesUtils.encrypt(tokenStr)); + cookie.setMaxAge(24 * 60 * 60); + cookie.setSecure(true); + cookie.setHttpOnly(true); + cookie.setPath("/"); + response.addCookie(cookie); + + return AjaxResult.success(); + } + + @GetMapping(value = "/checkCookie") + public AjaxResult checkCookie(HttpServletRequest request) { + JSONObject resultObj = CookieUtils.checkCookieValida(request, "token"); + if (resultObj.getInteger("code") == 200) { + return AjaxResult.success(resultObj.getString("msg")); + } else { + return AjaxResult.error(resultObj.getInteger("code"), resultObj.getString("msg")); + } + } + @GetMapping(value = "/getCode") public AjaxResult getCode(@RequestParam String phone) { @@ -159,13 +190,23 @@ public class SysWapController extends BaseController { } @GetMapping(value = "/checkCode") - public AjaxResult checkCode(@RequestParam String phone, @RequestParam String code) { + public AjaxResult checkCode(@RequestParam String phone, @RequestParam String code, HttpServletResponse response) { + if (StringUtils.isEmpty(code)) { return AjaxResult.error(50002, "验证码不能为空"); } + + int checkCode = iSysSmsConfirmServie.checkSmsCode(phone, code); if (checkCode == 0) { - return AjaxResult.success(); + String tokenStr = phone + "_" + new Date().getTime() + "_" + RandomStringUtils.randomAlphanumeric(8); + Cookie cookie = new Cookie("token", AesUtils.encrypt(tokenStr)); + cookie.setMaxAge(24 * 60 * 60); + cookie.setSecure(true); + cookie.setHttpOnly(true); + cookie.setPath("/"); + response.addCookie(cookie); + return new AjaxResult(20000, "登录成功"); } else if (checkCode == 1) { return AjaxResult.error(50003, "验证码失效"); } else if (checkCode == 2) { diff --git a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/VideoWebInterfaceController.java b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/VideoWebInterfaceController.java index 9a79b8e0e..db480199b 100644 --- a/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/VideoWebInterfaceController.java +++ b/stdiet-admin/src/main/java/com/stdiet/web/controller/custom/VideoWebInterfaceController.java @@ -1,5 +1,6 @@ package com.stdiet.web.controller.custom; +import com.alibaba.fastjson.JSONObject; import com.aliyun.vod20170321.models.GetPlayInfoResponseBody; import com.stdiet.common.core.controller.BaseController; import com.stdiet.common.core.domain.AjaxResult; @@ -8,14 +9,18 @@ import com.stdiet.common.utils.AliyunVideoUtils; import com.stdiet.common.utils.StringUtils; import com.stdiet.common.utils.oss.AliyunOSSUtils; import com.stdiet.custom.domain.SysNutritionalVideo; +import com.stdiet.custom.domain.SysOrder; import com.stdiet.custom.domain.SysVideoClassify; import com.stdiet.custom.dto.response.NutritionalVideoResponse; import com.stdiet.custom.dto.response.VideoClassifyResponse; import com.stdiet.custom.service.ISysNutritionalVideoService; +import com.stdiet.custom.service.ISysOrderService; import com.stdiet.custom.service.ISysVideoClassifyService; +import com.stdiet.custom.utils.CookieUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpServletRequest; import java.util.List; @@ -29,18 +34,34 @@ public class VideoWebInterfaceController extends BaseController { @Autowired private ISysNutritionalVideoService sysNutritionalVideoService; + @Autowired + private ISysOrderService iSysOrderService; + /** * 查询视频分类目录列表 */ @GetMapping("/getAllClassify") - public TableDataInfo getClassifyAndVideo(SysVideoClassify sysVideoClassify) - { + public TableDataInfo getClassifyAndVideo(SysVideoClassify sysVideoClassify, HttpServletRequest request) { + + JSONObject result = CookieUtils.checkCookieValida(request, "token"); + if (result.getInteger("code") != 200) { + TableDataInfo errInfo = new TableDataInfo(); + errInfo.setCode(result.getInteger("code")); + errInfo.setMsg(result.getString("msg")); + return errInfo; + } + + // 检查是否已消费 + SysOrder order = new SysOrder(); + order.setPhone(result.getString("phone")); + List orders = iSysOrderService.selectSysOrderList(order); + //不分页则不需要传pageNum、pageSize参数 startPage(); //父级分类ID 0表示主分类 - sysVideoClassify.setParentId(0L); - //客户类型 0 未付费客户 1已付费客户 - sysVideoClassify.setUserType(1); + sysVideoClassify.setParentId(sysVideoClassify.getParentId() == null ? 0L : sysVideoClassify.getParentId()); + //客户类型 0未付费客户-playLevel根据上传的标记 1已付费客户-playLevel全为0 + sysVideoClassify.setUserType(orders.size() > 0 ? 1 : 0); List list = sysVideoClassifyService.getAllClassifyAndVideo(sysVideoClassify); return getDataTable(list); } @@ -48,17 +69,22 @@ public class VideoWebInterfaceController extends BaseController { /** * 根据视频videoId获取视频详情以及播放地址 + * * @param videoId 阿里云中对应videoId * @return */ @GetMapping("/getVideoDetail/{videoId}") - public AjaxResult getVideoDetail(@PathVariable("videoId") String videoId) - { - if(StringUtils.isEmpty(videoId)){ + public AjaxResult getVideoDetail(@PathVariable("videoId") String videoId, HttpServletRequest request) { + JSONObject result = CookieUtils.checkCookieValida(request, "token"); + if (result.getInteger("code") != 200) { + return AjaxResult.error(result.getInteger("code"), result.getString("msg")); + } + + if (StringUtils.isEmpty(videoId)) { return AjaxResult.error("视频加载失败"); } SysNutritionalVideo sysNutritionalVideo = sysNutritionalVideoService.selectSysNutritionalVideByVideoId(videoId); - if(sysNutritionalVideo == null){ + if (sysNutritionalVideo == null) { return AjaxResult.error("视频加载失败"); } NutritionalVideoResponse nutritionalVideoResponse = new NutritionalVideoResponse(); @@ -69,7 +95,7 @@ public class VideoWebInterfaceController extends BaseController { //播放地址 nutritionalVideoResponse.setPlayUrl(playList.get(0).getPlayURL()); } - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); return AjaxResult.error("视频加载失败"); } @@ -91,12 +117,13 @@ public class VideoWebInterfaceController extends BaseController { /** * 更新视频播放次数 + * * @param videoId 阿里云中对应videoId * @return */ @GetMapping(value = "/updateVideoPlayNum") public AjaxResult updateVideoPlayNum(@RequestParam(value = "videoId") String videoId) { - if(StringUtils.isEmpty(videoId)){ + if (StringUtils.isEmpty(videoId)) { return AjaxResult.error("更新失败"); } return toAjax(sysNutritionalVideoService.updateVideoPlayNum(videoId)); diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/dto/response/VideoResponse.java b/stdiet-custom/src/main/java/com/stdiet/custom/dto/response/VideoResponse.java index 4d45331ef..b5c526607 100644 --- a/stdiet-custom/src/main/java/com/stdiet/custom/dto/response/VideoResponse.java +++ b/stdiet-custom/src/main/java/com/stdiet/custom/dto/response/VideoResponse.java @@ -23,6 +23,17 @@ public class VideoResponse implements Serializable { //播放次数 private Integer playNum; + // 播放等级 + private Integer playLevel; + + public void setPlayLevel(Integer playLevel) { + this.playLevel = playLevel; + } + + public Integer getPlayLevel() { + return playLevel; + } + public String getVideoId() { return videoId; } diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/utils/CookieUtils.java b/stdiet-custom/src/main/java/com/stdiet/custom/utils/CookieUtils.java new file mode 100644 index 000000000..413fefaf8 --- /dev/null +++ b/stdiet-custom/src/main/java/com/stdiet/custom/utils/CookieUtils.java @@ -0,0 +1,55 @@ +package com.stdiet.custom.utils; + +import com.alibaba.fastjson.JSONObject; +import com.stdiet.common.utils.StringUtils; +import com.stdiet.common.utils.sign.AesUtils; +import org.apache.commons.lang3.RandomStringUtils; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import java.util.Arrays; +import java.util.Date; + +public class CookieUtils { + + public static String getCookieValue(HttpServletRequest req, String cookieName) { + Cookie[] cookies = req.getCookies(); + if (StringUtils.isNull(cookies)) { + return null; + } + return Arrays.stream(cookies) + .filter(c -> c.getName().equals(cookieName)) + .findFirst() + .map(Cookie::getValue) + .orElse(null); + } + + public static JSONObject checkCookieValida(HttpServletRequest req, String cookieName) { + JSONObject resultObj = new JSONObject(); + + String token = getCookieValue(req, "token"); + if (StringUtils.isEmpty(token)) { + resultObj.put("code", 50102); + resultObj.put("msg", "请重新登录"); + return resultObj; + } + + String tokenStr = AesUtils.decrypt(token); + String[] vals = tokenStr.split("_"); + String phone = vals[0]; + Long fromDate = Long.parseLong(vals[1]); + String randomCode = vals[2]; + + resultObj.put("code", 200); + resultObj.put("phone", phone); + + + if (new Date().getTime() - fromDate > 24 * 60 * 60 * 1000) { + resultObj.put("code", 50102); + resultObj.put("msg", "请重新登录"); + return resultObj; + } + + return resultObj; + } +} diff --git a/stdiet-custom/src/main/resources/mapper/custom/SysVideoClassifyMapper.xml b/stdiet-custom/src/main/resources/mapper/custom/SysVideoClassifyMapper.xml index 3259c594c..8152233c2 100644 --- a/stdiet-custom/src/main/resources/mapper/custom/SysVideoClassifyMapper.xml +++ b/stdiet-custom/src/main/resources/mapper/custom/SysVideoClassifyMapper.xml @@ -5,16 +5,16 @@ - - - - - - - - - - + + + + + + + + + + @@ -32,7 +32,7 @@ @@ -91,7 +91,8 @@ - - - + + + @@ -110,19 +111,20 @@ - - + + - + - - - + + + @@ -146,16 +148,14 @@