Template.php 3.9 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 短信模板类:各种业务发送短信的模板
+-----------------------------------------------------------------------------------------------------------------------
 * PHP version 7
 *
 * @category  App\Sms
 * @package   App\Sms
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年6月15日,16:08:39
 * @copyright 2021-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Sms;

use App\Models\Lock\Lock;
use App\Models\Lock\LockPassword;
use App\Models\Warning;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;

/**
 * Class Template
 *
 * @category  App\Sms
 * @package   App\Sms
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年6月15日,16:08:39
 * @copyright 2021-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
class Template
{
    const SIGN = "云旅馆";

    const MESSAGE_OPTIONS = [
        // 开锁密码
        LockPassword::OBJ_NAME => [
            // 租客开锁密码(长期)
            'tenant' => [
                'content' => '【' . self::SIGN . '】' . '尊敬的租客,您所租的{s50}开锁密码是:{s12},有效期为{s20}至{s20},请妥善保管好您的密码!',
            ],
        ],
        // 火警提醒
        Warning::OBJ_NAME => [
            // 火警报警
            'warning' => [
                'content' => '【' . self::SIGN . '】' . '{s50}发生火灾,火警等级{s8},设备编号:{s10},临时开锁密码:{s12},请立即处理!',
            ],

        ],
    ];

    /**
     * 获取消息发送的模板
     *
     * @param string $module  模块
     * @param Model  $model   对象模型
     * @param string $target  发送目标
     * @param string $operate 操作
     *
     * @return array
     */
    public static function getTemplate($module, $model, $target = 'user', $operate = 'warning')
    {
        $options = Arr::get(self::MESSAGE_OPTIONS, $module. '.'. $operate);

        $params = [];
        switch ($module) {
            case Warning::OBJ_NAME:
                $params = app(WarningTemplate::class)->getParams($model, $target);
                break;
            case LockPassword::OBJ_NAME:
                $params = LockPasswordTemplate::getParams($model, $target);
                break;
            default:
        }
        return [
//            'mobile'    => $model->$target->mobile,
            'content'   => Arr::get($options, 'content'),
            'params'    => Arr::get($params, 'params'),
            'template'  => Arr::get($params, 'template'),
            'type'      => $module . '_' . $target . '_' . $operate,
        ];
    }

    /**
     * 获取消息发送的内容
     *
     * @param string $module 模块
     * @param Model $model 对象模型
     * @param string $target
     * @param string $operate 操作
     *
     * @return array
     */
    public static function getContent($module, $model, $target = 'users', $operate = 'warning')
    {
        $options = Arr::get(self::MESSAGE_OPTIONS, $module. '.'. $operate);

        $mobiles = $model->$target->map(function ($item) {
            return $item->mobile;
        })->all();

        $params = [];
        switch ($module) {
            case Warning::OBJ_NAME:
                $params = WarningTemplate::getParams($model, $target);
                break;
            case Lock::OBJ_NAME:
                //
                break;
            default:
        }

        return [
            'mobiles' => $mobiles,
            'content' => Arr::get($options, 'content'),
            'params' => $params,
            'type' => $module . '_' . $target . '_' . $operate,
        ];
    }
}