「小程序JAVA实战」小程序的视频展示页面初始化(63)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudeshipinzhanshiyemianchushihua62/
进入列表详情,展示点赞状态用户的名称,头像名称。源码:https://github.com/limingios/wxProgram.git 中No.15和springboot
后台开发
拦截器,不拦截获取视频初始化信息。游客可以直接观看。通过用户id,视频id,视频创建id获取是否点赞视频,并获取创建者的信息。
- 拦截器
package com.idig8;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.idig8.controller.interceptor.MiniInterceptor;
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Value("${server.file.path}")
private String fileSpace;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//资源的路径.swagger2的资源.所在的目录,
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/META-INF/resources/")
.addResourceLocations("file:"+fileSpace);
}
@Bean
public MiniInterceptor miniInterceptor() {
return new MiniInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(miniInterceptor()).addPathPatterns("/user/**")
.addPathPatterns("/video/upload", "/video/uploadCover","/video/userLike","/video/userUnLike")
.addPathPatterns("/bgm/**")
.excludePathPatterns("/user/queryPublisher");
super.addInterceptors(registry);
}
}
UserController.java
package com.idig8.controller;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.idig8.pojo.Users;
import com.idig8.pojo.vo.PublisherVideo;
import com.idig8.pojo.vo.UsersVO;
import com.idig8.service.UserService;
import com.idig8.utils.JSONResult;
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(value = "/user")
public class UserController extends BasicController{
@Autowired
private UserService userService;
@Value("${server.file.path}")
private String fileSpace;
@ApiOperation(value="用户上传头像",notes="用户上传头像的接口")
@ApiImplicitParams({
@ApiImplicitParam(name="userId",value="用户id",required=true,dataType="String",paramType="query"),
})
@PostMapping(value="/uploadFace",headers="content-type=multipart/form-data")
public JSONResult uploadFace(String userId,@ApiParam(value="图片",required=true) MultipartFile file) {
if (StringUtils.isBlank(userId)) {
return JSONResult.errorMsg("用户id不能为空...");
}
// 文件保存的命名空间
String fileName = file.getOriginalFilename();
// 保存到数据库中的相对路径
String path = "";
try {
path = FileUtil.uploadFile(file.getBytes(), fileSpace, fileName);
} catch (Exception e) {
e.getStackTrace();
return JSONResult.errorMsg(e.getMessage());
}
Users user = new Users();
user.setId(userId);
user.setFaceImage(path);
userService.updateUser(user);
return JSONResult.ok(path);
}
@ApiOperation(value="通过用户Id获取用户信息",notes="通过用户Id获取用户信息的接口")
@ApiImplicitParam(name="userId",value="用户id",required=true,dataType="String",paramType="query")
@PostMapping("/queryByUserId")
public JSONResult queryByUserId(String userId) {
if (StringUtils.isBlank(userId)) {
return JSONResult.errorMsg("用户id不能为空...");
}
Users user = userService.queryUserId(userId);
UsersVO usersVO= new UsersVO();
BeanUtils.copyProperties(user, usersVO);
return JSONResult.ok(usersVO);
}
@PostMapping("/queryPublisher")
public JSONResult queryPublisher(String loginUserId, String videoId,
String publishUserId) throws Exception {
if (StringUtils.isBlank(publishUserId)) {
return JSONResult.errorMsg("");
}
// 1. 查询视频发布者的信息
Users userInfo = userService.queryUserInfo(publishUserId);
UsersVO publisher = new UsersVO();
BeanUtils.copyProperties(userInfo, publisher);
// 2. 查询当前登录者和视频的点赞关系
boolean userLikeVideo = userService.isUserLikeVideo(loginUserId, videoId);
PublisherVideo bean = new PublisherVideo();
bean.setPublisher(publisher);
bean.setUserLikeVideo(userLikeVideo);
return JSONResult.ok(bean);
}
}
- service 和 serviceImp
package com.idig8.service;
import com.idig8.pojo.Users;
public interface UserService {
/**
* 判断用户名是否存在
* @param username
* @return
*/
public boolean queryUsernameIsExist(String username);
/**
* 保存用户
* @param user
* @return
*/
public void saveUser(Users user);
/**
* 查询用户对象
* @param username
* @return
*/
public Users queryUserIsExist(Users user);
/**
* 更新对象
* @param username
* @return
*/
public void updateUser(Users user);
/**
* userId查询用户对象
* @param username
* @return
*/
public Users queryUserId(String userId);
/**
* 查询用户信息
*/
public Users queryUserInfo(String userId);
/**
* 查询用户是否喜欢点赞视频
*/
public boolean isUserLikeVideo(String userId, String videoId);
}
package com.idig8.service.Impl;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
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.idig8.mapper.UsersLikeVideosMapper;
import com.idig8.mapper.UsersMapper;
import com.idig8.pojo.Users;
import com.idig8.pojo.UsersLikeVideos;
import com.idig8.service.UserService;
import com.idig8.utils.MD5Utils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UsersMapper usersMapper;
@Autowired
private UsersLikeVideosMapper usersLikeVideosMapper;
@Autowired
private UsersMapper userMapper;
@Autowired
private Sid sid;
@Transactional(propagation =Propagation.SUPPORTS)
@Override
public boolean queryUsernameIsExist(String username) {
Users user = new Users();
user.setUsername(username);
Users result = usersMapper.selectOne(user);
return result==null? false:true;
}
@Transactional(propagation =Propagation.REQUIRED)
@Override
public void saveUser(Users user) {
String userId =sid.nextShort();
user.setId(userId);
usersMapper.insert(user);
}
@Transactional(propagation =Propagation.SUPPORTS)
@Override
public Users queryUserIsExist(Users user) {
Example queryExample = new Example(Users.class);
Criteria criteria = queryExample.createCriteria();
criteria.andEqualTo("username",user.getUsername());
try {
criteria.andEqualTo("password",MD5Utils.getMD5Str(user.getPassword()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Users userOne = usersMapper.selectOneByExample(queryExample);
return userOne;
}
@Transactional(propagation =Propagation.REQUIRED)
@Override
public void updateUser(Users user) {
Example userExample = new Example(Users.class);
Criteria criteria = userExample.createCriteria();
criteria.andEqualTo("id", user.getId());
usersMapper.updateByExampleSelective(user, userExample);
}
@Transactional(propagation =Propagation.SUPPORTS)
@Override
public Users queryUserId(String userId){
Example queryExample = new Example(Users.class);
Criteria criteria = queryExample.createCriteria();
criteria.andEqualTo("id",userId);
Users userOne = usersMapper.selectOneByExample(queryExample);
return userOne;
}
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public Users queryUserInfo(String userId) {
Example userExample = new Example(Users.class);
Criteria criteria = userExample.createCriteria();
criteria.andEqualTo("id", userId);
Users user = userMapper.selectOneByExample(userExample);
return user;
}
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public boolean isUserLikeVideo(String userId, String videoId) {
if (StringUtils.isBlank(userId) || StringUtils.isBlank(videoId)) {
return false;
}
Example example = new Example(UsersLikeVideos.class);
Criteria criteria = example.createCriteria();
criteria.andEqualTo("userId", userId);
criteria.andEqualTo("videoId", videoId);
List<UsersLikeVideos> list = usersLikeVideosMapper.selectByExample(example);
if (list != null && list.size() >0) {
return true;
}
return false;
}
}
小程序修改
- videoInfo.js
var videoUtils = require('../../utils/videoUtils.js')
const app = getApp()
Page({
data: {
cover:'cover',
videoContext:"",
videoInfo:{},
videId:'',
src:'',
userLikeVideo:false,
serverUrl:'',
publisher:[]
},
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
})
var serverUrl = app.serverUrl;
var user = app.getGlobalUserInfo();
var loginUserId = "";
if (user != null && user != undefined && user != '') {
loginUserId = user.id;
}
wx.request({
url: serverUrl + '/user/queryPublisher?loginUserId=' + loginUserId + "&videoId=" + videoInfo.id + "&publishUserId=" + videoInfo.userId,
method: 'POST',
success: function (res) {
console.log(res.data);
var publisher = res.data.data.publisher;
var userLikeVideo = res.data.data.userLikeVideo;
me.setData({
serverUrl: serverUrl,
publisher: publisher,
userLikeVideo: userLikeVideo
});
}
})
},
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:拦截器excludePathPatterns可以不拦截,分析业务,那些需要登录后才可以获得,那些不需要登录就可以看到。
「小程序JAVA实战」小程序的视频展示页面初始化(63)的更多相关文章
- 「小程序JAVA实战」小程序的个人信息作品,收藏,关注(66)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudegerenxinxizuopinshoucangguanzhu65 ...
- 「小程序JAVA实战」小程序的flex布局(22)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-22/ 之前已经把小程序的框架说完了,接下来说说小程序的组件,在说组件之前,先说说布局吧.源码:ht ...
- 「小程序JAVA实战」小程序模块之间引用(19)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-19/ 上一节,讲了页面引用模块的概念,如果是模块之前引用呢?源码:https://github.c ...
- 「小程序JAVA实战」小程序模块页面引用(18)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-18/ 上一节,讲了模板的概念,其实小程序还提供了模块的概念.源码:https://github.c ...
- 「小程序JAVA实战」小程序查看视频发布者信息(64)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxuchakanshipinfabuzhexinxi63/ 当我们点击右下 ...
- 「小程序JAVA实战」小程序的留言和评价功能(70)
转自:https://idig8.com/2018/10/28/xiaochengxujavashizhanxiaochengxudeliuyanhepingjiagongneng69/ 目前小程序这 ...
- 「小程序JAVA实战」小程序的举报功能开发(68)
转自:https://idig8.com/2018/09/25/xiaochengxujavashizhanxiaochengxudeweixinapicaidancaozuo66-2/ 通过点击举报 ...
- 「小程序JAVA实战」小程序的关注功能(65)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudeguanzhugongneng64/ 在个人页面,根据发布者个人和 ...
- 「小程序JAVA实战」小程序的视频点赞功能开发(62)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudeshipindianzangongnengkaifa61/ 视频点 ...
- 「小程序JAVA实战」小程序的springboot后台拦截器(61)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudespringboothoutailanjieqi60/ 之前咱们把 ...
随机推荐
- recv,recvfrom,send,sendto
一般情况下:send(),recv()用于TCP,sendto()及recvfrom()用于UDP 但是send(),recv()也可以用于UDP,sendto()及recvfrom()也可以用于TC ...
- 使用PMD进行代码审查(转)
原文地址:使用PMD进行代码审查 很久没写博客了,自从上次写的设计模式的博客被不知名的鹳狸猿下架了一次之后兴趣大减,那时候就没什么兴致写博客了,但是这段时间还没有停下来,最近也在研究一些其他的东西,目 ...
- 通过VNC连接远程服务器,然后登陆服务器上的虚拟机,出现键盘输入问题的解决方法
前几天由于要在服务器上装一个虚拟机,然后就选择了vmware workstation,装好之后,进入虚拟机中的centOS系统,发现键盘上的Cpas Lock键不起作用,按下之后还是输入小写,而且按住 ...
- HDU 1198
http://acm.hdu.edu.cn/showproblem.php?pid=1198 裸并查集,主要工作在根据题目给出关系构图 #include <iostream> #inclu ...
- 故障处理:磁盘扩容出错:e2fsck: Bad magic number in super-block while trying to open /dev/vdb1
按照阿里云官网教程对云服务器进行磁盘扩容,使用fdisk重新分区,最后使用e2fsck和resize2fs来完成文件系统层面的扩容 在执行“e2fsck -f /dev/vdb1”命令时报错,如果你的 ...
- debian 安装deb软件
deb包 deb包是debian,ubuntu等LINUX发行版的软件安装包,是类似于rpm的软件包,而非debian,ubuntu系统不推荐使用deb软件包,因为要解决软件包依赖问题,安装也比较麻烦 ...
- 每天一个linux命令(网络):【转载】ping命令
Linux系统的ping命令是常用的网络命令,它通常用来测试与目标主机的连通性,我们经常会说“ping一下某机器,看是不是开着”.不能打开网页时会说“你先ping网关地址192.168.1.1试试”. ...
- java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy at sun.reflect.an ...
- webdriver的2种等待
隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉WebDriver查询Dom一定时间,默认值是0,但是设置之后,这个时间将在WebDriver对象实例整个生命周期都起作用 driver.man ...
- web本地存储(localStorage、sessionStorage)
web 本地存储 (localStorage.sessionStorage) 说明 对浏览器来说,使用 Web Storage 存储键值对比存储 Cookie 方式更直观,而且容量更大,它包含两种:l ...