Product.php 4.0 KB
<?php

namespace App\Models\Product;

use App\Helper\Arr;
use App\Models\Base;
use App\Models\File\Image;
use App\Models\RouteMap;
use App\Services\CosService;
use App\Services\Facades\Upload;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * @method static get()
 */
class Product extends Base
{
    use SoftDeletes;

    //设置关联表名
    protected $table = 'gl_product';

    const STATUS_DRAFT = 0;
    const STATUS_ON = 1;
    const STATUS_RECYCLE = 2;

    protected $appends  = ['route'];

    public static function statusMap(){
        return [
            self::STATUS_DRAFT    => '草稿',
            self::STATUS_ON   => '已发布',
            self::STATUS_RECYCLE  => '回收站',
        ];
    }

    public function setThumbAttribute($value){
        $value['url'] = Upload::url2path($value['url']);
        $this->attributes['thumb'] = Arr::a2s($value);
    }
    /**
     * @remark :获取图片链接
     * @name   :getImageUrl
     * @author :lyh
     * @method :post
     * @time   :2023/7/20 16:46
     */
    public function getImageUrl($hash){
        if(is_array($hash)){
            $url = [];
            foreach ($hash as $k => $v){
                $url['images_link'][$k] = $this->getImageUrl($v);
            }
        }else{
            $imageModel = new Image();
            $info = $imageModel->read(['hash'=>$hash]);
            if($info['is_cos'] == 1){
                $cos = new CosService();
                $url = $cos->getImageUrl($info['path']);
            }else{
                $url = url('b/image/'.$info['hash']);
            }
        }

        return $url;
    }
    public function getThumbAttribute($value){
        $value =  Arr::s2a($value);
        $value['url'] = Upload::path2url($value['url']);
        return $value;
    }

    public function setGalleryAttribute($value){
        foreach ($value as &$v){
            $v['url'] = Upload::url2path($v['url']);
        }
        $this->attributes['gallery'] = Arr::a2s($value);
    }

    public function getGalleryAttribute($value){
        $value = Arr::s2a($value);
        foreach ($value as &$v){
            $v['url'] = Upload::path2url($v['url']);
        }
        return $value;
    }

    public function setAttrsAttribute($value){
        $this->attributes['attrs'] = Arr::a2s($value);
    }

    public function getAttrsAttribute($value){
        return  Arr::s2a($value);
    }

    public function setDescribeAttribute($value){
        $this->attributes['describe'] = Arr::a2s($value);
    }

    public function getDescribeAttribute($value){
        return  Arr::s2a($value);
    }

    public function setSeoMateAttribute($value){
        $this->attributes['seo_mate'] = Arr::a2s($value);
    }

    public function getSeoMateAttribute($value){
        return  Arr::s2a($value);
    }

    public function setCategoryIdAttribute($value){
        $this->attributes['category_id'] = Arr::arrToSet($value);
    }

    public function getCategoryIdAttribute($value){
        return Arr::setToArr($value);
    }

    public function setAttrIdAttribute($value){
        $this->attributes['attr_id'] = Arr::arrToSet($value);
    }

    public function getAttrIdAttribute($value){
        return Arr::setToArr($value);
    }

    public function setDescribeIdAttribute($value){
        $this->attributes['describe_id'] = Arr::arrToSet($value);
    }

    public function getDescribeIdAttribute($value){
        return Arr::setToArr($value);
    }

    public function setKeywordIdAttribute($value){
        $this->attributes['keyword_id'] = Arr::arrToSet($value);
    }

    public function getKeywordIdAttribute($value){
        return Arr::setToArr($value);
    }

    public function setRelatedProductIdAttribute($value){
        $this->attributes['related_product_id'] = Arr::arrToSet($value);
    }

    public function getRelatedProductIdAttribute($value){
        return Arr::setToArr($value);
    }

    public function getRouteAttribute(){
        return RouteMap::getRoute(RouteMap::SOURCE_PRODUCT, $this->id, $this->project_id);
    }

}