SlideShowController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 17812
  5. * Date: 2020/9/16
  6. * Time: 9:55
  7. */
  8. namespace app\backend\modules\coupon\controllers;
  9. use app\common\components\BaseController;
  10. use app\common\exceptions\AppException;
  11. use app\common\exceptions\ShopException;
  12. use app\common\models\coupon\CouponSlideShow;
  13. class SlideShowController extends BaseController
  14. {
  15. public function index()
  16. {
  17. $list = CouponSlideShow::uniacid()->paginate('', ['*'], '', (int)request()->page);
  18. $list = $list->toArray();
  19. return view('coupon.slide-show', [
  20. 'list' => json_encode($list),
  21. ])->render();
  22. }
  23. public function search()
  24. {
  25. $list = CouponSlideShow::uniacid()->paginate('', ['*'], '', (int)request()->page);
  26. $list = $list->toArray();
  27. return $this->successJson('ok', $list);
  28. }
  29. public function add()
  30. {
  31. if (\Request::getMethod() == 'POST') {
  32. $data = request()->data;
  33. $model = new CouponSlideShow();
  34. $data['uniacid'] = \YunShop::app()->uniacid;
  35. $model->fill($data);
  36. $validator = $model->validator();
  37. if ($validator->fails()) {
  38. throw new AppException($validator->messages()->first());
  39. }
  40. if (!$model->save()) {
  41. throw new AppException('MySql error, please try again');
  42. } else {
  43. return $this->successJson('ok', '新增成功');
  44. }
  45. }
  46. return view('coupon.slide-show-add', [
  47. ])->render();
  48. }
  49. public function edit()
  50. {
  51. $id = request()->id;
  52. $Model = CouponSlideShow::uniacid()->where('id', $id)
  53. ->first();
  54. if (!$Model) {
  55. throw new ShopException('无此条数据');
  56. }
  57. if (\Request::getMethod() == 'POST') {
  58. $data = request()->data;
  59. $Model->fill($data);
  60. $validator = $Model->validator();
  61. if ($validator->fails()) {
  62. throw new AppException($validator->messages()->first());
  63. }
  64. if (!$Model->save()) {
  65. throw new AppException('MySql error, please try again');
  66. } else {
  67. return $this->successJson('ok', '修改成功');
  68. }
  69. }
  70. return view('coupon.slide-show-add', [
  71. 'data' => json_encode($Model)
  72. ])->render();
  73. }
  74. public function del()
  75. {
  76. $id = request()->id;
  77. $Model = CouponSlideShow::uniacid()->where('id', $id)
  78. ->first();
  79. if (!$Model) {
  80. throw new ShopException('无此条数据');
  81. }
  82. CouponSlideShow::uniacid()->where('id', $id)->delete();
  83. return $this->successJson('ok', '删除成功');
  84. }
  85. public function editSort()
  86. {
  87. $id = request()->id;
  88. $Model = CouponSlideShow::uniacid()->where('id', $id)
  89. ->first();
  90. if (!$Model) {
  91. throw new ShopException('无此条数据');
  92. }
  93. $Model->sort = request()->sort;
  94. if (!$Model->save()) {
  95. throw new AppException('MySql error, please try again');
  96. } else {
  97. return $this->successJson('ok', '修改成功');
  98. }
  99. }
  100. }