/**
* 属性赋值
* */
private void setModel(User user,Model model){
long currentUserId = getUserId();
long visitUserId = user.getId();
//是否是自己
boolean isSelf = currentUserId == visitUserId;
//两个用户是否已经是好友
boolean isFriend = groupService.isFriend(currentUserId,visitUserId);
Map<String,Object> userMap = new HashMap<>(8);
userMap.put("avatar",user.getAvatar());
userMap.put("name",user.getUserName());
userMap.put("addtime", TimeUtil.formatDate(user.getCreateAt())+" 加入");
if(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);
}
<div class="fly-home" style="background-image: url();">
<input type="hidden" th:value="${user.uid}" id="visitUid"/>
<img src="" th:src="${user.avatar}" th:alt="${user.name}"/>
<h1>
<p th:text="${user.name}"></p>
<i class="iconfont icon-nan"></i>
</h1>
<p class="fly-home-info">
<!--<i class="iconfont icon-zuichun" title="飞吻"></i><span style="color: #FF7200;">67206飞吻</span>-->
<i class="iconfont icon-shijian"></i><span th:text="${user.addtime}"></span>
<!--<i class="iconfont icon-chengshi"></i><span>来自杭州</span>-->
<i class="iconfont icon-qq" th:if="${user.self==false}"></i><a lay-event="addFriend" href="#" rel="external nofollow" title="添加TA为好友" th:if="${user.self==false}">加为好友</a>
</p>
<p class="fly-home-sign" th:text="${user.sign}"></p>
</div>
/**
* 提交好友申请
* */
public JsonResult saveFriendApply(long toId,String remark){
remark = HtmlUtils.htmlEscape(remark);
ContextUser user = ShiroUtil.getCurrentUser();
long userId = Long.parseLong(user.getUserid());
int record = applyRepository.countByToidAndUidAndTypeAndResult(toId,userId,ApplyType.friend,0);
if(record > 0){
return JsonResult.fail("已经申请过");
}
Apply apply = new Apply();
apply.setType(ApplyType.friend);
apply.setToid(toId);
apply.setRemark(remark);
apply.setUid(userId);
apply.setAvatar(user.getAvatar());
apply.setName(user.getUsername());
apply.setRead(false);
apply.setResult(0);
return saveApply(apply);
}
/**
* 获取starter
*/
private static LayimWebsocketStarter getStarter(){
return (LayimWebsocketStarter)SpringUtil.getBean("layimWebsocketStarter");
}
private static ServerGroupContext getServerGroupContext(){
return getStarter().getServerGroupContext();
}
/**
* 获取channelContext
* */
private static ChannelContext getChannelContext(String toId) {
ServerGroupContext context = getServerGroupContext();
//找到用户
ChannelContext channelContext = context.users.find(context, toId);
return channelContext;
}
/**
* 服务端主动推送消息
* */
public static void pushApplyMessage(String toId) {
logger.info("执行到了发送方法:pushApplyMessage");
LayimToClientNoticeMsgBody body = new LayimToClientNoticeMsgBody();
ChannelContext channelContext = getChannelContext(toId);
//先判断是否在线,再去查询数据库,减少查询次数
if (channelContext != null && !channelContext.isClosed()) {
int count = getApplyService().getUnreadMsgCount(Long.parseLong(toId));
body.setCount(count);
push(channelContext, body);
}
}
/**
* 服务端主动推送消息
* */
private static void push(ChannelContext channelContext,Object msg) {
try {
WsResponse response = BodyConvert.getInstance().convertToTextResponse(msg);
Aio.send(channelContext, response);
}catch (IOException ex){
}
}
public class ApplyEvent extends ApplicationEvent {
public ApplyEvent(Object source) {
super(source);
}
private long toid;
public long getToId(){
return toid;
}
public ApplyEvent(Object source, long toId) {
super(source);
this.toid = toId;
}
}
public class ApplyListener implements ApplicationListener<ApplyEvent> {
private Logger logger = LoggerFactory.getLogger(ApplyListener.class);
@Override
public void onApplicationEvent(ApplyEvent applyEvent) {
new Thread(){
public void run(){
Long toId = applyEvent.getToId();
//这里就要调用上文中的推送了
PushUtil.pushApplyMessage(toId.toString());
}
}.start();
}
}
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(LayimApplication.class);
/**
* 这里监听增加listener,listener才会触发
* ApplyListener 是监听好友申请的事件
* */
springApplication.addListeners(new ApplyListener());
springApplication.run(args);
}
/**
* 好友申请
* */
@PostMapping(value = "/apply-friend")
public JsonResult apply(@RequestParam("toid") Long toId,@RequestParam("remark") String remark){
JsonResult result = applyService.saveFriendApply(toId, remark);
//申请成功,发布申请事件,通知 toId处理消息,如果不在线,不会进行处理
if(result.isSuccess()){
applicationContext.publishEvent(new ApplyEvent("apply",toId));
}
return result;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有