Преглед изворни кода

add: 实现客户端消息协议

joe пре 4 година
родитељ
комит
2e4d3a5708

+ 44 - 3
app/api/controller/user/UserNoticeController.php

@@ -1,17 +1,58 @@
 <?php
 namespace app\api\controller\user;
 
+use app\models\user\UserNotice;
+use app\Request;
+use crmeb\services\UtilService;
+
+/**
+ *
+ * 消息协议 参看文档 docs/messages.md
+ *
+ * Class UserNoticeController
+ * @package app\api\controller\user
+ */
 class UserNoticeController {
 
-    public function messages() {
+    // 获取用户所有消息(分页)
+    public function messages(Request $request) {
+        [$page, $limit] = UtilService::getMore([
+            ['page', 1],
+            ['limit', 20]
+        ], $request, true);
 
+        $uid = $request->uid();
+        return app('json')->successful(UserNotice::getNoticeList($uid, intval($page), intval($limit)));
     }
 
-    public function read() {
+    // 标记已读
+    public function read(Request $request) {
+        list($ids, $read) = UtilService::postMore([
+            ['ids', []],
+            ['read', 1]
+        ], $request, true);
+
+        if (count($ids) <= 0) {
+            return app('json')->fail('未选择');
+        }
+        if ($read == 0 ) {
+            return app('json')->fail('暂不支持');
+        }
+        $uid = $request->uid();
+        UserNotice::seeNotice($uid, $ids);
 
+        return app('json')->successful([]);
     }
 
-    public function del() {
+    // 删除
+    public function del(Request $request) {
+        list($ids) = UtilService::postMore([
+            ['ids', []]
+        ], $request, true);
+
+        $uid =  $request->uid();
+        UserNotice::delNotice($uid, $ids);
 
+        return app('json')->successful([]);
     }
 }

+ 93 - 24
app/models/user/UserNotice.php

@@ -10,48 +10,117 @@ namespace app\models\user;
 use app\admin\model\user\UserNoticeSee;
 use crmeb\traits\ModelTrait;
 use crmeb\basic\BaseModel;
-
+use think\facade\Db;
+use think\facade\Log;
+use think\facade\Config;
 /**
- * TODO 用户通知Model
  * Class UserNotice
  * @package app\models\user
  */
 class UserNotice extends BaseModel
 {
     use ModelTrait;
+
+    /**
+     * 发送系统消息, 一般由管理员后台发出
+     * @param $title
+     * @param $content
+     * @param null $sender
+     * @param string $icon
+     */
+    public static function publishSystemNotice($title, $content, $sender=null, $icon='') {
+        if (!$sender) {
+            $sender = Config::get('app.notice_sender', '运营中心');
+        }
+        self::sendNotice($title, $content, 0, 0, $sender, $icon);
+    }
+
+    /**
+     * 发通知给指定用户, 一般由系统自动发出
+     * @param $uid
+     * @param $title
+     * @param $content
+     * @param string $icon
+     */
+    public static function sendNoticeTo($uid, $title, $content, $icon='') {
+        $sender = Config::get('app.notice_sender', '运营中心');
+        self::sendNotice($title, $content, $uid, 2, $sender, $icon);
+    }
+
+    protected static function sendNotice($title, $content, $uid, $type, $sender, $icon='') {
+        $data['uid'] = $uid;
+        $data['type'] = $type;
+        $data['user'] = $sender;
+        $data['title'] = $title;
+        $data['content'] = $content;
+        $data['add_time'] = time();
+        $data['icon'] = $icon;
+
+        self::create($data);
+    }
+    // 获取未读数
     public static function getNotice($uid){
-        $count_notice = self::whereIn('uid', [$uid, 0])->where("is_send",1)->count();
-        $see_notice = UserNoticeSee::where("uid", $uid)->count();
-        return $count_notice-$see_notice;
+        $row = Db::query("
+            select count(*) as unread from tw_user_notice where id not in (
+	            select nid from tw_user_notice_see where uid=?
+            ) and uid in (?, 0) and del_time=0
+            ", [$uid, $uid]
+        );
+
+        return intval($row[0]['unread']);
     }
 
     /**
+     * 获取消息列表
      * @return array
      */
-    public static function getNoticeList($uid, $page, $limit = 8){
-        $list = self::whereIn('uid', [$uid,0])
-            ->where('is_send', 1)
-            ->field('id,user,title,content,add_time')
-            ->order("add_time desc")
-            ->limit($page*$limit,$limit)->select()->toArray();
-
-        foreach ($list as $key => $value) {
-            $list[$key]["add_time"] = date("Y-m-d H:i:s",$value["add_time"]);
-            $list[$key]["is_see"] = 0; //UserNoticeSee::where("uid",$uid)->where("nid",$value["id"])->count() > 0 ? 1 : 0;
-        }
-        $data["list"] = $list;
+    public static function getNoticeList($uid, $page, $limit = 20){
+        $data = Db::query("
+            select n.id, n.icon, n.`user` as `from`, n.title as subject, n.content as `body`, n.add_time as ts, n.`type`, count(s.id) as `read`
+            from tw_user_notice n right join tw_user_notice_see s on n.uid = s.uid 
+            where n.uid in (?, 0) and n.del_time=0
+            group by s.id 
+            order by n.add_time desc limit ?,?
+        ", [$uid, ($page-1)*$limit, $limit]);
+
         return $data;
     }
 
     /**
-     * @return array
+     * 用户删除消息, 软删除
+     * @param $uid
+     * @param $ids
+     */
+    public static function delNotice($uid, $ids) {
+        $model = new self;
+        $model->where('uid', $uid)->whereIn('id', $ids)->update(['del_time'=>time()]);
+    }
+
+    /**
+     * 设为已读
+     * @param $uid
+     * @param $nids
      */
-    public static function seeNotice($uid, $nid){
-        if(UserNoticeSee::where("uid", $uid)->where("nid", $nid)->count() <= 0){
-            $data["nid"] = $nid;
-            $data["uid"] = $uid;
-            $data["add_time"] = time();
-            UserNoticeSee::create($data);
+    public static function seeNotice($uid, $nids) {
+        foreach ($nids as $nid) {
+            try {
+                $data["nid"] = $nid;
+                $data["uid"] = $uid;
+                $data["add_time"] = time();
+                UserNoticeSee::create($data);
+            } catch(\Exception $e) {
+                Log::warning("seeNotice exception.sql=".UserNoticeSee::getLastSql());
+            }
+        }
+    }
+
+    public static function unseeNotice($uid, $nids) {
+        foreach ($nids as $nid) {
+            try{
+                UserNoticeSee::where('uid', $uid)->where('nid', $nid)->delete();
+            }catch (\Exception $e){
+                Log::warning("unseeNotice exception.sql=". UserNoticeSee::getLastSql());
+            }
         }
     }
 }

+ 2 - 0
config/app.php

@@ -46,4 +46,6 @@ return [
     'redis_robot_msg_key' => 'qywechatpush',
     // 亚里士多得
     'qy_weixin_robot_aristotle' => 'a17b9d56-b566-404b-904e-4476b95dc8d7',
+    // 自动发消息的消息发送者
+    'notice_sender' => '运营管理员'
 ];

+ 6 - 4
public/install/crmeb.sql

@@ -7521,15 +7521,16 @@ CREATE TABLE IF NOT EXISTS `eb_user_level` (
 CREATE TABLE IF NOT EXISTS `eb_user_notice` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `uid` int(10) NOT NULL DEFAULT 0 COMMENT '接收消息的用户id(0 系统消息 -1 会员消息 + 指定用户消息)',
-  `type` tinyint(1) NOT NULL DEFAULT '1' COMMENT '消息通知类型(1:系统消息;2:用户通知)',
+  `type` tinyint(1) NOT NULL DEFAULT 0 COMMENT '消息通知类型(0:系统消息;1:分组消息;2:用户消息)',
   `user` varchar(20) NOT NULL DEFAULT '' COMMENT '发送人',
   `title` varchar(20) NOT NULL COMMENT '通知消息的标题信息',
   `content` varchar(500) NOT NULL DEFAULT '' COMMENT '通知消息的内容',
   `add_time` int(11) NOT NULL DEFAULT '0' COMMENT '通知消息发送的时间',
-  `is_send` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否发送(0:未发送;1:已发送)',
-  `send_time` int(11) NOT NULL COMMENT '发送时间',
+  `del_time` int(11) NOT NULL DEFAULT 0 COMMENT '删除时间 0 表示未删除',
+  `icon` varchar(255) NOT NULL DEFAULT '' COMMENT '图标',
   PRIMARY KEY (`id`) USING BTREE,
-  KEY `uid` (`uid`) USING BTREE
+  KEY `uid_del_time` (`uid`, `del_time`) USING BTREE,
+  KEY `add_time` (`add_time`) USING BTREE
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户通知表' AUTO_INCREMENT=1 ;
 
 -- --------------------------------------------------------
@@ -7544,6 +7545,7 @@ CREATE TABLE IF NOT EXISTS `eb_user_notice_see` (
   `uid` int(11) NOT NULL DEFAULT '0' COMMENT '查看通知的用户id',
   `add_time` int(11) NOT NULL DEFAULT '0' COMMENT '查看通知的时间',
   PRIMARY KEY (`id`) USING BTREE,
+  UNIQUE KEY `nid_uid` (`nid`,`uid`),
   KEY `uid` (`uid`) USING BTREE
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户通知发送记录表' AUTO_INCREMENT=1 ;
 

+ 1 - 1
route/api/route.php

@@ -38,7 +38,7 @@ Route::get('user/notifications', 'user.UserNotificationController/snapshot')->na
 
 Route::get('user/messages', 'user.UserNoticeController/messages')->name('userMessages');
 Route::post('user/messages', 'user.UserNoticeController/read')->name('userRead');
-Route::post('user/message/del', 'user.UserNoticeController/del')->name('userDel');
+Route::post('user/messages/del', 'user.UserNoticeController/del')->name('userDel');
 /**
  * 挖矿类 api
  */