Springboot+Vue实现在线聊天室项目-修改头像、添加好友接口的实现

Springboot+Vue实现在线聊天室项目

该聊天室为大二上学期计算机网络大作业,并且是本人第一次使用vue实现前后端分离的项目,前端架构尚未熟悉可能会出现一些不妥之处,还请大佬们指出。(本文章写于项目整体完成上线之后,所以一些细节并未写出)

# Springboot+Vue实现在线聊天室项目目录

修改头像接口
Controller
   @ResponseBody
    @RequestMapping({"/updateMyImg"})
    public Result updateMyImg(String img) {
        return this.userService.updateMyImg(img);
    }

Service
    public Result updateMyImg(String img) {
        Result result = new Result();
        int uid = getUserIdCurrent();
        try {
            this.userDao.updateMyImg(uid,img);
            this.userDao.updateMyImg2(uid,img);
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(403);
            result.setMsg("修改失败,出现异常");
            return result;
        }
        result.setStatus(200);
        result.setMsg("修改成功");
        return result;
    }

这里,getUserIdCurrent()是上一章所说的通过session获取当前用户的principal和id

添加好友接口
发送请求
 @ResponseBody
    @RequestMapping({"/sendFriendRequest"})
    public Result sendFriendRequest(int toUid) {
        return this.userService.sendFriendRequest(toUid);
    }

public Result sendFriendRequest(int toUid) {
        Result result = new Result();
        int fromUid = getUserIdCurrent();
        if (this.userDao.getInfor(fromUid, toUid) != null) {
            String msg = this.userDao.getInfor(fromUid, toUid).getFinalMsg();
            if (msg.equals(""))
                msg = "尚未响应";
            result.setMsg("好友申请已存在,对方"+ msg);
                    result.setStatus(200);
            return result;
        }
        try {
            this.userDao.insertInfor(fromUid, toUid);
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(403);
            result.setMsg("发送失败,出现异常");
        }
        result.setStatus(200);
        result.setMsg("发送成功");
        return result;
    }
同意他人的好友请求
@ResponseBody
    @RequestMapping({"/resInfor"})
    public Result resInfor(int fromUid, String finalMsg) {
        return this.userService.resInfor(fromUid, finalMsg);
    }

 public Result resInfor(int fromUid, String finalMsg) {
        Result result = new Result();
        int toUid = getUserIdCurrent();
        try {
            this.userDao.updateInfor(fromUid, toUid, finalMsg);
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(403);
            result.setMsg("出现异常");
            return result;
        }
        result.setStatus(200);
        result.setMsg("成功");
        int id1 = (fromUid > toUid) ? toUid : fromUid;
        int id2 = (fromUid > toUid) ? fromUid : toUid;
        String name = "|" + this.userDao.getMyName(id1) + "|" + this.userDao.getMyName(id2) + "|";
        if (this.userDao.getSmallRoom(name) != null) {
            result.setMsg("成功,房间已存在");
            return result;
        }
        this.userDao.addFriend(fromUid, toUid);
        this.userDao.addFriend(toUid, fromUid);
        String img = "|" + this.userDao.getMyImg(id1) + "|" + this.userDao.getMyImg(id2) + "|";
        String uids = "|" + id1 + "|" + id2 + "|";
        System.out.println("img:" + img + "uids:" + uids + "name:" + name);
        this.userDao.insertSmallRoom(name, uids, img, new Date());
        return result;
    }

这里奇奇怪怪的字符串拼接是因为数据库表的设计是这样的…
在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值