| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace App\Module\Ulogic\Models;
- use App\Module\AppMessage\Enums\APP_MESSAGE_TYPE;
- use Illuminate\Database\Eloquent\Model;
- /**
- * 应用消息模板模型
- *
- * @property int $id 模板ID
- * @property string $name 模板名称
- * @property string $code 模板代码
- * @property string $type 消息类型
- * @property string $title 消息标题模板
- * @property string $content 消息内容模板
- * @property array $variables 模板变量列表
- * @property bool $status 模板状态
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- */
- class AppMessageTemplate extends Model
- {
- /**
- * 数据表名称
- *
- * @var string
- */
- protected $table = 'app_message_templates';
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- protected $fillable = [
- 'name', // 模板名称
- 'code', // 模板代码
- 'type', // 消息类型
- 'title', // 消息标题模板
- 'content', // 消息内容模板
- 'variables', // 模板变量列表
- 'status' // 模板状态
- ];
- /**
- * 属性类型转换
- *
- * @var array
- */
- protected $casts = [
- 'variables' => 'array',
- 'type' => APP_MESSAGE_TYPE::class,
- 'status' => 'boolean'
- ];
- /**
- * 替换模板变量
- *
- * 使用提供的数据替换模板中的变量占位符
- * 变量格式为 {{变量名}}
- *
- * @param array $data 要替换的变量数据
- * @return array 替换后的标题和内容
- */
- public function replaceVariables(array $data): array
- {
- $title = $this->title;
- $content = $this->content;
- foreach ($data as $key => $value) {
- $title = str_replace('{{' . $key . '}}', $value, $title);
- $content = str_replace('{{' . $key . '}}', $value, $content);
- }
- return [
- 'title' => $title,
- 'content' => $content
- ];
- }
- }
|