UploadFile.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. use app\common\library\storage\Driver as StorageDriver;
  11. /**
  12. * 文件库模型
  13. * Class UploadFile
  14. *
  15. * @package app\common\model
  16. */
  17. class UploadFile extends BaseModel
  18. {
  19. protected $name = 'upload_file';
  20. protected $updateTime = false;
  21. protected $deleteTime = false;
  22. protected $append = ['file_path', 'cover_file_path'];
  23. /**
  24. * 关联文件库分组表
  25. *
  26. * @return \think\model\relation\BelongsTo
  27. */
  28. public function uploadGroup()
  29. {
  30. return $this->belongsTo('UploadGroup', 'group_id');
  31. }
  32. /**
  33. * 获取图片完整路径
  34. *
  35. * @param $value
  36. * @param $data
  37. * @return string
  38. */
  39. public function getFilePathAttr($value, $data)
  40. {
  41. if ($data['storage'] === 'local') {
  42. return self::$base_url . 'uploads/' . $data['file_name'];
  43. }
  44. if ($data['storage'] === 'third_party') {
  45. return $data['file_name'];
  46. }
  47. $domain = str_replace('http://dy-dxf.oss-cn-hangzhou.aliyuncs.com', 'https://dy-dxf.oss-cn-hangzhou.aliyuncs.com', $data['file_url']);
  48. return $domain . '/' . $data['file_name'];
  49. }
  50. /**
  51. * [getCoverFilePathAttr]
  52. * 获取封面图片
  53. *
  54. * @Author: Yeshihao
  55. * @DateTime: 2024/6/14 15:23
  56. * @param $value
  57. * @param $data
  58. * @return array|bool|float|int|mixed|object|\stdClass|string|null
  59. */
  60. public function getCoverFilePathAttr($value, $data)
  61. {
  62. if (empty($data['file_type'])) {
  63. return $this->file_path ?? '';
  64. }
  65. if ($data['file_type'] == 'video') {
  66. return static_file('store/img/video.png');
  67. }
  68. if ($data['file_type'] == 'file') {
  69. return static_file('store/img/file.png');
  70. }
  71. if ($data['file_type'] == 'pdf') {
  72. return static_file('store/img/pdf.png');
  73. }
  74. return $this->file_path;
  75. }
  76. /**
  77. * 通过mime获取文件类型
  78. *
  79. * @param string $mime
  80. * @return string
  81. * @Author Mark
  82. * @DateTime 2024-08-07
  83. */
  84. public static function mime2fileType(string $mime): string
  85. {
  86. $map = [
  87. //图片
  88. 'image/jpeg' => 'image',
  89. 'image/jpg' => 'image',
  90. 'image/png' => 'image',
  91. 'image/gif' => 'image',
  92. 'image/bmp' => 'image',
  93. 'image/webp' => 'image',
  94. //视频
  95. 'video/mp4' => 'video',
  96. //pdf
  97. 'application/pdf' => 'pdf',
  98. ];
  99. return $map[$mime] ?? '';
  100. }
  101. /**
  102. * 文件详情
  103. *
  104. * @param $file_id
  105. * @return null|static
  106. * @throws \think\exception\DbException
  107. */
  108. public static function detail($file_id)
  109. {
  110. return self::get($file_id);
  111. }
  112. /**
  113. * 根据文件名查询文件id
  114. *
  115. * @param $fileName
  116. * @return mixed
  117. */
  118. public static function getFildIdByName($fileName)
  119. {
  120. return (new static)->where(['file_name' => $fileName])->value('file_id');
  121. }
  122. /**
  123. * 查询文件id
  124. *
  125. * @param $fileId
  126. * @return mixed
  127. */
  128. public static function getFileName($fileId)
  129. {
  130. return (new static)->where(['file_id' => $fileId])->value('file_name');
  131. }
  132. /**
  133. * 添加新记录
  134. *
  135. * @param $data
  136. * @return false|int
  137. */
  138. public function add($data)
  139. {
  140. $data['wxapp_id'] = $data['wxapp_id'] ?? (self::$wxapp_id ?: 0);
  141. return $this->save($data);
  142. }
  143. /**
  144. * [imageArr]
  145. *
  146. * @Author: Yeshihao
  147. * @DateTime: 2023/6/16 20:47
  148. */
  149. public static function id2url($id)
  150. {
  151. $file = self::cache(3600)->find($id);
  152. return $file->file_path ?? '';
  153. }
  154. /**
  155. * 文件上传
  156. *
  157. * @param integer $wxappId
  158. * @param integer $group_id
  159. * @param string $fileField
  160. * @return static
  161. * @Author Mark
  162. * @DateTime 2024-08-22
  163. */
  164. public function uploadFile($wxappId, $group_id = -1, $fileField = 'iFile', $comapnyId = 0)
  165. {
  166. $config = Setting::getItem('storage', $wxappId);
  167. // 实例化存储驱动
  168. $StorageDriver = new StorageDriver($config);
  169. // 设置上传文件的信息
  170. $StorageDriver->setUploadFile($fileField);
  171. //判断上传类型
  172. $fileInfo = $StorageDriver->getFileInfo();
  173. if (!UploadFile::mime2fileType($fileInfo['type'])) {
  174. $this->error = '图片上传失败:mime类型错误';
  175. return false;
  176. }
  177. // 上传图片
  178. if (!$StorageDriver->upload()) {
  179. $this->error = '图片上传失败:' . $StorageDriver->getError();
  180. return false;
  181. }
  182. // 图片上传路径
  183. $fileName = $StorageDriver->getFileName();
  184. // 图片信息
  185. $fileInfo = $StorageDriver->getFileInfo();
  186. $uploadFile = self::addUploadFile($group_id, $fileName, $fileInfo, $config, $comapnyId);
  187. // 图片上传成功
  188. return $uploadFile;
  189. }
  190. /**
  191. * 写入数据库
  192. *
  193. * @param int $group_id
  194. * @param string $fileName
  195. * @param array $fileInfo
  196. * @param array $config
  197. * @return static
  198. * @Author Mark
  199. * @DateTime 2024-08-22
  200. */
  201. private function addUploadFile($group_id, $fileName, $fileInfo, $config, $companyId)
  202. {
  203. // 存储引擎
  204. $storage = $config['default'];
  205. // 存储域名
  206. $fileUrl = isset($config['engine'][$storage]['domain'])
  207. ? $config['engine'][$storage]['domain'] : '';
  208. // 添加文件库记录
  209. $model = new self;
  210. $model->add([
  211. 'group_id' => $group_id > 0 ? (int)$group_id : 0,
  212. 'storage' => $storage,
  213. 'file_url' => $fileUrl,
  214. 'file_name' => $fileName,
  215. 'file_size' => $fileInfo['size'],
  216. 'file_type' => self::mime2fileType($fileInfo['type']),
  217. 'extension' => pathinfo($fileInfo['name'], PATHINFO_EXTENSION),
  218. 'origin_name' => $fileInfo['name'],
  219. 'mime' => $fileInfo['type'],
  220. 'company_id' => $companyId,
  221. ]);
  222. return $model;
  223. }
  224. }