Attachment.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use app\admin\model\Admin;
  5. /**
  6. * Attachment模型
  7. * @controllerUrl 'routineAttachment'
  8. */
  9. class Attachment extends Model
  10. {
  11. protected $autoWriteTimestamp = 'int';
  12. protected $createTime = 'createtime';
  13. protected $updateTime = null;
  14. protected $append = [
  15. 'suffix',
  16. 'full_url'
  17. ];
  18. public function getSuffixAttr($value, $row)
  19. {
  20. if ($row['name']) {
  21. $suffix = strtolower(pathinfo($row['name'], PATHINFO_EXTENSION));
  22. return $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
  23. }
  24. return 'file';
  25. }
  26. public function getFullUrlAttr($value, $row)
  27. {
  28. return full_url($row['url']);
  29. }
  30. protected static function onBeforeInsert($model)
  31. {
  32. $repeat = $model->where([
  33. ['sha1', '=', $model->sha1],
  34. ['topic', '=', $model->topic],
  35. ['storage', '=', $model->storage],
  36. ])->find();
  37. if ($repeat) {
  38. $storageFile = path_transform(public_path() . ltrim($repeat['url'], '/'));
  39. if ($model->storage == 'local' && !file_exists($storageFile)) {
  40. $repeat->delete();
  41. return true;
  42. } else {
  43. $repeat->quote++;
  44. $repeat->lastuploadtime = time();
  45. $repeat->save();
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. protected static function onAfterInsert($model)
  52. {
  53. if (!$model->lastuploadtime) {
  54. $model->quote = 1;
  55. $model->lastuploadtime = time();
  56. $model->save();
  57. }
  58. }
  59. public function admin()
  60. {
  61. return $this->belongsTo(Admin::class);
  62. }
  63. public function user()
  64. {
  65. return $this->belongsTo(User::class);
  66. }
  67. }