ReUtils.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\utils;
  4. use think\Response;
  5. use think\response\File;
  6. /**
  7. * 返回操作 utils
  8. *
  9. * @version 0.0.1
  10. * @author by huwhois
  11. * @time 20228/10
  12. */
  13. class ReUtils
  14. {
  15. /**
  16. * 操作成功的返回
  17. * @access protected
  18. * @param mixed $msg 提示信息
  19. * @param mixed $data 返回的数据
  20. * @param array $header 发送的Header信息
  21. * @return void
  22. */
  23. public static function success($msg = 'success', array $header = [])
  24. {
  25. $result = [
  26. 'code' => 0,
  27. 'msg' => $msg,
  28. ];
  29. return Response::create($result, 'json')->header($header);
  30. }
  31. /**
  32. * 操作成功的返回
  33. * @access protected
  34. * @param mixed $msg 提示信息
  35. * @param array $header 发送的Header信息
  36. * @return void
  37. */
  38. public static function error($msg = 'fail', $code = 1, string $type = '', array $header = [])
  39. {
  40. $result = [
  41. 'code' => $code,
  42. 'msg' => $msg,
  43. ];
  44. $type = $type ?: 'json';
  45. return Response::create($result, $type)->header($header);
  46. }
  47. /**
  48. * 返回封装后的API数据到客户端
  49. * @param mixed $data 要返回的数据
  50. * @param integer $code 返回的code
  51. * @param mixed $msg 提示信息
  52. * @param string $type 返回数据格式
  53. * @param array $header 发送的Header信息
  54. * @return Response
  55. */
  56. public static function result($data, int $code = 0, $msg = '', string $type = '', array $header = []): Response
  57. {
  58. $result = [
  59. 'code' => $code,
  60. 'msg' => $msg,
  61. 'data' => $data,
  62. ];
  63. $type = $type ?: 'json';
  64. return Response::create($result, $type)->header($header);
  65. }
  66. /**
  67. * 获取\think\response\Download对象实例
  68. * @param string $filename 要下载的文件
  69. * @param string $name 下载文件显示名
  70. * @return \think\response\File
  71. */
  72. public static function sendTempZip(string $filename, string $name = "")
  73. {
  74. $fp = fopen($filename, "r");
  75. $file_size = filesize($filename); //获取文件的字节
  76. $name = $name ?: basename($filename);
  77. // halt($file_size);
  78. //下载文件需要用到的头
  79. Header("Content-type: application/zip");
  80. Header("Accept-Ranges: bytes");
  81. Header("Accept-Length:" . $file_size);
  82. Header("Content-Disposition: attachment; filename=$name");
  83. $buffer = 1024 * 4; //设置一次读取的字节数,每读一取次,就输出数据(即返回给浏览器)
  84. $file_count = 0; //读取的总字节数
  85. //向浏览器返回数据 如果下载完成就停止输出,如果未下载完成就一直在输出。根据文件的字节大小判断是否下载完成
  86. while (!feof($fp) && $file_count < $file_size) {
  87. $file_con = fread($fp, $buffer);
  88. $file_count += $buffer;
  89. echo $file_con;
  90. }
  91. fclose($fp);
  92. // 下载完成后删除压缩包,临时文件夹
  93. if ($file_count >= $file_size) {
  94. unlink($filename);
  95. }
  96. }
  97. }