Skip to content

Commit 5039d75

Browse files
committed
添加通知增加邮件推送功能
1 parent 69fab32 commit 5039d75

File tree

13 files changed

+459
-35
lines changed

13 files changed

+459
-35
lines changed

app/Controller/IndexController.php

+9-34
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace App\Controller;
66

7-
use App\Foundation\Utils\GroupAvatar;
8-
use App\Model\Auth\User;
9-
use App\Model\Laboratory\FriendRelation;
7+
use App\Foundation\Utils\Mail;
8+
use Hyperf\DbConnection\Db;
109
use Hyperf\HttpServer\Annotation\Controller;
1110
use Hyperf\HttpServer\Annotation\RequestMapping;
1211

@@ -17,43 +16,19 @@
1716
*/
1817
class IndexController extends AbstractController
1918
{
19+
public function __construct()
20+
{
21+
22+
}
23+
2024
/**
2125
* 获取用户数据列表
2226
* @RequestMapping(path="/test", methods="get,post")
2327
*/
2428
public function index()
2529
{
2630

27-
$user = User::query()->get()->toArray();
28-
foreach ($user as $item) {
29-
foreach ($user as $key) {
30-
if ($key['id'] == $item['id']) continue;
31-
FriendRelation::query()->insert([
32-
'uid' => $item['id'],
33-
'friend_id' => $key['id'],
34-
'created_at' => date('Y-m-d H:i:s'),
35-
'updated_at' => date('Y-m-d H:i:s'),
36-
]);
37-
}
38-
}
39-
// $picList = [
40-
// 'https://hyperf-cms.oss-cn-guangzhou.aliyuncs.com/admin_face/face1.png',
41-
// 'https://hyperf-cms.oss-cn-guangzhou.aliyuncs.com/admin_face/face2.png',
42-
// 'https://hyperf-cms.oss-cn-guangzhou.aliyuncs.com/admin_face/face3.png',
43-
// 'https://hyperf-cms.oss-cn-guangzhou.aliyuncs.com/admin_face/face4.png',
44-
// 'https://hyperf-cms.oss-cn-guangzhou.aliyuncs.com/admin_face/face5.png',
45-
// 'https://hyperf-cms.oss-cn-guangzhou.aliyuncs.com/admin_face/face6.png',
46-
// 'https://hyperf-cms.oss-cn-guangzhou.aliyuncs.com/admin_face/face7.png',
47-
// 'https://hyperf-cms.oss-cn-guangzhou.aliyuncs.com/admin_face/face8.png',
48-
// 'https://hyperf-cms.oss-cn-guangzhou.aliyuncs.com/admin_face/face9.png',
49-
// ];
50-
//
51-
// GroupAvatar::init($picList, false, '121312');
52-
// $res = GroupAvatar::build();
53-
//
54-
//
55-
// return $this->success([
56-
// 'list' => $res
57-
// ]);
5831
}
32+
33+
5934
}

app/Controller/System/NoticeController.php

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use App\Constants\StatusCode;
88
use App\Controller\AbstractController;
99
use App\Foundation\Annotation\Explanation;
10+
use App\Foundation\Utils\Queue;
11+
use App\Job\EmailNotificationJob;
1012
use App\Model\System\Notice;
1113
use Hyperf\Di\Annotation\Inject;
1214
use Hyperf\HttpServer\Annotation\Controller;
@@ -32,6 +34,12 @@ class NoticeController extends AbstractController
3234
*/
3335
private $notice;
3436

37+
/**
38+
* @Inject()
39+
* @var Queue
40+
*/
41+
private $queue;
42+
3543
/**
3644
* 获取系统通知列表
3745
* @RequestMapping(path="list", methods="get")
@@ -105,6 +113,11 @@ public function store()
105113

106114
if (!$noticeQuery->save()) $this->throwExp(StatusCode::ERR_EXCEPTION, '添加系统通知错误');
107115

116+
//分发队列
117+
$this->queue->push(new EmailNotificationJob([
118+
'title' => $params['title'],
119+
'content' => $params['content'],
120+
]));
108121
return $this->successByMessage('添加系统通知成功');
109122
}
110123

app/Foundation/Facades/Log.php

+10
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,14 @@ public static function sqlLog()
6464
return self::channel('sql_log', config('app_env', 'app'));
6565

6666
}
67+
68+
/**
69+
* 队列错误日志
70+
* @return \Psr\Log\LoggerInterface
71+
*/
72+
public static function jobLog()
73+
{
74+
return self::channel('jon_log', config('app_env', 'app'));
75+
76+
}
6777
}

