| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?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 更新时间
- */
- /**
- * App\Module\Ulogic\Models\AppMessageTemplate
- *
- * field start
- * @property int $id
- * @property string $name 模板名称
- * @property string $code 模板代码
- * @property string $type 消息类型:system=系统消息,user=用户消息
- * @property string $title 消息标题
- * @property string $content 消息内容
- * @property string $content_type 内容类型:text=纯文本,html=富文本,markdown=MD格式,json=JSON格式
- * @property array $variables 变量定义
- * @property int $allow_reply 是否允许回复:0不允许 1允许
- * @property bool $status 状态:0禁用 1启用
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class AppMessageTemplate extends Model
- {
- /**
- * 数据表名称
- *
- * @var string
- */
- protected $table = 'app_message_templates';
- // attrlist start
- protected $fillable = [
- 'id',
- 'name',
- 'code',
- 'type',
- 'title',
- 'content',
- 'content_type',
- 'variables',
- 'allow_reply',
- 'status',
- ];
- // attrlist end
-
- /**
- * 属性类型转换
- *
- * @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
- ];
- }
- }
|