Product.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2019/11/10
  6. * Time: 11:45 上午
  7. */
  8. namespace addons\unishop\model;
  9. use addons\unishop\extend\Hashids;
  10. use think\Exception;
  11. use think\Model;
  12. use traits\model\SoftDelete;
  13. /**
  14. * 商品模型
  15. * Class Product
  16. * @package addons\unishop\model
  17. */
  18. class Product extends Model
  19. {
  20. use SoftDelete;
  21. // 表名
  22. protected $name = 'unishop_product';
  23. // 自动写入时间戳字段
  24. protected $autoWriteTimestamp = 'int';
  25. // 定义时间戳字段名
  26. protected $createTime = 'createtime';
  27. protected $updateTime = 'updatetime';
  28. protected $deleteTime = 'deletetime';
  29. // 是否上架?
  30. const SWITCH_ON = 1; //是
  31. const SWITCH_OFF = 0; //否
  32. // 是否开启规格?
  33. const SPEC_ON = 1; //是
  34. const SPEC_OFF = 0; //否
  35. // 追加属性
  36. protected $append = [
  37. //'images_text',
  38. //'spec_list',
  39. //'spec_table_list',
  40. 'product_id'
  41. ];
  42. // 隐藏属性
  43. protected $hidden = [
  44. 'id',
  45. 'real_look',
  46. 'real_sales',
  47. 'images',
  48. 'videothumb',
  49. 'specList',
  50. 'specTableList',
  51. ];
  52. /**
  53. * 处理图片
  54. * @param $value
  55. * @return string
  56. */
  57. public function getImageAttr($value) {
  58. return Config::getImagesFullUrl($value);
  59. }
  60. public function getVideoFileAttr($value) {
  61. return Config::getImagesFullUrl($value);
  62. }
  63. public function getVideothumbAttr($value, $data) {
  64. $rs = '';
  65. if(!empty($data['video_file'])){
  66. $images_url = explode('.', $data['video_file']);
  67. unset($images_url[count($images_url) - 1]);
  68. $rs = join('.', $images_url) . '_0.jpg';
  69. }
  70. return Config::getImagesFullUrl($rs);
  71. }
  72. /**
  73. * 加密商品id
  74. * @param $value
  75. * @param $data
  76. * @return string
  77. */
  78. public function getProductIdAttr($value, $data) {
  79. return Hashids::encodeHex($data['id']);
  80. }
  81. /**
  82. * 获取销售量
  83. * @param $value
  84. * @param $data
  85. */
  86. public function getSalesAttr($value, $data) {
  87. return $data['sales'] + $data['real_sales'];
  88. }
  89. /**
  90. * 处理图片
  91. * @param $value
  92. * @param $data
  93. * @return string
  94. */
  95. public function getImagesTextAttr($value, $data){
  96. $images = explode(',', $data['images']);
  97. foreach ($images as &$image) {
  98. $image = Config::getImagesFullUrl($image);
  99. }
  100. return $images;
  101. }
  102. /**
  103. * 处理规格属性
  104. * @param $value
  105. * @param $data
  106. * @return mixed
  107. */
  108. public function getSpecListAttr($value, $data) {
  109. return !empty($data['specList']) ? json_decode($data['specList'], true) : [];
  110. }
  111. /**
  112. * 处理规格值
  113. * @param $value
  114. * @param $data
  115. * @return mixed
  116. */
  117. public function getSpecTableListAttr($value, $data) {
  118. $specs = !empty($data['specTableList']) ? json_decode($data['specTableList'], true) : [];
  119. foreach ($specs as &$spec) {
  120. unset($spec['code']);
  121. $spec['image'] = Config::getImagesFullUrl($spec['image']);
  122. $spec['value_text'] = '';
  123. if(!empty($spec['value'])){
  124. $spec['value_text'] = implode(',',$spec['value']);
  125. }
  126. }
  127. return $specs;
  128. }
  129. /**
  130. * 处理详情里面的图片
  131. * @param $value
  132. * @param $data
  133. */
  134. public function getDescAttr($value, $data) {
  135. return str_replace('src="/uploads/','src="'.Config::getHttpLocation().'/uploads/',$data['desc']);
  136. }
  137. /**
  138. * 获取创建订单需要的产品信息
  139. * @param string $spec
  140. * @param int $number
  141. * @return array
  142. * @throws Exception
  143. */
  144. public function getDataOnCreateOrder(string $spec = '', $number = 1)
  145. {
  146. $data = (new \addons\unishop\extend\Product)->getBaseData($this->getData(), $spec);
  147. if ($data['stock'] < 1) {
  148. throw new Exception('产品库存不足');
  149. }
  150. $product = $this->getData();
  151. $data['title'] = $product['title'];
  152. $data['spec'] = $spec;
  153. $data['number'] = $number;
  154. $data['id'] = Hashids::encodeHex($product['id']);
  155. $data['yunfei_price'] = $product['yunfei_price'];
  156. $data['total_price'] = bcmul($data['sales_price'],$number,2);
  157. return $data;
  158. }
  159. }