message send

This commit is contained in:
toogee 2020-03-28 21:41:07 +08:00
parent 6455e9f7ef
commit a2bf02e7c8
9 changed files with 189 additions and 10 deletions

View File

@ -0,0 +1,24 @@
<?php
namespace app\common\model;
use think\Model;
use think\model\concern\SoftDelete;
use think\Db;
class Message extends Model
{
protected $pk = 'id'; //主键
protected $autoWriteTimestamp = true; //开启自动时间戳
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
//用户关联评论
public function user()
{
return $this->hasMany('User','user_id','id');
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace app\common\model;
use think\Model;
use think\model\concern\SoftDelete;
use think\Db;
class MessageTo extends Model
{
protected $pk = 'id'; //主键
protected $autoWriteTimestamp = true; //开启自动时间戳
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
//用户关联评论
public function user()
{
return $this->hasMany('User','user_id','id');
}
}

13
app/facade/Jump.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace app\facade;
use think\Facade;
class Jump extends Facade
{
protected static function getFacadeClass()
{
return 'app\api\controller\Jump';
}
}

15
app/facade/Message.php Normal file
View File

@ -0,0 +1,15 @@
<?php
namespace app\facade;
use think\Facade;
class Message extends Facade
{
protected static function getFacadeClass()
{
return 'app\api\controller\Message';
}
}

View File

@ -11,9 +11,11 @@ use app\common\model\User;
use app\common\model\Comment; use app\common\model\Comment;
use app\common\model\Collection; use app\common\model\Collection;
use app\common\model\Article as ArticleModel; use app\common\model\Article as ArticleModel;
use app\facade\Message;
use think\exception\ValidateException; use think\exception\ValidateException;
use think\facade\Config; use think\facade\Config;
class Article extends BaseController class Article extends BaseController
{ {
protected $middleware = [ protected $middleware = [
@ -150,11 +152,27 @@ class Article extends BaseController
//if (Request::isAjax()){ //if (Request::isAjax()){
//获取评论 //获取评论
$data = Request::only(['content','article_id','user_id']); $data = Request::only(['content','article_id','user_id']);
$sendId = $data['user_id'];
//用户留言存入数据库 //用户留言存入数据库
if (Comment::create($data)) { if (Comment::create($data)) {
//站内信
$article = Db::name('article')->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'=>'留言成功']; $res = ['code'=>1, 'msg'=>'留言成功'];
} else{ } else {
$res = ['code'=>0, 'msg'=>'留言失败']; $res = ['code'=>0, 'msg'=>'留言失败'];
} }
return json($res); return json($res);

View File

@ -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'); $ad_index = Cache::get('adindex');
@ -94,7 +94,7 @@ class Index extends BaseController
//回帖榜 //回帖榜
public function reply() 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) if($user)
{ {
$res['status'] = 0; $res['status'] = 0;

View File

@ -4,17 +4,72 @@ namespace app\index\controller;
use app\common\controller\BaseController; use app\common\controller\BaseController;
use think\facade\Session; use think\facade\Session;
use think\facade\Request; use think\facade\Request;
use think\Db; use think\facade\Db;
use app\facade\Message as MessageApi;
class Message extends BaseController 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; $count = $msg->count();
$res=['status' =>0,'count' => 0, 'msg' => 'nums']; 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;
} }
} }

View File

@ -13,6 +13,7 @@ use app\common\model\Article;
use app\common\model\Collection; use app\common\model\Collection;
use app\common\model\User as userModel; use app\common\model\User as userModel;
use think\facade\Config; use think\facade\Config;
use app\facade\Message;
class User extends BaseController class User extends BaseController
{ {
@ -127,7 +128,11 @@ class User extends BaseController
public function message() public function message()
{ {
return view(); $uid = Session::get('user_id');
$msg = Message::receveMsg($uid);
View::assign('msg',$msg);
return View::fetch();
} }
//个人页 //个人页

View File

@ -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'); 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`; DROP TABLE IF EXISTS `tao_slider`;
CREATE TABLE `tao_slider` ( CREATE TABLE `tao_slider` (