Attachment.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\model;
  10. use think\Model;
  11. /**
  12. * 附件模型
  13. * @package app\admin\model
  14. */
  15. class Attachment extends Model
  16. {
  17. // 设置当前模型对应的完整数据表名称
  18. protected $name = 'admin_attachment';
  19. // 自动写入时间戳
  20. protected $autoWriteTimestamp = true;
  21. /**
  22. * 根据附件id获取路径
  23. * @param string|array $id 附件id
  24. * @param int $type 类型:0-补全目录,1-直接返回数据库记录的地址
  25. * @author 蔡伟明 <314013107@qq.com>
  26. * @return array|bool|mixed|string
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. * @throws \think\exception\DbException
  30. */
  31. public function getFilePath($id = '', $type = 0)
  32. {
  33. if (is_array($id)) {
  34. $data_list = $this->where('id', 'in', $id)->select();
  35. $paths = [];
  36. foreach ($data_list as $key => $value) {
  37. if ($value['driver'] == 'local') {
  38. $paths[$value['id']] = ($type == 0 ? PUBLIC_PATH : '').$value['path'];
  39. } else {
  40. $paths[$value['id']] = $value['path'];
  41. }
  42. }
  43. return $paths;
  44. } else {
  45. $data = $this->where('id', $id)->find();
  46. if ($data) {
  47. if ($data['driver'] == 'local') {
  48. return ($type == 0 ? PUBLIC_PATH : '').$data['path'];
  49. } else {
  50. return $data['path'];
  51. }
  52. } else {
  53. return false;
  54. }
  55. }
  56. }
  57. /**
  58. * 根据图片id获取缩略图路径,如果缩略图不存在,则返回原图路径
  59. * @param string $id 图片id
  60. * @author 蔡伟明 <314013107@qq.com>
  61. * @return array|mixed|string|Model|null
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. * @throws \think\exception\DbException
  65. */
  66. public function getThumbPath($id = '')
  67. {
  68. $result = $this->where('id', $id)->field('path,driver,thumb')->find();
  69. if ($result) {
  70. if ($result['driver'] == 'local') {
  71. return $result['thumb'] != '' ? PUBLIC_PATH.$result['thumb'] : PUBLIC_PATH.$result['path'];
  72. } else {
  73. return $result['thumb'] != '' ? $result['thumb'] : $result['path'];
  74. }
  75. } else {
  76. return $result;
  77. }
  78. }
  79. /**
  80. * 根据附件id获取名称
  81. * @param string $id 附件id
  82. * @return string 名称
  83. */
  84. public function getFileName($id = '')
  85. {
  86. return $this->where('id', $id)->value('name');
  87. }
  88. }