MessageTemplateEntity.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixDec\Message\Entity;
  4. use think\Model;
  5. /**
  6. * 消息模板实体类
  7. */
  8. class MessageTemplateEntity extends Model
  9. {
  10. // 设置表名
  11. protected $name = 'message_templates';
  12. // 设置字段信息
  13. protected $schema = [
  14. 'id' => 'int',
  15. 'code' => 'string',
  16. 'name' => 'string',
  17. 'title' => 'string',
  18. 'content' => 'string',
  19. 'type' => 'int',
  20. 'status' => 'int',
  21. 'create_time' => 'int',
  22. 'update_time' => 'int',
  23. ];
  24. // 自动时间戳
  25. protected $autoWriteTimestamp = true;
  26. /**
  27. * 获取模板类型文本
  28. */
  29. public function getTypeTextAttr()
  30. {
  31. $types = [
  32. 1 => '系统通知',
  33. 2 => '活动通知',
  34. 3 => '订单通知',
  35. 4 => '物流通知',
  36. 5 => '私信',
  37. 6 => '其他',
  38. ];
  39. return $types[$this->type] ?? '未知类型';
  40. }
  41. /**
  42. * 获取状态文本
  43. */
  44. public function getStatusTextAttr()
  45. {
  46. $statuses = [
  47. 0 => '禁用',
  48. 1 => '启用',
  49. ];
  50. return $statuses[$this->status] ?? '未知状态';
  51. }
  52. /**
  53. * 解析模板内容
  54. *
  55. * @param array $data 替换数据
  56. * @return array 解析后的标题和内容
  57. */
  58. public function parse(array $data)
  59. {
  60. $title = $this->title;
  61. $content = $this->content;
  62. // 替换变量
  63. foreach ($data as $key => $value) {
  64. $title = str_replace('{' . $key . '}', $value, $title);
  65. $content = str_replace('{' . $key . '}', $value, $content);
  66. }
  67. return [
  68. 'title' => $title,
  69. 'content' => $content,
  70. ];
  71. }
  72. }