TestController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * 测试.
  4. *
  5. * @author liulang <liulang201902@gmail.com>
  6. * @date 2020-01-22 09:37:54
  7. */
  8. declare(strict_types=1);
  9. namespace Modules\Api\Controllers;
  10. use App\Model\Mongo\VideoHistoryModel;
  11. use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
  12. use Carbon\Carbon;
  13. use Hyperf\HttpMessage\Stream\SwooleStream;
  14. class TestController extends BaseController
  15. {
  16. public function pay()
  17. {
  18. return json_success('haha');
  19. }
  20. public function mongo()
  21. {
  22. $pipeline = [
  23. [
  24. '$match' => [
  25. 'created_at' => [
  26. '$gte' => '2019-01-01',
  27. '$lte' => '2019-01-02',
  28. ],
  29. 'platform' => 'a',
  30. ],
  31. ],
  32. [
  33. '$group' => [
  34. '_id' => 'platform',
  35. 'count' => ['$sum' => 1],
  36. ],
  37. ]
  38. ];
  39. //$result1 = make(VideoHistoryModel::class)->db()->findOne(['date'=>'2020-02-18']);
  40. //$result2 = make(VideoHistoryModel::class)->db()->find()->toArray();
  41. //$result3 = make(VideoHistoryModel::class)->db()->insertOne(['uid'=>1,'vid'=>2])->getInsertedId();
  42. //$result4 = make(VideoHistoryModel::class)->db()->findOneAndUpdate(['_id'=>'1212'],['$set' =>['vid'=>3]]);
  43. //$result5 = make(VideoHistoryModel::class)->db()->deleteOne(['vid'=>3]);
  44. //$result6 = make(VideoHistoryModel::class)->db()->deleteMany(['vid'=>3])->getDeletedCount();
  45. //$result7 = make(VideoHistoryModel::class)->db()->countDocuments(['uid'=>1,'vid'=>4]);
  46. //$result8 = make(VideoHistoryModel::class)->db()->updateOne(['_id'=>'1212'],['$set' =>['vid'=>4]]);
  47. $result9 = make(VideoHistoryModel::class)->db()->aggregate($pipeline);
  48. return json_success($result7);
  49. }
  50. /**
  51. * 演示导出excel
  52. * 组件官方使用手册https://opensource.box.com/spout/docs/#configuration-for-xlsx-and-ods
  53. * 因为swoole 接管了response,不能使用它的方法openToBrowser
  54. * 需要定期删除storage/tmp目录,删除策略,glob文件,看每个文件的修改日期,超过1周则删除。可以写一个定时任务.
  55. * @return mixed
  56. */
  57. public function exportDemoExcel()
  58. {
  59. try {
  60. $fileName = 'export_demo_' . Carbon::now()->format('Y_m_d') . '_' . random(6) . '.xlsx';
  61. $filePath = BASE_PATH . '/storage/tmp/'. $fileName;
  62. $writer = WriterEntityFactory::createXLSXWriter();
  63. $writer->setTempFolder(BASE_PATH . '/storage/tmp');
  64. $writer->setShouldUseInlineStrings(true);
  65. $writer->openToFile($filePath);
  66. $firstColumns = [
  67. 'ID', '姓名',
  68. ];
  69. $lists = [
  70. [
  71. 'id' => '101',
  72. 'name' => '刘德华',
  73. ],
  74. [
  75. 'id' => '102',
  76. 'name' => '张曼玉',
  77. ],
  78. ];
  79. $mainRows = [];
  80. $cells = self::makeOneExportCell($firstColumns);
  81. $mainRows[] = WriterEntityFactory::createRow($cells);
  82. $cells = [];
  83. if (!empty($lists)) {
  84. // 故意写!empty,这是demo,foreach一定要判断是否存在.
  85. foreach ($lists as $k => $v) {
  86. $rowColumns = [
  87. $v['id'],
  88. $v['name'],
  89. ];
  90. $cells = self::makeOneExportCell($rowColumns);
  91. $mainRows[] = WriterEntityFactory::createRow($cells);
  92. }
  93. $writer->addRows($mainRows);
  94. }
  95. $writer->close();
  96. } catch (\Exception $e) {
  97. // Todo logger('xxxx').
  98. } catch (\Throwable $e) {
  99. // Todo logger('xxxx').
  100. }
  101. return $this->response
  102. ->withAddedHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
  103. ->withAddedHeader('Content-Disposition', 'attachment; filename='. $fileName)
  104. ->withAddedHeader('Cache-Control', 'max-age=0')
  105. ->withAddedHeader('Pragma', 'public')
  106. ->download($filePath, $fileName);
  107. }
  108. private function makeOneExportCell($data)
  109. {
  110. $cells = [];
  111. foreach ($data as $k => $v) {
  112. $cells[] = WriterEntityFactory::createCell($v);
  113. }
  114. return $cells;
  115. }
  116. }