Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

105 wiersze
3.2 KiB

3 lat temu
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class ImportController extends BaseController {
  5. //自动检测导入的文件类型从而选择不同的控制器方法
  6. public function auto(){
  7. set_time_limit(100);
  8. ini_set('memory_limit','200M');
  9. $login_user = $this->checkLogin();
  10. $filename = $_FILES["file"]["name"] ;
  11. $file = $_FILES["file"]["tmp_name"] ;
  12. //文件后缀
  13. $tail = substr(strrchr($filename, '.'), 1);
  14. if ($tail == 'zip') {
  15. $zipArc = new \ZipArchive();
  16. $ret = $zipArc->open($file, \ZipArchive::CREATE);
  17. $info = $zipArc->getFromName("prefix_info.json") ;
  18. if ($info) {
  19. $info_array = json_decode($info ,1 );
  20. if ($info_array) {
  21. $this->markdown($info_array);
  22. return ;
  23. }
  24. }
  25. }
  26. if ($tail == 'json') {
  27. $json = file_get_contents($file) ;
  28. $json_array = json_decode($json ,1 );
  29. unset($json);
  30. if (( $json_array['swagger'] || $json_array['openapi'] ) && $json_array['info']) {
  31. R("ImportSwagger/import");
  32. return ;
  33. }
  34. if ($json_array['id']) {
  35. R("ImportPostman/import");
  36. return ;
  37. }
  38. if ($json_array['info']) {
  39. R("ImportPostman/import");
  40. return ;
  41. }
  42. }
  43. $this->sendError(10101);
  44. }
  45. //导入markdown压缩包
  46. public function markdown($info_array){
  47. set_time_limit(100);
  48. ini_set('memory_limit','200M');
  49. $login_user = $this->checkLogin();
  50. $file = $_FILES["file"]["tmp_name"] ;
  51. //$file = "../Public/markdown.zip" ; //test
  52. if (!$info_array) {
  53. $zipArc = new \ZipArchive();
  54. $ret = $zipArc->open($file, \ZipArchive::CREATE);
  55. $info = $zipArc->getFromName("prefix_info.json") ;
  56. $info_array = json_decode($info ,1 );
  57. unset($info);
  58. }
  59. if ($info_array) {
  60. //$info_array = $this->_fileToMarkdown($info_array, $zipArc );
  61. //echo json_encode($info_array);return ;
  62. D("Item")->import( json_encode($info_array) , $login_user['uid'] );
  63. $this->sendResult(array());
  64. return ;
  65. }
  66. $this->sendError(10101);
  67. }
  68. //废弃
  69. private function _fileToMarkdown( $catalogData , $zipArc ){
  70. if ($catalogData['pages']) {
  71. foreach ($catalogData['pages'] as $key => $value) {
  72. if ($value['page_content']) {
  73. $catalogData['pages'][$key]['page_content'] = $zipArc->getFromName( $value['page_content']) ;//原来的内容由文件名变为文件内容
  74. }
  75. }
  76. }
  77. if ($catalogData['catalogs']) {
  78. foreach ($catalogData['catalogs'] as $key => $value) {
  79. if ($value) {
  80. $catalogData['catalogs'][$key] = $this->_markdownTofile($value , $zipArc);
  81. }
  82. }
  83. }
  84. return $catalogData ;
  85. }
  86. }