Skip to content

Commit 23de830

Browse files
committed
添加邮件通知
1 parent b7cd57d commit 23de830

File tree

13 files changed

+232
-3
lines changed

13 files changed

+232
-3
lines changed

application/common.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,18 @@ function make_url(https://github.com/$requestUri%20=%20'')
7676

7777
return (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $requestUri;
7878

79+
}
80+
81+
82+
/**
83+
* 构建邮件任务数组结构 topthink-queue 2.0队列
84+
*
85+
* @param $userIdArray 用户id数组
86+
* @param $title 邮件标题
87+
* @param $content 邮件内容
88+
* @return array
89+
*/
90+
function make_job($userIdArray,$title,$content)
91+
{
92+
return compact('userIdArray','title','content');
7993
}

application/handler/Email.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public function send($toUserEmail,$name,$title,$content)
2121

2222
try {
2323
//Server settings
24-
$mail->SMTPDebug = 0; // Enable verbose debug output
24+
$mail->SMTPDebug = 0;
25+
$mail->CharSet ="UTF-8";
2526
$mail->isSMTP(); // Set mailer to use SMTP
2627
$mail->Host = config('email.host'); // Specify main and backup SMTP servers
2728
$mail->SMTPAuth = true; // Enable SMTP authentication

application/index/controller/Base.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Lang;
1414
use app\index\service\Auth as AuthService;
1515

16+
1617
class Base extends Error
1718
{
1819

@@ -91,4 +92,6 @@ protected function isLogin()
9192
{
9293
return $this->user_id > 0;
9394
}
95+
96+
9497
}

application/index/controller/Test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
namespace app\index\controller;
55

66
use app\handler\Email;
7+
use app\index\service\Project as ProjectService;
78

89
class Test
910
{
1011
public function index(Email $email)
1112
{
12-
$email->send('[email protected]','phpzc','test','<h1>test</h1>');
13+
//$email->send('[email protected]','phpzc','test','<h1>test</h1>');
14+
1315
}
1416
}

application/index/service/Bug.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
use app\index\model\Bug as BugModel;
1212
use app\index\model\BugLog as BugLogModel;
13+
use app\index\model\ProjectUser;
1314
use app\index\validate\Bug as BugValidate;
15+
use app\job\BugJob;
16+
use think\Queue;
1417

1518
class Bug
1619
{
@@ -109,6 +112,8 @@ public static function create($project_id,$user_id,$data)
109112
if($add)
110113
{
111114

115+
static::notifyUser(BugJob::class,$model->id,'项目'.$model->project->project_name.'新增BUG','项目'.$model->project->project_name.'新增BUG成功');
116+
112117
return ServiceResult::Success(['id'=>$model->id],'创建成功');
113118

114119
}else{
@@ -160,6 +165,8 @@ public static function handle($bug_id,$user_id,$data)
160165

161166
$log->commit();
162167

168+
static::notifyUser(BugJob::class,$bug_id,'处理BUG '.$bug->bug_title.'【id:'.$bug_id.'','处理BUG【id:'.$bug_id.'');
169+
163170
return ServiceResult::Success([],'提交成功');
164171

165172
}
@@ -206,6 +213,9 @@ public static function edit($id,$user_id,$data)
206213
{
207214
$bug->commit();
208215

216+
static::notifyUser(BugJob::class,$id,'处理BUG '.$bug->bug_title.'【id:'.$id.'','处理BUG'.$bug->bug_title);
217+
218+
209219
return ServiceResult::Success([],'修改成功');
210220
}else{
211221
$bug->rollback();
@@ -214,4 +224,21 @@ public static function edit($id,$user_id,$data)
214224
}
215225

216226
}
227+
228+
229+
230+
231+
public static function notifyUser($jobClassName,$bugId,$title,$content)
232+
{
233+
$bug = \app\index\model\Bug::where(['id'=>$bugId])->find();
234+
235+
$all = ProjectUser::all(['project_id'=>$bug->project_id]);
236+
237+
$userIds = array_column($all->toArray(),'user_id');
238+
239+
$data = make_job($userIds,$title,BugEmailTemplate::getEmail($content));
240+
241+
242+
Queue::push($jobClassName,$data);
243+
}
217244
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace app\index\service;
5+
6+
7+
class BugEmailTemplate
8+
{
9+
public static function getEmail($content)
10+
{
11+
$date = date('Y-m-d H:i:s');
12+
13+
return <<<EOT
14+
类型:BUG<br />
15+
内容:{$content}<br />
16+
时间:{$date}<br />
17+
EOT;
18+
19+
}
20+
}

application/index/service/Project.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
use app\index\model\ProjectVersion;
1515
use app\index\model\User;
1616
use app\index\validate\Project as ProjectValidate;
17+
use app\job\ProjectJob;
1718
use think\facade\Request;
1819

20+
use think\Queue;
21+
1922
class Project
2023
{
2124
public static function create($project_name,$user_id)
@@ -94,6 +97,7 @@ public static function edit($project_name,$user_id,$id)
9497
return ServiceResult::Error('没有权限修改');
9598
}
9699

100+
$oldName = $project->project_name;
97101

98102
$project->startTrans();
99103
try{
@@ -125,6 +129,8 @@ public static function edit($project_name,$user_id,$id)
125129
if($update !== false)
126130
{
127131

132+
static::notifyUser(ProjectJob::class,$id, '修改项目名称','项目原名'.$oldName.'改为'.$project_name);
133+
128134
return ServiceResult::Success([],'修改成功');
129135

130136
}else{
@@ -279,4 +285,19 @@ public static function deleteProjectUser($user_id,$project_id)
279285

280286
return ServiceResult::Success([],'删除成功');
281287
}
288+
289+
290+
291+
292+
public static function notifyUser($jobClassName,$projectId,$title,$content)
293+
{
294+
$all = ProjectUser::all(['project_id'=>$projectId]);
295+
296+
$userIds = array_column($all->toArray(),'user_id');
297+
298+
$data = make_job($userIds,$title,ProjectEmailTemplate::getEmail($content));
299+
300+
301+
Queue::push($jobClassName,$data);
302+
}
282303
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace app\index\service;
5+
6+
7+
class ProjectEmailTemplate
8+
{
9+
public static function getEmail($content)
10+
{
11+
$date = date('Y-m-d H:i:s');
12+
13+
return <<<EOT
14+
类型:项目<br />
15+
内容:{$content}<br />
16+
时间:{$date}<br />
17+
EOT;
18+
19+
}
20+
}

application/job/BaseJob.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
4+
namespace app\job;
5+
6+
7+
class BaseJob
8+
{
9+
public function failed($data){
10+
11+
// ...任务达到最大重试次数后,失败了
12+
}
13+
}

application/job/BugJob.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
4+
namespace app\job;
5+
6+
use app\index\model\User;
7+
use think\queue\Job;
8+
9+
use app\handler\Email;
10+
11+
/**
12+
* Class BugJob 操作问题bug 发送邮件通知任务
13+
* @package app\job
14+
*/
15+
class BugJob extends BaseJob
16+
{
17+
public function fire(Job $job, $data){
18+
19+
//....这里执行具体的任务
20+
21+
if ($job->attempts() > 1) {
22+
//通过这个方法可以检查这个任务已经重试了几次了
23+
return $job->delete();
24+
}
25+
26+
27+
$email = new Email();
28+
29+
30+
//读取用户id 挨个发送邮件
31+
foreach ($data['userIdArray'] as $v){
32+
33+
$user = User::where(['id'=>$v])->find();
34+
35+
$email->send($user->email,$user->username,$data['title'],$data['content']);
36+
37+
}
38+
39+
40+
$job->delete();
41+
42+
43+
44+
}
45+
46+
47+
}

application/job/ProjectJob.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
4+
namespace app\job;
5+
6+
use app\handler\Email;
7+
use app\index\model\User;
8+
use think\queue\Job;
9+
10+
/**
11+
* Class ProjectJob 操作项目 发送邮件通知任务
12+
* @package app\job
13+
*/
14+
class ProjectJob extends BaseJob
15+
{
16+
public function fire(Job $job, $data){
17+
18+
//....这里执行具体的任务
19+
20+
if ($job->attempts() > 1) {
21+
//通过这个方法可以检查这个任务已经重试了几次了
22+
return $job->delete();
23+
}
24+
25+
var_dump($data);
26+
27+
28+
29+
30+
//读取用户id 挨个发送邮件
31+
foreach ($data['userIdArray'] as $v){
32+
33+
$user = User::where(['id'=>$v])->find();
34+
$email = new Email();
35+
$email->send($user->email,$user->username,$data['title'],$data['content']);
36+
37+
}
38+
39+
40+
$job->delete();
41+
42+
43+
44+
}
45+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"php": ">=5.6.0",
2020
"topthink/framework": "5.1.*",
2121
"topthink/think-captcha": "^2.0",
22-
"phpmailer/phpmailer": "^6.0"
22+
"phpmailer/phpmailer": "^6.0",
23+
"topthink/think-queue": "2.*"
2324
},
2425
"autoload": {
2526
"psr-4": {

config/queue.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
// +----------------------------------------------------------------------
3+
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
4+
// +----------------------------------------------------------------------
5+
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
6+
// +----------------------------------------------------------------------
7+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8+
// +----------------------------------------------------------------------
9+
// | Author: yunwuxin <[email protected]>
10+
// +----------------------------------------------------------------------
11+
12+
return [
13+
'connector' => 'database',
14+
'table' => 'jobs',//队列表 不写前缀
15+
];

0 commit comments

Comments
 (0)