Server : nginx/1.20.1 System : Linux iZ2ze9ojcl78uluczwag69Z 4.18.0-240.22.1.el8_3.x86_64 #1 SMP Thu Apr 8 19:01:30 UTC 2021 x86_64 User : www ( 1000) PHP Version : 7.3.28 Disable Function : passthru,exec,system,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv Directory : /www/wwwroot/0531yanglao.com/app/common/model/ |
<?php namespace app\common\model; use app\common\model\Attachment; use app\common\model\BaseModel; use app\common\utils\Arr; use think\facade\Cache; use think\helper\Str; /** * 分类模型 * @author aaron * */ class Category extends BaseModel { protected $pk = 'cid'; protected $name = 'category'; protected static $cacheKey = 'cache.category'; public static function onAfterWrite($data) { Cache::delete(self::$cacheKey); } public static function onAfterDelete($data) { /*Cache::delete(self::$cacheKey); //删除附件 Attachment::del($data['filename']);*/ } public function unit() { return $this->belongsTo(Unit::class, 'unit_id'); } public function setTypeAttr($value) { return Str::snake($value); } /** * @Description 获取数据 * @return array * @author Aaron * @date 2021/9/9 下午2:11 */ public static function getCacheData() { /*$data = Cache::get(self::$cacheKey); if(empty($data)) { $data = self::getAll([], 0, ['sort'=>'asc']); if(empty($data)) { return []; } $data = Arr::array_to_tree($data->toArray() ,'id', 'parent_id'); Cache::set(self::$cacheKey, $data); }*/ $data = self::getCategoryAll()['all']; return $data; } /** * @Description 获取分类tree * @param $type 类型 * @return array|mixed|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author Aaron * @date 2021/4/26 下午5:07 */ public static function getCategoryTree($type = 'default') { $data = self::getCategoryAll()['tree']; return $data[$type] ?? null; } /** * @Description 获取指定分类下所有子ID * @param $parent_id 主键 * @param array $all 所有数据 * @return array * @author Aaron * @date 2021/4/29 下午4:58 */ public static function getSubCategoryId($parent_id, $all = []) { $arrIds = [$parent_id]; empty($all) && $all = self::getCategoryAll()['all']; foreach ($all as $key => $item) { if ($item['parent_id'] == $parent_id) { unset($all[$key]); $subIds = self::getSubCategoryId($item['cid'], $all); !empty($subIds) && $arrIds = array_merge($arrIds, $subIds); } } return $arrIds; } /** * @Description 获取上级ID * @param $category_id 子分类ID * @return int|mixed * @author Aaron * @date 2021/8/4 上午11:33 */ public static function getPrevCategoryId($category_id) { $data = self::detail($category_id); return $data['parent_id'] ?? 0; } /** * @Description 获取分类数据 * @return array[]|mixed|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author Aaron * @date 2021/4/29 下午4:51 */ private static function getCategoryAll() { $data = Cache::get(self::$cacheKey); if(!$data) { $data = [ 'tree' => [], 'all' => [], ]; $category = self::getAll([], 0, ['sort'=>'asc'])->toArray(); if(empty($category)) { return $data; } $group = Arr::array_group_by($category, 'type'); if($group) { foreach ($group as $key => $item) { $group[$key] = Arr::array_to_tree($item ,'cid', 'parent_id'); } } //$tree = Arr::array_to_tree($category ,'cid', 'parent_id'); $data['tree'] = $group; $data['all'] = $category; Cache::set(self::$cacheKey, $data); } return $data; } /** * @Description 获取分类面包屑数组 * @param $value * @return array * @author Aaron * @date 2021/5/7 下午1:22 */ public static function getCrumbs($value) { $crumbs = self::getCrumbsData($value); if(empty($crumbs)) { return $crumbs; } $sort_key = []; foreach($crumbs as $key=>$item) { $sort_key[] = $key; } $sort_key = array_reverse($sort_key); foreach ($sort_key as $value) { $return[] = $crumbs[$value]; } return $return; } private static function getCrumbsData($value) { $crumbs = []; $category = self::detail($value); if(!$category) { return $crumbs; } $sub_crumbs = [ 'cid' => $category['cid'], 'title' => $category['title'], ]; array_push($crumbs, $sub_crumbs); if($category['parent_id']) { $sub_crumbs = self::getCrumbsData($category['parent_id']); $crumbs = array_merge($crumbs, $sub_crumbs); } return $crumbs; } public static function getLists($where, $limit = 10) { $data = self::getList($where, $limit); if(!$data->isEmpty()) { foreach ($data as $item) { $item->children = self::where(['parent_id'=>$item['cid']])->select(); } } return $data; } /*public static function getCrumbs($value) { $crumbs = []; $category = self::detail($value); if(!$category) { return $crumbs; } $sub_crumbs = [ 'cid' => $category['cid'], 'name' => $category['name'], ]; array_push($crumbs, $sub_crumbs); if($category['parent_id']) { $sub_crumbs = self::getCrumbs($category['parent_id']); $crumbs = array_merge($crumbs, $sub_crumbs); } $crumbs = array_reverse($crumbs, false); return $crumbs; }*/ }