Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

174 linhas
6.2 KiB

  1. <?php
  2. namespace app\api\controller;
  3. use addons\unishop\model\OrderProduct;
  4. use app\admin\controller\unishop\Order;
  5. use app\admin\model\unishop\Product;
  6. use app\common\controller\Api;
  7. /**
  8. * 示例接口
  9. */
  10. class Demo extends Api
  11. {
  12. //如果$noNeedLogin为空表示所有接口都需要登录才能请求
  13. //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
  14. //如果接口已经设置无需登录,那也就无需鉴权了
  15. //
  16. // 无需登录的接口,*表示全部
  17. protected $noNeedLogin = ['*'];
  18. // 无需鉴权的接口,*表示全部
  19. protected $noNeedRight = ["*"];
  20. /**
  21. * 测试方法
  22. *
  23. * @ApiTitle (测试名称)
  24. * @ApiSummary (测试描述信息)
  25. * @ApiMethod (POST)
  26. * @ApiRoute (/api/demo/test/id/{id}/name/{name})
  27. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  28. * @ApiParams (name="id", type="integer", required=true, description="会员ID")
  29. * @ApiParams (name="name", type="string", required=true, description="用户名")
  30. * @ApiParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
  31. * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
  32. * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
  33. * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
  34. * @ApiReturn ({
  35. 'code':'1',
  36. 'msg':'返回成功'
  37. })
  38. */
  39. public function test()
  40. {
  41. $this->success('返回成功', $this->request->param());
  42. }
  43. /**
  44. * 无需登录的接口
  45. *
  46. */
  47. public function test1()
  48. {
  49. $this->success('返回成功', ['action' => 'test1']);
  50. }
  51. /**
  52. * 需要登录的接口
  53. *
  54. */
  55. public function test2()
  56. {
  57. $this->success('返回成功', ['action' => 'test2']);
  58. }
  59. /**
  60. * 需要登录且需要验证有相应组的权限
  61. *
  62. */
  63. public function test3()
  64. {
  65. $this->success('返回成功', ['action' => 'test3']);
  66. }
  67. public function export1(){
  68. $order= new \addons\unishop\model\Order();
  69. $one=$order->where(["out_trade_no"=>"202009205f66f62a5169623"])->find();
  70. var_dump($one->total_price);
  71. // $order->save(['have_paid'=>time(),'pay_type'=>4],['out_trade_no'=>"202009205f66f62a5169623"]);
  72. }
  73. public function export(){
  74. $order_model=New \app\admin\model\unishop\Order();
  75. $list = $order_model
  76. ->alias('o')
  77. ->join('user', 'user.id = o.user_id')
  78. ->join('unishop_order_product', 'unishop_order_product.order_id = o.id')
  79. ->where(['o.have_received'=>[">",0]])
  80. ->field('
  81. user.username,
  82. user.nickname,
  83. user.floor,
  84. user.email,
  85. GROUP_CONCAT(unishop_order_product.title) as title,
  86. o.total_price,
  87. SUM(unishop_order_product.number) as number,
  88. o.remark')
  89. ->group("o.id")
  90. ->select();
  91. $list = collection($list)->toArray();
  92. $title = ['工号','姓名','楼层','邮箱','商品','金额','购买数量','备注'];
  93. // Create new Spreadsheet object
  94. $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  95. $sheet = $spreadsheet->getActiveSheet();
  96. $sheet->setTitle("订单信息");
  97. // 方法一,使用 setCellValueByColumnAndRow
  98. //表头
  99. //设置单元格内容
  100. foreach ($title as $key => $value) {
  101. // 单元格内容写入
  102. $sheet->setCellValueByColumnAndRow($key + 1, 1, $value);
  103. }
  104. $row = 2; // 从第二行开始
  105. foreach ($list as $item) {
  106. $column = 1;
  107. foreach ($item as $value) {
  108. // 单元格内容写入
  109. $sheet->setCellValueByColumnAndRow($column, $row, $value);
  110. $column++;
  111. }
  112. $row++;
  113. }
  114. $sheet_two = $spreadsheet->createSheet(2)->setTitle('商品信息');
  115. $title1=["商品名称","件数","单价","总价"];
  116. $product=new \app\admin\model\unishop\OrderProduct();
  117. $product_list = $product->alias("p")
  118. ->join("unishop_order","p.order_id = unishop_order.id")
  119. ->where(['unishop_order.have_received'=>[">",0]])
  120. ->field('
  121. p.title,
  122. SUM(p.number) num,
  123. p.price,
  124. SUM(p.number)*p.price')
  125. ->group("p.id")
  126. ->select();
  127. $product_list = collection($product_list)->toArray();
  128. foreach ($title1 as $key => $value) {
  129. // 单元格内容写入
  130. $sheet_two->setCellValueByColumnAndRow($key + 1, 1, $value);
  131. }
  132. $row=2;
  133. foreach ($product_list as $item) {
  134. $column = 1;
  135. foreach ($item as $value) {
  136. // 单元格内容写入
  137. $sheet_two->setCellValueByColumnAndRow($column, $row, $value);
  138. $column++;
  139. }
  140. $row++;
  141. }
  142. $file_name="导出订单.xlsx";
  143. // Redirect output to a client’s web browser (Xlsx)
  144. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  145. header('Content-Disposition: attachment;filename='.$file_name);
  146. header('Cache-Control: max-age=0');
  147. // If you're serving to IE 9, then the following may be needed
  148. header('Cache-Control: max-age=1');
  149. // If you're serving to IE over SSL, then the following may be needed
  150. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  151. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  152. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  153. header('Pragma: public'); // HTTP/1.0
  154. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  155. $writer->save('php://output');
  156. exit;
  157. }
  158. }