ShortLetterRepositories.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\admin\model\sms\SmsRecord;
  4. use crmeb\services\sms\Sms;
  5. use crmeb\services\ZjSMSServerService;
  6. use think\Exception;
  7. use think\facade\Log;
  8. /**
  9. * 短信发送
  10. * Class ShortLetterRepositories
  11. * @package crmeb\repositories
  12. */
  13. class ShortLetterRepositories
  14. {
  15. /**
  16. * 发送短信
  17. * @param $switch 发送开关
  18. * @param $phone 手机号码
  19. * @param array $data 模板替换内容
  20. * @param string $template 模板编号
  21. * @param string $logMsg 错误日志记录
  22. * @return bool|string
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\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. public static function NewSmsSend(string $phone, array $data, string $template)
  48. {
  49. try {
  50. $res = ZjSMSServerService::send($phone, $data);
  51. // var_dump($res);
  52. // exit;
  53. if ($res['status'] != '200') {
  54. return $res['msg'];
  55. } else {
  56. SmsRecord::sendRecord($phone, $data['code'], $template, '');
  57. }
  58. return true;
  59. } catch (Exception $exception) {
  60. // Log::info($exception->getMessage());
  61. return $exception->getMessage();
  62. }
  63. }
  64. }