Modules_Api_Controllers_TestController.proxy.php 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. use \Hyperf\Di\Aop\ProxyTrait;
  17. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  18. function __construct()
  19. {
  20. if (method_exists(parent::class, '__construct')) {
  21. parent::__construct(...func_get_args());
  22. }
  23. $this->__handlePropertyHandler(__CLASS__);
  24. }
  25. public function pay()
  26. {
  27. return json_success('haha');
  28. }
  29. public function mongo()
  30. {
  31. $pipeline = [['$match' => ['created_at' => ['$gte' => '2019-01-01', '$lte' => '2019-01-02'], 'platform' => 'a']], ['$group' => ['_id' => 'platform', 'count' => ['$sum' => 1]]]];
  32. //$result1 = make(VideoHistoryModel::class)->db()->findOne(['date'=>'2020-02-18']);
  33. //$result2 = make(VideoHistoryModel::class)->db()->find()->toArray();
  34. //$result3 = make(VideoHistoryModel::class)->db()->insertOne(['uid'=>1,'vid'=>2])->getInsertedId();
  35. //$result4 = make(VideoHistoryModel::class)->db()->findOneAndUpdate(['_id'=>'1212'],['$set' =>['vid'=>3]]);
  36. //$result5 = make(VideoHistoryModel::class)->db()->deleteOne(['vid'=>3]);
  37. //$result6 = make(VideoHistoryModel::class)->db()->deleteMany(['vid'=>3])->getDeletedCount();
  38. //$result7 = make(VideoHistoryModel::class)->db()->countDocuments(['uid'=>1,'vid'=>4]);
  39. //$result8 = make(VideoHistoryModel::class)->db()->updateOne(['_id'=>'1212'],['$set' =>['vid'=>4]]);
  40. $result9 = make(VideoHistoryModel::class)->db()->aggregate($pipeline);
  41. return json_success($result7);
  42. }
  43. /**
  44. * 演示导出excel
  45. * 组件官方使用手册https://opensource.box.com/spout/docs/#configuration-for-xlsx-and-ods
  46. * 因为swoole 接管了response,不能使用它的方法openToBrowser
  47. * 需要定期删除storage/tmp目录,删除策略,glob文件,看每个文件的修改日期,超过1周则删除。可以写一个定时任务.
  48. * @return mixed
  49. */
  50. public function exportDemoExcel()
  51. {
  52. try {
  53. $fileName = 'export_demo_' . Carbon::now()->format('Y_m_d') . '_' . random(6) . '.xlsx';
  54. $filePath = BASE_PATH . '/storage/tmp/' . $fileName;
  55. $writer = WriterEntityFactory::createXLSXWriter();
  56. $writer->setTempFolder(BASE_PATH . '/storage/tmp');
  57. $writer->setShouldUseInlineStrings(true);
  58. $writer->openToFile($filePath);
  59. $firstColumns = ['ID', '姓名'];
  60. $lists = [['id' => '101', 'name' => '刘德华'], ['id' => '102', 'name' => '张曼玉']];
  61. $mainRows = [];
  62. $cells = self::makeOneExportCell($firstColumns);
  63. $mainRows[] = WriterEntityFactory::createRow($cells);
  64. $cells = [];
  65. if (!empty($lists)) {
  66. // 故意写!empty,这是demo,foreach一定要判断是否存在.
  67. foreach ($lists as $k => $v) {
  68. $rowColumns = [$v['id'], $v['name']];
  69. $cells = self::makeOneExportCell($rowColumns);
  70. $mainRows[] = WriterEntityFactory::createRow($cells);
  71. }
  72. $writer->addRows($mainRows);
  73. }
  74. $writer->close();
  75. } catch (\Exception $e) {
  76. // Todo logger('xxxx').
  77. } catch (\Throwable $e) {
  78. // Todo logger('xxxx').
  79. }
  80. return $this->response->withAddedHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')->withAddedHeader('Content-Disposition', 'attachment; filename=' . $fileName)->withAddedHeader('Cache-Control', 'max-age=0')->withAddedHeader('Pragma', 'public')->download($filePath, $fileName);
  81. }
  82. private function makeOneExportCell($data)
  83. {
  84. $cells = [];
  85. foreach ($data as $k => $v) {
  86. $cells[] = WriterEntityFactory::createCell($v);
  87. }
  88. return $cells;
  89. }
  90. }