Driver.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\library\sms;
  10. use think\Exception;
  11. /**
  12. * 短信通知模块驱动
  13. * Class driver
  14. * @package app\common\library\sms
  15. */
  16. class Driver
  17. {
  18. private $config; // 配置信息
  19. private $engine; // 当前短信引擎类
  20. private $engineName; // 当前短信引擎名称
  21. /**
  22. * 构造方法
  23. * Driver constructor.
  24. * @param $config
  25. * @throws Exception
  26. */
  27. public function __construct($config)
  28. {
  29. // 配置信息
  30. $this->config = $config;
  31. // 当前引擎名称
  32. $this->engineName = $this->config['default'];
  33. // 实例化当前存储引擎
  34. $this->engine = $this->getEngineClass();
  35. }
  36. /**
  37. * 发送短信通知
  38. * @param $msgType
  39. * @param $templateParams
  40. * @param bool $isTest
  41. * @return bool
  42. */
  43. public function sendSms($msgType, $templateParams, $isTest = false)
  44. {
  45. if ($isTest === false
  46. && $this->config['engine'][$this->engineName][$msgType]['is_enable'] == '0') {
  47. return false;
  48. }
  49. return $this->engine->sendSms($msgType, $templateParams);
  50. }
  51. /**
  52. * 获取错误信息
  53. * @return mixed
  54. */
  55. public function getError()
  56. {
  57. return $this->engine->getError();
  58. }
  59. /**
  60. * 获取当前的存储引擎
  61. * @return mixed
  62. * @throws Exception
  63. */
  64. private function getEngineClass()
  65. {
  66. $classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst($this->engineName);
  67. if (!class_exists($classSpace)) {
  68. throw new Exception('未找到存储引擎类: ' . $this->engineName);
  69. }
  70. return new $classSpace($this->config['engine'][$this->engineName]);
  71. }
  72. }