全栈仿抖音小程序--个人主页模块

个人主页

先看前端需要什么?

全栈仿抖音小程序--个人主页模块_第1张图片

好,我知道了!

先把接口写好
在这里插入图片描述

 @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 users = usersMapper.selectOneByExample(userExample);
        return users;
    }

这里我们用了一个 基于Criteria类的条件查询
通过usersMapper.selectOneByExample方法查询是否有相应的数据。

这里的条件是: criteria.andEqualTo(“id”, userId);
id:对应数据库对象字段 userId则是传入的Id

再看Controller

   @PostMapping("/query")
    public JSONResult query(String userId, String fanId) throws Exception {
        if (StringUtils.isBlank(userId)) {
            return JSONResult.errorMsg("用户ID不能为空。。");
        }
        Users userInfo = userService.queryUserInfo(userId);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(userInfo, usersVO);
        usersVO.setFollow(userService.queryIfFollow(userId, fanId));

        return JSONResult.ok(usersVO);

    }

这里为什么传入了两个数据?userId, fanId?
因为我们可以看自己的个人界面也可以看别人的界面,别人的界面是可以关注的。已关注的也可以取消关注

我们返回的是UserVO对象,数据库中查到的是Users对象,我们利用
BeanUtils的copyProperties方法进行基本属性填充

我们还是来详细的看一下UserVO里面的属性吧:


    private boolean isFollow;
    @ApiModelProperty(hidden = true)
    @Id
    private String id;

    @ApiModelProperty(hidden = true)
    private String UserToken;

    /**
     * 用户名
     */
    @ApiModelProperty(value = "用户名", name = "username", example = "cc", required = true)
    private String username;

    /**
     * 密码
     */
    @ApiModelProperty(value = "密码", name = "password", example = "123123", required = true)
    @JsonIgnore
    private String password;

    /**
     * 我的头像,如果没有默认给一张
     */
    @ApiModelProperty(hidden = true)
    private String faceImage;

    /**
     * 昵称
     */
    private String nickname;

    /**
     * 我的粉丝数量
     */
    @ApiModelProperty(hidden = true)
    private Integer fansCounts;

    /**
     * 我关注的人总数
     */
    @ApiModelProperty(hidden = true)
    private Integer followCounts;

    /**
     * 我接受到的赞美/收藏 的数量
     */
    @ApiModelProperty(hidden = true)
    private Integer receiveLikeCounts;

可以看到password字段上有一个@JsonIgnore注解,这里的意思就是传输的时候不要传过去。为了安全。

这个UserVO返回的数据可以把上半部分给填好,那么下半部分呢?
全栈仿抖音小程序--个人主页模块_第2张图片
其实吧。。。就是写三个SQL 然后从数据库把信息拿出来!!!!

自定义一个映射文件 videosMapperCustom .xml 和videosMapperCustom .java接口:

三个接口方法:

    List <VideosVO> queryAllVideos(@Param("videoDesc") String videoDesc,
                                   @Param("userId") String userId);

    List <VideosVO> queryMyFollowVideos(String userId);

    List <VideosVO> queryMyLikeVideos(String userId);

实现(XML里的sql语句)

