diff --git a/app/controller/task/InvoiceIssuanceTask.php b/app/controller/task/InvoiceIssuanceTask.php
index 90512fc..83773bf 100644
--- a/app/controller/task/InvoiceIssuanceTask.php
+++ b/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();
}
diff --git a/app/model/EmailSendLog.php b/app/model/EmailSendLog.php
new file mode 100644
index 0000000..8502045
--- /dev/null
+++ b/app/model/EmailSendLog.php
@@ -0,0 +1,10 @@
+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())
diff --git a/app/service/user/EmailService.php b/app/service/user/EmailService.php
new file mode 100644
index 0000000..b17798d
--- /dev/null
+++ b/app/service/user/EmailService.php
@@ -0,0 +1,142 @@
+"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邮箱
+ $mail->Password = $Password; // 发送方的邮箱密码,注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码!
+ $mail->SMTPSecure = "ssl"; // 使用ssl协议方式
+ $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 = "您好请查看:发票的下载链接 点击下载";// 邮件正文
+ $mail->send();
+ return true;
+ } catch (\Exception $e) {
+ return $e->getMessage();
+ }
+ }
+}
diff --git a/app/service/user/SmsService.php b/app/service/user/SmsService.php
index ebe34a1..ae8c1f0 100644
--- a/app/service/user/SmsService.php
+++ b/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'];
+ }
+ }
}
/**
@@ -26,9 +31,19 @@ class SmsService extends BaseService
* @param $code
* @return string
*/
- public function smsCodeTemplate($code):string
+ public function smsCodeTemplate($code): string
{
- return '您的短信验证码为:'.$code.',用于手机号绑定,10分钟内有效。若非本人操作,请勿泄露,谨防被骗。';
+ return '您的短信验证码为:' . $code . ',用于手机号绑定,10分钟内有效。若非本人操作,请勿泄露,谨防被骗。';
+ }
+
+ /**
+ *
+ * @param $email
+ * @return string
+ */
+ public function smsInvoiceTemplate($email): string
+ {
+ return '发票已经发送至邮箱' . $email . ',请及时查收';
}
/**
@@ -37,15 +52,15 @@ class SmsService extends BaseService
* @param $message
* @throws FuncException
*/
- public function sendSms($mobile,$message)
+ public function sendSms($mobile, $message)
{
$content = $this->content_prefix . $message;
$param = [
'userName' => $this->smsUsername,
- 'sign' => md5($this->smsUsername . $this->smsPassword . $mobile . $content),
- 'mobile' => $mobile,
- 'content' => $content,
+ 'sign' => md5($this->smsUsername . $this->smsPassword . $mobile . $content),
+ 'mobile' => $mobile,
+ 'content' => $content,
];
$json = json_encode($param);
@@ -69,8 +84,8 @@ class SmsService extends BaseService
// 设置cURL选项
curl_setopt($ch, CURLOPT_URL, $this->sendUrl); // 目标URL
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回结果而不是输出
- curl_setopt($ch, CURLOPT_POST, true); // 发送POST请求
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回结果而不是输出
+ curl_setopt($ch, CURLOPT_POST, true); // 发送POST请求
// 设置POST字段
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
@@ -87,9 +102,8 @@ class SmsService extends BaseService
// 关闭cURL会话
curl_close($ch);
- return json_decode($response,true);
+ return json_decode($response, true);
}
-
-}
\ No newline at end of file
+}