Message.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. try {
  38. $serviceInstance = self::getInstance($conf['class'], 'IS_MESSAGE_SERVICE');
  39. if (!$serviceInstance instanceof MessageServiceInterface) {
  40. throw new \Exception(sprintf(lang('100056'), $conf['class']));
  41. }
  42. if ($serviceInstance->$method(...$params) && !$result) { // 任一方式送信成功即为成功
  43. $result = true;
  44. }
  45. } catch (\Throwable $e) {
  46. system_log(sprintf('%s送信失败:%s', $conf['name'], $e->getMessage()));
  47. }
  48. }
  49. self::$notEnabledTips = false;
  50. return $result;
  51. }
  52. }