From 03a46291c605b2a8e354d4c02ec6db16dc1e893b Mon Sep 17 00:00:00 2001 From: taoser Date: Mon, 30 Nov 2020 17:13:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Message=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E5=92=8C=E6=8E=A5=E6=94=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/Notice.php | 4 +- app/admin/view/notice/index.html | 2 +- app/common/model/User.php | 49 ++++++++++++------------- app/event/Message.php | 6 ++- app/facade/Article.php | 22 +++++++++++ app/facade/User.php | 15 ++++++++ app/index/controller/Article.php | 53 ++++++++++++++------------- app/index/controller/Index.php | 14 +++---- app/index/controller/Login.php | 9 ++--- app/index/middleware.php | 2 + app/listener/CommMsg.php | 21 ----------- app/listener/Message.php | 4 +- app/middleware/Message.php | 27 ++++++++++++++ config/middleware.php | 5 ++- config/taoler.php | 3 +- extend/taoler/com/Message.php | 14 +++---- public/static/res/mods/user.js | 24 +++++++----- view/taoler/index/article/detail.html | 1 + view/taoler/index/user/message.html | 2 +- 19 files changed, 164 insertions(+), 113 deletions(-) create mode 100644 app/facade/Article.php create mode 100644 app/facade/User.php delete mode 100644 app/listener/CommMsg.php create mode 100644 app/middleware/Message.php diff --git a/app/admin/controller/Notice.php b/app/admin/controller/Notice.php index ffea885..613d185 100644 --- a/app/admin/controller/Notice.php +++ b/app/admin/controller/Notice.php @@ -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'=>'发布失败']; diff --git a/app/admin/view/notice/index.html b/app/admin/view/notice/index.html index 907fe0c..aa5daaf 100644 --- a/app/admin/view/notice/index.html +++ b/app/admin/view/notice/index.html @@ -18,7 +18,7 @@
diff --git a/app/common/model/User.php b/app/common/model/User.php index 7180bea..d6344cd 100644 --- a/app/common/model/User.php +++ b/app/common/model/User.php @@ -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'); } //更新数据 diff --git a/app/event/Message.php b/app/event/Message.php index a7e8102..a239a01 100644 --- a/app/event/Message.php +++ b/app/event/Message.php @@ -5,7 +5,9 @@ namespace app\event; class Message { - public function msg(){ - echo "这是一个事件"; + //public $userId; + public function __construct(){ + //echo '1111'; + //$this->userId = $userId; } } diff --git a/app/facade/Article.php b/app/facade/Article.php new file mode 100644 index 0000000..33b2fdd --- /dev/null +++ b/app/facade/Article.php @@ -0,0 +1,22 @@ +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); } /** diff --git a/app/index/controller/Index.php b/app/index/controller/Index.php index 3535ef8..264a507 100644 --- a/app/index/controller/Index.php +++ b/app/index/controller/Index.php @@ -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]); diff --git a/app/index/controller/Login.php b/app/index/controller/Login.php index 4accd4c..4e21785 100644 --- a/app/index/controller/Login.php +++ b/app/index/controller/Login.php @@ -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'); diff --git a/app/index/middleware.php b/app/index/middleware.php index 10b6a9b..e81c984 100644 --- a/app/index/middleware.php +++ b/app/index/middleware.php @@ -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, diff --git a/app/listener/CommMsg.php b/app/listener/CommMsg.php deleted file mode 100644 index 657827d..0000000 --- a/app/listener/CommMsg.php +++ /dev/null @@ -1,21 +0,0 @@ - [ - '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' => [], diff --git a/config/taoler.php b/config/taoler.php index 4436beb..5f616b4 100644 --- a/config/taoler.php +++ b/config/taoler.php @@ -5,5 +5,6 @@ return [ //版本配置 - 'version'=> '1.6.3', + 'version' => '1.6.3', + 'salt' => 'taoler' ]; \ No newline at end of file diff --git a/extend/taoler/com/Message.php b/extend/taoler/com/Message.php index 552adeb..7d963d3 100644 --- a/extend/taoler/com/Message.php +++ b/extend/taoler/com/Message.php @@ -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(); diff --git a/public/static/res/mods/user.js b/public/static/res/mods/user.js index 36b4670..b8fe3b5 100644 --- a/public/static/res/mods/user.js +++ b/public/static/res/mods/user.js @@ -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){ }}\
您暂时没有最新消息
\ {{# } else { }}\ @@ -307,8 +307,12 @@ layui.define(['laypage', 'fly', 'element', 'flow'], function(exports){ {{# for(var i = 0; i < len; i++){ }}\
  • \ {{# if(d.rows[i].type == 1){ }}\ +
    {{ d.rows[i].name}}给您发了站内信{{ d.rows[i].title}} {{ d.rows[i].read}}
    \ + {{# } }}\ + {{# if(d.rows[i].type == 2){ }}\
    {{ d.rows[i].name}}回答了您的帖子{{ d.rows[i].title}} {{ d.rows[i].read}}
    \ - {{# } else { }}\ + {{# } }}\ + {{# if(d.rows[i].type == 0){ }}\
    系统消息:{{ d.rows[i].title}} {{ d.rows[i].read}}
    \ {{# } }}\

    {{d.rows[i].time}}删除

    \ @@ -316,11 +320,11 @@ layui.define(['laypage', 'fly', 'element', 'flow'], function(exports){ {{# } }}\ \ {{# } }}' - ,delEnd = function(clear){ - if(clear || dom.minemsg.find('.mine-msg li').length === 0){ - dom.minemsg.html('
    您暂时没有最新消息
    '); - } - } + ,delEnd = function(clear){ + if(clear || dom.minemsg.find('.mine-msg li').length === 0){ + dom.minemsg.html('
    您暂时没有最新消息
    '); + } + } fly.json(messageFind, {}, function(res){ diff --git a/view/taoler/index/article/detail.html b/view/taoler/index/article/detail.html index 79a2ea9..2bdb179 100644 --- a/view/taoler/index/article/detail.html +++ b/view/taoler/index/article/detail.html @@ -333,6 +333,7 @@ layui.config({ }); +//扫码阅读 $("#rdown").hover(function(){ $("#phonedl").show().stop(); },function(){ diff --git a/view/taoler/index/user/message.html b/view/taoler/index/user/message.html index 3644837..8cf9701 100644 --- a/view/taoler/index/user/message.html +++ b/view/taoler/index/user/message.html @@ -45,7 +45,7 @@ }); }); - //点开系统消息改变已读状态 + //读消息 $('#LAY_minemsg').on('click','.sys-title', function(){ var id = $(this).attr('id-data'); var othis = $(this);