Product.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2020/3/18
  6. * Time: 5:26 PM
  7. */
  8. namespace addons\unishop\extend;
  9. /**
  10. * 商品相关逻辑
  11. * Class Product
  12. * @package addons\unishop\extend
  13. */
  14. class Product
  15. {
  16. /**
  17. * 获取商品的基础信息
  18. * @param array $product 商品信息数组
  19. * @param string $spec 规格值,用,号隔开
  20. * @param string $key 要获取的字段
  21. * @return array
  22. */
  23. public function getBaseData(array $product, string $spec = '', string $key = '')
  24. {
  25. if (!$product) {
  26. return [];
  27. }
  28. $data = [];
  29. if ($spec && $product['use_spec'] == \addons\unishop\model\Product::SPEC_ON && !empty($product['specTableList'])) {
  30. $specValueArr = json_decode($product['specTableList'], true);
  31. foreach ($specValueArr as $k => $specItem) {
  32. if (implode(',', $specItem['value']) == $spec) {
  33. if ($key) {
  34. $data = $specItem[$key];
  35. } else {
  36. $specItem['stock'] = intval($specItem['stock']);//数据类型
  37. $specItem['sales'] = intval($specItem['sales']);//数据类型
  38. $data = $specItem;
  39. $data['key'] = $k;
  40. }
  41. }
  42. }
  43. }
  44. if (empty($data)) {
  45. if ($key) {
  46. $data = $product[$key];
  47. } else {
  48. $data['market_price'] = $product['market_price'];
  49. $data['sales_price'] = $product['sales_price'];
  50. $data['stock'] = $product['stock'];
  51. $data['sales'] = $product['sales'];
  52. $data['image'] = $product['image'];
  53. }
  54. }
  55. if (is_array($data)){
  56. $data['image'] = $data['image'] ? $data['image'] : $product['image'];
  57. }
  58. return $data;
  59. }
  60. }