作者 郭盛
1 个管道 的构建 通过 耗费 2 秒

修改平台消息

@@ -31,5 +31,39 @@ class Message extends Backend @@ -31,5 +31,39 @@ class Message extends Backend
31 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 31 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
32 */ 32 */
33 33
  34 + /**
  35 + * 查看
  36 + */
  37 + public function index()
  38 + {
  39 + //设置过滤方法
  40 + $this->request->filter(['strip_tags']);
  41 + if ($this->request->isAjax()) {
  42 + //如果发送的来源是Selectpage,则转发到Selectpage
  43 + if ($this->request->request('keyField')) {
  44 + return $this->selectpage();
  45 + }
  46 + list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47 + $total = $this->model
  48 + ->where($where)
  49 + ->where('user_id','null')
  50 + ->order($sort, $order)
  51 + ->count();
  52 +
  53 + $list = $this->model
  54 + ->where($where)
  55 + ->where('user_id','null')
  56 + ->order($sort, $order)
  57 + ->limit($offset, $limit)
  58 + ->select();
  59 +
  60 + $list = collection($list)->toArray();
  61 + $result = array("total" => $total, "rows" => $list);
  62 +
  63 + return json($result);
  64 + }
  65 + return $this->view->fetch();
  66 + }
  67 +
34 68
35 } 69 }
@@ -49,14 +49,27 @@ class Message extends Api @@ -49,14 +49,27 @@ class Message extends Api
49 ->whereOr('user_id','null') 49 ->whereOr('user_id','null')
50 ->order('is_read desc,createtime desc') 50 ->order('is_read desc,createtime desc')
51 ->select(); 51 ->select();
52 - $data['num'] = Db::name('message') 52 +
  53 + //查询出所有的平台信息
  54 + $read = Db::name('message')->where('user_id','null')->select();
  55 + //循环判断平台消息是否已读
  56 + $nums = 0;
  57 + foreach ($read as $val){
  58 + $info = Db::name('read')->where('user_id',$user_id)->where('message_id',$val['id'])->find();
  59 + if(empty($info)){
  60 + $nums += 1;
  61 + }
  62 + }
  63 + $num = Db::name('message')
53 ->where('is_read',0) 64 ->where('is_read',0)
54 - ->where(function ($query)use($user_id){  
55 - $query->where('user_id',$user_id)  
56 - ->whereOr('user_id','null');  
57 - })  
58 - ->order('is_read desc,createtime desc') 65 + ->where('user_id',$user_id)
  66 +// ->where(function ($query)use($user_id){
  67 +// $query->where('user_id',$user_id)
  68 +// ->whereOr('user_id','null');
  69 +// })
59 ->count(); 70 ->count();
  71 + $data['num'] = $num + $nums;
  72 + //通知数量
60 $data['total'] = Db::name('message') 73 $data['total'] = Db::name('message')
61 ->where('user_id',$user_id) 74 ->where('user_id',$user_id)
62 ->whereOr('user_id','null') 75 ->whereOr('user_id','null')
@@ -103,16 +116,22 @@ class Message extends Api @@ -103,16 +116,22 @@ class Message extends Api
103 $read['user_id'] = $user_id; 116 $read['user_id'] = $user_id;
104 $read['message_id'] = $id; 117 $read['message_id'] = $id;
105 $read['createtime'] = time(); 118 $read['createtime'] = time();
  119 + //判断该记录是否已经添加过了
  120 + $have = Db::name('read')->where('user_id',$read['user_id'])->where('message_id',$read['message_id'])->find();
  121 + if(empty($have)){
  122 + //没有添加过情况
106 Db::name('read')->insertGetId($read); 123 Db::name('read')->insertGetId($read);
  124 + }
107 $data = Db::name('message') 125 $data = Db::name('message')
108 ->where('id',$id) 126 ->where('id',$id)
  127 + ->field('id,title,content,createtime')
109 ->find(); 128 ->find();
110 - Db::name('message')->where('id',$id)->update(['is_read'=>1]);  
111 $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']); 129 $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
112 }else{ 130 }else{
113 //不是 131 //不是
114 $data = Db::name('message') 132 $data = Db::name('message')
115 ->where('id',$id) 133 ->where('id',$id)
  134 + ->field('id,title,content,createtime')
116 ->find(); 135 ->find();
117 Db::name('message')->where('id',$id)->update(['is_read'=>1]); 136 Db::name('message')->where('id',$id)->update(['is_read'=>1]);
118 $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']); 137 $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);