Notification.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\admin\model\shopro\notification;
  3. use app\admin\model\shopro\Common;
  4. use think\Collection;
  5. class Notification extends Common
  6. {
  7. protected $pk = 'id';
  8. protected $name = 'shopro_notification';
  9. protected $type = [
  10. 'read_time' => 'timestamp',
  11. 'data' => 'array',
  12. ];
  13. public static $notificationType = [
  14. 'system' => '系统消息',
  15. 'shop' => '商城消息',
  16. // 'site' => '网站消息'
  17. ];
  18. public function scopeNotificationType($query, $type)
  19. {
  20. if ($type) {
  21. $query = $query->where('notification_type', $type);
  22. }
  23. return $query;
  24. }
  25. /**
  26. * 将数据转换成可以显示成键值对的格式
  27. *
  28. * @param string $value
  29. * @param array $data
  30. * @return array
  31. */
  32. public function getDataAttr($value, $data)
  33. {
  34. $data = json_decode($data['data'], true);
  35. if (isset($data['message_type']) && $data['message_type'] == 'notification') {
  36. $messageData = $data['data'];
  37. $class = new $data['class_name']();
  38. $fields = $class->returnField['fields'] ?? [];
  39. $fields = array_column($fields, null, 'field');
  40. $newData = [];
  41. foreach ($messageData as $k => $d) {
  42. $da = $fields[$k] ?? [];
  43. if ($da) {
  44. $da['value'] = $d;
  45. $newData[] = $da;
  46. }
  47. }
  48. $data['data'] = $newData;
  49. }
  50. return $data;
  51. }
  52. }