where(['user_id'=>$this->auth->id,'status'=>0])->count('id'); $content = Db::name('message')->where(['user_id'=>$this->auth->id])->order('id desc')->value('content'); $data['num'] = $num; $data['content'] = $content ? : ''; //系统消息未读数量 $sys_ids = Db::name('message_sys')->column('id'); $all_num = count($sys_ids); $read_num = Db::name('user_messagesys')->where('user_id',$this->auth->id)->where('msg_id','IN',$sys_ids)->count(); $data['msgsys_unread_num'] = $all_num > $read_num ? $all_num - $read_num : 0; //因公告被删除,未读公告没来得及删除,相减可能会负数 $this->success('success', $data); } //我的消息列表 public function mylist(){ $list = Db::name('message')->where('user_id',$this->auth->id)->autopage()->order('id desc')->select(); //读取即为已读 $map = [ 'user_id' => $this->auth->id, 'status' => 0, ]; Db::name('message')->where($map)->update(['status'=>1]); $this->success('success',$list); } /* * 获取系统消息列表 */ public function getmessagesys() { $list = Db::name('message_sys')->autopage()->order('id desc')->select(); Db::startTrans(); //系统消息 //全部未读 $rs = Db::name('user_messagesys')->where('user_id',$this->auth->id)->delete(); if($rs === false){ Db::rollback(); $this->error('操作失败'); } //循环全部已读 $list2 = Db::name('message_sys')->column('id'); if(!empty($list2)){ $newall = []; foreach($list2 as $key => $msg_id){ $newall[] = [ 'user_id' => $this->auth->id, 'msg_id' => $msg_id, ]; } if(!empty($newall)){ $rs = Db::name('user_messagesys')->insertAll($newall); if($rs === false){ Db::rollback(); $this->error('操作失败'); } } } Db::commit(); $this->success("获取成功!",$list); } }