TaoLer/app/common/model/Cate.php

132 lines
3.7 KiB
PHP
Raw Normal View History

2020-01-01 13:17:19 +08:00
<?php
2022-08-16 15:21:08 +08:00
/*
* @Author: TaoLer <317927823@qq.com>
* @Date: 2021-12-06 16:04:50
2022-08-16 15:29:04 +08:00
* @LastEditTime: 2022-08-14 07:33:14
2022-08-16 15:21:08 +08:00
* @LastEditors: TaoLer
2022-08-16 15:29:04 +08:00
* @Description: 分类模型
2022-08-16 15:21:08 +08:00
* @FilePath: \github\TaoLer\app\common\model\Cate.php
* Copyright (c) 2020~2022 https://www.aieok.com All rights reserved.
*/
2020-01-01 13:17:19 +08:00
namespace app\common\model;
2023-03-16 22:54:11 +08:00
use think\db\exception\DbException;
2020-01-01 13:17:19 +08:00
use think\Model;
use think\model\concern\SoftDelete;
class Cate extends Model
{
//软删除
use SoftDelete;
protected $deleteTime = 'delete_time';
protected $defaultSoftDelete = 0;
2022-09-23 11:03:10 +08:00
//关联文章
public function article()
{
return $this->hasMany(Article::class);
}
2020-01-01 13:17:19 +08:00
2022-08-16 15:21:08 +08:00
// 查询类别信息
public function getCateInfo(string $ename)
{
//
2022-11-18 10:31:44 +08:00
return $this->field('ename,catename,detpl,desc')->where('ename',$ename)->cache('cate_'.$ename,600)->find();
2022-08-16 15:21:08 +08:00
}
2023-05-05 11:58:42 +08:00
// ID查询类别信息
public function getCateInfoById(int $id)
{
return $this->field('id,catename,ename,detpl,pid,icon,sort,desc')->find($id);
}
2023-03-16 22:30:36 +08:00
// 查询子分类
public function getSubCate(string $ename)
{
return $this->field('ename,catename')->where('pid', $this::where('ename', $ename)->value('id'))->select();
}
2023-07-03 12:50:57 +08:00
// 查询兄弟分类
public function getBrotherCate(string $ename)
{
return $this->field('id,ename,catename')->where('pid', $this::where('ename', $ename)->value('pid'))->append(['url'])->order('sort asc')->select();
}
2023-05-05 11:58:42 +08:00
/**
* 删除分类
* @param $id
* @return int|string
* @throws DbException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
*/
2021-08-03 17:33:48 +08:00
public function del($id)
2020-01-01 13:17:19 +08:00
{
2022-11-18 10:31:44 +08:00
$cates = $this->field('id,pid')->with('article')->find($id);
$sonCate = $this->field('id,pid')->where('pid',$cates['id'])->find();
2022-09-23 11:03:10 +08:00
if(empty($sonCate)) {
$res = $cates->together(['article'])->delete();
2023-05-05 11:58:42 +08:00
return $res ? 1 : '删除失败';
2020-01-01 13:17:19 +08:00
}
2023-05-05 11:58:42 +08:00
return '存在子分类,无法删除';
2020-01-01 13:17:19 +08:00
}
2022-11-18 10:31:44 +08:00
// 分类表
public function getList()
{
2023-05-05 11:58:42 +08:00
$data = $this->field('id,pid,sort,catename,ename,detpl,icon,status,is_hot,desc')->select()->toArray();
2022-11-18 10:31:44 +08:00
if(count($data)) {
2023-03-16 22:40:15 +08:00
// 排序
$cmf_arr = array_column($data, 'sort');
array_multisort($cmf_arr, SORT_ASC, $data);
return json(['code'=>0,'msg'=>'ok', 'count' => count($data),'data'=>$data]);
2022-11-18 10:31:44 +08:00
}
2023-05-05 11:58:42 +08:00
return json(['code'=>-1,'msg'=>'no data','data'=>'']);
2022-11-18 10:31:44 +08:00
}
2023-03-16 22:54:11 +08:00
// 如果菜单下无内容URl不能点击
public function menu()
{
try {
2023-05-05 12:03:45 +08:00
return $this->where(['status' => 1])
->cache('catename', 3600)
2023-03-16 22:54:11 +08:00
->append(['url'])
->select()
->toArray();
} catch (DbException $e) {
return $e->getMessage();
}
}
2023-07-03 12:50:57 +08:00
// 分类导航菜单
public function getNav()
{
try {
$cateList = $this->where(['status' => 1])
->cache('catename', 3600)
->append(['url'])
->select()
->toArray();
// 排序
$cmf_arr = array_column($cateList, 'sort');
array_multisort($cmf_arr, SORT_ASC, $cateList);
return getTree($cateList);
} catch (DbException $e) {
return $e->getMessage();
}
}
2023-03-16 22:54:11 +08:00
// 获取url
public function getUrlAttr($value,$data)
{
// 栏目下存在帖子则返回正常url,否则为死链
2023-05-05 11:58:42 +08:00
$articleArr = Article::field('id')->where('cate_id', $data['id'])->find();
2023-03-16 22:54:11 +08:00
if(empty($articleArr)) {
return 'javascript:void(0);';
}
return (string) url('cate',['ename' => $data['ename']]);;
}
2020-01-01 13:17:19 +08:00
}