NewsCate.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\system;
  4. use app\Request;
  5. use library\basic\BaseModel;
  6. use library\services\UtilService;
  7. use library\utils\Arr;
  8. use think\db\exception\DbException;
  9. use think\Model;
  10. /**
  11. * @mixin \think\Model
  12. */
  13. class NewsCate extends BaseModel
  14. {
  15. private $_data;
  16. /**
  17. * 获取分类数据
  18. * @return array
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\DbException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. */
  23. public function getArCate($show = 0){
  24. $cateData = $this->when(!empty($show), function ($query){
  25. $query->where('is_show',1);
  26. })->order("seq","desc")->select();
  27. $data = [];
  28. foreach ($cateData as $item) {
  29. $data[] = $item->getData();
  30. }
  31. $menuAr = Arr::getTree($data);
  32. return $menuAr;
  33. }
  34. /**
  35. * 保存分类
  36. * @param $post
  37. * @return bool
  38. */
  39. public function cateSave($post){
  40. if(!empty($post['id'])) {
  41. $this->where('id',$post['id'])->save($post);
  42. return true;
  43. } else {
  44. unset($post['id']);
  45. $bool = $this->insert($post);
  46. return $bool;
  47. }
  48. }
  49. /**
  50. * 删除分类
  51. * @param $id
  52. */
  53. public function cateDel($id){
  54. $this->where('pid',$id)->delete();
  55. $this->where('id',$id)->delete();
  56. return true;
  57. }
  58. /**
  59. * 设置显示状态
  60. * @param $id
  61. * @param $status
  62. */
  63. public function cateShowSet($id,$status) {
  64. self::beginTrans();
  65. try{
  66. $this->where('id', $id)->save(['is_show'=>$status]);
  67. $this->where('pid',$id)->save(['is_show'=>$status]);
  68. self::commitTrans();
  69. return true;
  70. }catch (DbException $db) {
  71. self::rollbackTrans();
  72. return false;
  73. }
  74. }
  75. }