* @date 2020-01-22 09:37:54 */ declare(strict_types=1); namespace Modules\Api\Controllers; use App\Model\Mongo\VideoHistoryModel; use Box\Spout\Writer\Common\Creator\WriterEntityFactory; use Carbon\Carbon; use Hyperf\HttpMessage\Stream\SwooleStream; class TestController extends BaseController { public function pay() { return json_success('haha'); } public function mongo() { $pipeline = [ [ '$match' => [ 'created_at' => [ '$gte' => '2019-01-01', '$lte' => '2019-01-02', ], 'platform' => 'a', ], ], [ '$group' => [ '_id' => 'platform', 'count' => ['$sum' => 1], ], ] ]; //$result1 = make(VideoHistoryModel::class)->db()->findOne(['date'=>'2020-02-18']); //$result2 = make(VideoHistoryModel::class)->db()->find()->toArray(); //$result3 = make(VideoHistoryModel::class)->db()->insertOne(['uid'=>1,'vid'=>2])->getInsertedId(); //$result4 = make(VideoHistoryModel::class)->db()->findOneAndUpdate(['_id'=>'1212'],['$set' =>['vid'=>3]]); //$result5 = make(VideoHistoryModel::class)->db()->deleteOne(['vid'=>3]); //$result6 = make(VideoHistoryModel::class)->db()->deleteMany(['vid'=>3])->getDeletedCount(); //$result7 = make(VideoHistoryModel::class)->db()->countDocuments(['uid'=>1,'vid'=>4]); //$result8 = make(VideoHistoryModel::class)->db()->updateOne(['_id'=>'1212'],['$set' =>['vid'=>4]]); $result9 = make(VideoHistoryModel::class)->db()->aggregate($pipeline); return json_success($result7); } /** * 演示导出excel * 组件官方使用手册https://opensource.box.com/spout/docs/#configuration-for-xlsx-and-ods * 因为swoole 接管了response,不能使用它的方法openToBrowser * 需要定期删除storage/tmp目录,删除策略,glob文件,看每个文件的修改日期,超过1周则删除。可以写一个定时任务. * @return mixed */ public function exportDemoExcel() { try { $fileName = 'export_demo_' . Carbon::now()->format('Y_m_d') . '_' . random(6) . '.xlsx'; $filePath = BASE_PATH . '/storage/tmp/'. $fileName; $writer = WriterEntityFactory::createXLSXWriter(); $writer->setTempFolder(BASE_PATH . '/storage/tmp'); $writer->setShouldUseInlineStrings(true); $writer->openToFile($filePath); $firstColumns = [ 'ID', '姓名', ]; $lists = [ [ 'id' => '101', 'name' => '刘德华', ], [ 'id' => '102', 'name' => '张曼玉', ], ]; $mainRows = []; $cells = self::makeOneExportCell($firstColumns); $mainRows[] = WriterEntityFactory::createRow($cells); $cells = []; if (!empty($lists)) { // 故意写!empty,这是demo,foreach一定要判断是否存在. foreach ($lists as $k => $v) { $rowColumns = [ $v['id'], $v['name'], ]; $cells = self::makeOneExportCell($rowColumns); $mainRows[] = WriterEntityFactory::createRow($cells); } $writer->addRows($mainRows); } $writer->close(); } catch (\Exception $e) { // Todo logger('xxxx'). } catch (\Throwable $e) { // Todo logger('xxxx'). } 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); } private function makeOneExportCell($data) { $cells = []; foreach ($data as $k => $v) { $cells[] = WriterEntityFactory::createCell($v); } return $cells; } }