ActivityMaterialMail.php
3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace App\Mail;
use App\Enums\ActivitySongTypeEnum;
use App\Models\Activity;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Attachment;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Str;
class ActivityMaterialMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public string $guide;
public string $guideName;
public string $karaoke;
public string $karaokeName;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(protected Activity $activity, protected User $user)
{
$this->queue = 'mail';
$this->guide = data_get($this->activity, 'expand.guide_source.url', '');
$this->guideName = $this->getFileName('guide');
$this->karaoke = data_get($this->activity, 'expand.karaoke_source.url', '');
$this->karaokeName = $this->getFileName('karaoke');
$this->to($this->user->getAttribute('email'), $this->user->getAttribute('nick_name'));
}
/**
* @return int
*/
protected function getActivityType(): int
{
return $this->activity->getAttribute('song_type');
}
/**
* @return string
*/
protected function getActivityName(): string
{
return $this->activity->getAttribute('song_name');
}
/**
* @return string
*/
protected function getActivityLyric(): string
{
return $this->activity->getAttribute('lyric');
}
/**
* @param string $type
* @return string
*/
protected function getFileName(string $type): string
{
return match ($type) {
'guide' => $this->getActivityType() === ActivitySongTypeEnum::SONG->value ? '导唱(含人声)' : '音频',
'karaoke' => $this->getActivityType() === ActivitySongTypeEnum::SONG->value ? '伴奏(不含人声)' : '伴奏'
};
}
/**
* @param string $url
* @return string
*/
protected function getFileType(string $url): string
{
return Str::afterLast($url, '.');
}
/**
* Get the message envelope.
*
* @return \Illuminate\Mail\Mailables\Envelope
*/
public function envelope(): Envelope
{
return new Envelope(
subject: '【泡泡留声】《' . $this->getActivityName() . '》- 物料资源',
);
}
/**
* Get the message content definition.
*
* @return \Illuminate\Mail\Mailables\Content
*/
public function content(): Content
{
return new Content(
view: 'emails.activity-material',
with: [
'userName' => $this->user->getAttribute('nick_name'),
'songName' => $this->getActivityName(),
'lyric' => $this->getActivityLyric(),
]
);
}
// public function attachments(): array
// {
// $data = [Attachment::fromPath($this->guide)->as($this->guideName . '.' . $this->getFileType($this->guide))->withMime('audio/*')];
//
// if ($this->karaoke) {
// $data[] = Attachment::fromPath($this->karaoke)->as($this->karaokeName . '.' . $this->getFileType($this->karaoke))->withMime('audio/*');
// }
//
// if ($this->getActivityLyric()) {
// $data[] = Attachment::fromData(fn() => $this->getActivityLyric(), '歌词.lyric');
// }
//
// return $data;
// }
}