123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace crmeb\repositories;
- use app\admin\model\sms\SmsRecord;
- use crmeb\services\AlismsService;
- use crmeb\services\sms\Sms;
- use crmeb\services\ZjSMSServerService;
- use PHPMailer\PHPMailer\PHPMailer;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\ModelNotFoundException;
- use think\Exception;
- use think\facade\Config;
- use think\facade\Log;
- /**
- * 短信发送
- * Class ShortLetterRepositories
- * @package crmeb\repositories
- */
- class ShortLetterRepositories
- {
- /**
- * 发送短信
- * @param $switch 发送开关
- * @param $phone 手机号码
- * @param array $data 模板替换内容
- * @param string $template 模板编号
- * @param string $logMsg 错误日志记录
- * @return bool|string
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function send($switch, $phone, array $data, string $template, $logMsg = '')
- {
- if ($switch && $phone) {
- $sms = new Sms([
- 'sms_account' => sys_config('sms_account'),
- 'sms_token' => sys_config('sms_token'),
- 'site_url' => sys_config('site_url')
- ]);
- $res = $sms->send($phone, $template, $data);
- if ($res === false) {
- $errorSmg = $sms->getError();
- Log::info($logMsg ?? $errorSmg);
- return $errorSmg;
- } else {
- SmsRecord::sendRecord($phone, $res['data']['content'], $res['data']['template'], $res['data']['id']);
- }
- return true;
- } else {
- return false;
- }
- }
- /**
- * 发送短信
- * @param string $phone 手机号码
- * @param array $data 模板替换内容
- * @param string $template 模板编号
- * @return bool|string
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- */
- public static function AliSend(string $phone, array $data, string $template, $site_id = 0)
- {
- try {
- $sms = new AlismsService(sys_config('sms_account', '', true), sys_config('sms_token', '', true));
- $res = $sms->setAction('SendSms')->setOptions([
- 'PhoneNumbers' => $phone,
- 'SignName' => '六牛',
- 'TemplateCode' => Config::get('sms.stores.aliyun.template_id.' . $template, 'sms.stores.aliyun.template_id.DEFAULT'),
- 'TemplateParam' => json_encode($data),
- ])->execute();
- // var_dump($res);
- // exit;
- if ($res['Code'] != 'OK') {
- return $res['Message'];
- } else {
- SmsRecord::sendRecord($phone, $data['code'], $template, '', $res['BizId'], $site_id);
- }
- return true;
- } catch (Exception $exception) {
- // Log::info($exception->getMessage());
- return $exception->getMessage();
- }
- }
- /**
- * 发送短信
- * @param string $phone 手机号码
- * @param array $data 模板替换内容
- * @param string $template 模板编号
- * @return bool|string
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- */
- public static function NewSmsSend(string $phone, array $data, string $template, $site_id = 0)
- {
- try {
- $res = ZjSMSServerService::send($phone, $data);
- // var_dump($res);
- // exit;
- if ($res['status'] != '200') {
- return $res['msg'];
- } else {
- SmsRecord::sendRecord($phone, $data['code'], $template, '', '', $site_id);
- }
- return true;
- } catch (Exception $exception) {
- // Log::info($exception->getMessage());
- return $exception->getMessage();
- }
- }
- /**
- * 发送短信
- * @param string $phone 手机号码
- * @param array $data 模板替换内容
- * @param string $template 模板编号
- * @return bool|string
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- */
- public static function EmailSend(string $phone, array $data, $site_id)
- {
- $mail = new PHPMailer(true);
- try {
- $mail->CharSet = $mail::CHARSET_UTF8;
- //Server settings
- // $mail->SMTPDebug = SMTP::DEBUG_SERVER;
- $mail->isSMTP();
- $mail->Host = 'smtp.163.com';
- $mail->SMTPAuth = true;
- $mail->Username = 'mybotao@163.com';
- $mail->Password = 'CBVRANJUYSJLYQVT';
- $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
- $mail->Port = 465;
- //Recipients
- $mail->setFrom('mybotao@163.com', '星拼乐');
- $mail->addAddress($phone); //Add a recipient
- //Content
- $mail->isHTML(true); //Set email format to HTML
- $mail->Subject = '【星拼乐】验证码';
- $mail->Body = '您的验证码为:' . $data['code'] . ',若非本人操作,请勿泄露。验证码30分钟内有效。';
- $res = $mail->send();
- if ($res) {
- SmsRecord::sendRecord($phone, $data['code'], 'DEFAULT', '', '', $site_id);
- return true;
- } else
- return $mail->ErrorInfo;
- } catch (\PHPMailer\PHPMailer\Exception $exception) {
- // Log::info($exception->getMessage());
- return $exception->getMessage();
- }
- }
- }
|