前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >「小程序JAVA实战」小程序的举报功能开发(68)

「小程序JAVA实战」小程序的举报功能开发(68)

作者头像
IT架构圈
发布2019-07-08 10:22:53
8490
发布2019-07-08 10:22:53
举报
文章被收录于专栏:IT架构圈

通过点击举报按钮,跳转到举报页面完成举报操作。

后台开发

获取发布人的userId,videoId,创建者的Id

  • controller

UserController.java

代码语言:javascript
复制
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.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.Users;
import com.idig8.pojo.UsersReport;
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, String fanId) {
        if (StringUtils.isBlank(userId)) {
            return JSONResult.errorMsg("用户id不能为空...");
        }

        Users user = userService.queryUserId(userId);
        UsersVO usersVO= new UsersVO();
        BeanUtils.copyProperties(user, usersVO);

        usersVO.setFollow(userService.queryIfFollow(userId, fanId));
        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);
    }


    @PostMapping("/beyourfans")
    public JSONResult beyourfans(String userId, String fanId) throws Exception {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(fanId)) {
            return JSONResult.errorMsg("");
        }

        userService.saveUserFanRelation(userId, fanId);

        return JSONResult.ok("关注成功...");
    }

    @PostMapping("/dontbeyourfans")
    public JSONResult dontbeyourfans(String userId, String fanId) throws Exception {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(fanId)) {
            return JSONResult.errorMsg("");
        }

        userService.deleteUserFanRelation(userId, fanId);

        return JSONResult.ok("取消关注成功...");
    }

    @PostMapping("/reportUser")
    public JSONResult reportUser(@RequestBody UsersReport usersReport) throws Exception {

        // 保存举报信息
        userService.reportUser(usersReport);

        return JSONResult.errorMsg("举报成功...有你平台变得更美好...");
    }


}

···

* service
UserService.java

java package com.idig8.service;

import com.idig8.pojo.Users; import com.idig8.pojo.UsersReport;

public interface UserService {

代码语言:javascript
复制
/**
 * 判断用户名是否存在
 * @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);

/**
 * @Description: 增加用户和粉丝的关系
 */
public void saveUserFanRelation(String userId, String fanId);

/**
 * @Description: 删除用户和粉丝的关系
 */
public void deleteUserFanRelation(String userId, String fanId);

/**
 * @Description: 查询用户是否关注
 */
public boolean queryIfFollow(String userId, String fanId);

/**
 * @Description: 举报用户
 */
public void reportUser(UsersReport userReport);

}

代码语言:javascript
复制
* service
UserServiceImpl.java

java package com.idig8.service.Impl;

import java.util.Date; 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.UsersFansMapper; import com.idig8.mapper.UsersLikeVideosMapper; import com.idig8.mapper.UsersMapper; import com.idig8.mapper.UsersReportMapper; import com.idig8.pojo.Users; import com.idig8.pojo.UsersFans; import com.idig8.pojo.UsersLikeVideos; import com.idig8.pojo.UsersReport; 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 {

代码语言:javascript
复制
@Autowired
private UsersMapper usersMapper;

@Autowired
private UsersLikeVideosMapper usersLikeVideosMapper;

@Autowired
private UsersMapper userMapper;

@Autowired
private Sid sid;

@Autowired
private UsersFansMapper usersFansMapper;

@Autowired
private UsersReportMapper usersReportMapper;

@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;
}

@Transactional(propagation = Propagation.REQUIRED)
@Override
public void saveUserFanRelation(String userId, String fanId) {

    String relId = sid.nextShort();

    UsersFans userFan = new UsersFans();
    userFan.setId(relId);
    userFan.setUserId(userId);
    userFan.setFanId(fanId);

    usersFansMapper.insert(userFan);

    userMapper.addFansCount(userId);
    userMapper.addFollersCount(fanId);

}

@Transactional(propagation = Propagation.REQUIRED)
@Override
public void deleteUserFanRelation(String userId, String fanId) {

    Example example = new Example(UsersFans.class);
    Criteria criteria = example.createCriteria();

    criteria.andEqualTo("userId", userId);
    criteria.andEqualTo("fanId", fanId);

    usersFansMapper.deleteByExample(example);

    userMapper.reduceFansCount(userId);
    userMapper.reduceFollersCount(fanId);

}


@Override
public boolean queryIfFollow(String userId, String fanId) {

    Example example = new Example(UsersFans.class);
    Criteria criteria = example.createCriteria();

    criteria.andEqualTo("userId", userId);
    criteria.andEqualTo("fanId", fanId);

    List<UsersFans> list = usersFansMapper.selectByExample(example);

    if (list != null && !list.isEmpty() && list.size() > 0) {
        return true;
    }

    return false;
}

@Transactional(propagation = Propagation.REQUIRED)
@Override
public void reportUser(UsersReport userReport) {

    String urId = sid.nextShort();
    userReport.setId(urId);
    userReport.setCreateDate(new Date());

    usersReportMapper.insert(userReport);
}

}

代码语言:javascript
复制
### 小程序开发

* videoInfo
videoInfo.js

javascript 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 })

