ShortLetterRepositories.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\models\sms\SmsRecord;
  4. use crmeb\services\sms\Sms;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\ModelNotFoundException;
  7. use think\facade\Log;
  8. /**
  9. * 短信发送
  10. * Class ShortLetterRepositories
  11. * @package crmeb\repositories
  12. */
  13. class ShortLetterRepositories
  14. {
  15. /**
  16. * 发送短信
  17. * @param bool $switch 发送开关
  18. * @param string $phone 手机号码
  19. * @param array $data 模板替换内容
  20. * @param string $template 模板编号
  21. * @param string $logMsg 错误日志记录
  22. * @return bool|string
  23. * @throws DataNotFoundException
  24. * @throws ModelNotFoundException
  25. */
  26. public static function send($switch, $phone, array $data, string $template, $logMsg = '')
  27. {
  28. if ($switch && $phone) {
  29. $sms = new Sms([
  30. 'sms_account' => sys_config('sms_account'),
  31. 'sms_token' => sys_config('sms_token'),
  32. 'site_url' => sys_config('site_url')
  33. ]);
  34. $res = $sms->send($phone, $template, $data);
  35. if ($res === false) {
  36. $errorSmg = $sms->getError();
  37. Log::info($logMsg ?? $errorSmg);
  38. return $errorSmg;
  39. } else {
  40. SmsRecord::sendRecord($phone, $res['data']['content'], $res['data']['template'], $res['data']['id']);
  41. }
  42. return true;
  43. } else {
  44. return false;
  45. }
  46. }
  47. }