链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

92 lines
2.4 KiB

<?php
namespace App\Models\v3;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
class Goods extends Model
{
use HasDateTimeFormatter;
use SoftDeletes;
protected $table = 'lanzu_goods';
protected $dateFormat = 'U';
/* 查询记录数 limit */
protected $perPage = 10;
protected $appends = [
'cover_img_url',
'on_sale_text',
'is_infinite_text'
];
protected $casts = [
'details_imgs'=>'array',
// 'tags'=>'array',
'spec'=>'array',
];
public static $_ONSALE = ['否','是'];
public static $_ISINFINITE = ['关闭','开启'];
public function getCoverImgUrlAttribute($value)
{
$value = $value ? $value : $this->cover_img;
return $this->imageUrl($value);
}
public function getOnSaleTextAttribute($value)
{
$value = $value ? $value : $this->on_sale;
return isset(self::$_ONSALE[$value]) ? self::$_ONSALE[$value] : '';
}
public function getIsInfiniteTextAttribute($value)
{
$value = $value ? $value : $this->is_infinite;
return isset(self::$_ISINFINITE[$value]) ? self::$_ISINFINITE[$value] : '';
}
public function ImageBanners(){
return $this->hasMany('\App\Models\v3\GoodsBanners','goods_id','id')->where('type',1);
}
public function VideoBanners(){
return $this->hasMany('\App\Models\v3\GoodsBanners','goods_id','id')->where('type',2);
}
/**
* 处理旧图片
* @param $value
* @return string
*/
public function imageUrl($value)
{
if(strripos($value,"http") === false){
return config('filesystems.disks.oss.img_host').'/'.$value;
}else{
return $value;
}
}
/**
* add:2022-04-11,获取产品毛重,主要用于计算顺丰运费
*/
public function getWeightAttribute($value): int
{
if (empty($value) && !empty($this->attributes['goods_unit'])) {
$goods_unit = $this->attributes['goods_unit'];
switch (true) {
case preg_match('/(\d{2,})\D*±(\d{2,})/', $goods_unit, $matches):
array_shift($matches);
$value = (int)max($matches);
break;
case preg_match_all('/(\d{2,})[g|克]/', $goods_unit, $matches):
$value = (int)max($matches[1]);
break;
default:
$value = 0;
}
}
return $value ?: 0;
}
}