Attachment.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. public function setUploadtimeAttr($value)
  18. {
  19. return is_numeric($value) ? $value : strtotime($value);
  20. }
  21. public function getCategoryAttr($value)
  22. {
  23. return $value == '' ? 'unclassed' : $value;
  24. }
  25. public function setCategoryAttr($value)
  26. {
  27. return $value == 'unclassed' ? '' : $value;
  28. }
  29. /**
  30. * 获取云储存的缩略图样式字符
  31. */
  32. public function getThumbStyleAttr($value, $data)
  33. {
  34. if (!isset($data['storage']) || $data['storage'] == 'local') {
  35. return '';
  36. } else {
  37. $config = get_addon_config($data['storage']);
  38. if ($config && isset($config['thumbstyle'])) {
  39. return $config['thumbstyle'];
  40. }
  41. }
  42. return '';
  43. }
  44. public static function getMimetypeList()
  45. {
  46. $data = [
  47. "image/*" => __("Image"),
  48. "audio/*" => __("Audio"),
  49. "video/*" => __("Video"),
  50. "text/*" => __("Text"),
  51. "application/*" => __("Application"),
  52. "zip,rar,7z,tar" => __("Zip"),
  53. ];
  54. return $data;
  55. }
  56. /**
  57. * 获取定义的附件类别列表
  58. * @return array
  59. */
  60. public static function getCategoryList()
  61. {
  62. $data = config('site.attachmentcategory')??[];
  63. foreach ($data as $index => &$datum) {
  64. $datum = __($datum);
  65. }
  66. $data['unclassed'] = __('Unclassed');
  67. return $data;
  68. }
  69. protected static function init()
  70. {
  71. // 如果已经上传该资源,则不再记录
  72. self::beforeInsert(function ($model) {
  73. if (self::where('url', '=', $model['url'])->where('storage', $model['storage'])->find()) {
  74. return false;
  75. }
  76. });
  77. }
  78. }