123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace addons\unishop\model;
- use addons\unishop\extend\Hashids;
- use think\Exception;
- use think\Model;
- use traits\model\SoftDelete;
- class Product extends Model
- {
- use SoftDelete;
-
- protected $name = 'unishop_product';
-
- protected $autoWriteTimestamp = 'int';
-
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
-
- const SWITCH_ON = 1;
- const SWITCH_OFF = 0;
-
- const SPEC_ON = 1;
- const SPEC_OFF = 0;
-
- protected $append = [
-
-
-
- 'product_id'
- ];
-
- protected $hidden = [
- 'id',
- 'real_look',
- 'real_sales',
- 'images',
- 'videothumb',
- 'specList',
- 'specTableList',
- ];
-
- public function getImageAttr($value) {
- return Config::getImagesFullUrl($value);
- }
- public function getVideoFileAttr($value) {
- return Config::getImagesFullUrl($value);
- }
- public function getVideothumbAttr($value, $data) {
- $rs = '';
- if($data['video_file']){
- $images_url = explode('.', $data['video_file']);
- unset($images_url[count($images_url) - 1]);
- $rs = join('.', $images_url) . '_0.jpg';
- }
- return Config::getImagesFullUrl($rs);
- }
-
- public function getProductIdAttr($value, $data) {
- return Hashids::encodeHex($data['id']);
- }
-
- public function getSalesAttr($value, $data) {
- return $data['sales'] + $data['real_sales'];
- }
-
- public function getImagesTextAttr($value, $data){
- $images = explode(',', $data['images']);
- foreach ($images as &$image) {
- $image = Config::getImagesFullUrl($image);
- }
- return $images;
- }
-
- public function getSpecListAttr($value, $data) {
- return !empty($data['specList']) ? json_decode($data['specList'], true) : [];
- }
-
- public function getSpecTableListAttr($value, $data) {
- $specs = !empty($data['specTableList']) ? json_decode($data['specTableList'], true) : [];
- foreach ($specs as &$spec) {
- unset($spec['code']);
- $spec['image'] = Config::getImagesFullUrl($spec['image']);
- $spec['value_text'] = '';
- if(!empty($spec['value'])){
- $spec['value_text'] = implode(',',$spec['value']);
- }
- }
- return $specs;
- }
-
- public function getDescAttr($value, $data) {
- return str_replace('src="/uploads/','src="'.Config::getHttpLocation().'/uploads/',$data['desc']);
- }
-
- public function getDataOnCreateOrder(string $spec = '', $number = 1)
- {
- $data = (new \addons\unishop\extend\Product)->getBaseData($this->getData(), $spec);
- if ($data['stock'] < 1) {
- throw new Exception('产品库存不足');
- }
- $product = $this->getData();
- $data['title'] = $product['title'];
- $data['spec'] = $spec;
- $data['number'] = $number;
- $data['id'] = Hashids::encodeHex($product['id']);
- $data['yunfei_price'] = $product['yunfei_price'];
- $data['total_price'] = bcmul($data['sales_price'],$number,2);
- return $data;
- }
- }
|