NotificationTransformer.php
3.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
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 数据转换层: 系统消息通知转换类
+-----------------------------------------------------------------------------------------------------------------------
*
* @copyright Copyright
* @author Richer
* @package App\Transformers\System
* @version 2019年10月14日,21:12:49
* @link
*/
namespace App\Transformers\System;
use App\Models\System\Notification;
use App\Transformers\BaseTransformer;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
class NotificationTransformer extends BaseTransformer
{
public function transform(Model $model)
{
return [
'id' => (string) $model->id,
/* place your other model properties here */
'type' => (string)$model->type,
'type_zh' => (string)$model->type_zh,
'notifiable_type' => (string)$model->notifiable_type,
'notifiable_id' => (int)$model->notifiable_id,
'title' => Notification::transformTitle($model->type, $model->data),
'content' => Notification::transformContent($model->type, $model->data),
'read_at' => format_date($model->read_at, 'Y-m-d H:i:s'),
'is_read' => $model->read_at ? 1 : 0,
'created_at' => format_date($model->created_at, 'Y-m-d H:i:s'),
'notice_time' => $model->created_at->diffForHumans(),
];
}
/**
* 根据消息内容获取模板
* @param $type
* @param $data
* @return mixed|string
*/
private function transformContentZH($type, $model)
{
$data = $model->data;
$content = Notification::getTemplate($type);
$type = Arr::get($data, 'type');
if (is_array($content)) {
$content = Arr::get($content, $type);
}
//$content = str_replace('{type}', $type, $content);
$name = Arr::get($data, 'name');
$content = str_replace('{name}', $name, $content);
$username = Arr::get($data, 'follow_user_name');
$content = str_replace('{nickname}', $username, $content);
$title = Arr::get($data, 'title');
$content = str_replace('{title}', $title, $content);
$nickname = $model->nickname;
$title = Arr::get($data, 'title');
$content = str_replace('{nickname}', $nickname, $content);
// 转换
$number = Arr::get($data, 'number');
$content = str_replace('{number}', $number, $content);
$contact = Arr::get($data, 'contact');
$content = str_replace('{contact}', $contact, $content);
$contact_phone = Arr::get($data, 'contact_phone');
$content = str_replace('{contact_phone}', $contact_phone, $content);
return $content;
}
}