123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace App\Services\NoticeService;
- use App\Services\NoticeService\Channels\Channel;
- use Ramsey\Uuid\Uuid;
- class Notification
- {
- protected $title;
- protected $uuid;
- protected $to_uid;
- public function __construct($to_uid)
- {
- $this->to_uid = $to_uid;
- $this->uuid = Uuid::uuid4()->toString();
- }
- /**
- * 发送
- * @return bool
- */
- public function send()
- {
- if (!$this->subscribe()) {
- return false;
- }
- if (!$this->throttle()) {
- return false;
- }
- $channels = $this->via();
- foreach ($channels as $channel) {
- $channel->setUuid($this->uuid);
- $channel->setPath($this->getPagePath());
- if ($channel->send()) {
- $this->logger($channel);
- return true;
- }
- }
- return false;
- }
- /**
- * 订阅开关
- * @return bool
- */
- protected function subscribe()
- {
- return true;
- }
- /**
- * 节流阀
- * @return bool
- */
- protected function throttle()
- {
- return true;
- }
- /**
- * 通知渠道
- * @return array
- */
- public function via(): array
- {
- return [];
- }
- /**
- * 跳转地址
- * @return string
- */
- protected function getPagePath()
- {
- return 'pages/index/index';
- }
- /**
- * 成功通知日志
- * @param Channel $channel
- * @return array
- */
- public function logger(Channel $channel)
- {
- $log = [
- 'uuid' => $this->uuid,
- 'title' => $this->title,
- 'uid' => $this->to_uid,
- 'notice_type' => $channel->getNoticeType(),
- 'template_id' => $channel->getTemplateId(),
- 'content' => $channel->getContent(),
- 'result' => true,
- 'page' => $this->getPagePath(),
- 'created_at' => time(),
- 'date' => date('Y-m-d'),
- ];
- if (app()->environment() == 'production') {
- \DB::connection('mysql_datalog')->table("notice_logs")->insert($log);
- }
- return $log;
- }
- /**
- * 失败日志
- * @param string $account
- */
- public function failLogger($account)
- {
- if (app()->environment() == 'production') {
- \DB::connection('mysql_datalog')->table("notice_logs")->insert([
- 'uuid' => $this->uuid,
- 'title' => $this->title,
- 'uid' => $this->to_uid,
- 'notice_type' => $account,
- 'content' => "",
- 'result' => false,
- 'created_at' => time(),
- 'date' => date('Y-m-d'),
- ]);
- }
- }
- }
|