Attachment.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. public static function getMimetypeList()
  59. {
  60. $data = [
  61. "image/*" => __("Image"),
  62. "audio/*" => __("Audio"),
  63. "video/*" => __("Video"),
  64. "text/*" => __("Text"),
  65. "application/*" => __("Application"),
  66. "zip,rar,7z,tar" => __("Zip"),
  67. ];
  68. return $data;
  69. }
  70. /**
  71. * 获取定义的附件类别列表
  72. * @return array
  73. */
  74. public static function getCategoryList()
  75. {
  76. $data = config('site.attachmentcategory') ?? [];
  77. foreach ($data as $index => &$datum) {
  78. $datum = __($datum);
  79. }
  80. $data['unclassed'] = __('Unclassed');
  81. return $data;
  82. }
  83. }