SlideController.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers;
  8. use app\models\Banner;
  9. use app\modules\admin\models\SlideForm;
  10. use yii\base\BaseObject;
  11. class SlideController extends BaseController
  12. {
  13. // 幻灯片列表
  14. public function actionList()
  15. {
  16. $type = !empty(get_params('type')) ?: Banner::TYPE_SAAS;
  17. $form = new SlideForm();
  18. $form->type = $type;
  19. return $this->asJson($form->getList(get_store_id()));
  20. }
  21. // 幻灯片编辑
  22. public function actionEdit()
  23. {
  24. $id = input_params('id');
  25. $type = input_params('type') ?: Banner::TYPE_SAAS;
  26. $banner = Banner::findOne(['id' => $id, 'type' => $type]);
  27. if (!$banner) {
  28. $banner = new Banner();
  29. }
  30. if (\Yii::$app->request->isPost) {
  31. $form = new SlideForm();
  32. $form->attributes = post_params();
  33. $form->store_id = get_store_id();
  34. $form->banner = $banner;
  35. $form->type = $type;
  36. return $this->asJson($form->save());
  37. }
  38. foreach ($banner as $index => $value) {
  39. $banner[$index] = str_replace("\"", "&quot;", $value);
  40. }
  41. return $this->asJson([
  42. 'code' => 0,
  43. 'msg' => 'success',
  44. 'data' => $banner
  45. ]);
  46. }
  47. // 幻灯片删除
  48. public function actionDelete()
  49. {
  50. $id = input_params('id');
  51. $type = !empty(input_params('type')) ?: Banner::TYPE_SAAS;
  52. $banner = Banner::findOne(['id' => $id, 'is_delete' => 0, 'type' => $type]);
  53. if (!$banner) {
  54. return $this->asJson([
  55. 'code' => 1,
  56. 'msg' => '幻灯片不存在或已经删除',
  57. ]);
  58. }
  59. $banner->is_delete = 1;
  60. if ($banner->save()) {
  61. return $this->asJson([
  62. 'code' => 0,
  63. 'msg' => '成功',
  64. ]);
  65. } else {
  66. foreach ($banner->errors as $errors) {
  67. return $this->asJson([
  68. 'code' => 1,
  69. 'msg' => $errors[0],
  70. ]);
  71. }
  72. }
  73. }
  74. }