lizhen_gitee 1 سال پیش
والد
کامیت
ef50c9a43e
1فایلهای تغییر یافته به همراه109 افزوده شده و 45 حذف شده
  1. 109 45
      application/api/controller/Message.php

+ 109 - 45
application/api/controller/Message.php

@@ -12,71 +12,135 @@ class Message extends Api
     protected $noNeedLogin = [];
     protected $noNeedRight = ['*'];
 
-        /*
-     * 获取系统消息列表
-     */
-    public function getMessageSys() {
-
-        $flag = input("flag",1,"intval"); //标识:1=只取一条,0=全部
-        $type = input("type",1);
-        $obj = Db::name('message_sys')->where('type',$type)->order("id","desc")->autopage();
-        if($flag == 1) {
-            $list = $obj->find();
-            $list || $list = [];
-            $list && $list["createtime"] = get_last_time($list["createtime"]);
-        } else {
-            $list = $obj->select();
-            if($list) foreach($list as $k => &$v) {
-                $v["createtime"] = get_last_time($v["createtime"]);
+    //消息页信息
+    public function index(){
+        $rs = [
+            'msg_first'      => Db::name('message')->where('user_id',$this->auth->id)->order('id desc')->find(),
+            'msg_unread_num' => Db::name('message')->where('user_id',$this->auth->id)->where('status',0)->count(),
+            'msgsys_first'   => Db::name('message_sys')->order('id desc')->find(),
+        ];
+
+        //系统消息未读数量
+        $all_num = Db::name('message_sys')->count();
+        $read_num = Db::name('user_messagesys')->where('user_id',$this->auth->id)->count();
+        $rs['msgsys_unread_num'] = abs($all_num - $read_num);
+
+        $this->success(1,$rs);
+    }
+
+    //个人消息全部改为已读
+    public function message_read(){
+        Db::startTrans();
+        //读取即为已读
+        $map = [
+            'user_id'  => $this->auth->id,
+            'status'   => 0,
+        ];
+        $rs = Db::name('message')->where($map)->update(['status'=>1]);
+        if($rs === false){
+            Db::rollback();
+            $this->error('操作失败');
+        }
+
+
+        //系统消息
+        //全部未读
+        $rs = Db::name('user_messagesys')->where('user_id',$this->auth->id)->delete();
+        if($rs === false){
+            Db::rollback();
+            $this->error('操作失败');
+        }
+
+        //循环全部已读
+        $list = Db::name('message_sys')->column('id');
+        if(!empty($list)){
+            $newall = [];
+            foreach($list 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('操作失败');
+                }
             }
         }
 
-        $this->success("获取成功!",$list);
+        Db::commit();
+        $this->success();
+    }
 
+    //我的个人消息列表
+    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 getMessage() {
+    public function getmessagesys() {
 
+        $list = Db::name('message_sys')->autopage()->order('id desc')->select();
 
-        $flag = $this->request->request("flag",1,"intval"); //标识:1=只取一条,0=全部
-        $user_id = $this->auth->id;
+        Db::startTrans();
+        //系统消息
+        //全部未读
+        $rs = Db::name('user_messagesys')->where('user_id',$this->auth->id)->delete();
+        if($rs === false){
+            Db::rollback();
+            $this->error('操作失败');
+        }
 
-        $obj = Db::name('message')->where(["user_id"=>$user_id])->order("createtime","desc")->autopage();
-        if($flag == 1) {
-            $list = $obj->find();
-            $list || $list = [];
-            $list && $list["createtime"] = get_last_time($list["createtime"]);
-        } else {
-            $list = $obj->select();
-            if($list) foreach($list as $k => &$v) {
-                $v["createtime"] = get_last_time($v["createtime"]);
+        //循环全部已读
+        $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);
-
     }
 
-    /**
-     * 删除个人消息
-     */
-    public function delMessage() {
-        $id = $this->request->request("id",0,"intval"); //消息ID
-        if($id <= 0) {
-            $this->error("参数传入错误!");
-        }
-        $res = \app\common\model\Message::where(["id"=>$id,"user_id"=>$this->auth->id])->delete();
+    //最新一条房间内公告
+    public function getmessageroom() {
 
-        if($res) {
-            $this->success("删除成功!");
-        } else {
-            $this->error("删除失败!");
-        }
+        $info = Db::name('message_room')->order('id desc')->find();
+
+        $this->success("获取成功!",$info);
     }
 
 
 
+
+
+
+
+
+
+
 }