NotificationService.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: sdf_sky
  5. * Date: 2017/4/24
  6. * Time: 下午5:08
  7. */
  8. namespace App\Services;
  9. use App\Models\Notification;
  10. use App\Models\User;
  11. use Illuminate\Support\Facades\Mail;
  12. class NotificationService
  13. {
  14. /**全局站内通知处理
  15. * @param $from_user_id
  16. * @param $to_user_id
  17. * @param $type
  18. * @param string $subject
  19. * @param int $source_id
  20. * @param string $content
  21. * @param string $refer_type
  22. * @param int $refer_id
  23. * @return bool|static
  24. */
  25. public static function notify($from_user_id,$to_user_id,$type,$subject='',$source_id=0,$content='',$refer_type='',$refer_id=0){
  26. /*不能自己给自己发通知*/
  27. if( $from_user_id == $to_user_id ){
  28. return false;
  29. }
  30. $toUser = User::find($to_user_id);
  31. if( !$toUser ){
  32. return false;
  33. }
  34. /*站内消息策略*/
  35. if(!in_array($type,explode(",",$toUser->site_notifications))){
  36. return false;
  37. }
  38. return Notification::create([
  39. 'user_id' => $from_user_id,
  40. 'to_user_id' => $to_user_id,
  41. 'type' => $type,
  42. 'subject' => strip_tags($subject),
  43. 'source_id' => $source_id,
  44. 'content' => $content,
  45. 'refer_type' => $refer_type,
  46. 'refer_id' => $refer_id,
  47. 'is_read' => 0
  48. ]);
  49. }
  50. /**全局邮件发送处理
  51. * @param $email
  52. * @param $subject
  53. * @param $message
  54. * @return bool
  55. */
  56. public static function sendEmail($email,$subject,$message){
  57. if(Setting()->get('mail_open') != 1){//关闭邮件发送
  58. return false;
  59. }
  60. $data = [
  61. 'email' => $email,
  62. 'subject' => $subject,
  63. 'body' => $message,
  64. ];
  65. MailService::sendMail($email, $subject, $data);
  66. }
  67. }