Product.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. 'specList',
  49. 'specTableList',
  50. ];
  51. /**
  52. * 处理图片
  53. * @param $value
  54. * @return string
  55. */
  56. public function getImageAttr($value) {
  57. return Config::getImagesFullUrl($value);
  58. }
  59. /**
  60. * 加密商品id
  61. * @param $value
  62. * @param $data
  63. * @return string
  64. */
  65. public function getProductIdAttr($value, $data) {
  66. return $data['id'];
  67. // return Hashids::encodeHex($data['id']);
  68. }
  69. /**
  70. * 获取销售量
  71. * @param $value
  72. * @param $data
  73. */
  74. public function getSalesAttr($value, $data) {
  75. return $data['sales'] + $data['real_sales'];
  76. }
  77. /**
  78. * 处理图片
  79. * @param $value
  80. * @param $data
  81. * @return string
  82. */
  83. public function getImagesTextAttr($value, $data){
  84. $images = explode(',', $data['images']);
  85. foreach ($images as &$image) {
  86. $image = Config::getImagesFullUrl($image);
  87. }
  88. return $images;
  89. }
  90. /**
  91. * 处理规格属性
  92. * @param $value
  93. * @param $data
  94. * @return mixed
  95. */
  96. public function getSpecListAttr($value, $data) {
  97. return !empty($data['specList']) ? json_decode($data['specList'], true) : [];
  98. }
  99. /**
  100. * 处理规格值
  101. * @param $value
  102. * @param $data
  103. * @return mixed
  104. */
  105. public function getSpecTableListAttr($value, $data) {
  106. $specs = !empty($data['specTableList']) ? json_decode($data['specTableList'], true) : [];
  107. foreach ($specs as &$spec) {
  108. $spec['image'] = Config::getImagesFullUrl($spec['image']);
  109. }
  110. return $specs;
  111. }
  112. /**
  113. * 处理详情里面的图片
  114. * @param $value
  115. * @param $data
  116. */
  117. public function getDescAttr($value, $data) {
  118. return str_replace('src="/uploads/','src="'.Config::getHttpLocation().'/uploads/',$data['desc']);
  119. }
  120. /**
  121. * 获取创建订单需要的产品信息
  122. * @param string $spec
  123. * @param int $number
  124. * @return array
  125. * @throws Exception
  126. */
  127. public function getDataOnCreateOrder(string $spec = '', $number = 1)
  128. {
  129. $data = (new \addons\unishop\extend\Product)->getBaseData($this->getData(), $spec);
  130. if ($data['stock'] < 1) {
  131. throw new Exception('产品库存不足');
  132. }
  133. $product = $this->getData();
  134. $data['title'] = $product['title'];
  135. $data['info'] = $product['info'];
  136. $data['spec'] = $spec;
  137. $data['number'] = $number;
  138. // $data['id'] = Hashids::encodeHex($product['id']);
  139. $data['id'] = $product['id'];
  140. $data['total_price'] = bcmul($data['sales_price'],$number,2);
  141. return $data;
  142. }
  143. }