diff --git a/src/Notifications/Channels/WechatChannel.php b/src/Notifications/Channels/WechatChannel.php index 81bbcb63470bcb1f5c6f84f6847a1104993b6ad4..d9fca5082ad9b65e9ac5859f44cca10cd7228d1e 100644 --- a/src/Notifications/Channels/WechatChannel.php +++ b/src/Notifications/Channels/WechatChannel.php @@ -15,10 +15,8 @@ namespace Discuz\Notifications\Channels; -use App\Models\NotificationTiming; +use App\Api\Controller\NotificationV3\NotificationTimingTrait; 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; @@ -37,6 +35,8 @@ class WechatChannel { use EasyWechatTrait; + use NotificationTimingTrait; + protected $settings; /** @@ -61,15 +61,18 @@ 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)) { + $tplId = collect($notification)->get('tplId'); + $wechatNoticeId = $tplId['wechat']; + + $sendResponse = $this->sendNotification($notifiable->id, $wechatNoticeId); // wechat post json - $build = $notification->toWechat($notifiable); + $build = $notification->toWechat($notifiable, $sendResponse['noticeTimingId']); + + if ($sendResponse['result'] == false) { + return false; + } // 替换掉内容中的换行符 $content = str_replace(PHP_EOL, '', Arr::get($build, 'content')); @@ -134,54 +137,4 @@ 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; - } }