進入列表詳情,展現點贊狀態用戶的名稱,頭像名稱。源碼:https://github.com/limingios/wxProgram.git 中No.15和springbootjava
攔截器,不攔截獲取視頻初始化信息。遊客能夠直接觀看。經過用戶id,視頻id,視頻建立id獲取是否點贊視頻,並獲取建立者的信息。ios
攔截器git
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;@Configurationpublic 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.javagithub
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 和 serviceImpweb
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;@Servicepublic 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.jsspring
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能夠不攔截,分析業務,那些須要登陸後才能夠得到,那些不須要登陸就能夠看到。數據庫