Banner.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class Banner extends Model
  7. {
  8. public function add(){
  9. $data = [
  10. 'id' => input('id/d',0),
  11. 'title' => input('title','','trim'),
  12. 'img' => input('img','','trim'),
  13. 'type' => input('type/d','0'),
  14. 'enable' => input('enable/d',0),
  15. 'sort' => input('sort/d',0)
  16. ];
  17. $logdata = json_encode($data);
  18. $result = validate('Banner')->check($data,[],'');
  19. if(true !== $result){
  20. HelpHander::error(validate('Banner')->getError());
  21. }
  22. $id = $data['id'];
  23. unset($data['id']);
  24. if($id > 0){
  25. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  26. }else{
  27. $ret = $this->allowField(true)->save($data);
  28. }
  29. if(!$ret){
  30. HelpHander::error('操作失败');
  31. }
  32. if($id > 0){
  33. $content = '修改轮播图';
  34. }else{
  35. $content = '添加轮播图';
  36. }
  37. model('ActionLog')->add(18,$content,0,$logdata);
  38. return true;
  39. }
  40. public function info($id){
  41. $info = $this->where('id',$id)->find();
  42. if(!$info){
  43. HelpHander::error('数据不存在');
  44. }
  45. return $info->toArray();
  46. }
  47. public function lists($page,$size,$type,$title){
  48. $map[] = ['del','=',0];
  49. if($title != ''){
  50. $map[] = ['title','like','%'.$title.'%'];
  51. }
  52. if(in_array($type,[0,1])){
  53. $map[] = ['type','=',$type];
  54. }
  55. $lists = $this
  56. ->where($map)
  57. ->page($page,$size)
  58. ->order('sort asc,id asc')
  59. ->select();
  60. $total = $this->where($map)->count();
  61. $data = [
  62. 'total' => $total,
  63. 'list' => $lists?$lists->toArray():[]
  64. ];
  65. return $data;
  66. }
  67. public function del($id){
  68. $ret = $this->where('id',$id)->delete();
  69. if(!$ret){
  70. HelpHander::error('删除失败');
  71. }
  72. $logdata = json_encode(['id' => $id]);
  73. model('ActionLog')->add(18,'删除轮播图',0,$logdata);
  74. return true;
  75. }
  76. }