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.
 
 
 
 
 
 

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