BaseMessage.php 3.2 KB

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