@@ -45,9 +45,22 @@ public class CommentController {
45
45
MailProperties mailProperties ;
46
46
@ Autowired
47
47
MailUtils mailUtils ;
48
+
49
+ private static String blogName ;
50
+ private static String cmsUrl ;
48
51
private static String websiteUrl ;
49
52
50
- @ Value ("${server.website.url}" )
53
+ @ Value ("${custom.mail.blog.name}" )
54
+ public void setBlogName (String blogName ) {
55
+ this .blogName = blogName ;
56
+ }
57
+
58
+ @ Value ("${custom.mail.url.cms}" )
59
+ public void setCmsUrl (String cmsUrl ) {
60
+ this .cmsUrl = cmsUrl ;
61
+ }
62
+
63
+ @ Value ("${custom.mail.url.website}" )
51
64
public void setWebsiteUrl (String websiteUrl ) {
52
65
this .websiteUrl = websiteUrl ;
53
66
}
@@ -246,18 +259,7 @@ public Result postComment(@RequestBody Comment comment,
246
259
}
247
260
}
248
261
commentService .saveComment (comment );
249
- String path = "" ;
250
- if (comment .getPage () == 0 ) {
251
- //普通博客
252
- path = "/blog/" + comment .getBlogId ();
253
- } else if (comment .getPage () == 1 ) {
254
- //关于我页面
255
- path = "/about" ;
256
- } else if (comment .getPage () == 2 ) {
257
- //友链页面
258
- path = "/friends" ;
259
- }
260
- judgeSendMail (comment , isVisitorComment , path );
262
+ judgeSendMail (comment , isVisitorComment );
261
263
return Result .ok ("评论成功" );
262
264
}
263
265
@@ -329,40 +331,42 @@ private void setCommentRandomAvatar(Comment comment) {
329
331
comment .setAvatar (avatar );
330
332
}
331
333
332
- private void judgeSendMail (Comment comment , boolean isVisitorComment , String path ) {
333
- //6种情况:
334
- //我以父评论提交:不用邮件提醒
335
- //我回复我自己:不用邮件提醒
336
- //我回复访客的评论:只提醒该访客
337
- //访客以父评论提交:只提醒我自己
338
- //访客回复我的评论:只提醒我自己
339
- //访客回复访客的评论(即使是他自己先前的评论):提醒我自己和他回复的评论
334
+ private void judgeSendMail (Comment comment , boolean isVisitorComment ) {
335
+ /*
336
+ 6种情况:
337
+ 我以父评论提交:不用邮件提醒
338
+ 我回复我自己:不用邮件提醒
339
+ 我回复访客的评论:只提醒该访客
340
+ 访客以父评论提交:只提醒我自己
341
+ 访客回复我的评论:只提醒我自己
342
+ 访客回复访客的评论(即使是他自己先前的评论):提醒我自己和他回复的评论
343
+ */
340
344
if (isVisitorComment ) {
341
345
//访客的评论
342
346
if (comment .getParentCommentId () != -1 ) {
343
347
com .yoyling .entity .Comment parentComment = commentService .getCommentById (comment .getParentCommentId ());
344
348
//访客回复我的评论,邮件提醒我自己
345
349
if (parentComment .getAdminComment ()) {
346
- sendMailToMe (parentComment . getEmail (), path );
350
+ sendMailToMe (comment );
347
351
} else {
348
352
if (parentComment .getNotice ()) {
349
353
//访客回复访客的评论(即使是他自己先前的评论),且对方接收提醒,邮件提醒对方
350
- sendMailToParentComment (parentComment . getEmail (), path );
354
+ sendMailToParentComment (parentComment , comment );
351
355
}
352
356
//不管对方是否接收提醒,都要提醒我有新评论
353
- sendMailToMe (mailProperties . getUsername (), path );
357
+ sendMailToMe (comment );
354
358
}
355
359
} else {
356
360
//访客以父评论提交,只邮件提醒我自己
357
- sendMailToMe (mailProperties . getUsername (), path );
361
+ sendMailToMe (comment );
358
362
}
359
363
} else {
360
364
//我的评论
361
365
if (comment .getParentCommentId () != -1 ) {
362
366
com .yoyling .entity .Comment parentComment = commentService .getCommentById (comment .getParentCommentId ());
363
367
//我回复访客的评论,且对方接收提醒,邮件提醒对方
364
368
if (!parentComment .getAdminComment () && parentComment .getNotice ()) {
365
- sendMailToParentComment (parentComment . getEmail (), path );
369
+ sendMailToParentComment (parentComment , comment );
366
370
}
367
371
}
368
372
}
@@ -371,28 +375,71 @@ private void judgeSendMail(Comment comment, boolean isVisitorComment, String pat
371
375
/**
372
376
* 发送邮件提醒回复对象
373
377
*
374
- * @param email 邮件接收方
375
- * @param path 评论所在的页面
378
+ * @param parentComment 父评论
379
+ * @param comment 当前评论
376
380
*/
377
- private void sendMailToParentComment (String email , String path ) {
378
- String url = websiteUrl + path ;
379
- String toAccount = email ;
380
- String subject = "YOYLING · BLOG 评论回复" ;
381
- String content = "<body><h2>您的评论有新回复</h2><p><a href='" + url + "'>详情请看" + url + "</a></p><p>此邮件为自动发送,如不想再收到此类消息,请回复TD</p></body>" ;
382
- mailUtils .sendHTMLMail (toAccount , subject , content );
381
+ private void sendMailToParentComment (top .naccl .entity .Comment parentComment , Comment comment ) {
382
+ String path = "" ;
383
+ String title = "" ;
384
+ if (comment .getPage () == 0 ) {
385
+ //普通博客
386
+ title = parentComment .getBlog ().getTitle ();
387
+ path = "/blog/" + comment .getBlogId ();
388
+ } else if (comment .getPage () == 1 ) {
389
+ //关于我页面
390
+ title = "关于我" ;
391
+ path = "/about" ;
392
+ } else if (comment .getPage () == 2 ) {
393
+ //友链页面
394
+ title = "友人帐" ;
395
+ path = "/friends" ;
396
+ }
397
+ Map <String , Object > map = new HashMap <>();
398
+ map .put ("parentNickname" , parentComment .getNickname ());
399
+ map .put ("nickname" , comment .getNickname ());
400
+ map .put ("title" , title );
401
+ map .put ("time" , comment .getCreateTime ());
402
+ map .put ("parentContent" , parentComment .getContent ());
403
+ map .put ("content" , comment .getContent ());
404
+ map .put ("url" , websiteUrl + path );
405
+ String toAccount = parentComment .getEmail ();
406
+ String subject = "您在 " + blogName + " 的评论有了新回复" ;
407
+ mailUtils .sendHtmlTemplateMail (map , toAccount , subject , "guest.html" );
383
408
}
384
409
385
410
/**
386
411
* 发送邮件提醒我自己
387
412
*
388
- * @param email 邮件接收方
389
- * @param path 评论所在的页面
413
+ * @param comment 当前评论
390
414
*/
391
- private void sendMailToMe (String email , String path ) {
392
- String url = websiteUrl + path ;
393
- String toAccount = email ;
394
- String subject = "YOYLING · BLOG 新评论" ;
395
- String content = "<body><p><a href='" + url + "'>新评论" + url + "</a></p></body>" ;
396
- mailUtils .sendHTMLMail (toAccount , subject , content );
415
+ private void sendMailToMe (Comment comment ) {
416
+ String path = "" ;
417
+ String title = "" ;
418
+ if (comment .getPage () == 0 ) {
419
+ //普通博客
420
+ title = blogService .getTitleByBlogId (comment .getBlogId ());
421
+ path = "/blog/" + comment .getBlogId ();
422
+ } else if (comment .getPage () == 1 ) {
423
+ //关于我页面
424
+ title = "关于我" ;
425
+ path = "/about" ;
426
+ } else if (comment .getPage () == 2 ) {
427
+ //友链页面
428
+ title = "友人帐" ;
429
+ path = "/friends" ;
430
+ }
431
+ Map <String , Object > map = new HashMap <>();
432
+ map .put ("title" , title );
433
+ map .put ("time" , comment .getCreateTime ());
434
+ map .put ("nickname" , comment .getNickname ());
435
+ map .put ("content" , comment .getContent ());
436
+ map .put ("ip" , comment .getIp ());
437
+ map .put ("email" , comment .getEmail ());
438
+ map .put ("status" , comment .getPublished () ? "公开" : "待审核" );
439
+ map .put ("url" , websiteUrl + path );
440
+ map .put ("manageUrl" , cmsUrl + "/comments" );
441
+ String toAccount = mailProperties .getUsername ();
442
+ String subject = blogName + " 收到新评论" ;
443
+ mailUtils .sendHtmlTemplateMail (map , toAccount , subject , "owner.html" );
397
444
}
398
445
}
0 commit comments