优化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){
$res = ['code'=>0,'msg'=>'','count'=>$count];
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 {
$res = ['code'=>-1,'msg'=>'还没有发布任何通知'];
@ -48,8 +48,8 @@ class Notice extends AdminController
$data['user_id'] = $sendId; //发信人入信息库
//写入信息库
$result = Message::sendMsg($sendId,$receveId,$data);
if($result){
//event('Message');
$res = ['code'=>0,'msg'=>'发布成功'];
} else {
$res = ['code'=>-1,'msg'=>'发布失败'];

View File

@ -18,7 +18,7 @@
<div class="layui-input-block">
<select lay-verify="required" name="type" lay-filter="type">
<option value="0">站内通知</option>
<option value="1">用户通知</option>
<option value="1">用户通知</option>
</select>
</div>
</div>

View File

@ -3,11 +3,10 @@ namespace app\common\model;
use think\Model;
use think\model\concern\SoftDelete;
use think\facade\Db;
use think\facade\Session;
use think\facade\Cookie;
use think\facade\Log;
use taoler\com\Api;
use think\facade\Config;
use think\facade\Lang;
class User extends Model
{
@ -44,31 +43,31 @@ class User extends Model
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加密再进行数据库的查询
$salt = substr(md5($user['create_time']),-6);
$pwd = substr_replace(md5($data['password']),$salt,0,6);
$data['password'] = md5($pwd);
//对输入的密码字段进行MD5加密再进行数据库的查询
$salt = substr(md5($user['create_time']),-6);
$pwd = substr_replace(md5($data['password']),$salt,0,6);
$data['password'] = md5($pwd);
if($user['password'] == $data['password']){
//将用户数据写入Session
Session::set('user_id',$user['id']);
Session::set('user_name',$user['name']);
if(isset($data['remember'])){
$salt = Config::get('taoler.salt');
//加密auth存入cookie
$auth = md5($user['name'].$salt).":".$user['id'];
Cookie::set('auth',$auth,604800);
//Cookie::set('user_id', $user['id'], 604800);
//Cookie::set('user_name', $user['name'], 604800);
}
if($user['password'] == $data['password']){
//将用户数据写入Session
Session::set('user_id',$user['id']);
Session::set('user_name',$user['name']);
if(isset($data['remember'])){
$salt = 'taoler';
//加密auth存入cookie
$auth = md5($user['name'].$salt).":".$user['id'];
Cookie::set('auth',$auth,604800);
//Cookie::set('user_id', $user['id'], 604800);
//Cookie::set('user_name', $user['name'], 604800);
}
//查询结果1表示有用户用户名密码正确
return 1;
}else{
return '用户名或密码错误';
//查询结果1表示有用户用户名密码正确
return 1;
}
}
return Lang::get('username or password error');
}
//更新数据

View File

@ -5,7 +5,9 @@ namespace app\event;
class Message
{
public function msg(){
echo "这是一个事件";
//public $userId;
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,36 +113,37 @@ class Article extends BaseController
//文章评论
public function comment()
{
//if (Request::isAjax()){
//获取评论
$data = Request::only(['content','article_id','user_id']);
$sendId = $data['user_id'];
if(empty($data['content'])){
return json(['code'=>0, 'msg'=>'评论不能为空!']);
}
//用户留言存入数据库
if (Comment::create($data)) {
//站内信
$article = Db::name('article')->field('id,title,user_id')->where('id',$data['article_id'])->find();
$title = $article['title'];
$link = (string) url('article/detail',['id'=>$data['article_id']]);
if (Request::isAjax()){
//获取评论
$data = Request::only(['content','article_id','user_id']);
$sendId = $data['user_id'];
if(empty($data['content'])){
return json(['code'=>0, 'msg'=>'评论不能为空!']);
}
//用户留言存入数据库
if (Comment::create($data)) {
//站内信
$article = Db::name('article')->field('id,title,user_id')->where('id',$data['article_id'])->find();
$title = $article['title'];
$link = (string) url('article/detail',['id'=>$data['article_id']]);
//@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');
//评论中回复@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,'content'=>'评论通知','link'=>$link,'user_id'=>$sendId,'type'=>2]; //type=2为评论留言
Message::sendMsg($sendId,$receveId,$data);
$res = ['code'=>0, 'msg'=>'留言成功'];
} else {
$receveId = $article['user_id'];
$res = ['code'=>-1, 'msg'=>'留言失败'];
}
$data = ['title'=>$title,'content'=>'评论通知','link'=>$link,'user_id'=>$sendId,'type'=>1];
Message::sendMsg($sendId,$receveId,$data);
//event('CommMsg');
$res = ['code'=>0, 'msg'=>'留言成功'];
} else {
$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\Db;
use think\facade\Cache;
use app\common\model\Article;
use app\common\model\User;
use app\facade\Article;
use app\common\lib\Msgres;
class Index extends BaseController
@ -28,13 +27,12 @@ class Index extends BaseController
$slider = new \app\common\model\Slider();
$sliders = $slider->getSliderList();
$article = new Article();
//置顶文章
$artTop = $article->getArtTop(5);
$artTop = Article::getArtTop(5);
//首页文章列表,显示20个
$artList = $article->getArtList(20);
$artList = Article::getArtList(20);
//热议文章
$artHot = $article->getArtHot(10);
$artHot = Article::getArtHot(10);
//首页赞助
$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();
// 查询热议
$article = new Article();
$artHot = $article->getArtHot(10);
//$article = new Article()1;
$artHot = Article::getArtHot(10);
View::assign($searchs);
View::assign(['flinks'=>$friend_links,'artHot'=>$artHot]);

View File

@ -69,17 +69,16 @@ class Login extends BaseController
$user = new User();
$res = $user->login($data);
if ($res == 1) {
//登陆成功
$ip = request()->ip();
$name = $data['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'));
} else {
return json(['code'=>-1,'msg'=>$res]);
return Msgres::error($res);
}
}
return View::fetch('login');

View File

@ -6,6 +6,8 @@ return [
\app\middleware\Browse::class,
// 多语言加载
\think\middleware\LoadLangPack::class,
//接收消息
\app\middleware\Message::class,
//app\middleware\LoginCheck::class,
//app\middleware\CheckRegister::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;
use taoler\com\Message as MessageIns;
//use app\facade\Message;
class Message
{
@ -15,7 +14,8 @@ class Message
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

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

View File

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

View File

@ -10,9 +10,9 @@ class Message
{
/**
* 发送消息
* @param $sendId 发送者
* @param $receveId 接收者
* @param $data 发送数据
* @param $sendId
* @param $receveId
* @param $data
* @return bool
*/
public static function sendMsg($sendId,$receveId,$data)
@ -22,7 +22,7 @@ class Message
$msgId = $msg->id;
//类型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']]);
if(!$result){
return false;
@ -58,13 +58,13 @@ class Message
}
/**
* 登录用户把消息写入数据表
* @param $uid 登陆用户ID
* 登录用户把系统消息写入数据表
* @param int $uid
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @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();

View File

@ -296,10 +296,10 @@ layui.define(['laypage', 'fly', 'element', 'flow'], function(exports){
});
//我的消息
gather.minemsg = function(){
var delAll = $('#LAY_delallmsg')
,tpl = '{{# var len = d.rows.length;\
//我的消息
gather.minemsg = function(){
var delAll = $('#LAY_delallmsg')
,tpl = '{{# var len = d.rows.length;\
if(len === 0){ }}\
<div class="fly-none">您暂时没有最新消息</div>\
{{# } else { }}\
@ -307,8 +307,12 @@ layui.define(['laypage', 'fly', 'element', 'flow'], function(exports){
{{# for(var i = 0; i < len; i++){ }}\
<li data-id="{{d.rows[i].id}}">\
{{# 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>\
{{# } 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>\
{{# } }}\
<p><span>{{d.rows[i].time}}</span><a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-danger fly-delete"></a></p>\
@ -316,11 +320,11 @@ layui.define(['laypage', 'fly', 'element', 'flow'], function(exports){
{{# } }}\
</ul>\
{{# } }}'
,delEnd = function(clear){
if(clear || dom.minemsg.find('.mine-msg li').length === 0){
dom.minemsg.html('<div class="fly-none">您暂时没有最新消息</div>');
}
}
,delEnd = function(clear){
if(clear || dom.minemsg.find('.mine-msg li').length === 0){
dom.minemsg.html('<div class="fly-none">您暂时没有最新消息</div>');
}
}
fly.json(messageFind, {}, function(res){

View File

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

View File

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