CsController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\admin\classes\AdminRoleClass;
  4. use bizGhs\item\classes\ItemClassClass;
  5. use bizGhs\merchant\classes\ShopClass;
  6. use bizGhs\product\classes\ProductClass;
  7. use bizGhs\product\models\Product;
  8. use bizGhs\product\services\ProductService;
  9. use common\components\stringUtil;
  10. use common\components\util;
  11. use Yii;
  12. class CsController extends PublicController
  13. {
  14. public $sjId = 12568, $shopId = 214;
  15. public function actionItem()
  16. {
  17. $shopId = 214;
  18. if (getenv('YII_ENV', 'local') != 'production') {
  19. $shopId = 36225;
  20. $this->shopId = 36225;
  21. $this->sjId = 12715;
  22. }
  23. $py = Yii::$app->request->get('py', '');
  24. $type = Yii::$app->request->get('type', '');
  25. //默认只显示上架的商品
  26. $status = Yii::$app->request->get('status', 1);
  27. //获取所有当前供应商的分类 (全部、常用)
  28. //根据分类组装分类下的花材信息
  29. $classIds = ItemClassClass::getGhsItemClassAll($this->sjId);
  30. $where['sjId'] = $this->sjId;
  31. if ($py) {
  32. $pyName = strtolower($py);
  33. $where['py'] = $pyName;
  34. }
  35. $where['shopId'] = $shopId;
  36. if (!empty($status)) {
  37. $where['status'] = $status;
  38. }
  39. $where['delStatus'] = 0;
  40. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  41. $itemInfoData = ProductClass::groupProductInfo($itemInfoData);
  42. //常用的itemIds
  43. $oftenItemIds = [];
  44. //$oftenItemIds = ProductClass::getOftenItemIds($shopId);
  45. //获取供应商下所有花材ID (itemIds)
  46. $itemIdsInfo = ProductClass::getGhsItemIds($this->sjId);
  47. //dd($itemInfoData);
  48. //组装数据: [{classId:'','className:'',child:[{itemInfoData}]}]
  49. $data = ProductService::assembleData($classIds, $itemIdsInfo, $itemInfoData, $oftenItemIds, $type);
  50. $formatData = [];
  51. foreach ($data as $key => $class) {
  52. $name = "【{$class['className']}】" ?? '';
  53. $formatData[] = ['name' => $name, 'price' => ''];
  54. if (isset($class['child']) && !empty($class['child'])) {
  55. foreach ($class['child'] as $cKey => $child) {
  56. $cName = $child['name'] ?? '';
  57. $price = $child['price'] ?? '';
  58. $formatData[] = [
  59. 'name' => $cName,
  60. 'price' => $price,
  61. ];
  62. }
  63. }
  64. }
  65. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  66. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  67. $objPHPExcel = new \PHPExcel();
  68. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  69. ->setLastModifiedBy("Maarten Balliauw")
  70. ->setTitle("Office 2007 XLSX Test Document")
  71. ->setSubject("Office 2007 XLSX Test Document")
  72. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  73. ->setKeywords("office 2007 openxml php")
  74. ->setCategory("Test result file");
  75. $i = 1;
  76. foreach ($formatData as $key => $product) {
  77. // $objPHPExcel->setActiveSheetIndex(0)//表头的信息
  78. // ->setCellValue('A1', "编号")
  79. // ->setCellValue('B1', "所属上级")
  80. // ->setCellValue('C1', "路由名称")
  81. // ->setCellValue('D1', "菜单标题")
  82. // ->setCellValue('E1', "状态");
  83. // $i = 2;
  84. $objPHPExcel->getActiveSheet()// 设置第一个内置表(一个xls文件里可以有多个表)为活动的
  85. ->setCellValue('A' . $i, $product['name'])//给表的单元格设置数据
  86. ->setCellValue('B' . $i, $product['price']);//数据格式可以为字符串
  87. $i++;
  88. }
  89. $objPHPExcel->getActiveSheet()->setTitle('Simple');
  90. $objPHPExcel->setActiveSheetIndex(0);
  91. header('Content-Type: application/vnd.ms-excel');
  92. header('Content-Disposition: attachment;filename="01simple.xls"');
  93. header('Cache-Control: max-age=0');
  94. header('Cache-Control: max-age=1');
  95. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  96. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  97. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  98. header('Pragma: public'); // HTTP/1.0
  99. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  100. $objWriter->save('php://output');
  101. exit;
  102. }
  103. }