ShortLetterRepositories.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\admin\model\sms\SmsRecord;
  4. use crmeb\services\AlismsService;
  5. use crmeb\services\sms\Sms;
  6. use crmeb\services\ZjSMSServerService;
  7. use PHPMailer\PHPMailer\PHPMailer;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\Exception;
  11. use think\facade\Config;
  12. use think\facade\Log;
  13. /**
  14. * 短信发送
  15. * Class ShortLetterRepositories
  16. * @package crmeb\repositories
  17. */
  18. class ShortLetterRepositories
  19. {
  20. /**
  21. * 发送短信
  22. * @param $switch 发送开关
  23. * @param $phone 手机号码
  24. * @param array $data 模板替换内容
  25. * @param string $template 模板编号
  26. * @param string $logMsg 错误日志记录
  27. * @return bool|string
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. */
  31. public static function send($switch, $phone, array $data, string $template, $logMsg = '')
  32. {
  33. if ($switch && $phone) {
  34. $sms = new Sms([
  35. 'sms_account' => sys_config('sms_account'),
  36. 'sms_token' => sys_config('sms_token'),
  37. 'site_url' => sys_config('site_url')
  38. ]);
  39. $res = $sms->send($phone, $template, $data);
  40. if ($res === false) {
  41. $errorSmg = $sms->getError();
  42. Log::info($logMsg ?? $errorSmg);
  43. return $errorSmg;
  44. } else {
  45. SmsRecord::sendRecord($phone, $res['data']['content'], $res['data']['template'], $res['data']['id']);
  46. }
  47. return true;
  48. } else {
  49. return false;
  50. }
  51. }
  52. /**
  53. * 发送短信
  54. * @param string $phone 手机号码
  55. * @param array $data 模板替换内容
  56. * @param string $template 模板编号
  57. * @return bool|string
  58. * @throws DataNotFoundException
  59. * @throws ModelNotFoundException
  60. */
  61. public static function AliSend(string $phone, array $data, string $template, $site_id = 0)
  62. {
  63. try {
  64. $sms = new AlismsService(sys_config('sms_account', '', true), sys_config('sms_token', '', true));
  65. $res = $sms->setAction('SendSms')->setOptions([
  66. 'PhoneNumbers' => $phone,
  67. 'SignName' => '六牛',
  68. 'TemplateCode' => Config::get('sms.stores.aliyun.template_id.' . $template, 'sms.stores.aliyun.template_id.DEFAULT'),
  69. 'TemplateParam' => json_encode($data),
  70. ])->execute();
  71. // var_dump($res);
  72. // exit;
  73. if ($res['Code'] != 'OK') {
  74. return $res['Message'];
  75. } else {
  76. SmsRecord::sendRecord($phone, $data['code'], $template, '', $res['BizId'], $site_id);
  77. }
  78. return true;
  79. } catch (Exception $exception) {
  80. // Log::info($exception->getMessage());
  81. return $exception->getMessage();
  82. }
  83. }
  84. /**
  85. * 发送短信
  86. * @param string $phone 手机号码
  87. * @param array $data 模板替换内容
  88. * @param string $template 模板编号
  89. * @return bool|string
  90. * @throws DataNotFoundException
  91. * @throws ModelNotFoundException
  92. */
  93. public static function NewSmsSend(string $phone, array $data, string $template, $site_id = 0)
  94. {
  95. try {
  96. $res = ZjSMSServerService::send($phone, $data);
  97. // var_dump($res);
  98. // exit;
  99. if ($res['status'] != '200') {
  100. return $res['msg'];
  101. } else {
  102. SmsRecord::sendRecord($phone, $data['code'], $template, '', '', $site_id);
  103. }
  104. return true;
  105. } catch (Exception $exception) {
  106. // Log::info($exception->getMessage());
  107. return $exception->getMessage();
  108. }
  109. }
  110. /**
  111. * 发送短信
  112. * @param string $phone 手机号码
  113. * @param array $data 模板替换内容
  114. * @param string $template 模板编号
  115. * @return bool|string
  116. * @throws DataNotFoundException
  117. * @throws ModelNotFoundException
  118. */
  119. public static function EmailSend(string $phone, array $data, $site_id)
  120. {
  121. $mail = new PHPMailer(true);
  122. try {
  123. $mail->CharSet = $mail::CHARSET_UTF8;
  124. //Server settings
  125. // $mail->SMTPDebug = SMTP::DEBUG_SERVER;
  126. $mail->isSMTP();
  127. $mail->Host = 'smtp.163.com';
  128. $mail->SMTPAuth = true;
  129. $mail->Username = 'mybotao@163.com';
  130. $mail->Password = 'CBVRANJUYSJLYQVT';
  131. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
  132. $mail->Port = 465;
  133. //Recipients
  134. $mail->setFrom('mybotao@163.com', '星拼乐');
  135. $mail->addAddress($phone); //Add a recipient
  136. //Content
  137. $mail->isHTML(true); //Set email format to HTML
  138. $mail->Subject = '【星拼乐】验证码';
  139. $mail->Body = '您的验证码为:' . $data['code'] . ',若非本人操作,请勿泄露。验证码30分钟内有效。';
  140. $res = $mail->send();
  141. if ($res) {
  142. SmsRecord::sendRecord($phone, $data['code'], 'DEFAULT', '', '', $site_id);
  143. return true;
  144. } else
  145. return $mail->ErrorInfo;
  146. } catch (\PHPMailer\PHPMailer\Exception $exception) {
  147. // Log::info($exception->getMessage());
  148. return $exception->getMessage();
  149. }
  150. }
  151. }