ExportService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Author: 芸众商城 www.yunzshop.com
  4. * Date: 2017/7/25
  5. * Time: 上午11:31
  6. */
  7. namespace app\common\services;
  8. use app\common\exceptions\ShopException;
  9. use app\common\helpers\Url;
  10. use app\exports\FromArray;
  11. class ExportService
  12. {
  13. protected $file_name;
  14. protected $export_data;
  15. private $page_count;
  16. public $builder_model;
  17. private $export_page;
  18. private $page_size = 500;
  19. public function __construct($builder, $export_page = 1)
  20. {
  21. $this->export_page = $export_page;
  22. $builder_count = $builder->count();
  23. $this->page_count = ceil($builder_count / $this->page_size);
  24. $this->builder_model = $builder->skip(($export_page - 1) * $this->page_size)->take($this->page_size)->get();
  25. }
  26. private function swith()
  27. {
  28. if ($this->page_count > 1) {
  29. $this->bigExcel();
  30. } else {
  31. return $this->smallExcel();
  32. }
  33. }
  34. public function getExportBuilder()
  35. {
  36. return new FromArray($this->export_data);
  37. }
  38. private function smallExcel()
  39. {
  40. return app('excel')->download($this->getExportBuilder(),$this->file_name.'.xlsx');
  41. }
  42. private function bigExcel()
  43. {
  44. app('excel')->store($this->getExportBuilder(),$this->file_name.'.xlsx','export');
  45. }
  46. public function export($file_name, $export_data, $route = null, $type = 'export')
  47. {
  48. $this->file_name = $file_name;
  49. $this->export_data = $export_data;
  50. if ($this->page_count > 1) {
  51. $this->bigExcel();
  52. } else {
  53. $response = $this->smallExcel();
  54. return $response->send();
  55. }
  56. if ($this->export_page == $this->page_count) {
  57. setlocale(LC_ALL,'zh_CN.GBK');
  58. if (!file_exists(storage_path('framework/laravel-excel/'))){
  59. mkdir(storage_path('framework/laravel-excel/'));
  60. }
  61. $filename = storage_path('framework/laravel-excel/' . time() . 'down.zip');
  62. $time = time();
  63. $zip = new \ZipArchive(); // 使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
  64. if ($zip->open ( $filename, \ZipArchive::CREATE ) !== TRUE) {
  65. exit ( '无法打开文件,或者文件创建失败' );
  66. }
  67. //$fileNameArr 就是一个存储文件路径的数组 比如 array('/a/1.jpg,/a/2.jpg....');
  68. $fileNameArr = file_tree(storage_path('exports'));
  69. foreach ($fileNameArr as $val ) {
  70. // 当你使用addFile添加到zip包时,必须确保你添加的文件是存在的,否则close时会返回FALSE,而且使用addFile时,即使文件不存在也会返回TRUE
  71. if(file_exists(storage_path('exports/' . basename($val)))){
  72. $zip->addFile (storage_path('exports/') . basename($val), basename($val) ); // 第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下
  73. }
  74. }
  75. $zip->close (); // 关闭
  76. foreach ($fileNameArr as $val ) {
  77. file_delete(storage_path('exports/' . basename($val)));
  78. }
  79. //下面是输出下载;
  80. if (config('app.framework') == 'platform') {
  81. $url = "https://". $_SERVER['HTTP_HOST'].'/storage/framework/laravel-excel/' . $time ."down.zip";
  82. } else {
  83. $url = "https://". $_SERVER['HTTP_HOST'].'/addons/yun_shop/storage/framework/laravel-excel/' . $time ."down.zip";
  84. }
  85. $backurl = "https://". $_SERVER['HTTP_HOST']. config('app.isWeb') . "?c=site&a=entry&m=yun_shop&do=4302&route=" . $route;
  86. echo '<div style="border: 6px solid #e0e0e0;width: 12%;margin: 0 auto;margin-top: 12%;padding: 26px 100px;box-shadow: 0 0 14px #a2a2a2;color: #616161;"><a style="color:red;text-decorationnone;" href="'.$url.'">点击获取下载文件</a><a style="color:#616161" href="'.$backurl.'">返回</a><div>';
  87. exit;
  88. } else {
  89. echo '<div style="border: 6px solid #e0e0e0;width: 12%;margin: 0 auto;margin-top: 12%;padding: 26px 100px;box-shadow: 0 0 14px #a2a2a2;color: #616161;">共'.$this->page_count.'个excel文件, 已完成'.$this->export_page. '个。 <div>';
  90. $this->export_page += 1;
  91. $params = [];
  92. // $filts_params = ['c', 'a', 'm', 'do', 'route'];
  93. // foreach (request()->input() as $key => $val) {
  94. // if (!in_array($key, $filts_params)) {
  95. // $params[$key] = $val;
  96. // }
  97. // }
  98. $request_params = request()->except(['c', 'a', 'm', 'do', 'route']);
  99. foreach ($request_params as $key => $val) {
  100. if (is_array($val)) {
  101. if (key($val) === 0) {
  102. $params[$key] = implode(',', $val);
  103. } else {
  104. foreach ($val as $v_key => $value) {
  105. $params[$key][$v_key] = (is_array($value) && key($value) === 0) ? implode(',', $value) : $value;
  106. }
  107. }
  108. } else {
  109. $params[$key] = $val;
  110. }
  111. }
  112. $params[$type] = 1;
  113. $params['export_page'] = $this->export_page;
  114. $url = Url::absoluteWeb(\Request::query('route'), $params);
  115. echo '<meta http-equiv="Refresh" content="1; url='.$url.'" />';
  116. exit;
  117. }
  118. }
  119. /*
  120. * 消除特殊字符.
  121. */
  122. public function eliminateSpecialSymbol($data)
  123. {
  124. return preg_replace("/[ '.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/", '', $data);
  125. }
  126. /*
  127. * 防止科学计数法.
  128. */
  129. public function unScientificNotation($param)
  130. {
  131. return $param . "\t";
  132. }
  133. }