Banner.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\common\controller\AdminController;
  4. use app\common\validate\IDMustBePositiveInt;
  5. use app\common\validate\PagingParameter;
  6. use app\http\IResponse;
  7. class Banner extends AdminController
  8. {
  9. protected $model = "common/Banner";
  10. public function index()
  11. {
  12. $where = [];
  13. // 验证数据
  14. (new PagingParameter())->valid();
  15. $banners = model($this->model);
  16. return IResponse::paginate($banners->where($where)
  17. ->paginate(input('limit'),false));
  18. }
  19. /**
  20. * 读取这个ID下的轮播图列表
  21. *
  22. * @param int $id 位置ID
  23. *
  24. * @return mixed
  25. * @throws \Lettered\Support\Exceptions\EvidentException
  26. * @throws \think\exception\DbException
  27. */
  28. public function read($id){
  29. // 输入参数检查
  30. (new IDMustBePositiveInt())->valid();
  31. // 获取ID数据
  32. $items = model("common/BannerItem");
  33. return IResponse::paginate($items->where(['banner_id' => $id])
  34. ->paginate(input('limit'),false));
  35. }
  36. public function bannerItem()
  37. {
  38. // 数据校验
  39. $params = $this->request->param();
  40. $valid = $this->validate($params, [
  41. 'banner_id|所属位置' => 'require',
  42. 'name|轮播名' => 'require',
  43. 'open_type|跳转类型' => 'require',
  44. 'open_url|跳转地址' => 'requireWith:open_type',
  45. 'img|轮播图' => 'require',
  46. ]);
  47. // 错误返回
  48. (true !== $valid) && IResponse::failure($valid);
  49. // 保存数据
  50. $bannerId = model("common/BannerItem")->storeBy($params);
  51. return $bannerId ? IResponse::success([],'新增轮播图成功'):
  52. IResponse::failure('新增轮播图异常');
  53. }
  54. /**
  55. * @param $id
  56. *
  57. * @return mixed
  58. * @throws \Lettered\Support\Exceptions\EvidentException
  59. */
  60. public function updateBannerItem($id){
  61. // 输入参数检查
  62. (new IDMustBePositiveInt())->valid();
  63. // 数据校验
  64. $params = $this->request->param();
  65. $valid = $this->validate($params, [
  66. 'banner_id|所属位置' => 'require',
  67. 'name|轮播名' => 'require',
  68. 'open_type|跳转类型' => 'require',
  69. 'open_url|跳转地址' => 'requireWith:open_type',
  70. 'img|轮播图' => 'require',
  71. ]);
  72. // 错误返回
  73. (true !== $valid) && IResponse::failure($valid);
  74. // 查询用户
  75. $bannerItem = model("common/BannerItem")->findBy($id);
  76. $bannerItem->updateBy($id, $params);
  77. return IResponse::success([],'操作成功');
  78. }
  79. /**
  80. * 删除
  81. *
  82. * @param $id
  83. * @return \think\response\Json
  84. */
  85. public function deleteBannerItem($id)
  86. {
  87. model("common/BannerItem")->deleteBy($id);
  88. return IResponse::success([],'删除成功');
  89. }
  90. /**
  91. * 批量操作
  92. * @return mixed
  93. */
  94. public function bannerItemPlectron()
  95. {
  96. // 参数
  97. $params = $this->request->param();
  98. foreach (str2arr($params['ids']) as $id){
  99. $user = model("common/BannerItem")->getBy($id);
  100. if ($this->request->isDelete()){
  101. $user->deleteBy($id);
  102. return IResponse::success([],'删除轮播图成功');
  103. }
  104. $user->allowField(true)->updateBy($id, $params);
  105. }
  106. return IResponse::success([],'操作成功');
  107. }
  108. public function update($id)
  109. {
  110. // 接收数据
  111. $params = $this->request->param();
  112. // 查询
  113. $banner = model($this->model)->findBy($id);
  114. // 是否更改状态操作
  115. if (isset($params['status']) && $params['status'] != '') {
  116. $valid = $this->validate($params, [
  117. 'status|轮播图状态' => 'require|integer'
  118. ]);
  119. }else {
  120. // 数据校验
  121. $valid = $this->validate($params, [
  122. 'name|轮播图名称' => 'require|email',
  123. 'description|轮播图描述' => 'require|alpha'
  124. ]);
  125. }
  126. // 错误返回
  127. (true !== $valid) && IResponse::failure($valid);
  128. // 更新信息
  129. $banner->updateBy($id, $params);
  130. return IResponse::success('更新轮播图信息成功');
  131. }
  132. /**
  133. * 删除
  134. *
  135. * @param $id
  136. * @return \think\response\Json
  137. */
  138. public function delete($id)
  139. {
  140. model($this->model)->deleteBy($id);
  141. return IResponse::success([],'删除成功');
  142. }
  143. }