|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPHub\Listeners; |
|
4
|
|
|
|
|
5
|
|
|
use JPush\Exception\APIRequestException; |
|
6
|
|
|
use PHPHub\Events\Event; |
|
7
|
|
|
use PHPHub\Events\NewReply; |
|
8
|
|
|
use PHPHub\Events\TopicUpVoted; |
|
9
|
|
|
use PHPHub\Repositories\NotificationRepositoryInterface; |
|
10
|
|
|
use PHPHub\Services\PushService\Jpush; |
|
11
|
|
|
|
|
12
|
|
|
class NotificationListener |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* 订阅的时间和处理的方法. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $subscribe_events = [ |
|
20
|
|
|
TopicUpVoted::class => 'handle', |
|
21
|
|
|
NewReply::class => 'handle', |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Jpush 对象 |
|
26
|
|
|
* |
|
27
|
|
|
* @var Jpush |
|
28
|
|
|
*/ |
|
29
|
|
|
private $jpush = null; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var NotificationRepositoryInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $notifications; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* PushNotificationHandler constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param NotificationRepositoryInterface $notifications |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(NotificationRepositoryInterface $notifications) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->notifications = $notifications; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* 推送消息. |
|
47
|
|
|
* |
|
48
|
|
|
* @param $user_ids |
|
49
|
|
|
* @param $msg |
|
50
|
|
|
* @param $extras |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function push($user_ids, $msg, $extras = null) |
|
53
|
|
|
{ |
|
54
|
|
|
if (! $this->jpush) { |
|
55
|
|
|
$this->jpush = new Jpush(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$user_ids = (array) $user_ids; |
|
59
|
|
|
$user_ids = array_map(function ($user_id) { |
|
60
|
|
|
return 'userid_'.$user_id; |
|
61
|
|
|
}, $user_ids); |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
|
|
$this->jpush |
|
65
|
|
|
->platform('all') |
|
66
|
|
|
->message($msg) |
|
67
|
|
|
->toAlias($user_ids) |
|
68
|
|
|
->extras($extras) |
|
69
|
|
|
->send(); |
|
70
|
|
|
} catch (APIRequestException $e) { |
|
71
|
|
|
// Ignore |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Handle the event. |
|
77
|
|
|
* |
|
78
|
|
|
* @param Event|TopicUpVoted $event |
|
79
|
|
|
*/ |
|
80
|
|
|
public function handle(Event $event) |
|
81
|
|
|
{ |
|
82
|
|
|
$data = [ |
|
83
|
|
|
'topic_id' => $event->getTopicId(), |
|
|
|
|
|
|
84
|
|
|
'body' => $event->getBody(), |
|
|
|
|
|
|
85
|
|
|
'from_user_id' => $event->getFromUserId(), |
|
|
|
|
|
|
86
|
|
|
'user_id' => $event->getUserId(), |
|
|
|
|
|
|
87
|
|
|
'type' => $event->getType(), |
|
|
|
|
|
|
88
|
|
|
'reply_id' => $event->getReplyId(), |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
$notification = $this->notifications->store($data); |
|
92
|
|
|
|
|
93
|
|
|
$presenter = app('autopresenter')->decorate($notification); |
|
94
|
|
|
|
|
95
|
|
|
$push_data = array_only($data, [ |
|
96
|
|
|
'topic_id', |
|
97
|
|
|
'from_user_id', |
|
98
|
|
|
'type', |
|
99
|
|
|
]); |
|
100
|
|
|
|
|
101
|
|
|
if ($data['reply_id'] !== 0) { |
|
102
|
|
|
$push_data['reply_id'] = $data['reply_id']; |
|
103
|
|
|
$push_data['replies_url'] = route('replies.web_view', $data['topic_id']); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->push($event->getUserId(), $presenter->message(), $push_data); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* 注册监听器给订阅者。. |
|
111
|
|
|
* |
|
112
|
|
|
* @param \Illuminate\Events\Dispatcher $events |
|
113
|
|
|
*/ |
|
114
|
|
|
public function subscribe($events) |
|
115
|
|
|
{ |
|
116
|
|
|
foreach ($this->subscribe_events as $event => $handle_method) { |
|
117
|
|
|
$events->listen($event, self::class.'@'.$handle_method); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.