Attachment.php 2.4 KB

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