Banner.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\admin\model\Banner as BannerModel;
  5. use Exception;
  6. use think\facade\Db;
  7. class Banner extends BaseController
  8. {
  9. /**
  10. * 删除轮播
  11. */
  12. function delete()
  13. {
  14. Db::startTrans();
  15. try {
  16. $id = $this->request->param('id');
  17. $banner = BannerModel::findOrFail($id);
  18. $banner->delete();
  19. DB::commit();
  20. } catch (Exception $e) {
  21. DB::rollBack();
  22. return $this->error($e->getMessage());
  23. }
  24. return $this->success();
  25. }
  26. /**
  27. * 编辑轮播
  28. */
  29. function update()
  30. {
  31. $errors = [];
  32. Db::startTrans();
  33. try {
  34. $params = $this->request->param();
  35. $params['id'] = $this->request->param('id', 0);
  36. $id = $params['id'];
  37. unset($params['id']);
  38. $params['link'] = isset($params['link']) ? $params['link'] : '';
  39. $params['remarks'] = isset($params['remarks']) ? $params['remarks'] : '';
  40. $params['img_url'] = replacePartInUrl($params['img_url']);
  41. if ($id > 0) {
  42. BannerModel::where('id', $id)->update($params);
  43. } else {
  44. BannerModel::create($params);
  45. }
  46. Db::commit();
  47. } catch (Exception $e) {
  48. Db::rollBack();
  49. return $this->error($e->getMessage(), $errors);
  50. }
  51. return $this->success();
  52. }
  53. /**
  54. * 首页轮播
  55. */
  56. public function index()
  57. {
  58. try {
  59. $page = $this->request->param('page', 1);
  60. $limit = $this->request->param('limit', 15);
  61. $count = BannerModel::count();
  62. $list = BannerModel::limit($limit)
  63. ->page($page)
  64. ->order('sort','asc')
  65. ->select()->toArray();
  66. } catch (Exception $e) {
  67. return $this->error($e->getMessage());
  68. }
  69. return $this->success(['count' => $count, 'list' => $list]);
  70. }
  71. }