From a2bf02e7c8ec3870423f119c53fd9d6c6b95e681 Mon Sep 17 00:00:00 2001 From: toogee Date: Sat, 28 Mar 2020 21:41:07 +0800 Subject: [PATCH] message send --- app/common/model/Message.php | 24 ++++++++++++ app/common/model/MessageTo.php | 23 +++++++++++ app/facade/Jump.php | 13 +++++++ app/facade/Message.php | 15 +++++++ app/index/controller/Article.php | 20 +++++++++- app/index/controller/Index.php | 4 +- app/index/controller/Message.php | 67 +++++++++++++++++++++++++++++--- app/index/controller/User.php | 7 +++- app/install/data/taoler.sql | 26 +++++++++++++ 9 files changed, 189 insertions(+), 10 deletions(-) create mode 100644 app/common/model/Message.php create mode 100644 app/common/model/MessageTo.php create mode 100644 app/facade/Jump.php create mode 100644 app/facade/Message.php diff --git a/app/common/model/Message.php b/app/common/model/Message.php new file mode 100644 index 0000000..0a0f4c4 --- /dev/null +++ b/app/common/model/Message.php @@ -0,0 +1,24 @@ +hasMany('User','user_id','id'); + } + + + +} \ No newline at end of file diff --git a/app/common/model/MessageTo.php b/app/common/model/MessageTo.php new file mode 100644 index 0000000..951e62a --- /dev/null +++ b/app/common/model/MessageTo.php @@ -0,0 +1,23 @@ +hasMany('User','user_id','id'); + } + + + +} \ No newline at end of file diff --git a/app/facade/Jump.php b/app/facade/Jump.php new file mode 100644 index 0000000..eb36ec1 --- /dev/null +++ b/app/facade/Jump.php @@ -0,0 +1,13 @@ +field('id,title,user_id')->where('id',$data['article_id'])->find(); + $title = $article['title']; + $link = '/index/jie/'.$data['article_id'].'.html'; + //@user comment + $preg = "/@([^@\s]*)\s/"; + preg_match($preg,$data['content'],$username); + if(isset($username[1])){ + $receveId = Db::name('user')->whereOr('nickname', $username[1])->whereOr('name', $username[1])->value('id'); + } else { + $receveId = $article['user_id']; + } + $data = ['title'=>$title,'link'=>$link,'user_id'=>$sendId]; + Message::sendMsg($sendId,$receveId,$data); + $res = ['code'=>1, 'msg'=>'留言成功']; - } else{ + } else { $res = ['code'=>0, 'msg'=>'留言失败']; } return json($res); diff --git a/app/index/controller/Index.php b/app/index/controller/Index.php index 4a72708..0a4f82e 100644 --- a/app/index/controller/Index.php +++ b/app/index/controller/Index.php @@ -53,7 +53,7 @@ class Index extends BaseController } //热议文章 - $artHot = Article::field('id,title')->withCount('comments')->where(['status'=>1,'delete_time'=>0])->whereTime('create_time', 'year')->order('comments_count','desc')->limit(10)->select(); + $artHot = Article::field('id,title')->withCount('comments')->where(['status'=>1,'delete_time'=>0])->whereTime('create_time', 'year')->order('comments_count','desc')->limit(10)->withCache(60)->select(); //首页赞助 $ad_index = Cache::get('adindex'); @@ -94,7 +94,7 @@ class Index extends BaseController //回帖榜 public function reply() { - $user = User::withCount('comments')->order('comments_count','desc')->limit(20)->select(); + $user = User::withCount('comments')->order(['comments_count'=>'desc','last_login_time'=>'desc'])->limit(20)->select(); if($user) { $res['status'] = 0; diff --git a/app/index/controller/Message.php b/app/index/controller/Message.php index b517e2d..ff82fed 100644 --- a/app/index/controller/Message.php +++ b/app/index/controller/Message.php @@ -4,17 +4,72 @@ namespace app\index\controller; use app\common\controller\BaseController; use think\facade\Session; use think\facade\Request; -use think\Db; +use think\facade\Db; +use app\facade\Message as MessageApi; class Message extends BaseController { - //消息 - public function nums(){ + //消息数目 + public function nums() + { + $msg = Db::name('message_to')->where('receve_id',Session::get('user_id'))->where(['is_read'=>0,'delete_time'=>0])->select(); - //$res['status'] = 0; - $res=['status' =>0,'count' => 0, 'msg' => 'nums']; + $count = $msg->count(); + if($count){ + $res=['status' =>0,'count' => $count, 'msg' => 'nums']; + } else { + $res=['status' =>0,'count' => 0, 'msg' => $count]; + } + return json($res); + } + + //消息查询 + public function find() + { + $uid = Session::get('user_id'); + $msg = MessageApi::receveMsg($uid); + $count = $msg->count(); + $res = []; + if($count){ + $res = ['status'=>0,'msg'=>'','count'=>$count]; + foreach ($msg as $k => $v){ + $data = ['id'=>$v['id'],'name'=>$v['name'],'content'=>$v['title'],'time'=>date("Y-m-d H:i",$v['create_time']),'link'=>$v['link']]; + $res['rows'][] = $data; + } + } else { + $res = ['status'=>0,'msg'=>'','rows'=>''];; + } + return json($res); + } + + //读消息 + public function read() + { + $id =input('id'); + $msg = Db::name('message_to')->where('id',$id)->save(['is_read'=>1]); + + $res=['status' =>0]; - return $res; + return json($res); + + } + + //消息删除 + public function remove() + { + $uid = Session::get('user_id'); + $id = Request::only(['id']); + if($id['id'] == 'true'){ + $msg = Db::name('message_to')->where(['receve_id'=>$uid,'delete_time'=>0])->useSoftDelete('delete_time',time())->delete(); + } else { + $msg = Db::name('message_to')->where('id',$id['id'])->useSoftDelete('delete_time',time())->delete(); + } + + if($msg){ + $res = ['status'=>0]; + } + return $res; + } } \ No newline at end of file diff --git a/app/index/controller/User.php b/app/index/controller/User.php index c6c119e..311b591 100644 --- a/app/index/controller/User.php +++ b/app/index/controller/User.php @@ -13,6 +13,7 @@ use app\common\model\Article; use app\common\model\Collection; use app\common\model\User as userModel; use think\facade\Config; +use app\facade\Message; class User extends BaseController { @@ -127,7 +128,11 @@ class User extends BaseController public function message() { - return view(); + $uid = Session::get('user_id'); + $msg = Message::receveMsg($uid); + + View::assign('msg',$msg); + return View::fetch(); } //个人页 diff --git a/app/install/data/taoler.sql b/app/install/data/taoler.sql index d0f24cb..0c292ff 100644 --- a/app/install/data/taoler.sql +++ b/app/install/data/taoler.sql @@ -225,6 +225,32 @@ CREATE TABLE `tao_mail_server` ( INSERT INTO `tao_mail_server` VALUES ('1', 'xxxx@aliyun.com', 'smtp.aliyun.com', '25', 'user', '123456', '0'); +DROP TABLE IF EXISTS `tao_message`; +CREATE TABLE `tao_message` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '消息ID', + `title` varchar(255) NOT NULL COMMENT '消息标题', + `content` tinytext NOT NULL COMMENT '消息内容', + `user_id` int(11) NOT NULL COMMENT '发送人ID', + `link` varchar(255) NOT NULL COMMENT '链接', + `create_time` int(11) NOT NULL COMMENT '创建时间', + `update_time` int(11) NOT NULL COMMENT '更新时间', + `delete_time` int(11) NOT NULL COMMENT '删除时间', + PRIMARY KEY (`id`) +) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8; + +DROP TABLE IF EXISTS `tao_message_to`; +CREATE TABLE `tao_message_to` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '消息ID', + `send_id` int(11) NOT NULL COMMENT '发送人ID', + `receve_id` int(11) NOT NULL COMMENT '接收人ID', + `message_id` varchar(255) NOT NULL COMMENT '消息标题', + `is_read` tinyint(1) NOT NULL DEFAULT '0' COMMENT '消息状态', + `create_time` int(11) NOT NULL DEFAULT '0' COMMENT '创建时间', + `update_time` int(11) NOT NULL DEFAULT '0' COMMENT '更新时间', + `delete_time` int(11) NOT NULL DEFAULT '0' COMMENT '删除时间', + PRIMARY KEY (`id`) +) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8; + DROP TABLE IF EXISTS `tao_slider`; CREATE TABLE `tao_slider` (