BannerController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace addons\Bargain\backend\controllers;
  3. use addons\Bargain\common\models\BargainBanners;
  4. use common\enums\StatusEnum;
  5. class BannerController extends BaseController
  6. {
  7. public function actionIndex()
  8. {
  9. if (\Yii::$app->request->isAjax) {
  10. if (\Yii::$app->request->isGet) {
  11. $keyword = \Yii::$app->request->get('keyword');
  12. $page = \Yii::$app->request->get('page', 1);
  13. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  14. $where[] = ['status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id];
  15. if (!empty($keyword)) {
  16. $where[] = ['like', 'title', $keyword];
  17. }
  18. $params = [
  19. 'order' => 'sort desc,created_at desc',
  20. 'where' => $where,
  21. 'page' => $page,
  22. 'limit' => $limit,
  23. ];
  24. $list = BargainBanners::listPage($params);
  25. if (false === $list['list']) {
  26. return $this->error(BargainBanners::getStaticError());
  27. }
  28. return $this->success('success', $list);
  29. } else {
  30. }
  31. } else {
  32. return $this->render('/bannar/index');
  33. }
  34. }
  35. public function actionStorage()
  36. {
  37. if (\Yii::$app->request->isAjax) {
  38. if (\Yii::$app->request->isGet) {
  39. $id = \Yii::$app->request->get('id');
  40. if (empty($id)) {
  41. // 添加
  42. } else {
  43. // 编辑
  44. $bannar_info = BargainBanners::findOne(['id' => $id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]);
  45. $bannar_info['pic_link'] = json_decode($bannar_info['pic_link'], true);
  46. return $this->success('操作成功', $bannar_info);
  47. }
  48. } else {
  49. $post = \Yii::$app->request->post();
  50. $rules = [
  51. [['sort', 'is_show', 'pic_path'], 'required'],
  52. [['pic_path'], 'string'],
  53. [['sort', 'is_show'], 'integer'],
  54. [['id', 'pic_link'], 'safe'],
  55. ];
  56. $res = \Yii::$app->services->validate->validate($post, $rules);
  57. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  58. $res = BargainBanners::setData($this->mall_id, $post);
  59. if (false === $res) {
  60. return $this->error(BargainBanners::getStaticError());
  61. }
  62. return $this->success('操作成功');
  63. }
  64. } else {
  65. return $this->render('/bannar/add-edit');
  66. }
  67. }
  68. public function actionSwitchStatus()
  69. {
  70. if (!\Yii::$app->request->isPost) {
  71. return $this->error('请求方式错误');
  72. }
  73. $id = \Yii::$app->request->post('id');
  74. $is_show = \Yii::$app->request->post('val');
  75. if (empty($id) || (empty($is_show) && 0 != $is_show)) {
  76. return $this->error('参数错误');
  77. }
  78. $model = BargainBanners::findOne(['id' => $id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
  79. $model->is_show = $is_show;
  80. $model->updated_at = time();
  81. $res = $model->save();
  82. // dd($res);
  83. if (false === $res) {
  84. return $this->error('操作失败');
  85. } else {
  86. return $this->success('操作成功');
  87. }
  88. }
  89. public function actionDelete()
  90. {
  91. if (!\Yii::$app->request->isPost) {
  92. return $this->error('请求方式错误');
  93. }
  94. $id = \Yii::$app->request->post('id');
  95. if (empty($id)) {
  96. return $this->error('参数错误');
  97. }
  98. $model = BargainBanners::findOne(['id' => $id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
  99. $model->updated_at = time();
  100. $model->status = StatusEnum::DELETE;
  101. $res = $model->save();
  102. if (false === $res) {
  103. return $this->error('删除失败');
  104. }
  105. return $this->success('删除成功');
  106. }
  107. }