Notification.php 8.0 KB
<?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;
    }
}