ShortLetterRepositories.php 1.4 KB

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