You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.8 KiB

  1. <?php
  2. namespace backend\logic\file;
  3. use common\models\ars\File;
  4. use common\models\ars\TemFile;
  5. class FileManager
  6. {
  7. //数据表ats_file和ats_tem_file的类型字段type
  8. const TYPE_IMAGE = 1;//图片
  9. const TYPE_VIDEO = 2;//影视
  10. const TYPE_EXCEL = 3;//excel表单
  11. const TYPE_WORD = 4;//word文本
  12. const TYPE_TXT = 5;//txt文本
  13. public static $extension = [
  14. self::TYPE_IMAGE => ['jpg', 'png', 'jpeg'],
  15. self::TYPE_VIDEO => ['mp4'],
  16. self::TYPE_EXCEL => [],
  17. self::TYPE_WORD => ['docx'],
  18. self::TYPE_TXT => ['txt'],
  19. ];
  20. /**
  21. * @param $array
  22. * @param $keyword
  23. * @return bool|int|string
  24. * 根据文件拓展名在$extension中查找对应的文件类型,若不存在则返回false
  25. */
  26. public function searchType($array, $keyword){
  27. foreach($array as $key => $value){
  28. foreach ($value as $k => $v) {
  29. if($v == $keyword){
  30. return $key;
  31. }
  32. }
  33. }
  34. return false;
  35. }
  36. /**
  37. * @param $imgIdStr
  38. * @param $ownId
  39. * @param $ownType
  40. * @return array
  41. * 根据临时文件id(字符串)将临时文件保存在文件中
  42. */
  43. public function saveTemFileToFile($temFIleIdStr, $ownId, $ownType)
  44. {
  45. if(!$temFIleIdStr || !$ownId) {
  46. return ['status' => false, 'info' => '参数错误'];
  47. }
  48. $img_id_arr = explode(',', $temFIleIdStr);
  49. $i = 1;
  50. $first_file_id = 0;
  51. foreach ($img_id_arr as $key => $value) {
  52. $tem_file = TemFile::findOne($value);
  53. if(!$tem_file) {
  54. return ['status' => false, 'info' => '存在查找不到的文件'];
  55. }
  56. $res = self::saveNewFile($tem_file, $ownId, $ownType);
  57. if(!$res['status']) {
  58. return ['status' => false, 'info' => '存在文件保存失败'];
  59. }
  60. if($i == 1) {
  61. $first_file_id = $res['file_id'];
  62. }
  63. $i++;
  64. }
  65. return ['status' => true, 'info' => '保存成功', 'first_file_id' => $first_file_id];
  66. }
  67. /**
  68. * @param $temFile
  69. * @param $ownId
  70. * @param $ownType
  71. * @return bool
  72. * 创建新的文件
  73. */
  74. private function saveNewFile($temFile, $ownId, $ownType)
  75. {
  76. $new_file = new File();
  77. $new_file->name = $temFile->name;
  78. $new_file->type = $temFile->type;
  79. $new_file->own_id = $ownId;
  80. $new_file->own_type = $ownType;
  81. $new_file->alias = $temFile->alias;
  82. $new_file->path = $temFile->path;
  83. if($new_file->save()) {
  84. return ['status' => true, 'file_id' => $new_file->id];
  85. } else {
  86. return ['status' => false];
  87. }
  88. }
  89. }