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.
 
 
 
 
 
 

162 lines
3.6 KiB

  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 Hashids::encodeHex($data['id']);
  67. }
  68. /**
  69. * 获取销售量
  70. * @param $value
  71. * @param $data
  72. */
  73. public function getSalesAttr($value, $data) {
  74. return $data['sales'] + $data['real_sales'];
  75. }
  76. /**
  77. * 处理图片
  78. * @param $value
  79. * @param $data
  80. * @return string
  81. */
  82. public function getImagesTextAttr($value, $data){
  83. $images = explode(',', $data['images']);
  84. foreach ($images as &$image) {
  85. $image = Config::getImagesFullUrl($image);
  86. }
  87. return $images;
  88. }
  89. public function getDetailImagesTextAttr($value, $data){
  90. $images = explode(',', $data['detail_images']);
  91. foreach ($images as &$image) {
  92. $image = Config::getImagesFullUrl($image);
  93. }
  94. return $images;
  95. }
  96. /**
  97. * 处理规格属性
  98. * @param $value
  99. * @param $data
  100. * @return mixed
  101. */
  102. public function getSpecListAttr($value, $data) {
  103. return !empty($data['specList']) ? json_decode($data['specList'], true) : [];
  104. }
  105. /**
  106. * 处理规格值
  107. * @param $value
  108. * @param $data
  109. * @return mixed
  110. */
  111. public function getSpecTableListAttr($value, $data) {
  112. $specs = !empty($data['specTableList']) ? json_decode($data['specTableList'], true) : [];
  113. foreach ($specs as &$spec) {
  114. $spec['image'] = Config::getImagesFullUrl($spec['image']);
  115. }
  116. return $specs;
  117. }
  118. /**
  119. * 获取创建订单需要的产品信息
  120. * @param string $spec
  121. * @param int $number
  122. * @return array
  123. * @throws Exception
  124. */
  125. public function getDataOnCreateOrder(string $spec = '', $number = 1)
  126. {
  127. $data = (new \addons\unishop\extend\Product)->getBaseData($this->getData(), $spec);
  128. if ($data['stock'] < 1) {
  129. throw new Exception('产品库存不足');
  130. }
  131. $product = $this->getData();
  132. $data['title'] = $product['title'];
  133. $data['spec'] = $spec;
  134. $data['number'] = $number;
  135. $data['id'] = Hashids::encodeHex($product['id']);
  136. return $data;
  137. }
  138. }