Banner.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\common\controller\AdminController;
  4. use app\common\model\SystemBannerModel;
  5. use think\App;
  6. use utils\RedisCache;
  7. /**
  8. * @ControllerAnnotation(title="系统banner配置")
  9. */
  10. class Banner extends AdminController
  11. {
  12. use \app\admin\traits\Curd;
  13. public function __construct (App $app)
  14. {
  15. parent::__construct($app);
  16. $this->model = new SystemBannerModel();
  17. }
  18. public function index ()
  19. {
  20. if ($this->request->isAjax()) {
  21. if (input('selectFields')) {
  22. return $this->selectList();
  23. }
  24. list($page, $limit, $where) = $this->buildTableParames();
  25. $count = $this->model
  26. ->where($where)
  27. ->count();
  28. // return 11;
  29. // return json_decode('[{"price":10, "fee":1},{"price":50, "fee":0.5},{"price":100, "fee":0}]');
  30. $list = $this->model
  31. ->where($where)
  32. ->withAttr('img_pic', function ($val, $data){
  33. return __HTTPGETIMGADMIN($val);
  34. })
  35. ->page($page, $limit)
  36. ->order($this->sort)
  37. ->select();
  38. $data = [
  39. 'code' => 0,
  40. 'msg' => '',
  41. 'count' => $count,
  42. 'data' => $list,
  43. ];
  44. return json($data);
  45. }
  46. return $this->fetch();
  47. }
  48. /**
  49. * @NodeAnotation(title="修改")
  50. */
  51. public function edit ($id)
  52. {
  53. $row = $this->model->find($id);
  54. empty($row) && $this->error('数据不存在');
  55. if ($this->request->isAjax()) {
  56. $post = $this->request->post();
  57. $rule = [];
  58. $this->validate($post, $rule);
  59. try {
  60. $post['banner_desc'] = htmlspecialchars_decode($post['banner_desc']);
  61. $post['img_pic'] = __HTTPSAVEIMGADMIN($post['img_pic']);
  62. $save = $row->save($post);
  63. RedisCache::clear('getAppBanner'.$row['type']);
  64. } catch (\Exception $e) {
  65. $this->error('保存失败');
  66. }
  67. $save ? $this->success('保存成功') : $this->error('保存失败');
  68. }
  69. $row['img_pic'] = __HTTPGETIMGADMIN($row['img_pic']);
  70. $this->assign('row', $row);
  71. return $this->fetch();
  72. }
  73. /**
  74. * @NodeAnotation(title="添加")
  75. */
  76. public function add()
  77. {
  78. if ($this->request->isAjax()){
  79. $row = request()->post();
  80. $row['img_pic'] = __HTTPSAVEIMGADMIN($row['img_pic']);
  81. $row['banner_desc'] = htmlspecialchars_decode($row['banner_desc']);
  82. $row['create_time'] = sr_getcurtime(time());
  83. unset($row['file']);
  84. $save = $this->model->insert($row);
  85. RedisCache::clear('getAppBanner'.$row['type']);
  86. $save?$this->success('添加成功'):$this->error('添加失败');
  87. }
  88. return $this->fetch();
  89. }
  90. }