Attachment.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 附件上传模型
  13. // +----------------------------------------------------------------------
  14. namespace app\common\model;
  15. use think\Model;
  16. class Attachment extends Model
  17. {
  18. // 自动写入时间戳
  19. protected $autoWriteTimestamp = true;
  20. public static function onBeforeInsert($row)
  21. {
  22. if (self::where('path', $row['path'])->where('driver', $row['driver'])->find()) {
  23. return false;
  24. }
  25. }
  26. public function getCategoryAttr($value): string
  27. {
  28. return $value == '' ? 'unclassed' : $value;
  29. }
  30. public function setCategoryAttr($value)
  31. {
  32. return $value == 'unclassed' ? '' : $value;
  33. }
  34. /**
  35. * 获取Mimetype列表
  36. * @return array
  37. */
  38. public static function getMimetypeList()
  39. {
  40. $data = [
  41. "image/*" => "图片",
  42. "audio/*" => "音频",
  43. "video/*" => "视频",
  44. "text/*" => "文档",
  45. "application/*" => "应用",
  46. "zip,rar,7z,tar" => "压缩包",
  47. ];
  48. return $data;
  49. }
  50. /**
  51. * 获取定义的附件类别列表
  52. * @return array
  53. */
  54. public static function getCategoryList(): array
  55. {
  56. $data = config('site.attachmentcategory') ?? [];
  57. // 添加未归类选项
  58. $data['unclassed'] = '未归类';
  59. return $data;
  60. }
  61. /**
  62. * 返回上传文件的相关信息
  63. */
  64. public function getBaseData()
  65. {
  66. $data = [
  67. 'url' => $this->path,
  68. 'fullurl' => cdnurl($this->path, true),
  69. 'category' => $this->category,
  70. 'filename' => $this->name,
  71. 'filesize' => $this->size,
  72. 'mimetype' => $this->mime,
  73. ];
  74. return $data;
  75. }
  76. }