Driver.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\common\server\sms;
  20. use app\common\server\ConfigServer;
  21. use think\facade\Log;
  22. /**
  23. * 短信驱动
  24. * Class Driver
  25. * @package app\common\server\smsEngine
  26. */
  27. class Driver
  28. {
  29. protected $config;
  30. protected $smsEngine;
  31. protected $defaultEngine;
  32. protected $error;
  33. public function getError()
  34. {
  35. return $this->error;
  36. }
  37. public function __construct()
  38. {
  39. $this->initialize();
  40. }
  41. /**
  42. * @notes 初始化配置
  43. * @throws \Exception
  44. * @author 段誉
  45. * @date 2022/1/10 15:47
  46. */
  47. public function initialize()
  48. {
  49. $defaultEngine = ConfigServer::get('sms_driver', 'default', '');
  50. if (empty($defaultEngine)) {
  51. throw new \Exception('请开启短信配置');
  52. }
  53. $this->defaultEngine = $defaultEngine;
  54. $classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst($defaultEngine.'Sms');
  55. if (!class_exists($classSpace)) {
  56. throw new \Exception('对应短信配置类不存在');
  57. }
  58. $engineConfig = ConfigServer::get('sms_engine', $defaultEngine, []);
  59. if (empty($engineConfig)) {
  60. throw new \Exception('请在后台设置好('.$defaultEngine.')的配置');
  61. }
  62. $this->smsEngine = new $classSpace($engineConfig);
  63. }
  64. /**
  65. * Notes: 发送短信
  66. * @param $mobile
  67. * @param $data
  68. * @author 段誉(2021/6/22 0:42)
  69. * @return bool
  70. */
  71. public function send($mobile, $data)
  72. {
  73. try{
  74. $res = $this->smsEngine
  75. ->setMobile($mobile)
  76. ->setTemplateId($data['template_id'])
  77. ->setTemplateParam($data['param'])
  78. ->send();
  79. if (false === $res) {
  80. throw new \Exception($this->smsEngine->getError());
  81. }
  82. return $res;
  83. } catch (\Throwable $e) {
  84. Log::write($e->__toString(), 'sms_send_base_error');
  85. $this->error = $e->getMessage();
  86. return false;
  87. }
  88. }
  89. }