UploadFile.php 1.8 KB

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