You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.0 KiB

  1. <?php
  2. namespace backend\modules\shop\controllers;
  3. use Throwable;
  4. use Yii;
  5. use backend\modules\shop\models\ars\Order;
  6. use backend\modules\shop\models\searchs\OrderSearch;
  7. use yii\db\StaleObjectException;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11. use iron\widget\Excel;
  12. /**
  13. * OrderController implements the CRUD actions for Order model.
  14. */
  15. class OrderController extends Controller
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function behaviors()
  21. {
  22. return [
  23. 'verbs' => [
  24. 'class' => VerbFilter::className(),
  25. 'actions' => [
  26. 'delete' => ['POST'],
  27. ],
  28. ],
  29. ];
  30. }
  31. /**
  32. * Lists all Order models.
  33. * @return mixed
  34. */
  35. public function actionIndex()
  36. {
  37. $searchModel = new OrderSearch();
  38. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  39. return $this->render('index', [
  40. 'searchModel' => $searchModel,
  41. 'dataProvider' => $dataProvider,
  42. 'columns' => $searchModel->columns()
  43. ]);
  44. }
  45. /**
  46. * Displays a single Order model.
  47. * @param integer $id
  48. * @return mixed
  49. * @throws NotFoundHttpException if the model cannot be found
  50. */
  51. public function actionView($id)
  52. {
  53. return $this->render('view', [
  54. 'model' => $this->findModel($id),
  55. ]);
  56. }
  57. /**
  58. * Creates a new Order model.
  59. * If creation is successful, the browser will be redirected to the 'view' page.
  60. * @return mixed
  61. */
  62. public function actionCreate()
  63. {
  64. $model = new Order();
  65. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  66. return $this->redirect('index');
  67. }
  68. return $this->render('create', [
  69. 'model' => $model,
  70. ]);
  71. }
  72. /**
  73. * Updates an existing Order model.
  74. * If update is successful, the browser will be redirected to the 'view' page.
  75. * @param integer $id
  76. * @return mixed
  77. * @throws NotFoundHttpException if the model cannot be found
  78. */
  79. public function actionUpdate($id)
  80. {
  81. $model = $this->findModel($id);
  82. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  83. return $this->redirect('index');
  84. }
  85. return $this->render('update', [
  86. 'model' => $model,
  87. ]);
  88. }
  89. /**
  90. * Deletes an existing Order model.
  91. * If deletion is successful, the browser will be redirected to the 'index' page.
  92. * @param integer $id
  93. * @return mixed
  94. * @throws NotFoundHttpException if the model cannot be found
  95. * @throws Throwable
  96. * @throws StaleObjectException
  97. */
  98. public function actionDelete($id)
  99. {
  100. $this->findModel($id)->delete();
  101. return $this->redirect(['index']);
  102. }
  103. /**
  104. * Finds the Order model based on its primary key value.
  105. * If the model is not found, a 404 HTTP exception will be thrown.
  106. * @param integer $id
  107. * @return Order the loaded model
  108. * @throws NotFoundHttpException if the model cannot be found
  109. */
  110. protected function findModel($id)
  111. {
  112. if (($model = Order::findOne($id)) !== null) {
  113. return $model;
  114. }
  115. throw new NotFoundHttpException('The requested page does not exist.');
  116. }
  117. /**
  118. * @author iron
  119. * 文件导出
  120. */
  121. public function actionExport()
  122. {
  123. $searchModel = new OrderSearch();
  124. $params = Yii::$app->request->queryParams;
  125. if ($params['page-type'] == 'all') {
  126. $dataProvider = $searchModel->allData($params);
  127. } else {
  128. $dataProvider = $searchModel->search($params);
  129. }
  130. Excel::export([
  131. 'models' => $dataProvider->getModels(),
  132. 'format' => 'Xlsx',
  133. 'asAttachment' => true,
  134. 'fileName' =>'Orders'. "-" .date('Y-m-d H/i/s', time()),
  135. 'columns' => $searchModel->columns()
  136. ]);
  137. }
  138. }