NotificationTransformer.php 3.0 KB
<?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;
    }
}