Driver.php 1.7 KB

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