diff --git a/src/Notifications/Channels/WechatChannel.php b/src/Notifications/Channels/WechatChannel.php index d9aac49c455525d2ea39f78a20454053446ddfdd..52062e6e30dd299a0ce0e45549c368f4ac3142d1 100644 --- a/src/Notifications/Channels/WechatChannel.php +++ b/src/Notifications/Channels/WechatChannel.php @@ -15,9 +15,9 @@ namespace Discuz\Notifications\Channels; -use App\Api\Controller\Notification\NotificationTimingTrait; use App\Models\NotificationTpl; use Discuz\Contracts\Setting\SettingsRepository; +use Discuz\Notifications\Traits\NotificationTimingTrait; use Discuz\Wechat\EasyWechatTrait; use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; use EasyWeChat\Kernel\Exceptions\InvalidConfigException; diff --git a/src/Notifications/Traits/NotificationTimingTrait.php b/src/Notifications/Traits/NotificationTimingTrait.php new file mode 100755 index 0000000000000000000000000000000000000000..3b370edf95cf0c3d8f57261a86bf11b8ddafa823 --- /dev/null +++ b/src/Notifications/Traits/NotificationTimingTrait.php @@ -0,0 +1,118 @@ + RegisterMessage::class, + 'wechat.registered.approved' => StatusMessage::class, + 'wechat.registered.unapproved' => StatusMessage::class, + 'wechat.post.approved' => PostMessage::class, + 'wechat.post.unapproved' => PostMessage::class, + 'wechat.post.deleted' => PostMessage::class, + 'wechat.post.essence' => PostMessage::class, + 'wechat.post.sticky' => PostMessage::class, + 'wechat.post.update' => PostMessage::class, + 'wechat.user.disable' => StatusMessage::class, + 'wechat.user.normal' => StatusMessage::class, + 'wechat.user.group' => GroupMessage::class, + ]; + + public $nonSystemMethod = [ + 'wechat.post.replied' => 'Replied', + 'wechat.post.liked' => 'Liked', + 'wechat.post.paid' => 'Rewarded', + 'wechat.post.reminded' => 'Related', + 'wechat.withdraw.noticed' => 'Withdrawal', + 'wechat.withdraw.withdraw' => 'Withdrawal', + 'wechat.divide.income' => 'Rewarded', + 'wechat.question.asked' => 'Questioned', + 'wechat.question.answered' => 'Questioned', + 'wechat.question.expired' => 'Rewarded', + 'wechat.red_packet.gotten' => 'ReceiveRedPacket', + 'wechat.question.rewarded' => 'ThreadRewarded', + 'wechat.question.rewarded.expired' => 'ThreadRewardedExpired', + ]; + + public function sendNotification($receiveUserId, $wechatNoticeId, $isCount = true): array + { + $response = [ + 'result' => true, + 'noticeTimingId' => 0 + ]; + $pushType = NotificationTpl::getPushType($wechatNoticeId); + if ($pushType === false) { + DzqLog::error('notice_id_not_exist', ['receiveUserId' => $receiveUserId, 'wechatNoticeId' => $wechatNoticeId]); + $response['result'] = false; + return $response; + } + + 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)) { + $response['noticeTimingId'] = $currentNotification['id']; + // ToDo: 累计通知次数 + NotificationTiming::addNotificationNumber($currentNotification['id'], $isCount); + if ($notNotification) { + $response['result'] = false; + return $response; + } 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 = NotificationTiming::createNotificationTiming($wechatNoticeId, $receiveUserId, $expiredAt); + $response['noticeTimingId'] = $notificationTiming['id']; + $response['result'] = ($expiredAt == null ? false : true); + return $response; + } + } else { + // ToDo: 初始化发送即时通知 + } + } + + $response['result'] = true; + return $response; + } +}