BannarController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace addons\Course\backend\controllers;
  3. use addons\Course\common\models\AddonsCourseBanners;
  4. use common\enums\StatusEnum;
  5. class BannarController extends BaseController
  6. {
  7. public function actionStorage()
  8. {
  9. if (\Yii::$app->request->isAjax) {
  10. if (\Yii::$app->request->isGet) {
  11. $id = \Yii::$app->request->get('id');
  12. if (empty($id)) {
  13. // 添加
  14. } else {
  15. // 编辑
  16. $bannar_info = AddonsCourseBanners::findOne(['id' => $id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]);
  17. $bannar_info['pic_link'] = json_decode($bannar_info['pic_link'], true);
  18. return $this->success('操作成功', $bannar_info);
  19. }
  20. } else {
  21. $post = \Yii::$app->request->post();
  22. $rules = [
  23. [['title', 'sort', 'is_show', 'pic_path'], 'required'],
  24. [['title', 'pic_path'], 'string'],
  25. [['sort', 'is_show'], 'integer'],
  26. [['id', 'pic_link'], 'safe'],
  27. ];
  28. $res = \Yii::$app->services->validate->validate($post, $rules);
  29. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  30. $res = AddonsCourseBanners::setData($this->mall_id, $post);
  31. if (false === $res) {
  32. return $this->error(AddonsCourseBanners::getStaticError());
  33. }
  34. return $this->success('操作成功');
  35. }
  36. } else {
  37. return $this->render('/bannar/add-edit');
  38. }
  39. }
  40. public function actionSwitchStatus()
  41. {
  42. if (!\Yii::$app->request->isPost) {
  43. return $this->error('请求方式错误');
  44. }
  45. $id = \Yii::$app->request->post('id');
  46. $is_show = \Yii::$app->request->post('val');
  47. if (empty($id) || (empty($is_show) && 0 != $is_show)) {
  48. return $this->error('参数错误');
  49. }
  50. $model = AddonsCourseBanners::findOne(['id' => $id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
  51. $model->is_show = $is_show;
  52. $model->updated_at = time();
  53. $res = $model->save();
  54. // dd($res);
  55. if (false === $res) {
  56. return $this->error('操作失败');
  57. } else {
  58. return $this->success('操作成功');
  59. }
  60. }
  61. public function actionDelete()
  62. {
  63. if (!\Yii::$app->request->isPost) {
  64. return $this->error('请求方式错误');
  65. }
  66. $id = \Yii::$app->request->post('id');
  67. if (empty($id)) {
  68. return $this->error('参数错误');
  69. }
  70. $model = AddonsCourseBanners::findOne(['id' => $id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
  71. $model->updated_at = time();
  72. $model->status = StatusEnum::DELETE;
  73. $res = $model->save();
  74. if (false === $res) {
  75. return $this->error('删除失败');
  76. }
  77. return $this->success('删除成功');
  78. }
  79. }