ShortLetterRepositories.php 1.5 KB

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