ShortLetterRepositories.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 PHPMailer\PHPMailer\PHPMailer;
  7. use PHPMailer\PHPMailer\SMTP;
  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 DataNotFoundException
  29. * @throws 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)
  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']);
  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 EmailSend(string $phone, array $data)
  94. {
  95. $mail = new PHPMailer(true);
  96. try {
  97. //Server settings
  98. // $mail->SMTPDebug = SMTP::DEBUG_SERVER;
  99. $mail->isSMTP();
  100. $mail->Host = 'smtp.163.com';
  101. $mail->SMTPAuth = true;
  102. $mail->Username = 'mybotao@163.com';
  103. $mail->Password = 'CBVRANJUYSJLYQVT';
  104. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
  105. $mail->Port = 465;
  106. //Recipients
  107. $mail->setFrom('mybotao@163.com', 'BTEX集市');
  108. $mail->addAddress($phone); //Add a recipient
  109. //Content
  110. $mail->isHTML(true); //Set email format to HTML
  111. $mail->Subject = '【BTEX】验证码';
  112. $mail->Body = '您的验证码为:' . $data['code'] . ',若非本人操作,请勿泄露。验证码30分钟内有效。';
  113. $res = $mail->send();
  114. if ($res) {
  115. SmsRecord::sendRecord($phone, $data['code'], 'DEFAULT', '', '');
  116. return true;
  117. } else
  118. return $mail->ErrorInfo;
  119. } catch (\PHPMailer\PHPMailer\Exception $exception) {
  120. // Log::info($exception->getMessage());
  121. return $exception->getMessage();
  122. }
  123. }
  124. }