ActivityToUpNotification.php 2.2 KB
<?php

namespace App\Notifications;

use App\Models\Activity;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Arr;

class ActivityToUpNotification extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * @var \App\Models\Activity
     */
    public Activity $activity;

    /**
     * @var \App\Models\User
     */
    public User $operator;

    public function __construct(Activity $activity, User $operator)
    {
        $this->afterCommit();
        $this->activity = $activity->loadMissing('project:id,name');
        $this->operator = $operator;
    }

    public function via(mixed $notifiable): array
    {
        return ['wechat', 'jPush'];
    }

    private function getProjectName(): string
    {
        return Arr::get($this->activity, 'project.name', '');
    }

    /**
     * @return bool
     */
    private function hasProject(): bool
    {
        return !empty($this->getProjectName());
    }

    public function toJPush(User $notifiable): array
    {
        return [
            'user'    => $notifiable->getKey(),
            'title'   => sprintf('《%s》重新上架', $this->activity->getAttribute('song_name')),
            'content' => '重新上架(自动通过,无需审批)',
            'data'    => ['type' => 'ToActivity', 'value' => $this->activity->getKey()]
        ];
    }

    public function toWechat(User $notifiable): array
    {
        return [
            'openId'   => $notifiable->getWechatNotificationKey(),
            'template' => 'o9i6qtgr4ghtGvU-piimB9_mAaSOAXDDMzqx9zXYkss',
            'data'     => [
                'first'    => $this->hasProject() ? sprintf('[%s]试唱歌曲重新上架', $this->getProjectName()) : '试唱歌曲重新上架',
                'keyword1' => sprintf('《%s》', $this->activity->getAttribute('song_name')),
                'keyword2' => $this->operator->getFullName(),
                'keyword3' => Carbon::now()->toDateTimeString(),
                'keyword4' => '试唱歌曲重新上架(自动通过,无需审批)',
                'remark'   => ''
            ]
        ];
    }
}