代码语言:javascript
复制
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(){

代码语言:javascript
复制
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();

代码语言:javascript
复制
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();

代码语言:javascript
复制
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,
      })
    }
  })

}

}, showPublisher:function(){ var me = this; var userInfo = app.getGlobalUserInfo();

代码语言:javascript
复制
var videoInfo = me.data.videoInfo;
var realUrl = '../mine/mine#publisherId@' + videoInfo.userId;
if (userInfo.id == '' || userInfo.id == undefined) {
  wx.navigateTo({
    url: '../userLogin/userLogin?realUrl=' + realUrl,
  })
} else {
  wx.navigateTo({
    url: '../mine/mine?publisherId=' + videoInfo.userId,
  })

}

}, shareMe:function(){ var me = this; var user = app.getGlobalUserInfo(); wx.showActionSheet({ itemList: ["下载到本地","举报用户","分享到好友"], success:function(res){ if (res.tapIndex==0){

代码语言:javascript
复制
} else if (res.tapIndex==1){
      // 举报
      var videoInfo = JSON.stringify(me.data.videoInfo);
      var realUrl = '../videoInfo/videoInfo#videoInfo@' + videoInfo;

      if (user == null || user == undefined || user == '') {
        wx.navigateTo({
          url: '../userLogin/userLogin?realUrl=' + realUrl,
        })
      } else {
        var publishUserId = me.data.videoInfo.userId;
        var videoId = me.data.videoInfo.id;
        var currentUserId = user.id;
        wx.navigateTo({
          url: '../report/report?videoId=' + videoId + "&publishUserId=" + publishUserId
        })
      }

    } else{

    }
  }
})

} })

代码语言:javascript
复制
* report

javacript const app = getApp()

Page({ data: { reasonType: "请选择原因", reportReasonArray: app.reportReasonArray, publishUserId:"", videoId:"" },

代码语言:javascript
复制
onLoad:function(params) {
  var me = this;

  var videoId = params.videoId;
  var publishUserId = params.publishUserId;

  me.setData({
    publishUserId: publishUserId,
    videoId: videoId
  });
},

changeMe:function(e) {
  var me = this;

  var index = e.detail.value;
  var reasonType = app.reportReasonArray[index];

  me.setData({
    reasonType: reasonType
  });
},

submitReport:function(e) {
  var me = this;

  var reasonIndex = e.detail.value.reasonIndex;
  var reasonContent = e.detail.value.reasonContent;

  var user = app.getGlobalUserInfo();
  var currentUserId = user.id;

  if (reasonIndex == null || reasonIndex == '' || reasonIndex == undefined) {
    wx.showToast({
      title: '选择举报理由',
      icon: "none"
    })
    return;
  }

  var serverUrl = app.serverUrl;
  wx.request({
    url: serverUrl + '/user/reportUser',
    method: 'POST',
    data: {
      dealUserId: me.data.publishUserId,
      dealVideoId: me.data.videoId,
      title: app.reportReasonArray[reasonIndex],
      content:reasonContent,
      userid: currentUserId
    },
    header: {
      'content-type': 'application/json', // 默认值
      'headerUserId': user.id,
      'headerUserToken': user.userToken
    },
    success:function(res) {
      wx.showToast({
        title: res.data.msg,
        duration: 2000,
        icon: 'none',
        success: function() {
          wx.navigateBack();
        }
      })
    }

  })

}

})

代码语言:javascript
复制

html

代码语言:javascript
复制
<form bindsubmit='submitReport'>

    <view class='container-reason'>
        <label class='label'>举报理由:</label>
        <picker name="reasonIndex" value="{{index}}" range="{{reportReasonArray}}" bindchange="changeMe">
            <view class="picker">
                {{reasonType}}
            </view>
        </picker>
    </view>

    <view class='container-content'>
        <label class='label'>举报描述(选填):</label>
        <textarea name="reasonContent" class='content' placeholder='请详细说明举报原因...'>
        </textarea>
    </view>

    <view class='container-tips'>
        <image src='../../resource/images/report_face.png' class='report-face'></image>
        收到举报后,我们会在12小时内处理,感谢您的举报,互联网环境因你更美好!~~
    </view>

    <view>
        <button class="submitBtn" type="" form-type='submit'>提  交</button>
    </view>

</form>
代码语言:javascript
复制

css page { background-color: #0e0f1a; /* background-color: red; */ }

.container-reason { display: flex; color: whitesmoke; padding: 15px; background-color: #161823; }

.label { /* margin-left: 15px; */ margin-right: 15px; color: white; }

.container-content { margin-top: 10px; color: whitesmoke; padding: 15px; background-color: #161823; }

.content { margin-top: 10px; padding: 10px; color: white; background-color: #242630; width: 95%; }

.container-tips{ padding: 25px; color: #56575e; font-size: 15px; }

.report-face{ width:12px; height:13px; }

.submitBtn { width: 80%; margin-top: 10px; background-color: #fe2c55; color: white; }

代码语言:javascript
复制
* app.js

javascript //app.js App({ serverUrl:"http://127.0.0.1:8081/", userInfo:null, setGlobalUserInfo:function(user){ wx.setStorageSync("userInfo", user); }, getGlobalUserInfo: function () { return wx.getStorageSync("userInfo"); }, reportReasonArray: [ "色情低俗", "政治敏感", "涉嫌诈骗", "辱骂谩骂", "广告垃圾", "诱导分享", "引人不适", "过于暴力", "违法违纪", "其它原因" ] }) ```

PS:举报功能已经开发完毕,后面这几次很少语言描述,基本的业务逻辑想下就通了。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-01-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 编程坑太多 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 后台开发
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档