Attachment.php 2.4 KB

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