'富文本', self::TEXT => '纯文本', self::IMG => '图片', self::GROUP => '多图', self::FILE => '文件', self::VIDEO => '视频', self::ING_LIST => '图文列表', ]; } //根据键值和类目获取数据 public static function getByKeyword($keyword, $categoryId) { return self::where('keyword', $keyword)->where('category_id', $categoryId)->findOrEmpty()->toArray(); } //根据栏目ID获取块列表 public static function getByCategoryId($categoryId) { if($categoryId <= 0){ return []; } $items = self::where('category_id', $categoryId) ->order('id desc') ->select() ->toArray(); if(empty($items)){ return []; } $data = []; foreach($items as $item){ $data[$item['name']] = $item; } return $data; } //获取在使用中的文件 public static function getFilesInUse() { $items = self::whereIn('type', [self::IMG, self::GROUP, self::VIDEO, self::TEXT]) ->select() ->toArray(); $data = []; foreach($items as $item){ if($item['type'] == self::IMG){ $file = trim($item['value']); if(!empty($file)){ $key = getKeyByPath($file); $data[$key] = $file; } }elseif($item['type'] == self::GROUP){ $val = trim($item['value']); if (!empty($val) && $val != '[]' && $val != 'null') { $files = json_decode($val, true); foreach($files as $file){ $src = trim($file['src']); if(!empty($src)){ $key = getKeyByPath($src); $data[$key] = $src; } } } }elseif($item['type'] == self::VIDEO){ $video = trim($item['value']); if(!empty($video)){ $key = getKeyByPath($video); $data[$key] = $video; } $img = trim($item['img']); if(!empty($img)){ $key = getKeyByPath($img); $data[$key] = $img; } }elseif($item['type'] == self::TEXT){ $imgs = getImageUrlFromText($item['value']); if(!empty($imgs)){ $data = array_merge($data, $imgs); } $videos = getVideoUrlFromText($item['value']); if(!empty($videos)){ $data = array_merge($data, $videos); } } } return $data; } /** * 栏目 * * @return HasOne */ public function category(): HasOne { return $this->HasOne(ArchivesCategory::class, 'id', 'category_id'); } /** * 设置内容 * */ public function setContentAttr($value,$data) { if($data['type'] == self::ING_LIST){ $content = array_column($value,null,"sort"); $contentKey = array_keys($content); sort($contentKey); $contentData =[]; foreach ($contentKey as $ck){ $contentData[]= $content[$ck]; } return json_encode($contentData); } return $value; } /** * 设置内容 * */ public function getContentAttr($value,$data) { if($data['type'] == self::ING_LIST){ return json_decode($value,true); } return $value; } }