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.

111 lines
3.0 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 $value
  23. * @param int $key
  24. * @return int
  25. * 查看$extension数组中是否存在文件类型,不存在则返回-1
  26. */
  27. public function searchType($array,$value, $key=-1){
  28. foreach($array as $k=>$row){
  29. if(!is_array($row)){
  30. if($row == $value){
  31. if($key != -1) {
  32. return $key;
  33. }else{
  34. return -1;
  35. }
  36. }
  37. }else{
  38. $r = self::searchType($row,$value, $k);
  39. if($r != -1){
  40. return $r;
  41. }
  42. }
  43. }
  44. return -1;
  45. }
  46. /**
  47. * @param $imgIdStr
  48. * @param $ownId
  49. * @param $ownType
  50. * @return array
  51. * 根据临时文件id(字符串)将临时文件保存在文件中
  52. */
  53. public function saveTemFileToFile($temFIleIdStr, $ownId, $ownType)
  54. {
  55. if(!$temFIleIdStr || !$ownId) {
  56. return ['status' => false, 'info' => '参数错误'];
  57. }
  58. $img_id_arr = explode(',', $temFIleIdStr);
  59. $i = 1;
  60. $first_file_id = 0;
  61. foreach ($img_id_arr as $key => $value) {
  62. $tem_file = TemFile::findOne($value);
  63. if(!$tem_file) {
  64. return ['status' => false, 'info' => '存在查找不到的文件'];
  65. }
  66. $res = self::saveNewFile($tem_file, $ownId, $ownType);
  67. if(!$res['status']) {
  68. return ['status' => false, 'info' => '存在文件保存失败'];
  69. }
  70. if($i == 1) {
  71. $first_file_id = $res['file_id'];
  72. }
  73. $i++;
  74. }
  75. return ['status' => true, 'info' => '保存成功', 'first_file_id' => $first_file_id];
  76. }
  77. /**
  78. * @param $temFile
  79. * @param $ownId
  80. * @param $ownType
  81. * @return bool
  82. * 创建新的文件
  83. */
  84. private function saveNewFile($temFile, $ownId, $ownType)
  85. {
  86. $new_file = new File();
  87. $new_file->name = $temFile->name;
  88. $new_file->type = $temFile->type;
  89. $new_file->own_id = $ownId;
  90. $new_file->own_type = $ownType;
  91. $new_file->alias = $temFile->alias;
  92. $new_file->path = $temFile->path;
  93. if($new_file->save()) {
  94. return ['status' => true, 'file_id' => $new_file->id];
  95. } else {
  96. return ['status' => false];
  97. }
  98. }
  99. }