Records.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace addons\workorder\model;
  3. use addons\workorder\library\General;
  4. use think\Model;
  5. /**
  6. * 工单沟通记录模型
  7. */
  8. class Records extends Model
  9. {
  10. protected $name = "workorder_records";
  11. // 开启自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = null;
  16. // 追加属性
  17. protected $append = [
  18. 'sender',
  19. 'createtime_text',
  20. ];
  21. protected static function init()
  22. {
  23. }
  24. public function getSenderAttr($value, $data)
  25. {
  26. if (isset($data['user_id']) && $data['user_id']) {
  27. return 'user';
  28. } elseif (isset($data['engineer_id']) && $data['engineer_id']) {
  29. return 'engineer';
  30. } else {
  31. return 'none';
  32. }
  33. }
  34. public function getCreatetimeTextAttr($value, $data)
  35. {
  36. return human_date($data['createtime']);
  37. }
  38. public function engineer()
  39. {
  40. return $this->hasOne('Engineer', 'id', 'engineer_id', [], 'LEFT')->setEagerlyType(0);
  41. }
  42. public static function getMessageAttr($value, $data)
  43. {
  44. if ($data['message_type'] == 0 || $data['message_type'] == 3) {
  45. return [
  46. 'original' => $value,
  47. 'html' => "<div class='rich_text'>{$value}</div>"
  48. ];
  49. } elseif ($data['message_type'] == 1) {
  50. $value = cdnurl($value, true);
  51. return [
  52. 'original' => $value,
  53. 'html' => "<img src='{$value}' class='img_message' alt='' />"
  54. ];
  55. } elseif ($data['message_type'] == 2) {
  56. // 文件地址可能是完整的url,可能是cdn的url
  57. // 此处只能计算本地文件的大小
  58. $domain = cdnurl('', true);
  59. if (strpos($value, $domain) !== false) {
  60. $value = str_replace($domain, '', $value);
  61. }
  62. $filePath = str_replace('/', DS, ROOT_PATH . 'public' . $value);
  63. if (!file_exists($filePath)) {
  64. $fileExt = General::getFileExtension($value);
  65. $fileSize = false;
  66. } else {
  67. $fileObj = new \think\File($filePath);
  68. $fileExt = $fileObj->getExtension();
  69. $fileSize = $fileObj->getSize();
  70. unset($fileObj);
  71. }
  72. $fileSize = $fileSize ? General::formatFileSize($fileSize) : __('Unknown size');
  73. $value = cdnurl($value, true);
  74. $imgIconUrl = url('index/workorder/icon', ['suffix' => $fileExt], true, true);
  75. $fileName = __('%s file', [$fileExt]);
  76. $textHtml = <<<HTML
  77. <a target='_blank' href='{$value}'>
  78. <div class='file_message'>
  79. <img src='{$imgIconUrl}' alt=''>
  80. <div>
  81. <p class='file_name'>{$fileName}</p>
  82. <p class='file_size'>{$fileSize}</p>
  83. </div>
  84. <div class='down_file'>
  85. <i class='fa fa-download'></i>
  86. </div>
  87. </div>
  88. </a>
  89. HTML;
  90. return [
  91. 'original' => [
  92. 'url' => $value,
  93. 'ext' => $fileExt,
  94. 'size' => $fileSize
  95. ],
  96. 'html' => $textHtml
  97. ];
  98. } elseif ($data['message_type'] == 4) {
  99. return [
  100. 'original' => $data['id'],
  101. 'html' => "<div id='confidential_{$data['id']}'><span class='confidential text-gray'>" . __('Confidential information is hidden') . "</span><button data-confidential_id='{$data['id']}' class='btn btn-danger btn-xs confidential_btn'>" . __('check') . "</button></div>"
  102. ];
  103. }
  104. }
  105. }