「小程式JAVA實戰」小程式的影片點贊功能開發(62)
影片點贊關係有3張表,使用者表(獲得點贊數量),影片表(獲得點贊數量),使用者喜歡影片的關聯表,需要同時操作三張表。原始碼: 中No.15和springboot
後臺開發
mapper.xml開發
VideosUserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "" ><mapper namespace="com.idig8.mapper.VideosUsersMapper" > <resultMap id="BaseResultMap" type="com.idig8.pojo.vo.VideosVO" > <!-- WARNING - @mbg.generated --> <id column="id" property="id" jdbcType="VARCHAR" /> <result column="user_id" property="userId" jdbcType="VARCHAR" /> <result column="audio_id" property="audioId" jdbcType="VARCHAR" /> <result column="video_desc" property="videoDesc" jdbcType="VARCHAR" /> <result column="video_path" property="videoPath" jdbcType="VARCHAR" /> <result column="video_seconds" property="videoSeconds" jdbcType="REAL" /> <result column="video_width" property="videoWidth" jdbcType="INTEGER" /> <result column="video_height" property="videoHeight" jdbcType="INTEGER" /> <result column="cover_path" property="coverPath" jdbcType="VARCHAR" /> <result column="like_counts" property="likeCounts" jdbcType="BIGINT" /> <result column="status" property="status" jdbcType="INTEGER" /> <result column="create_time" property="createTime" jdbcType="TIMESTAMP" /> <result column="username" property="username" jdbcType="VARCHAR" /> <result column="face_image" property="faceImage" jdbcType="VARCHAR" /> <result column="nickname" property="nickname" jdbcType="VARCHAR" /> </resultMap> <select id="queryAllVideos" resultMap="BaseResultMap" parameterType="String"> select v.*,u.face_image,u.username,u.nickname from videos v left join users u on v.user_id = u.id where 1 = 1 <if test="videoDesc !=null and videoDesc != '' "> and v.video_desc like '%${videoDesc}%' </if> and v.status = 1 order by v.create_time </select> <update id="addVideoLikeCount" parameterType="String"> update videos set like_counts=like_counts+1 where id=#{videoId} </update> <update id="reduceVideoLikeCount" parameterType="String"> update videos set like_counts=like_counts-1 where id=#{videoId} </update></mapper>
UsersMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "" ><mapper namespace="com.idig8.mapper.UsersMapper" > <resultMap id="BaseResultMap" type="com.idig8.pojo.Users" > <!-- WARNING - @mbg.generated --> <id column="id" property="id" jdbcType="VARCHAR" /> <result column="username" property="username" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="face_image" property="faceImage" jdbcType="VARCHAR" /> <result column="nickname" property="nickname" jdbcType="VARCHAR" /> <result column="fans_counts" property="fansCounts" jdbcType="INTEGER" /> <result column="follow_counts" property="followCounts" jdbcType="INTEGER" /> <result column="receive_like_counts" property="receiveLikeCounts" jdbcType="INTEGER" /> </resultMap> <update id="addReceiveLikeCount" parameterType="String"> update users set receive_like_counts = receive_like_counts+1 where id=#{userId} </update> <update id="reduceReceiveLikeCount" parameterType="String"> update users set receive_like_counts = receive_like_counts-1 where id=#{userId} </update> </mapper>
service 和 serviceImpl
VideoService.java
package com.idig8.service;import java.util.List;import com.idig8.pojo.Videos;import com.idig8.utils.PagedResult;public interface VideoService { /** * 儲存影片資訊 * @param Id * @return */ public String saveVideo(Videos video); /** * 分析查詢影片列表 * @param video * @param isSaveRecord * @param page * @param pageSize * @return */ public PagedResult getAllVideos(Videos video,Integer isSaveRecord,Integer page,Integer pageSize); /** * 獲取熱搜詞列表 * @return */ public List<String> gethostList(); public void userLikeVideo(String userId,String videoId,String videoCreaterId); public void userUnLikeVideo(String userId,String videoId,String videoCreaterId); }
VideoServiceImpl.java
package com.idig8.service.Impl;import java.util.List;import org.n3r.idworker.Sid;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import com.idig8.mapper.SearchRecordsMapper;import com.idig8.mapper.UsersLikeVideosMapper;import com.idig8.mapper.UsersMapper;import com.idig8.mapper.VideosMapper;import com.idig8.mapper.VideosUsersMapper;import com.idig8.pojo.SearchRecords;import com.idig8.pojo.UsersLikeVideos;import com.idig8.pojo.Videos;import com.idig8.pojo.vo.VideosVO;import com.idig8.service.VideoService;import com.idig8.utils.PagedResult;import tk.mybatis.mapper.entity.Example;import tk.mybatis.mapper.entity.Example.Criteria;@Servicepublic class VideoServiceImpl implements VideoService { @Autowired private VideosMapper videosMapper; @Autowired private UsersMapper usersMapper; @Autowired private VideosUsersMapper videosUsersMapper; @Autowired private SearchRecordsMapper searchRecordsMapper; @Autowired private UsersLikeVideosMapper usersLikeVideosMapper; @Autowired private Sid sid; @Transactional(propagation = Propagation.REQUIRED) public String saveVideo(Videos video) { String id = sid.nextShort(); video.setId(id); videosMapper.insertSelective(video); return id; } @Override @Transactional(propagation = Propagation.REQUIRED) public PagedResult getAllVideos(Videos video, Integer isSaveRecord, Integer page, Integer pageSize) { String desc = video.getVideoDesc(); if (isSaveRecord != null && isSaveRecord == 1) { SearchRecords record = new SearchRecords(); String recordId = sid.nextShort(); record.setId(recordId); record.setContent(desc); searchRecordsMapper.insert(record); } PageHelper.startPage(page, pageSize); List<VideosVO> list = videosUsersMapper.queryAllVideos(desc); PageInfo<VideosVO> pageList = new PageInfo<>(list); PagedResult result = new PagedResult(); result.setPage(page); result.setTotal(pageList.getPages()); result.setRows(list); result.setRecords(pageList.getTotal()); return result; } @Transactional(propagation = Propagation.SUPPORTS) @Override public List<String> gethostList() { return searchRecordsMapper.gethotList(); } @Override public void userLikeVideo(String userId, String videoId, String videoCreaterId) { // 1.儲存使用者和影片的關聯關係 String likeId = sid.nextShort(); UsersLikeVideos usersLikeVideos = new UsersLikeVideos(); usersLikeVideos.setId(likeId); usersLikeVideos.setUserId(userId); usersLikeVideos.setVideoId(videoId); usersLikeVideosMapper.insert(usersLikeVideos); // 2.影片喜歡的累加 videosUsersMapper.addVideoLikeCount(videoId); // 3. 使用者喜歡的累加 usersMapper.addReceiveLikeCount(userId); } @Override public void userUnLikeVideo(String userId, String videoId, String videoCreaterId) { Example example = new Example(UsersLikeVideos.class); Criteria criteria = example.createCriteria(); criteria.andEqualTo("userId", userId); criteria.andEqualTo("videoId", videoId); usersLikeVideosMapper.deleteByExample(example); // 2.影片喜歡的累減 videosUsersMapper.reduceVideoLikeCount(videoId); // 3. 使用者喜歡的累減 usersMapper.reduceReceiveLikeCount(userId); } }
mapper.java檔案
UsersMapper.java
package com.idig8.mapper;import com.idig8.pojo.Users;import com.idig8.utils.MyMapper;public interface UsersMapper extends MyMapper<Users> { public void addReceiveLikeCount(String userId); public void reduceReceiveLikeCount(String userId); }
VideosUsersMapper.java
package com.idig8.mapper;import java.util.List;import org.apache.ibatis.annotations.Param;import com.idig8.pojo.Videos;import com.idig8.pojo.vo.VideosVO;import com.idig8.utils.MyMapper;public interface VideosUsersMapper extends MyMapper<VideosVO> { public List<VideosVO> queryAllVideos(@Param("videoDesc") String videoDesc); public void addVideoLikeCount(String videoId); public void reduceVideoLikeCount(String videoId); }
controller.java
VideoController.java
package com.idig8.controller;import java.io.File;import java.util.Date;import java.util.UUID;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.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import com.idig8.pojo.Bgm;import com.idig8.pojo.Videos;import com.idig8.service.BgmService;import com.idig8.service.VideoService;import com.idig8.utils.FetchVideoCover;import com.idig8.utils.JSONResult;import com.idig8.utils.MergeVideoMp3;import com.idig8.utils.PagedResult;import com.idig8.utils.enums.VideoStatusEnum;import com.idig8.utils.file.FileUtil;import io.swagger.annotations.Api;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;@RestController@Api(value="影片相關業務的介面", tags= {"影片相關業務的controller"})@RequestMapping("/video")public class VideoController extends BasicController { @Autowired private BgmService bgmService; @Autowired private VideoService videosService; @Value("${server.file.path}") private String fileSpace; @Value("${server.ffmpeg.path}") private String ffmpegexe; @ApiOperation(value="上傳影片", notes="上傳影片的介面") @ApiImplicitParams({ @ApiImplicitParam(name="userId", value="使用者id", required=true, dataType="String", paramType="form"), @ApiImplicitParam(name="bgmId", value="背景音樂id", required=false, dataType="String", paramType="form"), @ApiImplicitParam(name="videoSeconds", value="背景音樂播放長度", required=true, dataType="String", paramType="form"), @ApiImplicitParam(name="videoWidth", value="影片寬度", required=true, dataType="String", paramType="form"), @ApiImplicitParam(name="videoHeight", value="影片高度", required=true, dataType="String", paramType="form"), @ApiImplicitParam(name="desc", value="影片描述", required=false, dataType="String", paramType="form") }) @PostMapping(value="/upload", headers="content-type=multipart/form-data") public JSONResult upload(String userId, String bgmId, double videoSeconds, int videoWidth, int videoHeight, String desc, @ApiParam(value="短影片", required=true) MultipartFile file) throws Exception { if (StringUtils.isBlank(userId)) { return JSONResult.errorMsg("使用者id不能為空..."); } // 檔案儲存的名稱空間 String fileName = file.getOriginalFilename(); // 儲存到資料庫中的相對路徑 String path = ""; String videOutPath = ""; String ImagePath = ""; try { path = FileUtil.uploadFile(file.getBytes(), fileSpace, fileName); } catch (Exception e) { e.getStackTrace(); return JSONResult.errorMsg(e.getMessage()); } if(StringUtils.isNotBlank(bgmId)){ Bgm bgm = bgmService.queryBgmById(bgmId); String mp3BgmPath = fileSpace + bgm.getPath(); MergeVideoMp3 mergeVideoMp3 = new MergeVideoMp3(ffmpegexe); String videOutPathName = UUID.randomUUID().toString()+".mp4"; File targetFile = new File(fileSpace + userId); if (!targetFile.exists()) { targetFile.mkdirs(); } videOutPath = "/"+userId+"/"+videOutPathName; String videoInput = fileSpace +path; mergeVideoMp3.convertor(videoInput, mp3BgmPath, videoSeconds, fileSpace +videOutPath); }else{ videOutPath = path; } ImagePath = "/"+userId+"/"+UUID.randomUUID().toString()+".jpg";; FetchVideoCover fetchVideoCover = new FetchVideoCover(ffmpegexe); fetchVideoCover.getCover(fileSpace +videOutPath, fileSpace +ImagePath); Videos videos = new Videos(); videos.setAudioId(bgmId); videos.setCreateTime(new Date()); videos.setVideoDesc(desc); videos.setId(UUID.randomUUID().toString()); videos.setUserId(userId); videos.setVideoHeight(videoHeight); videos.setVideoWidth(videoWidth); videos.setVideoPath(videOutPath); videos.setCoverPath(ImagePath); videos.setStatus(VideoStatusEnum.SUCCESS.value); videosService.saveVideo(videos); return JSONResult.ok(path); } @PostMapping(value="/showAll") @ApiOperation(value="影片列表", notes="分頁的影片列表") public JSONResult upload(@RequestBody Videos video,Integer isSaveRecord, Integer page) throws Exception { if(page == null){ page = 1; } PagedResult result = videosService.getAllVideos(video,isSaveRecord,page, PAGE_SIZE); return JSONResult.ok(result); } @PostMapping(value="/userLike") @ApiOperation(value="熱搜詞列表", notes="熱搜詞列表") public JSONResult userLike(String userId,String videoId,String videoCreaterId) throws Exception { videosService.userLikeVideo(userId, videoId, videoCreaterId); return JSONResult.ok(); } @PostMapping(value="/userUnLike") public JSONResult userUnLike(String userId,String videoId,String videoCreaterId) throws Exception { videosService.userUnLikeVideo(userId, videoId, videoCreaterId); return JSONResult.ok(); } @PostMapping(value="/hot") public JSONResult upload() throws Exception { return JSONResult.ok(videosService.gethostList()); } }
小程式前端修改
var videoUtils = require('../../utils/videoUtils.js')const app = getApp() Page({ data: { cover:'cover', videoContext:"", videoInfo:{}, videId:'', src:'', userLikeVideo:false }, showSearch:function(){ wx.navigateTo({ url: '../videoSearch/videoSearch', }) }, onLoad:function(params){ var me = this; me.videoContext = wx.createVideoContext('myVideo', me); var videoInfo = JSON.parse(params.videoInfo); var videoWidth = videoInfo.videoWidth; var videoHeight = videoInfo.videoHeight; var cover = 'cover'; if (videoWidth > videoHeight){ cover = ''; } me.setData({ videId: videoInfo.id, src: app.serverUrl + videoInfo.videoPath, videoInfo: videoInfo, cover: cover }) }, showIndex:function(){ wx.redirectTo({ url: '../index/index', }) }, onShow:function(){ var me = this; me.videoContext.play(); }, onHide:function(){ var me = this; me.videoContext.pause(); }, upload:function(){ var me = this; var userInfo = app.getGlobalUserInfo(); var videoInfo = JSON.stringify(me.data.videoInfo); var realUrl = '../videoInfo/videoInfo#videoInfo@' + videoInfo; if (userInfo.id == '' || userInfo.id == undefined) { wx.navigateTo({ url: '../userLogin/userLogin?realUrl=' + realUrl, }) } else { videoUtils.uploadVideo(); } }, showMine: function () { var me = this; var userInfo = app.getGlobalUserInfo(); var videoInfo = JSON.parse if (userInfo.id == '' || userInfo.id == undefined){ wx.navigateTo({ url: '../userLogin/userLogin', }) }else{ wx.navigateTo({ url: '../mine/mine', }) } }, likeVideoOrNot: function () { var me = this; var userInfo = app.getGlobalUserInfo(); var videoInfoStr = JSON.stringify(me.data.videoInfo); var realUrl = '../videoInfo/videoInfo#videoInfo@' + videoInfoStr; if (userInfo.id == '' || userInfo.id == undefined) { wx.navigateTo({ url: '../userLogin/userLogin?realUrl=' + realUrl, }) } else { var videoInfo = me.data.videoInfo; var userLikeVideo = me.data.userLikeVideo; var url = "/video/userLike?userId=" + userInfo.id + "&videoId=" + videoInfo.id + "&videoCreaterId=" + userLikeVideo.userId; if (userLikeVideo){ var url = "/video/userUnLike?userId=" + userInfo.id + "&videoId=" + videoInfo.id + "&videoCreaterId=" + userLikeVideo.userId; } wx.showLoading({ title: '....', }) wx.request({ url: app.serverUrl + url, method: "POST", header: { 'content-type': 'application/json', // 預設值 'headerUserId': userInfo.id, 'headerUserToken': userInfo.userToken }, success: function (res) { wx.hideLoading(); me.setData({ userLikeVideo: !userLikeVideo, }) } }) } } })
PS:許多的功能如果分解,邏輯清晰的話,剩下的都是搬磚活。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1762/viewspace-2824111/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 「小程式JAVA實戰」小程式影片列表到詳情功能(58)Java
- 微信小程式實現點贊、取消點贊,和多項點選功能微信小程式
- 煙臺小程式開發——微信小程式功能特點都有哪些微信小程式
- 10行程式碼實現微信小程式支付功能,使用小程式雲開發實現小程式支付功能(行程微信小程式
- 「小程式JAVA實戰」小程式的表單元件(25)Java元件
- Taro + 小程式雲開發實戰
- 「小程式JAVA實戰」小程式的上傳(終結)(72)Java
- 【小程式】微信小程式開發實踐微信小程式
- 「小程式JAVA實戰」小程式模組頁面引用(18)Java
- 小程式·雲開發實戰 - 迷你微博
- 小程式雲開發專案實戰
- 雲原生微信小程式開發實戰微信小程式
- 藉助小程式雲開發實現小程式的登陸註冊功能
- 電影院開發小程式的功能
- mpvue實戰開發美團外賣小程式Vue
- 跑腿小程式系統開發的功能
- 小程式實戰—答題類小程式
- 傢俱小程式開發優勢與功能特點
- 小程式開發,小程式代理,小程式加盟,小程式創業創業
- 實戰·使用taro+雲開發快速開發小程式
- 微信小程式開發常用功能微信小程式
- 實戰:雲開發·實現奶茶店小程式(一)
- Taro小程式跨端開發入門實戰跨端
- 『小程式開發』關於微信小程式內建元件swiper,circular(銜接)使用小技巧(實戰)微信小程式元件
- 微信小程式開發的一點心得微信小程式
- 美美優選小程式開發(開發小程式)
- 「小程式JAVA實戰」 小程式wxss樣式檔案的使用(七)Java
- 盲盒小程式開發的核心功能
- 乾貨:如何藉助小程式雲開發實現小程式支付功能(含原始碼)原始碼
- 有贊招聘小程式,有你就很贊
- 微信小程式音訊功能開發實(cai)踐(keng)微信小程式音訊AI
- 小程式開發點滴積累
- 物流小程式開發優勢與功能
- 微信小程式開發抖音去水印功能微信小程式
- 盲盒商城小程式開發功能列表
- 「小程式JAVA實戰」 小程式抽離公用方法進行模組化(12)Java
- 【小程式】微信小程式開發準備微信小程式
- 小程式–關於小程式未上線二維碼識別功能開發