BaseMessage.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\template;
  12. use crmeb\basic\BaseStorage;
  13. use think\facade\Config;
  14. abstract class BaseMessage extends BaseStorage
  15. {
  16. /**
  17. * 模板id
  18. * @var array
  19. */
  20. protected $templateIds = [];
  21. /**
  22. * openId
  23. * @var string
  24. */
  25. protected $openId;
  26. /**
  27. * 跳转链接
  28. * @var string
  29. */
  30. protected $toUrl;
  31. /**
  32. * 颜色
  33. * @var string
  34. */
  35. protected $color;
  36. /**
  37. * 初始化
  38. * @param array $config
  39. * @return mixed|void
  40. */
  41. protected function initialize(array $config)
  42. {
  43. $this->templateIds = Config::get($this->configFile . '.stores.' . $this->name . '.template_id', []);
  44. }
  45. /**
  46. * 是否记录日志
  47. * @return mixed
  48. */
  49. public function isLog()
  50. {
  51. $isLog = Config::get($this->configFile . 'isLog', false);
  52. return Config::get($this->configFile . '.stores.' . $this->name . '.isLog', $isLog);
  53. }
  54. /**
  55. * 获取模板id
  56. * @return array
  57. */
  58. public function getTemplateId()
  59. {
  60. return $this->templateIds;
  61. }
  62. /**
  63. * openid
  64. * @param string $openId
  65. * @return $this
  66. */
  67. public function to(string $openId)
  68. {
  69. $this->openId = $openId;
  70. return $this;
  71. }
  72. /**
  73. * 跳转路径
  74. * @param string $url
  75. * @return $this
  76. */
  77. public function url(string $url)
  78. {
  79. $this->toUrl = $url;
  80. return $this;
  81. }
  82. /**
  83. * 设置背景颜色
  84. * @param string $color
  85. * @return $this
  86. */
  87. public function color(?string $color)
  88. {
  89. $this->color = $color;
  90. return $this;
  91. }
  92. /**
  93. * 提取模板code
  94. * @param string $templateId
  95. * @return null
  96. */
  97. protected function getTemplateCode(string $templateId)
  98. {
  99. return $this->templateIds[$templateId] ?? null;
  100. }
  101. /**
  102. * 恢复默认值
  103. */
  104. protected function clear()
  105. {
  106. $this->openId = null;
  107. $this->toUrl = null;
  108. $this->color = null;
  109. }
  110. /**
  111. * 发送消息
  112. * @param string $templateId
  113. * @param array $data
  114. * @return mixed
  115. */
  116. abstract public function send(string $templateId, array $data = []);
  117. /**
  118. * 添加模板
  119. * @param string $shortId
  120. * @return mixed
  121. */
  122. abstract public function add(string $shortId);
  123. /**
  124. * 删除模板
  125. * @param string $templateId
  126. * @return mixed
  127. */
  128. abstract public function delete(string $templateId);
  129. /**
  130. * 获取所有模板
  131. * @return mixed
  132. */
  133. abstract public function list();
  134. }