171 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			171 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
<?php
 | 
						|
namespace app\model;
 | 
						|
 | 
						|
class Block extends Base
 | 
						|
{
 | 
						|
    const BLOCK     = 1;
 | 
						|
    const IMG       = 2;
 | 
						|
    const TEXT      = 3;
 | 
						|
    const GROUP     = 4;
 | 
						|
    const VIDEO     = 5;
 | 
						|
    const CODE      = 6;
 | 
						|
    const AUDIOS    = 7;
 | 
						|
    const VIDEOS    = 8;
 | 
						|
    const MAP       = 9;
 | 
						|
 | 
						|
    // json化存储的区块类型
 | 
						|
    public static $jsonValueTypes = [self::GROUP,self::AUDIOS,self::MAP];
 | 
						|
    public static $mapType = [
 | 
						|
        "gaode"=>"高德",
 | 
						|
        "baidu"=>"百度",
 | 
						|
        "tengxun"=>"腾讯",
 | 
						|
    ];
 | 
						|
 | 
						|
    //获取类型键值对
 | 
						|
    public static function getTypes()
 | 
						|
    {
 | 
						|
        return [
 | 
						|
            '1' => 'block',
 | 
						|
            '2' => 'img',
 | 
						|
            '3' => 'text',
 | 
						|
            '4' => 'group',
 | 
						|
            '5' => 'video',
 | 
						|
            '6' => 'code',
 | 
						|
            '7' => 'audios',
 | 
						|
            '8' => 'videos',
 | 
						|
            '9' => 'map',
 | 
						|
        ];
 | 
						|
    }
 | 
						|
 | 
						|
    //根据键值和类目获取数据
 | 
						|
    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 [];
 | 
						|
        }
 | 
						|
        $category = Category::getById($categoryId);
 | 
						|
        if(empty($category)){
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
        $items = self::where('category_id', $categoryId)
 | 
						|
        ->order('sort asc')
 | 
						|
        ->select()
 | 
						|
        ->toArray();
 | 
						|
        if(empty($items)){
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
        $data = [];
 | 
						|
        foreach($items as $item){
 | 
						|
            $data[$item['keyword']] = $item;
 | 
						|
        }
 | 
						|
        return $data;
 | 
						|
    }
 | 
						|
 | 
						|
    //根据栏目ID获取块列表
 | 
						|
    public static function getChildrenByCategoryId($categoryId)
 | 
						|
    {
 | 
						|
 | 
						|
        if($categoryId <= 0){
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
        $category = Category::getById($categoryId);
 | 
						|
        if(empty($category)){
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
        $categoryIds = Category::where('path', 'like', $category['path'].$category['id'].',%')->column('id');
 | 
						|
        if(empty($categoryIds)){
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
        $items = self::whereIn('category_id', $categoryIds)
 | 
						|
            ->order('sort asc')
 | 
						|
            ->select()
 | 
						|
            ->toArray();
 | 
						|
        if(empty($items)){
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
        $data = [];
 | 
						|
        foreach($items as $item){
 | 
						|
            $data[$item['keyword']] = $item;
 | 
						|
        }
 | 
						|
        return $data;
 | 
						|
    }
 | 
						|
 | 
						|
    public static function onAfterInsert($item)
 | 
						|
    {
 | 
						|
        $item->sort  = $item->id;
 | 
						|
        $item->save();
 | 
						|
    }
 | 
						|
 | 
						|
    //获取在使用中的文件
 | 
						|
    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;
 | 
						|
    }
 | 
						|
 | 
						|
    // 转化json化存储的数据
 | 
						|
    public static function convertValue($items)
 | 
						|
    {
 | 
						|
        foreach ($items as &$item) {
 | 
						|
            $item['json_value_list'] = [];
 | 
						|
            if(in_array($item['type'], self::$jsonValueTypes) && !empty($item['value'])) {
 | 
						|
                $item['json_value_list'] = json_decode($item['value'], true);
 | 
						|
            }
 | 
						|
        }
 | 
						|
        unset($item);
 | 
						|
 | 
						|
        return $items;
 | 
						|
    }
 | 
						|
}
 |