Message.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. * @var bool 防止同一个执行周期里每次送信都提示未启用
  18. */
  19. protected static $notEnabledTips = true;
  20. /**
  21. * @param $method
  22. * @param $params
  23. *
  24. * @return bool
  25. * @throws \Exception
  26. */
  27. public static function __callStatic($method, $params)
  28. {
  29. $result = false;
  30. foreach (config('message') as $conf) {
  31. if ($conf['enable'] !== 1) {
  32. if ($conf['not_enabled_tips'] && self::$notEnabledTips) { // 仅在存在配置的送信项未启用的情况下提醒
  33. system_log(sprintf(lang('100055'), $conf['name'], $conf['name']));
  34. }
  35. continue;
  36. }
  37. $serviceInstance = self::getInstance($conf['class'], 'IS_MESSAGE_SERVICE');
  38. if (!$serviceInstance instanceof MessageServiceInterface) {
  39. throw new \Exception(sprintf(lang('100056'), $conf['class']));
  40. }
  41. if ($serviceInstance->$method(...$params) && !$result) { // 任一方式送信成功即为成功
  42. $result = true;
  43. }
  44. }
  45. self::$notEnabledTips = false;
  46. return $result;
  47. }
  48. }