小编给大家分享一下SpringBoot + LayIM + t-io如何实现好友申请通知,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获、下面让我们一起去了解一下吧!
<强>简介强>
加好友,大家用过QQ都知道,无非是发起好友申请,对方收到消息通知,然后处理,不过,本篇只讲前半部分,消息通知的处理留到下一篇去讲。因为内容有点多,怕是一时半会消化不了。在介绍主体流程之前,先给大家介绍一下准备工作。
<强>准备工作强>
首先,为了让数据更贴近实战,所以我用了比较“真”实的用户数据。结合模飞板,完善了用户中心头部的用户信息的数据绑定。数据绑定部分判断了是否已经是好友,来决定是否出现“加为好友”的按钮。示例如下,当用户自己看到自己的主页时,是这样的:
,
看到非好友的用户主页,是这样的:
,
绑定数据部分,简单给大家介绍一下,就是用thymleaf模板绑定。后台访问页面的时候,将模型赋值即可。
/* * ,*属性赋值 *,*/,private void  setModel (User 用户,Model 模型){ ,long currentUserId =, getUserId (); ,long visitUserId =, user.getId (); ,//是否是自己 ,boolean isSelf =, currentUserId ==, visitUserId; ,//两个用户是否已经是好友 ,boolean isFriend =, groupService.isFriend (currentUserId visitUserId); ,Map<字符串,Object> userMap =, new HashMap<祝辞(8); ,userMap.put (“avatar" user.getAvatar ()); ,userMap.put (“name" user.getUserName ()); ,userMap.put (“addtime", TimeUtil.formatDate (user.getCreateAt()) +“,加入“); ,如果(user.getSign ()==null | | user.getSign () . length ()==0), { userMap.put才能(“sign",,““); ,}else { userMap.put才能(“sign",,“(“, +, user.getSign (), +,“)“); ,} ,userMap.put (“uid" user.getId ()); ,userMap.put (“self" isSelf | |, isFriend); ,model.addAttribute (“user" userMap); 以前,}>然后页面上,将模型中的数据取出来。
& lt; div 类=癴ly-home",在 ,& lt; input 类型=癶idden" https://www.yisu.com/zixun/, th:价值=" $ {user.uid} " id=" visitUid "/>
<我类=" iconfont icon-nan ">
67206飞吻 - -> <我类=" iconfont icon-shijian "> 来自杭州 - -> <我类=" iconfont icon-qq“th:如果=" ${用户。自我==false} ">
加为好友>
好的,以上就是简单的准备工作。想了解详情代码的可以去文末的github地址去搜寻。
<>强发起好友申请强>
我们先根据layim的业务分析。首先,要知道我们要加谁(toId)为好友。然后在加上一个备注(备注)。这些东西交给后台就好了。为了避免连表查询,对于系统消息的存储我做了用户名和用户头像的冗余。表主要包含字段:用户ID,用户头像,用户名,被申请用户ID,申请时间,申请类型,备注,已读等其他属性。
所以,发起好友申请就很简单了。就是一个添加功能,前端传的就是被申请人用户ID和申请备注,后端组织数据插入到数据库,代码如下:
/* * ,*提交好友申请 *,*/,public JsonResult  saveFriendApply (long toId String 的话){=,,remark  HtmlUtils.htmlEscape(备注); ,ContextUser user =, ShiroUtil.getCurrentUser (); ,long userId =, Long.parseLong (user.getUserid ()); ,int record =, applyRepository.countByToidAndUidAndTypeAndResult (toId, userId ApplyType.friend 0); ,如果(record 祝辞,0){ return 才能;JsonResult.fail(“已经申请过“); ,} ,Apply Apply =, new 应用(); ,apply.setType (ApplyType.friend); ,apply.setToid (toId); ,apply.setRemark(备注); ,apply.setUid (userId); ,apply.setAvatar (user.getAvatar ()); ,apply.setName (user.getUsername ()); ,apply.setRead(假); ,apply.setResult (0); ,return saveApply(应用); 以前,}SpringBoot + LayIM + t-io如何实现好友申请通知