SysArticle.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class SysArticle extends Model
  7. {
  8. public function add(){
  9. $data = [
  10. 'id' => input('id/d',0),
  11. 'title' => input('title','','trim'),
  12. 'name' => input('name','','trim'),
  13. 'content' => input('content','','trim')
  14. ];
  15. $result = validate('SysArticle')->check($data,[],'');
  16. if(true !== $result){
  17. HelpHander::error(validate('SysArticle')->getError());
  18. }
  19. $id = $data['id'];
  20. unset($data['id']);
  21. if($id > 0){
  22. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  23. }else{
  24. $ret = $this->allowField(true)->save($data);
  25. }
  26. if(!$ret){
  27. HelpHander::error('操作失败');
  28. }
  29. return true;
  30. }
  31. public function info($id){
  32. $info = $this->where('id',$id)->find();
  33. if(!$info){
  34. HelpHander::error('数据不存在');
  35. }
  36. return $info->toArray();
  37. }
  38. public function lists($page,$size){
  39. $lists = $this
  40. ->page($page,$size)
  41. ->order('id desc')
  42. ->select();
  43. $total = $this->count();
  44. $data = [
  45. 'total' => $total,
  46. 'list' => $lists?$lists->toArray():[]
  47. ];
  48. return $data;
  49. }
  50. public function applists(){
  51. $lists = $this
  52. ->order('id desc')
  53. ->select();
  54. return $lists?$lists->toArray():[];
  55. }
  56. public function del($id){
  57. $ret = $this->where('id',$id)->delete();
  58. if(!$ret){
  59. HelpHander::error('删除失败');
  60. }
  61. return true;
  62. }
  63. }