<select id="queryAllVideos" resultMap="BaseResultMap" parameterType="String">
  
  	select v.*,u.face_image as face_image,u.nickname as nickname from videos v
  	left join users u on u.id = v.user_id
  	where 
  		1 = 1
  		<if test=" videoDesc != null and videoDesc != '' ">
  			and v.video_desc like '%${videoDesc}%'
  		if>
      <if test=" userId != null and userId != '' ">
          and v.user_id = #{userId}
      if>
  		and v.status = 1
  	order by v.create_time desc
  
  select>
  
  	
	<select id="queryMyFollowVideos" resultMap="BaseResultMap" parameterType="String">
		select v.*,u.face_image as face_image,u.nickname as nickname from videos v 
		left join users u on v.user_id = u.id
		where 
			v.user_id in (select uf.user_id from users_fans uf where uf.fan_id = #{userId})
			and v.status = 1
			order by v.create_time desc
	select>
	
	
	<select id="queryMyLikeVideos" resultMap="BaseResultMap" parameterType="String">
		select v.*,u.face_image as face_image,u.nickname as nickname from videos v 
		left join users u on v.user_id = u.id
		where 
			v.id in (select ulv.video_id from users_like_videos ulv where ulv.user_id = #{userId})
			and v.status = 1
			order by v.create_time desc
	select>

三个service里的接口(利用PagedResult 分页查询)

//分页查询
    PagedResult queryMyFollowVideos(String userId, Integer page, int pageSize);

    PagedResult getAllVideos(Videos video, Integer isSaveRecored, Integer page, Integer pageSize);

    PagedResult queryMyLikeVideos(String userId, Integer page, Integer pageSize);
@Transactional(propagation = Propagation.REQUIRED)
   @Override
   public PagedResult getAllVideos(Videos video, Integer isSaveRecored, Integer page, Integer pageSize) {
       //保存热搜词
       String desc = video.getVideoDesc();
       String userId = video.getUserId();
       if (isSaveRecored != null && isSaveRecored == 1) {
           SearchRecords record = new SearchRecords();
           record.setId(sid.nextShort());
           record.setContent(desc);
           searchRecordsMapper.insert(record);
       }

       PageHelper.startPage(page, pageSize);
       List <VideosVO> list = videosMapperCustom.queryAllVideos(desc, userId);

       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 PagedResult queryMyFollowVideos(String userId, Integer page, int pageSize) {
       PageHelper.startPage(page, pageSize);
       List <VideosVO> list = videosMapperCustom.queryMyFollowVideos(userId);

       PageInfo <VideosVO> pageList = new PageInfo <>(list);

       PagedResult pagedResult = new PagedResult();
       pagedResult.setTotal(pageList.getPages());
       pagedResult.setRows(list);
       pagedResult.setPage(page);
       pagedResult.setRecords(pageList.getTotal());

       return pagedResult;
   }
   @Transactional(propagation = Propagation.SUPPORTS)
   @Override
   public PagedResult queryMyLikeVideos(String userId, Integer page, Integer pageSize) {
       PageHelper.startPage(page, pageSize);
       List <VideosVO> list = videosMapperCustom.queryMyLikeVideos(userId);

       PageInfo <VideosVO> pageList = new PageInfo <>(list);

       PagedResult pagedResult = new PagedResult();
       pagedResult.setTotal(pageList.getPages());
       pagedResult.setRows(list);
       pagedResult.setPage(page);
       pagedResult.setRecords(pageList.getTotal());

       return pagedResult;
   }

Controller层:

 /**
     * 我的作品
     *
     * @param video
     * @param isSaveRecored
     * @param page
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/showAll")
    public JSONResult showAll(@RequestBody Videos video, Integer isSaveRecored, Integer page) throws Exception {

        if (page == null) {
            page = 1;
        }
        PagedResult result = videoService.getAllVideos(video, isSaveRecored, page, PAGE_SIZE);
        return JSONResult.ok(result);
    }

    /**
     * @Description: 我关注的人发的视频
     */
    @PostMapping("/showMyFollow")
    public JSONResult showMyFollow(String userId, Integer page) throws Exception {

        if (StringUtils.isBlank(userId)) {
            return JSONResult.ok();
        }

        if (page == null) {
            page = 1;
        }

        int pageSize = 6;

        PagedResult videosList = videoService.queryMyFollowVideos(userId, page, pageSize);

        return JSONResult.ok(videosList);
    }

    /**
     * @Description: 我收藏(点赞)过的视频列表
     */
    @PostMapping("/showMyLike")
    public JSONResult showMyLike(String userId, Integer page, Integer pageSize) throws Exception {

        if (StringUtils.isBlank(userId)) {
            return JSONResult.ok();
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = 6;
        }

        PagedResult videosList = videoService.queryMyLikeVideos(userId, page, pageSize);

        return JSONResult.ok(videosList);
    }

三个方法返回给前端的都是PagedResult 的分页对象内容
我们看一下前端拿到的是什么:
在这里插入图片描述
这个rows里面得就是对应的所有视频的数据
把这个row根据选择的信息绑定到前端的videoList中进行展示

<view hidden='{{myWorkFalg}}'>
    <block wx:for="{{myVideoList}}" >
        <image src='{{serverUrl2}}{{item.coverPath}}' class='videoImage' mode="aspectFill" bindtap='showVideo' data-arrindex='{{index}}'>image>
    block>
view>

<view hidden='{{myLikesFalg}}'>
    <block wx:for="{{likeVideoList}}" >
        <image src='{{serverUrl2}}{{item.coverPath}}' class='videoImage' mode="aspectFill" bindtap='showVideo' data-arrindex='{{index}}'>image>
    block>
view>

<view hidden='{{myFollowFalg}}'>
    <block wx:for="{{followVideoList}}" >
        <image src='{{serverUrl2}}{{item.coverPath}}' class='videoImage' mode="aspectFill" bindtap='showVideo' data-arrindex='{{index}}'>image>
    block>
view>

全栈仿抖音小程序--个人主页模块_第3张图片
OK! GOOD!

你可能感兴趣的:(【开发】全栈仿抖音小程序)