| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace app\admin\controller\store;
- use app\common\controller\AdminController;
- use app\common\validate\IDMustBePositiveInt;
- use app\common\validate\PagingParameter;
- use app\http\IResponse;
- class Banner extends AdminController
- {
- protected $model = "common/Banner";
- public function index()
- {
- $where = [];
- // 验证数据
- (new PagingParameter())->valid();
- $banners = model($this->model);
- return IResponse::paginate($banners->where($where)
- ->paginate(input('limit'),false));
- }
- /**
- * 读取这个ID下的轮播图列表
- *
- * @param int $id 位置ID
- *
- * @return mixed
- * @throws \Lettered\Support\Exceptions\EvidentException
- * @throws \think\exception\DbException
- */
- public function read($id){
- // 输入参数检查
- (new IDMustBePositiveInt())->valid();
- // 获取ID数据
- $items = model("common/BannerItem");
- return IResponse::paginate($items->where(['banner_id' => $id])
- ->paginate(input('limit'),false));
- }
- public function bannerItem()
- {
- // 数据校验
- $params = $this->request->param();
- $valid = $this->validate($params, [
- 'banner_id|所属位置' => 'require',
- 'name|轮播名' => 'require',
- 'open_type|跳转类型' => 'require',
- 'open_url|跳转地址' => 'requireWith:open_type',
- 'img|轮播图' => 'require',
- ]);
- // 错误返回
- (true !== $valid) && IResponse::failure($valid);
- // 保存数据
- $bannerId = model("common/BannerItem")->storeBy($params);
- return $bannerId ? IResponse::success([],'新增轮播图成功'):
- IResponse::failure('新增轮播图异常');
- }
- /**
- * @param $id
- *
- * @return mixed
- * @throws \Lettered\Support\Exceptions\EvidentException
- */
- public function updateBannerItem($id){
- // 输入参数检查
- (new IDMustBePositiveInt())->valid();
- // 数据校验
- $params = $this->request->param();
- $valid = $this->validate($params, [
- 'banner_id|所属位置' => 'require',
- 'name|轮播名' => 'require',
- 'open_type|跳转类型' => 'require',
- 'open_url|跳转地址' => 'requireWith:open_type',
- 'img|轮播图' => 'require',
- ]);
- // 错误返回
- (true !== $valid) && IResponse::failure($valid);
- // 查询用户
- $bannerItem = model("common/BannerItem")->findBy($id);
- $bannerItem->updateBy($id, $params);
- return IResponse::success([],'操作成功');
- }
- /**
- * 删除
- *
- * @param $id
- * @return \think\response\Json
- */
- public function deleteBannerItem($id)
- {
- model("common/BannerItem")->deleteBy($id);
- return IResponse::success([],'删除成功');
- }
- /**
- * 批量操作
- * @return mixed
- */
- public function bannerItemPlectron()
- {
- // 参数
- $params = $this->request->param();
- foreach (str2arr($params['ids']) as $id){
- $user = model("common/BannerItem")->getBy($id);
- if ($this->request->isDelete()){
- $user->deleteBy($id);
- return IResponse::success([],'删除轮播图成功');
- }
- $user->allowField(true)->updateBy($id, $params);
- }
- return IResponse::success([],'操作成功');
- }
- public function update($id)
- {
- // 接收数据
- $params = $this->request->param();
- // 查询
- $banner = model($this->model)->findBy($id);
- // 是否更改状态操作
- if (isset($params['status']) && $params['status'] != '') {
- $valid = $this->validate($params, [
- 'status|轮播图状态' => 'require|integer'
- ]);
- }else {
- // 数据校验
- $valid = $this->validate($params, [
- 'name|轮播图名称' => 'require|email',
- 'description|轮播图描述' => 'require|alpha'
- ]);
- }
- // 错误返回
- (true !== $valid) && IResponse::failure($valid);
- // 更新信息
- $banner->updateBy($id, $params);
- return IResponse::success('更新轮播图信息成功');
- }
- /**
- * 删除
- *
- * @param $id
- * @return \think\response\Json
- */
- public function delete($id)
- {
- model($this->model)->deleteBy($id);
- return IResponse::success([],'删除成功');
- }
- }
|