ShortLetterRepositories.php 5.1 KB

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