Browse Source

开票信息 开票成功 邮箱推送消息

master
wanghongjun 1 year ago
parent
commit
7a79723735
  1. 20
      app/controller/task/InvoiceIssuanceTask.php
  2. 10
      app/model/EmailSendLog.php
  3. 15
      app/service/invoice/InvoiceIssuanceService.php
  4. 142
      app/service/user/EmailService.php
  5. 28
      app/service/user/SmsService.php

20
app/controller/task/InvoiceIssuanceTask.php

@ -4,6 +4,9 @@ namespace app\controller\task;
use app\model\InvoiceIssuance as InvoiceIssuanceModel;
use app\service\invoice\InvoiceIssuanceService;
use app\service\user\EmailService;
use app\service\user\SmsService;
use think\facade\Request;
class InvoiceIssuanceTask extends Base
{
@ -21,7 +24,22 @@ class InvoiceIssuanceTask extends Base
$where = ['status' => 0, 'delete_time' => 0];
$InvoiceIssuanceData = (new InvoiceIssuanceModel())->where($where)->page(1, $limit)->select();
foreach ($InvoiceIssuanceData as $InvoiceIssuanceValue) {
(new InvoiceIssuanceService())->IssueAnInvoice($InvoiceIssuanceValue);
$result = (new InvoiceIssuanceService())->IssueAnInvoice($InvoiceIssuanceValue);
if ($result === true) {
if ($InvoiceIssuanceValue['email']) {
// 发送发票到邮箱
$path = InvoiceIssuanceService::getQrCode($InvoiceIssuanceValue['id']);
$emailResult = (new EmailService())->sendInvoice($InvoiceIssuanceValue['email'], '发票', $path);
if ($emailResult === true && $InvoiceIssuanceValue['mobile']) {
// 发送短信
$SmsService = new SmsService();
$SmsService->sendSms($InvoiceIssuanceValue['mobile'], $SmsService->smsInvoiceTemplate($InvoiceIssuanceValue['email']));
}
}
}
}
return $this->buildSuccess();
}

10
app/model/EmailSendLog.php

@ -0,0 +1,10 @@
<?php
namespace app\model;
use think\Model;
class EmailSendLog extends Model
{
}

15
app/service/invoice/InvoiceIssuanceService.php

