diff --git a/src/Notifications/Channels/WechatChannel.php b/src/Notifications/Channels/WechatChannel.php index afd7d32bfa731f6d3668e99c2f24091a9ba10ff1..81bbcb63470bcb1f5c6f84f6847a1104993b6ad4 100644 --- a/src/Notifications/Channels/WechatChannel.php +++ b/src/Notifications/Channels/WechatChannel.php @@ -15,7 +15,10 @@ namespace Discuz\Notifications\Channels; +use App\Models\NotificationTiming; use App\Models\NotificationTpl; +use Carbon\Carbon; +use Discuz\Base\DzqLog; use Discuz\Contracts\Setting\SettingsRepository; use Discuz\Wechat\EasyWechatTrait; use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; @@ -58,6 +61,11 @@ class WechatChannel */ public function send($notifiable, Notification $notification) { + $sendNotification = $this->sendNotification($notifiable, $notification); + if ($sendNotification == false) { + return false; + } + if (! empty($notifiable->wechat) && ! empty($notifiable->wechat->mp_openid)) { // wechat post json @@ -127,4 +135,53 @@ class WechatChannel } } + private function sendNotification($notifiable, $notification): bool + { + $receiveUserId = $notifiable->id; + $tplId = collect($notification)->get('tplId'); + $wechatNoticeId = $tplId['wechat']; + + $pushType = NotificationTpl::getPushType($wechatNoticeId); + if ($pushType === false) { + DzqLog::error('notice_id_not_exist', ['receiveUserId' => $receiveUserId, 'tplId' => $tplId]); + return false; + } + + if ($pushType == NotificationTpl::PUSH_TYPE_DELAY) { + $lastNotification = NotificationTiming::getLastNotification($wechatNoticeId, $receiveUserId); + $lastNotificationTime = strtotime($lastNotification['expired_at']); + $delayTime = NotificationTpl::getDelayTime($wechatNoticeId); + + $nowTime = strtotime(Carbon::now()); + if (abs($nowTime - $lastNotificationTime) > 1) { + $currentNotification = NotificationTiming::getCurrentNotification($wechatNoticeId, $receiveUserId); + $notNotification = $lastNotificationTime + $delayTime > $nowTime; // 不进行通知 + if (!empty($currentNotification)) { + // ToDo: 累计通知次数 + NotificationTiming::addNotificationNumber($currentNotification['id']); + if ($notNotification) { + return false; + } else { + // ToDo: 发送即时通知,将过期时间置为当前时间 + $updateNum = NotificationTiming::setExpireAt($currentNotification['id']); + if ($updateNum == 0) { + DzqLog::error('set_notice_expire_at_error', ['notificationId' => $lastNotification['id'], 'updateNum' => $updateNum]); + } + } + } else { + if ($notNotification) { + $expiredAt = null; + } else { + $expiredAt = Carbon::now(); + } + NotificationTiming::createNotificationTiming($wechatNoticeId, $receiveUserId, $expiredAt); + return ($expiredAt == null ? false : true); + } + } else { + // ToDo: 初始化发送即时通知 + } + } + + return true; + } }