app/Foundation/Utils/Mail.php

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Foundation\Utils;
6+
7+
use App\Exception\Handler\BusinessException;
8+
use PHPMailer\PHPMailer\PHPMailer;
9+
10+
/**
11+
* 邮件工具类
12+
* Class Mail
13+
* @package App\Foundation\Utils
14+
* @Author YiYuan-Lin
15+
* @Date: 2021/8/17
16+
*/
17+
class Mail
18+
{
19+
/**
20+
* 邮件类实例化
21+
* @var
22+
*/
23+
static private $mail;
24+
25+
/**
26+
* Mail类对象
27+
* @var
28+
*/
29+
static public $obj = null;
30+
31+
/**
32+
* 初始化邮件工具类
33+
* @param array $config
34+
* config['char_set'] string 邮件编码
35+
* config['smtp_debug'] bool 是否调试模式输出
36+
* config['is_html'] bool 是否HTML输出
37+
* config['host'] string SMTP服务器
38+
* config['port'] int 服务器端口
39+
* config['smtp_auth'] bool 允许SMTP认证
40+
* config['username'] string SMTP用户名
41+
* config['password'] string SMTP密码
42+
* config['smtp_secure'] tls/ssl 允许 TLS 或者ssl协议
43+
* @return Mail|null
44+
*/
45+
public static function init(array $config = [])
46+
{
47+
self::$mail = new PHPMailer(true);
48+
self::__setConfig($config);
49+
if (!is_object(self::$obj)) self::$obj = new self;
50+
return self::$obj;
51+
}
52+
53+
/**
54+
* 设置参数
55+
* @param array $config
56+
*/
57+
private static function __setConfig(array $config)
58+
{
59+
//使用SMTP
60+
self::$mail->isSMTP();
61+
//设定邮件编码
62+
self::$mail->CharSet = $config['char_set'] ?? "UTF-8";
63+
//调试模式输出
64+
self::$mail->SMTPDebug = $config['smtp_debug'] ?? 0;
65+
//是否HTML输出
66+
self::$mail->isHTML($config['is_html'] ?? true);
67+
//SMTP服务器
68+
self::$mail->Host = $config['host'] ?? env('MAIL_SMTP_HOST', 'smtp.163.com');
69+
//服务器端口 25 或者465 具体要看邮箱服务器支持
70+
self::$mail->Port = $config['port'] ?? env('MAIL_SMTP_PORT', 465);
71+
//允许SMTP认证
72+
self::$mail->SMTPAuth = $config['smtp_auth'] ?? true;
73+
//SMTP用户名
74+
self::$mail->Username = $config['username'] ?? env('MAIL_SMTP_USERNAME', 'SMTP用户名');
75+
//SMTP密码 部分邮箱是授权码(例如163邮箱)
76+
self::$mail->Password = $config['password'] ?? env('MAIL_SMTP_PASSWORD', '密码或者授权码');
77+
//允许 TLS 或者ssl协议
78+
self::$mail->SMTPSecure = $config['smtp_secure'] ?? env('MAIL_SMTP_ENCRYPTION', 'ssl');
79+
}
80+
81+
/**
82+
* 设置发件人
83+
* @param string $email
84+
* @param string $name
85+
* @return mixed
86+
*/
87+
public function setFromAddress(string $email, string $name) : self
88+
{
89+
self::$mail->setFrom($email, $name);
90+
return $this;
91+
}
92+
93+
/**
94+
* 设置收件人信息
95+
* @param string $email
96+
* @param string $name
97+
* @return self
98+
*/
99+
public function setAddress(string $email, string $name) : self
100+
{
101+
self::$mail->addAddress($email, $name);
102+
return $this;
103+
}
104+
105+
/**
106+
* 设置收件人信息(多收件人)
107+
* @param array $emailInfo
108+
* @return self
109+
*/
110+
public function setMoreAddress(array $emailInfo) : self
111+
{
112+
foreach ($emailInfo as $item) {
113+
self::$mail->addAddress($item['email'], $item['name']);
114+
}
115+
return $this;
116+
}
117+
118+
/**
119+
* 抄送
120+
* @param string $email
121+
* @return self
122+
*/
123+
public function addCC(string $email) : self
124+
{
125+
self::$mail->addCC($email);
126+
return $this;
127+
}
128+
129+
/**
130+
* 密送
131+
* @param string $email
132+
* @return self
133+
*/
134+
public function addBCC(string $email) : self
135+
{
136+
self::$mail->addBCC($email);
137+
return $this;
138+
}
139+
140+
/**
141+
* 添加附件
142+
* @param string $url
143+
* @param string $fileName
144+
* @return self
145+
*/
146+
public function addAttachment(string $url, string $fileName = '') : self
147+
{
148+
self::$mail->addAttachment($url, $fileName);
149+
return $this;
150+
}
151+
152+
/**
153+
* 设置邮件标题
154+
* @param string $title
155+
* @return self
156+
*/
157+
public function setSubject(string $title) : self
158+
{
159+
self::$mail->Subject = $title;
160+
return $this;
161+
}
162+
163+
/**
164+
* 设置邮件内容
165+
* @param string $body
166+
* @return self
167+
*/
168+
public function setBody(string $body) : self
169+
{
170+
self::$mail->Body = $body;
171+
return $this;
172+
}
173+
174+
/**
175+
* 发送邮件
176+
* @return bool
177+
*/
178+
public function send()
179+
{
180+
try {
181+
if (self::$mail->send()) return true;
182+
return false;
183+
}catch (\Exception $e) {
184+
Throw new BusinessException(400, $e->getMessage());
185+
}
186+
}
187+
}

app/Foundation/Utils/Queue.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Foundation\Utils;
6+
7+
use Hyperf\AsyncQueue\Driver\DriverFactory;
8+
use Hyperf\AsyncQueue\Driver\DriverInterface;
9+
use Hyperf\AsyncQueue\JobInterface;
10+
11+
/**
12+
* 队列生产类
13+
* Class Queue
14+
* @package App\Foundation\Utils
15+
* @Author YiYuan-Lin
16+
* @Date: 2021/8/18
17+
*/
18+
class Queue
19+
{
20+
/**
21+
* @var DriverInterface
22+
*/
23+
protected $driver;
24+
25+
public function __construct(DriverFactory $driverFactory)
26+
{
27+
$this->driver = $driverFactory->get('default');
28+
}
29+
30+
/**
31+
* 生产消息
32+
* @param JobInterface $jobObj
33+
* @param int $delay
34+
* @return bool
35+
*/
36+
public function push(JobInterface $jobObj, int $delay = 0): bool
37+
{
38+
return $this->driver->push($jobObj, $delay);
39+
}
40+
}

0 commit comments

Comments
 (0)