Notification.php
8.0 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 模型层:系统消息模型类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Models\System
* @package App\Models\System
* @author Richer <yangzi1028@163.com>
* @date 2019年10月14日,21:12:49
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Models\System;
use App\Models\Order\Order;
use App\Models\User\Role;
use App\Models\Wishlist\WishlistMailedRecord;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
/**
* Class Notification
*
* @category App\Models\System
* @package App\Models\System
* @author Richer <yangzi1028@163.com>
* @date 2020年11月12日09:36:38
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://carrylg.dev.com License
* @link http://carrylg.dev.com
*/
class Notification extends DatabaseNotification
{
// 指定数据库表
const TABLE = 'notifications';
protected $table = self::TABLE ;
/**
* 访问器被附加到模型数组的形式。
*
* @var array
*/
protected $appends = ['is_read','type_zh','title','content'];
const TEMPLATE = [
'UserFollow' =>'{nickname} 关注了您!',
// 愿望清单
'WishlistSent' => '{customer}客户的愿望清单已提交,请进行分配!',
'WishlistAllocated' => [
'tourist' => '尊敬的{customer}客户,您的愿望清单厂家已分配,请等待后续的进一步处理!',
'employee' => '有新分配的愿望清单,请尽快处理!',
],
'WishlistHandled' => '尊敬的{customer}客户,您的愿望清单厂家已处理,请等待后续的进一步处理!',
'WishlistMailed' => '尊敬的{customer}客户,您的愿望清单厂家已寄出,请留意!',
'WishlistCompleted' => '尊敬的{customer}客户,您的愿望清单已完成。期待您的下一次合作!',
];
/**
* 多态关联 创建订单的产品
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
/* public function notifiable()
{
return $this->morphTo();
}*/
/**
* 根据当前的消息类型获取消息的模板
*
* @param string $type 类型
*
* @return string
*/
public static function getTemplate($type)
{
$tpl = self::TEMPLATE;
$content ='';
foreach ($tpl as $key => $value) {
if (Str::endsWith($type, $key)) {
$content = $value;
break;
}
}
return $content;
}
/**
* 获取消息正文
*
* @param string $value value
*
* @return mixed|string
*/
public function getContentAttribute($value)
{
return self::transformContent($this->type, $this->data);
}
/**
* 获取
*
* @param string $value value
*
* @return mixed|string
*/
public function getDataAttribute($value)
{
return json_decode($value, true) ?: [];
}
/**
* 获取
*
* @param string $value value
*
* @return mixed|string
*/
public function getIsReadAttribute($value)
{
return $this->read_at !== null ? 1 : 0;
}
/**
* 获取
*
* @param string $value value
*
* @return mixed|string
*/
public function getTypeZhAttribute($value)
{
//dump($this->type);
//return app($this->type)->OBJ_NAME;
}
/**
* 获取 通知标题
*
* @return mixed|string
*/
public function getTitleAttribute()
{
return self::transformTitle($this->type, $this->data);
}
/**
* 根据消息内容获取模板
*
* @param string $type 类型
* @param array $data 数据
*
* @return mixed|string
*/
public static function transformTitle($type, $data)
{
$title = Notification::getTemplate($type);
$name = Arr::get($data, 'name');
$title = str_replace('{name}', $name, $title);
$username = Arr::get($data, 'follow_user_name');
$title = str_replace('{nickname}', $username, $title);
$nickname = Arr::get($data, 'nickname');
$title = str_replace('{nickname}', $nickname, $title);
$user = Arr::get(Arr::get($data, 'user'), 'truename');
if (!$user) {
$user = Arr::get(Arr::get($data, 'user'), 'nickname');
}
$title = str_replace('{user}', $user, $title);
$title = str_replace('{customer}', $user, $title);
if (is_array($title)) {
$user = \Auth::user();
return Arr::get($title, $user->role ?? Role::TOURIST);
}
return $title;
}
/**
* 根据消息内容获取模板
*
* @param string $type 类型
* @param array $data 数据
*
* @param bool $is_html
* @return mixed|string
*/
public static function transformContent($type, $data, $is_html = true)
{
$content = [];
$product = Arr::get($data, 'product');
$product['cover_image'] = splice_file_path($product['cover_image']);
switch (true) {
case Str::endsWith($type, 'WishlistHandled'):// 愿望清单处理好后
$content['product'] = $product;
$handle_records = Arr::get($data, 'handle_records');
$content['title'] = Arr::get($handle_records, 'title');
$content['handled_at'] = Arr::get($handle_records, 'handled_at');
$content['handler'] = Arr::get($handle_records, 'handler');
$content['items'] = Arr::get($handle_records, 'items');
break;
case Str::endsWith($type, 'WishlistMailed'):// 愿望清单寄出处理好后
$mailed_records = Arr::get($data, 'mailed_records');
$items = Arr::get($mailed_records, 'items');
$mailed_length = 0;
if ($items && is_array($items)) {
foreach ($items as $item) {
$mailed_length += Arr::get($item, 'mailed_length');
}
}
$content['product'] = $product;
// $content['mailed_records'] = Arr::get($data, 'mailed_records');
// $content['express_company'] = Arr::get($data, 'mailed_records.express_company');
$content['express_company'] = Arr::get(WishlistMailedRecord::EXPRESS_COMPANY_OPTIONS, Arr::get($mailed_records, 'express_company'));
$content['express_number'] = Arr::get($mailed_records, 'express_number');
$content['mailed_at'] = Arr::get($mailed_records, 'mailed_at');
$content['mailed_length'] = $mailed_length;
$content['unit'] = Arr::get(config('constants.UNIT_OPTIONS'), config('constants.METER'));
$content['mailed_type'] = Arr::get(WishlistMailedRecord::TYPE_OPTIONS, Arr::get($mailed_records, 'type') ?? 1);
// $content['mailer'] = Arr::get($data, 'mailed_records.mailer');
$content['items'] = $items;
break;
case Str::endsWith($type, 'WishlistAllocated')://
$content['product'] = $product;
$handle_records = Arr::get($data, 'handleRecords');
// $content['title'] = Arr::get($handle_records, 'title');
$content['handled_at'] = Arr::get($handle_records, 'handled_at');
// $content['handler'] = Arr::get($handle_records, 'handler');
// $content['items'] = Arr::get($handle_records, 'items');
break;
}
return $content;
}
}