Attachment.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class Attachment extends BaseModel
  5. {
  6. // 表名
  7. protected $name = 'attachment';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected int $is_status_search = 0;// 默认使用 status = 1 筛选
  14. protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
  15. /**
  16. * 默认查询字段
  17. *
  18. * @var array|string[]
  19. */
  20. public array $select = [
  21. '*'
  22. ];
  23. // 定义字段类型
  24. protected $type = [
  25. ];
  26. protected $append = [
  27. 'thumb_style'
  28. ];
  29. protected static function init()
  30. {
  31. // 如果已经上传该资源,则不再记录
  32. self::beforeInsert(function ($model) {
  33. if (self::where('url', '=', $model['url'])->where('storage', $model['storage'])->find()) {
  34. return false;
  35. }
  36. });
  37. self::beforeWrite(function ($row) {
  38. if (isset($row['category']) && $row['category'] == 'unclassed') {
  39. $row['category'] = '';
  40. }
  41. });
  42. }
  43. public function setUploadtimeAttr($value)
  44. {
  45. return is_numeric($value) ? $value : strtotime($value);
  46. }
  47. public function getCategoryAttr($value)
  48. {
  49. return $value == '' ? 'unclassed' : $value;
  50. }
  51. public function setCategoryAttr($value)
  52. {
  53. return $value == 'unclassed' ? '' : $value;
  54. }
  55. /**
  56. * 获取云储存的缩略图样式字符
  57. */
  58. public function getThumbStyleAttr($value, $data)
  59. {
  60. if (!isset($data['storage']) || $data['storage'] == 'local') {
  61. return '';
  62. } else {
  63. $config = get_addon_config($data['storage']);
  64. if ($config && isset($config['thumbstyle'])) {
  65. return $config['thumbstyle'];
  66. }
  67. }
  68. return '';
  69. }
  70. /**
  71. * 获取Mimetype列表
  72. * @return array
  73. */
  74. public static function getMimetypeList()
  75. {
  76. $data = [
  77. "image/*" => __("Image"),
  78. "audio/*" => __("Audio"),
  79. "video/*" => __("Video"),
  80. "text/*" => __("Text"),
  81. "application/*" => __("Application"),
  82. "zip,rar,7z,tar" => __("Zip"),
  83. ];
  84. return $data;
  85. }
  86. /**
  87. * 获取定义的附件类别列表
  88. * @return array
  89. */
  90. public static function getCategoryList()
  91. {
  92. $data = config('site.attachmentcategory') ?? [];
  93. foreach ($data as $index => &$datum) {
  94. $datum = __($datum);
  95. }
  96. $data['unclassed'] = __('Unclassed');
  97. return $data;
  98. }
  99. }