@ -130,6 +130,17 @@ class InvoiceIssuanceService
return $FeeService->getComputeDetail($param);
}
/**
*
* @param $id
* @return string
*/
public static function getDownFileUrl($id): string
{
$url = Request::domain();
return $url . '/api/InvoiceIssuance/downFile?id='.$id;
}
/**
* 获取二维码
* @param $id
@ -137,9 +148,7 @@ class InvoiceIssuanceService
*/
public static function getQrCode($id): string
{
$url = Request::domain();
$codeUrl = $url . '/api/InvoiceIssuance/downFile?id='.$id;
$codeUrl = self::getDownFileUrl($id);
$result = Builder::create()
->writer(new PngWriter())

142
app/service/user/EmailService.php

@ -0,0 +1,142 @@
<?php
namespace app\service\user;
use app\model\AdminConfig;
use app\model\EmailSendLog;
use PHPMailer\PHPMailer\PHPMailer;
/**
* 发送邮箱
* 需用用到扩展 composer require phpmailer/phpmailer
*/
class EmailService
{
/**
*
* [
* 'Host'=>"smtp.163.com",
* 'Port'=>"465",
* 'Username'=>"xxx@163.com",
* 'Password'=>"YGFDAVZVRGIQWBXC",
* ],
* [
* 'Host'=>"smtp.qq.com",
* 'Port'=>"465",
* 'Username'=>"xxx@qq.com",
* 'Password'=>"oxdkempurasfbjjd",
* ],
* @var array
*/
public $mailList = [];
public function __construct()
{
// 取数据库配置
$AdminConfig = new AdminConfig();
$emailContentJson = $AdminConfig->where('title', 'email')->where('delete_time', 0)->value('content');
if ($emailContentJson) {
$emailContentArr = json_decode($emailContentJson, true);
foreach ($emailContentArr as $emailContent) {
if ($emailContent['title'] == 'QQ邮箱' && !empty($emailContent['email']) && !empty($emailContent['token'])) {
$this->mailList[] = [
'Host' => "smtp.qq.com",
'Port' => "465",
'Username' => $emailContent['email'],
'Password' => $emailContent['token'],
];
}
if ($emailContent['title'] == '网易邮箱' && !empty($emailContent['email']) && !empty($emailContent['token'])) {
$this->mailList[] = [
'Host' => "smtp.163.com",
'Port' => "465",
'Username' => $emailContent['email'],
'Password' => $emailContent['token'],
];
}
}
}
}
/**
*
* @param $toEmail
* @param $bookName
* @param $path
* @return bool|string|void
*/
public function sendInvoice($toEmail, $bookName, $path)
{
try {
if (empty($this->mailList)) {
throw new \Exception('邮箱未配置');
}
$EmailSendLog = new EmailSendLog();
$list = [];
foreach ($this->mailList as $i => $m) {
$time = strtotime(date("Y-m-d"), time());
$errorCount = $EmailSendLog->where(['username' => $m['Username'], 'status' => 0])->whereTime('create_time', $time)->count();
if ($errorCount < 10) {//错误超过十条记录选择下一个邮箱
$logsCount = $EmailSendLog->where(['username' => $m['Username']])->whereTime('create_time', $time)->count();
if ($logsCount < 590) {//每日限量六百超过六百选择下一个邮箱
$list = $m;
break;
}
}
}
if ($list) {
$status = $this->sendEmail($toEmail, $bookName, $path, $list['Username'], $list['Password'], $list['Host'], $list['Port']);
$EmailSendLog->insert(['toemail' => $toEmail, 'bookname' => $bookName, 'path' => $path, 'username' => $list['Username'], 'status' => $status, 'create_time' => time()]);
if ($status == 1) {
return true;
} elseif ($status == 0) {
throw new \Exception('发送失败,请稍后再试');
}
} else {
throw new \Exception('邮件全部额度使用完毕');
}
} catch (\Exception $e) {
return $e->getMessage();
}
}
/**
* 发送邮箱
* @param $toEmail
* @param $bookName
* @param $path
* @param $Username
* @param $Password
* @param $Host
* @param $Port
* @return bool|string
*/
public function sendEmail($toEmail, $bookName, $path, $Username, $Password, $Host, $Port)
{
try {
$mail = new PHPMailer(true);
$mail->isSMTP(); // 使用SMTP服务
$mail->CharSet = "utf8"; // 编码格式为utf8,不设置编码的话,中文会出现乱码
$mail->Host = $Host; // 发送方的SMTP服务器地址
$mail->SMTPAuth = true; // 是否使用身份验证
$mail->Username = $Username; // 发送方的163邮箱用户名,就是你申请163的SMTP服务使用的163邮箱</span><span style="color:#333333;">
$mail->Password = $Password; // 发送方的邮箱密码,注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码!</span><span style="color:#333333;">
$mail->SMTPSecure = "ssl"; // 使用ssl协议方式</span><span style="color:#333333;">
$mail->Port = $Port; // 163邮箱的ssl协议方式端口号是465/994
$mail->setFrom($Username, $bookName); // 设置发件人信息,如邮件格式说明中的发件人,这里会显示为Mailer(xxxx@163.com),Mailer是当做名字显示
$mail->addAddress($toEmail); // 设置收件人信息,如邮件格式说明中的收件人,这里会显示为Liang(yyyy@163.com)
$mail->Subject = "需要的发票下载地址"; // 邮件标题
$mail->IsHTML(true);
$mail->Body = "您好请查看:发票的下载链接 <a href='" . $path . "'>点击下载</a>";// 邮件正文
$mail->send();
return true;
} catch (\Exception $e) {
return $e->getMessage();
}
}
}

28
app/service/user/SmsService.php

@ -2,10 +2,10 @@
namespace app\service\user;
use app\service\BaseService;
use app\model\AdminConfig;
use fast\FuncException;
class SmsService extends BaseService
class SmsService
{
protected $smsUsername;
protected $smsPassword;
@ -15,10 +15,15 @@ class SmsService extends BaseService
public function __construct()
{
parent::__construct();
$this->smsUsername = env('sms.username');
$this->smsPassword = env('sms.password');
$this->sendUrl = env('sms.api_url');
$smsContentJson = (new AdminConfig())->where('title', 'sms')->where('delete_time', 0)->value('content');
if ($smsContentJson) {
$smsContent = json_decode($smsContentJson, true);
if (isset($smsContent[0]['api_url']) && isset($smsContent[0]['username']) && isset($smsContent[0]['password'])) {
$this->smsUsername = $smsContent[0]['username'];
$this->smsPassword = $smsContent[0]['password'];
$this->sendUrl = $smsContent[0]['api_url'];
}
}
}
/**
@ -31,6 +36,16 @@ class SmsService extends BaseService
return '您的短信验证码为:' . $code . ',用于手机号绑定,10分钟内有效。若非本人操作,请勿泄露,谨防被骗。';
}
/**
*
* @param $email
* @return string
*/
public function smsInvoiceTemplate($email): string
{
return '发票已经发送至邮箱' . $email . ',请及时查收';
}
/**
*
* @param $mobile
@ -91,5 +106,4 @@ class SmsService extends BaseService
}
}
Loading…
Cancel
Save