UploadFile.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\common\model;
  3. /**
  4. * 文件库模型
  5. * Class UploadFile
  6. * @package app\common\model
  7. */
  8. class UploadFile extends BaseModel
  9. {
  10. protected $name = 'upload_file';
  11. protected $updateTime = false;
  12. protected $deleteTime = false;
  13. protected $append = ['file_path'];
  14. /**
  15. * 关联文件库分组表
  16. * @return \think\model\relation\BelongsTo
  17. */
  18. public function uploadGroup()
  19. {
  20. return $this->belongsTo('UploadGroup', 'group_id');
  21. }
  22. /**
  23. * 获取图片完整路径
  24. * @param $value
  25. * @param $data
  26. * @return string
  27. */
  28. public function getFilePathAttr($value, $data)
  29. {
  30. if ($data['storage'] === 'local') {
  31. return self::$base_url . 'uploads/' . $data['file_name'];
  32. }
  33. return $data['file_url'] . '/' . $data['file_name'];
  34. }
  35. /**
  36. * 文件详情
  37. * @param $file_id
  38. * @return null|static
  39. * @throws \think\exception\DbException
  40. */
  41. public static function detail($file_id)
  42. {
  43. return self::get($file_id);
  44. }
  45. /**
  46. * 根据文件名查询文件id
  47. * @param $fileName
  48. * @return mixed
  49. */
  50. public static function getFildIdByName($fileName)
  51. {
  52. return (new static)->where(['file_name' => $fileName])->value('file_id');
  53. }
  54. /**
  55. * 查询文件id
  56. * @param $fileId
  57. * @return mixed
  58. */
  59. public static function getFileName($fileId)
  60. {
  61. return (new static)->where(['file_id' => $fileId])->value('file_name');
  62. }
  63. /**
  64. * 添加新记录
  65. * @param $data
  66. * @return false|int
  67. */
  68. public function add($data)
  69. {
  70. $data['wxapp_id'] = self::$wxapp_id;
  71. return $this->save($data);
  72. }
  73. }