优化Message消息推送和接收

This commit is contained in:
taoser 2020-11-30 17:13:55 +08:00
parent f07f24175c
commit 03a46291c6
19 changed files with 164 additions and 113 deletions

View File

@ -23,7 +23,7 @@ class Notice extends AdminController
if($count){ if($count){
$res = ['code'=>0,'msg'=>'','count'=>$count]; $res = ['code'=>0,'msg'=>'','count'=>$count];
foreach($notices as $msg){ foreach($notices as $msg){
$res['data'][] = ['id'=>$msg['id'],'type'=>$msg['type'],'title'=>$msg['title'],'user_id'=>$msg['user_id'],'content'=>$msg['content'],'ctime'=>$msg['create_time']]; $res['data'][] = ['id'=>$msg['id'],'type'=>$msg['type'] == 0 ? '系统消息': '个人消息','title'=>$msg['title'],'user_id'=>$msg['user_id'],'content'=>$msg['content'],'ctime'=>$msg['create_time']];
} }
} else { } else {
$res = ['code'=>-1,'msg'=>'还没有发布任何通知']; $res = ['code'=>-1,'msg'=>'还没有发布任何通知'];
@ -48,8 +48,8 @@ class Notice extends AdminController
$data['user_id'] = $sendId; //发信人入信息库 $data['user_id'] = $sendId; //发信人入信息库
//写入信息库 //写入信息库
$result = Message::sendMsg($sendId,$receveId,$data); $result = Message::sendMsg($sendId,$receveId,$data);
if($result){ if($result){
//event('Message');
$res = ['code'=>0,'msg'=>'发布成功']; $res = ['code'=>0,'msg'=>'发布成功'];
} else { } else {
$res = ['code'=>-1,'msg'=>'发布失败']; $res = ['code'=>-1,'msg'=>'发布失败'];

View File

@ -3,11 +3,10 @@ namespace app\common\model;
use think\Model; use think\Model;
use think\model\concern\SoftDelete; use think\model\concern\SoftDelete;
use think\facade\Db;
use think\facade\Session; use think\facade\Session;
use think\facade\Cookie; use think\facade\Cookie;
use think\facade\Log; use think\facade\Config;
use taoler\com\Api; use think\facade\Lang;
class User extends Model class User extends Model
{ {
@ -44,8 +43,8 @@ class User extends Model
public function login($data) public function login($data)
{ {
//查询使用邮箱或者用户名登陆 //查询使用邮箱或者用户名登陆
$user = $this::whereOr('email',$data['name'])->whereOr('name',$data['name'])->find(); $user = $this::whereOr('email',$data['name'])->whereOr('name',$data['name'])->findOrEmpty();
if(!$user->isEmpty()){
//对输入的密码字段进行MD5加密再进行数据库的查询 //对输入的密码字段进行MD5加密再进行数据库的查询
$salt = substr(md5($user['create_time']),-6); $salt = substr(md5($user['create_time']),-6);
$pwd = substr_replace(md5($data['password']),$salt,0,6); $pwd = substr_replace(md5($data['password']),$salt,0,6);
@ -56,7 +55,7 @@ class User extends Model
Session::set('user_id',$user['id']); Session::set('user_id',$user['id']);
Session::set('user_name',$user['name']); Session::set('user_name',$user['name']);
if(isset($data['remember'])){ if(isset($data['remember'])){
$salt = 'taoler'; $salt = Config::get('taoler.salt');
//加密auth存入cookie //加密auth存入cookie
$auth = md5($user['name'].$salt).":".$user['id']; $auth = md5($user['name'].$salt).":".$user['id'];
Cookie::set('auth',$auth,604800); Cookie::set('auth',$auth,604800);
@ -66,10 +65,10 @@ class User extends Model
//查询结果1表示有用户用户名密码正确 //查询结果1表示有用户用户名密码正确
return 1; return 1;
}else{
return '用户名或密码错误';
} }
} }
return Lang::get('username or password error');
}
//更新数据 //更新数据
public function updata($data) public function updata($data)

View File

@ -5,7 +5,9 @@ namespace app\event;
class Message class Message
{ {
public function msg(){ //public $userId;
echo "这是一个事件"; public function __construct(){
//echo '1111';
//$this->userId = $userId;
} }
} }

22
app/facade/Article.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace app\facade;
use think\Facade;
/**
* Class Article
* @package app\facade
* @method static array getArtTop(int $num) 获取置顶文章
* @method static array getArtList(int $num) 获取文章列表
* @method static array getArtHot(int $num) 获取精华文章
*/
class Article extends Facade
{
protected static function getFacadeClass()
{
return 'app\common\model\Article';
}
}

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

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

View File

@ -113,7 +113,7 @@ class Article extends BaseController
//文章评论 //文章评论
public function comment() public function comment()
{ {
//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']; $sendId = $data['user_id'];
@ -127,7 +127,7 @@ class Article extends BaseController
$title = $article['title']; $title = $article['title'];
$link = (string) url('article/detail',['id'=>$data['article_id']]); $link = (string) url('article/detail',['id'=>$data['article_id']]);
//@user comment //评论中回复@user comment
$preg = "/@([^@\s]*)\s/"; $preg = "/@([^@\s]*)\s/";
preg_match($preg,$data['content'],$username); preg_match($preg,$data['content'],$username);
if(isset($username[1])){ if(isset($username[1])){
@ -135,15 +135,16 @@ class Article extends BaseController
} else { } else {
$receveId = $article['user_id']; $receveId = $article['user_id'];
} }
$data = ['title'=>$title,'content'=>'评论通知','link'=>$link,'user_id'=>$sendId,'type'=>1]; $data = ['title'=>$title,'content'=>'评论通知','link'=>$link,'user_id'=>$sendId,'type'=>2]; //type=2为评论留言
Message::sendMsg($sendId,$receveId,$data); Message::sendMsg($sendId,$receveId,$data);
//event('CommMsg');
$res = ['code'=>0, 'msg'=>'留言成功']; $res = ['code'=>0, 'msg'=>'留言成功'];
} else { } else {
$res = ['code'=>-1, 'msg'=>'留言失败']; $res = ['code'=>-1, 'msg'=>'留言失败'];
} }
return json($res); return json($res);
} }
}
/** /**
* 添加帖子文章 * 添加帖子文章

View File

@ -7,8 +7,7 @@ use think\facade\View;
use think\facade\Request; use think\facade\Request;
use think\facade\Db; use think\facade\Db;
use think\facade\Cache; use think\facade\Cache;
use app\common\model\Article; use app\facade\Article;
use app\common\model\User;
use app\common\lib\Msgres; use app\common\lib\Msgres;
class Index extends BaseController class Index extends BaseController
@ -28,13 +27,12 @@ class Index extends BaseController
$slider = new \app\common\model\Slider(); $slider = new \app\common\model\Slider();
$sliders = $slider->getSliderList(); $sliders = $slider->getSliderList();
$article = new Article();
//置顶文章 //置顶文章
$artTop = $article->getArtTop(5); $artTop = Article::getArtTop(5);
//首页文章列表,显示20个 //首页文章列表,显示20个
$artList = $article->getArtList(20); $artList = Article::getArtList(20);
//热议文章 //热议文章
$artHot = $article->getArtHot(10); $artHot = Article::getArtHot(10);
//首页赞助 //首页赞助
$ad_index = Cache::get('adindex'); $ad_index = Cache::get('adindex');
@ -96,8 +94,8 @@ class Index extends BaseController
//友情链接 //友情链接
$friend_links = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>6])->whereTime('slid_over','>=',time())->field('slid_name,slid_href')->select(); $friend_links = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>6])->whereTime('slid_over','>=',time())->field('slid_name,slid_href')->select();
// 查询热议 // 查询热议
$article = new Article(); //$article = new Article()1;
$artHot = $article->getArtHot(10); $artHot = Article::getArtHot(10);
View::assign($searchs); View::assign($searchs);
View::assign(['flinks'=>$friend_links,'artHot'=>$artHot]); View::assign(['flinks'=>$friend_links,'artHot'=>$artHot]);

View File

@ -69,17 +69,16 @@ class Login extends BaseController
$user = new User(); $user = new User();
$res = $user->login($data); $res = $user->login($data);
if ($res == 1) { if ($res == 1) {
//登陆成功
$ip = request()->ip(); $ip = request()->ip();
$name = $data['name']; $name = $data['name'];
//时间更新ip和日志 //时间更新ip和日志
event(new UserLogin($name,$ip)); event(new UserLogin($name,$ip));
//获取系统站内通知信息
//Message::insertMsg(session('user_id'));
//跳转到登陆前页面
//return json(['code'=>0,'msg'=>'登陆成功','url'=> Cookie::get('url')]);
return Msgres::success('login_success',Cookie::get('url')); return Msgres::success('login_success',Cookie::get('url'));
} else { } else {
return json(['code'=>-1,'msg'=>$res]); return Msgres::error($res);
} }
} }
return View::fetch('login'); return View::fetch('login');

View File

@ -6,6 +6,8 @@ return [
\app\middleware\Browse::class, \app\middleware\Browse::class,
// 多语言加载 // 多语言加载
\think\middleware\LoadLangPack::class, \think\middleware\LoadLangPack::class,
//接收消息
\app\middleware\Message::class,
//app\middleware\LoginCheck::class, //app\middleware\LoginCheck::class,
//app\middleware\CheckRegister::class, //app\middleware\CheckRegister::class,
//'logedcheck' => \app\middleware\logedCheck::class, //'logedcheck' => \app\middleware\logedCheck::class,

View File

@ -1,21 +0,0 @@
<?php
declare (strict_types = 1);
namespace app\listener;
use taoler\com\Message;
//use app\facade\Message;
class CommMsg
{
/**
* 事件监听处理
*
* @return mixed
*/
public function handle($event)
{
//执行登陆用户消息数据写入
//Message::sendMsg();
}
}

View File

@ -3,7 +3,6 @@ declare (strict_types = 1);
namespace app\listener; namespace app\listener;
use taoler\com\Message as MessageIns; use taoler\com\Message as MessageIns;
//use app\facade\Message;
class Message class Message
{ {
@ -15,7 +14,8 @@ class Message
public function handle($event) public function handle($event)
{ {
//执行登陆用户消息数据写入 //执行登陆用户消息数据写入
MessageIns::insertMsg(session('user_id')); //dump($event);
//MessageIns::insertMsg(session('user_id'));
} }
} }

View File

@ -0,0 +1,27 @@
<?php
declare (strict_types = 1);
namespace app\middleware;
use taoler\com\Message as MessageIns;
class Message
{
/**
* @param $request
* @param \Closure $next
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function handle($request, \Closure $next)
{
//后置中间件
$response = $next($request);
if(session('user_id')){
MessageIns::insertMsg(session('user_id'));
}
return $response;
}
}

View File

@ -5,6 +5,7 @@ return [
'alias' => [ 'alias' => [
'logedcheck' => \app\middleware\LogedCheck::class, 'logedcheck' => \app\middleware\LogedCheck::class,
'logincheck' => \app\middleware\LoginCheck::class, 'logincheck' => \app\middleware\LoginCheck::class,
'message' => \app\middleware\Message::class,
], ],
// 优先级设置,此数组中的中间件会按照数组中的顺序优先执行 // 优先级设置,此数组中的中间件会按照数组中的顺序优先执行
'priority' => [], 'priority' => [],

View File

@ -6,4 +6,5 @@
return [ return [
//版本配置 //版本配置
'version' => '1.6.3', 'version' => '1.6.3',
'salt' => 'taoler'
]; ];

View File

@ -10,9 +10,9 @@ class Message
{ {
/** /**
* 发送消息 * 发送消息
* @param $sendId 发送者 * @param $sendId
* @param $receveId 接收者 * @param $receveId
* @param $data 发送数据 * @param $data
* @return bool * @return bool
*/ */
public static function sendMsg($sendId,$receveId,$data) public static function sendMsg($sendId,$receveId,$data)
@ -22,7 +22,7 @@ class Message
$msgId = $msg->id; $msgId = $msg->id;
//类型1为用户写入用户收件箱 //类型1为用户写入用户收件箱
if($data['type'] == 1){ if($data['type'] == 1 || $data['type'] == 2 ){
$result = MessageTo::create(['send_id'=>$sendId,'receve_id'=>$receveId,'message_id'=>$msgId,'message_type'=>$data['type']]); $result = MessageTo::create(['send_id'=>$sendId,'receve_id'=>$receveId,'message_id'=>$msgId,'message_type'=>$data['type']]);
if(!$result){ if(!$result){
return false; return false;
@ -58,13 +58,13 @@ class Message
} }
/** /**
* 登录用户把消息写入数据表 * 登录用户把系统消息写入数据表
* @param $uid 登陆用户ID * @param int $uid
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public static function insertMsg($uid) public static function insertMsg(int $uid)
{ {
//得到所有系统消息 //得到所有系统消息
$sysmsg = MessageModel::where(['type'=>0,'delete_time'=>0])->select(); $sysmsg = MessageModel::where(['type'=>0,'delete_time'=>0])->select();

View File

@ -307,8 +307,12 @@ layui.define(['laypage', 'fly', 'element', 'flow'], function(exports){
{{# for(var i = 0; i < len; i++){ }}\ {{# for(var i = 0; i < len; i++){ }}\
<li data-id="{{d.rows[i].id}}">\ <li data-id="{{d.rows[i].id}}">\
{{# if(d.rows[i].type == 1){ }}\ {{# if(d.rows[i].type == 1){ }}\
<blockquote class="layui-elem-quote"><a href= "'+ userNameJump +'?username={{ d.rows[i].name}}" target="_blank"><cite>{{ d.rows[i].name}}</cite></a><a class="sys-title" id-data="{{ d.rows[i].id}}" href="javascript:;"><cite>{{ d.rows[i].title}}</cite></a> <span class="float:right">{{ d.rows[i].read}}</span></blockquote>\
{{# } }}\
{{# if(d.rows[i].type == 2){ }}\
<blockquote class="layui-elem-quote"><a href= "'+ userNameJump +'?username={{ d.rows[i].name}}" target="_blank"><cite>{{ d.rows[i].name}}</cite></a><a target="_blank" class="art-title" id-data="{{ d.rows[i].id}}" href="{{ d.rows[i].link}}"><cite>{{ d.rows[i].title}}</cite></a> <span class="float:right">{{ d.rows[i].read}}</span></blockquote>\ <blockquote class="layui-elem-quote"><a href= "'+ userNameJump +'?username={{ d.rows[i].name}}" target="_blank"><cite>{{ d.rows[i].name}}</cite></a><a target="_blank" class="art-title" id-data="{{ d.rows[i].id}}" href="{{ d.rows[i].link}}"><cite>{{ d.rows[i].title}}</cite></a> <span class="float:right">{{ d.rows[i].read}}</span></blockquote>\
{{# } else { }}\ {{# } }}\
{{# if(d.rows[i].type == 0){ }}\
<blockquote class="layui-elem-quote">系统消息<a class="sys-title" id-data="{{ d.rows[i].id}}" href="javascript:;"><cite>{{ d.rows[i].title}}</cite></a> <span class="float:right">{{ d.rows[i].read}}</span></blockquote>\ <blockquote class="layui-elem-quote">系统消息<a class="sys-title" id-data="{{ d.rows[i].id}}" href="javascript:;"><cite>{{ d.rows[i].title}}</cite></a> <span class="float:right">{{ d.rows[i].read}}</span></blockquote>\
{{# } }}\ {{# } }}\
<p><span>{{d.rows[i].time}}</span><a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-danger fly-delete"></a></p>\ <p><span>{{d.rows[i].time}}</span><a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-danger fly-delete"></a></p>\

View File

@ -333,6 +333,7 @@ layui.config({
}); });
//扫码阅读
$("#rdown").hover(function(){ $("#rdown").hover(function(){
$("#phonedl").show().stop(); $("#phonedl").show().stop();
},function(){ },function(){

View File

@ -45,7 +45,7 @@
}); });
}); });
//点开系统消息改变已读状态 //读消息
$('#LAY_minemsg').on('click','.sys-title', function(){ $('#LAY_minemsg').on('click','.sys-title', function(){
var id = $(this).attr('id-data'); var id = $(this).attr('id-data');
var othis = $(this); var othis = $(this);