Message.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * @author mybsdc <[email protected]>
  4. * @date 2021/10/20
  5. * @time 13:46
  6. */
  7. namespace Luolongfei\Libs;
  8. use Luolongfei\Libs\Connector\MessageServiceInterface;
  9. /**
  10. * Class Message
  11. *
  12. * @method bool send(string $content, string $subject = '', int $type = 1, array $data = [], ?string $recipient = null, ...$params) 送信
  13. */
  14. abstract class Message extends Base
  15. {
  16. /**
  17. * @param $method
  18. * @param $params
  19. *
  20. * @return bool
  21. * @throws \Exception
  22. */
  23. public static function __callStatic($method, $params)
  24. {
  25. $result = false;
  26. foreach (config('message') as $conf) {
  27. if ($conf['enable'] !== 1) {
  28. if ($conf['not_enabled_tips']) { // 仅在存在配置的送信项未启用的情况下提醒
  29. system_log(sprintf('由于没有启用「%s」功能,故本次不通过「%s」送信,尽管检测到相关配置。', $conf['name'], $conf['name']));
  30. }
  31. continue;
  32. }
  33. $serviceInstance = self::getInstance($conf['class'], 'IS_MESSAGE_SERVICE');
  34. if (!$serviceInstance instanceof MessageServiceInterface) {
  35. throw new \Exception(sprintf('消息服务类 %s 必须继承并实现 MessageServiceInterface 接口', $conf['class']));
  36. }
  37. if ($serviceInstance->$method(...$params) && !$result) { // 任一方式送信成功即为成功
  38. $result = true;
  39. }
  40. }
  41. return $result;
  42. }
  43. }