截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Driver.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  22. * 短信驱动
  23. * Class Driver
  24. * @package app\common\server\smsEngine
  25. */
  26. class Driver
  27. {
  28. protected $config;
  29. protected $smsEngine;
  30. protected $defaultEngine;
  31. protected $error;
  32. public function getError()
  33. {
  34. return $this->error;
  35. }
  36. public function __construct()
  37. {
  38. $this->initialize();
  39. }
  40. /**
  41. * @notes 初始化配置
  42. * @throws \Exception
  43. * @author 段誉
  44. * @date 2022/1/10 15:47
  45. */
  46. public function initialize()
  47. {
  48. $defaultEngine = ConfigServer::get('sms_driver', 'default', '');
  49. if (empty($defaultEngine)) {
  50. throw new \Exception('请开启短信配置');
  51. }
  52. $this->defaultEngine = $defaultEngine;
  53. $classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst($defaultEngine.'Sms');
  54. if (!class_exists($classSpace)) {
  55. throw new \Exception('对应短信配置类不存在');
  56. }
  57. $engineConfig = ConfigServer::get('sms_engine', $defaultEngine, []);
  58. if (empty($engineConfig)) {
  59. throw new \Exception('请在后台设置好('.$defaultEngine.')的配置');
  60. }
  61. $this->smsEngine = new $classSpace($engineConfig);
  62. }
  63. /**
  64. * Notes: 发送短信
  65. * @param $mobile
  66. * @param $data
  67. * @author 段誉(2021/6/22 0:42)
  68. * @return bool
  69. */
  70. public function send($mobile, $data)
  71. {
  72. try{
  73. $res = $this->smsEngine
  74. ->setMobile($mobile)
  75. ->setTemplateId($data['template_id'])
  76. ->setTemplateParam($data['param'])
  77. ->send();
  78. if (false === $res) {
  79. throw new \Exception($this->smsEngine->getError());
  80. }
  81. return $res;
  82. } catch (\Exception $e) {
  83. $this->error = $e->getMessage();
  84. return false;
  85. }
  86. }
  87. }