「拓展」实现评论 @ 多人通知功能
以「Laravel 教程 」为基础评论 AT 多人通知多人功能。
前言
这是你想要的 AT 某人的功能
由于自己也要做个评论 AT 某人高亮功能,正好看到帖子就有。但是读了下源码发现老哥好像只通知了一人。所以自己根据老哥的部分源码加以改动。以下是代码。
php artisan make:job ReplySomePeople
<?php
namespace App\Jobs;
use App\Models\Reply;
use App\Models\User;
use App\Notifications\ArticleReplied;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Notification;
class ReplySomePeople implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $reply;
/**
* ReplySomePeople constructor.
*
* [[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)param \App\Models\Reply $reply
*/
public function __construct(Reply $reply)
{
$this->reply = $reply;
}
/**
* Execute the job.
*
* [[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)return void
*/
public function handle()
{
$content = $this->reply->content;
$contentFormat = matchAt($content);
$this->reply->content = $contentFormat['body'];
$this->reply->update([
'content' => $this->reply->content
]);
if (isset($contentFormat['reply_user_ids'])) {
$reply_user_ids = array_unique($contentFormat['reply_user_ids']);//去重
$users = User::whereIn('id', $reply_user_ids)->get();//用户列表
User::whereIn('id', $reply_user_ids)->increment('notification_count');//评论+1
Notification::send($users, new ArticleReplied($this->reply));
}
}
}
matchAt 方法「bootstrap/helpers.php」
/**
* 正则替换 [[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278) 用户
* [[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)param $body
*
* [[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)return array
*/
function matchAt($body){
$userModel = new \App\Models\User();
preg_match_all("/\[[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)([^\#|\s]+)\s/", $body, $matched_name);
if (!empty($matched_name[1])) {
$reply_user_ids = [];
foreach($matched_name[1] as $key => $name) {
$user_id = $userModel->where('name', 'like binary', $name)->value('id');
if(!$user_id) {
continue;
}
$reply_user_ids[$key] = $user_id;
$body = str_replace('[[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)'.$name,'[[[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)'.$name.']('.config('app.url').'/users/'.$reply_user_ids[$key].')',$body);
}
return ['reply_user_ids' => $reply_user_ids, 'body' => $body];
} else {
return ['body' => $body];
}
}
最后在 「ReplyObserver」修改 「created 」方法
use App\Jobs\ReplySomePeople;
public function created(Reply $reply)
{
//文章评论数
$article = $reply->article;
$article->last_reply_user_id = Auth::id();//最后评论
$article->increment('reply_count', 1);//评论数+1
dispatch(new ReplySomePeople($reply));
// $article->user->notify(new ArticleReplied($reply));
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 6年前 自动加精
发现问题了居然不@我一下,我很生气,开玩笑。
源码里写的确实有问题,也确实只通知了一个人,
因为通知作者和通知被@的人发送的信息(或邮件)是不同的,我之前的通知其实只有一种,是有问题的。
所以我需要的是两种通知,嗯,目前正在测试。
再说我的代码那么烂 ,你也看得懂,历害了。
@Jourdon 啊,那个不是特意写的吗 :joy:
@Prefect丶 额,其实准确的说,目前的情况,不是只通知一个人,是只有一种通知,这种是有问题的,作者和被@的人不应该是一种通知。
在foreach里写sql我是不喜欢的 :(
@Prefect丶 @Jourdon 我没看懂当前作者写的正则 /@([^#|\s]+)\s/这样不行吗? 为啥还有[[@(地址)...
@Mr_lan 我拿Laravel China 的正则的,地址符是laravel China 自动加上去的,很尴尬
@Prefect丶 在哪找到的 你说这个正则是LC的是吧 特殊而已 感觉我们自己的直接 /@([^#|\s]+)\s/这样就可以了
@Mr_lan 额,不是我写的,@我干嘛。。
@Jourdon 我知道啊 让你帮忙看看有什么玄机 发现我@了你好多次了 之前的es scout的哈哈
@Mr_lan 正则的写法并不是只有一种,只要能满足你的要求,匹配到你想要的就可以啦,比如我的正则是这样的,"/@.*?(?=( |$))/"
快来看啊 @Morgan 快来 快来
块来看啊啊@morgan 块来 快来