Notification.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\admin\controller\shopro\notification;
  3. use app\admin\controller\shopro\Common;
  4. use app\admin\model\shopro\notification\Notification as NotificationModel;
  5. use app\admin\model\shopro\Admin;
  6. class Notification extends Common
  7. {
  8. protected $noNeedRight = ['index', 'read', 'delete', 'notificationType'];
  9. public function _initialize()
  10. {
  11. parent::_initialize();
  12. $this->model = new NotificationModel;
  13. }
  14. /**
  15. * 获取管理员的消息列表
  16. *
  17. * @return \think\Response
  18. */
  19. public function index()
  20. {
  21. $admin = auth_admin();
  22. $admin = Admin::where('id', $admin['id'])->find();
  23. $notifiable_type = $admin->getNotifiableType();
  24. $notifications = NotificationModel::sheepFilter(false)
  25. ->where('notifiable_type', $notifiable_type)
  26. ->where('notifiable_id', $admin['id'])
  27. ->order('createtime', 'desc')
  28. ->paginate($this->request->param('list_rows', 10));
  29. $this->success('消息列表', null, $notifications);
  30. }
  31. /**
  32. * 指定消息标记已读
  33. *
  34. * @param string $id
  35. * @return void
  36. */
  37. public function read($id)
  38. {
  39. $admin = auth_admin();
  40. $admin = Admin::where('id', $admin['id'])->find();
  41. $notifiable_type = $admin->getNotifiableType();
  42. $notification = NotificationModel::sheepFilter()
  43. ->where('notifiable_type', $notifiable_type)
  44. ->where('notifiable_id', $admin['id'])
  45. ->where('id', $id)
  46. ->find();
  47. if (!$notification) {
  48. $this->error(__('No Results were found'));
  49. }
  50. $notification->read_time = time();
  51. $notification->save();
  52. $this->success('已读成功', null, $notification);
  53. }
  54. /**
  55. * 删除已读消息
  56. *
  57. * @return void
  58. */
  59. public function delete()
  60. {
  61. $admin = auth_admin();
  62. $admin = Admin::where('id', $admin['id'])->find();
  63. // 将已读的消息全部删除
  64. $notifiable_type = $admin->getNotifiableType();
  65. NotificationModel::sheepFilter()
  66. ->where('notifiable_type', $notifiable_type)
  67. ->where('notifiable_id', $admin['id'])
  68. ->whereNotNull('read_time')
  69. ->delete();
  70. $this->success('删除成功');
  71. }
  72. /**
  73. * 消息类别,以及未读消息数
  74. *
  75. * @return void
  76. */
  77. public function notificationType()
  78. {
  79. $admin = auth_admin();
  80. $notificationType = NotificationModel::$notificationType;
  81. $newType = [];
  82. foreach ($notificationType as $type => $name) {
  83. // 未读消息数
  84. $unread_num = NotificationModel::where('notifiable_type', 'admin')->where('notifiable_id', $admin['id'])
  85. ->notificationType($type)
  86. ->where('read_time', null)
  87. ->order('createtime', 'desc')->count();
  88. $newType[] = [
  89. 'label' => $name,
  90. 'value' => $type,
  91. 'unread_num' => $unread_num
  92. ];
  93. }
  94. $result = [
  95. 'unread_num' => array_sum(array_column($newType, 'unread_num')),
  96. 'notification_type' => $newType
  97. ];
  98. $this->success('消息类型', null, $result);
  99. }
  100. }