ProductServices.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\pc;
  13. use app\services\BaseServices;
  14. use app\services\product\product\StoreProductServices;
  15. use app\services\system\attachment\SystemAttachmentServices;
  16. use app\services\user\UserServices;
  17. use crmeb\services\UploadService;
  18. use crmeb\services\UtilService;
  19. use crmeb\services\wechat\MiniProgram;
  20. use GuzzleHttp\Psr7\Utils;
  21. /**
  22. * Class ProductServices
  23. * @package app\services\pc
  24. */
  25. class ProductServices extends BaseServices
  26. {
  27. /**
  28. * PC端获取商品列表
  29. * @param array $where
  30. * @param int $uid
  31. * @return mixed
  32. */
  33. public function getProductList(array $where, int $uid)
  34. {
  35. /** @var StoreProductServices $product */
  36. $product = app()->make(StoreProductServices::class);
  37. $where['is_show'] = 1;
  38. $where['is_del'] = 0;
  39. $where['is_verify'] = 1;
  40. $data['count'] = $product->getCount($where);
  41. [$page, $limit] = $this->getPageValue();
  42. $where['is_vip_product'] = 0;
  43. if ($uid) {
  44. /** @var UserServices $userServices */
  45. $userServices = app()->make(UserServices::class);
  46. $is_vip = $userServices->value(['uid' => $uid], 'is_money_level');
  47. $where['is_vip_product'] = $is_vip ? -1 : 0;
  48. }
  49. $list = $product->getSearchList($where + ['star' => 1], $page, $limit, ['id,store_name,cate_id,image,IFNULL(sales, 0) + IFNULL(ficti, 0) as sales,price,stock,activity,ot_price,spec_type,recommend_image,unit_name'], '', ['couponId']);
  50. foreach ($list as &$item) {
  51. if (isset($item['star']) && count($item['star'])) {
  52. $item['star'] = bcdiv((string)array_sum(array_column($item['star'], 'product_score')), (string)count($item['star']), 1);
  53. } else {
  54. $item['star'] = config('admin.product_default_star');
  55. }
  56. $item['presale_pay_status'] = $product->checkPresaleProductPay((int)$item['id'], $item);
  57. }
  58. $list = $product->getActivityList($list);
  59. $data['list'] = get_thumb_water($product->getProduceOtherList($list, $uid, !!$where['status']), 'mid');
  60. return $data;
  61. }
  62. /**
  63. * PC端商品详情小程序码
  64. * @param int $product_id
  65. * @return bool|int|mixed|string
  66. */
  67. public function getProductRoutineCode(int $product_id)
  68. {
  69. try {
  70. $namePath = 'routine_product_' . $product_id . '.jpg';
  71. $data = 'id=' . $product_id;
  72. /** @var SystemAttachmentServices $systemAttachmentService */
  73. $systemAttachmentService = app()->make(SystemAttachmentServices::class);
  74. $imageInfo = $systemAttachmentService->getOne(['name' => $namePath]);
  75. $siteUrl = sys_config('site_url');
  76. if (!$imageInfo) {
  77. $res = MiniProgram::appCodeUnlimit($data, 'pages/goods_details/index', 280);
  78. if (!$res) return false;
  79. $uploadType = (int)sys_config('upload_type', 1);
  80. $upload = UploadService::init($uploadType);
  81. $res = (string)Utils::streamFor($res);
  82. $res = $upload->to('routine/product')->validate()->stream($res, $namePath);
  83. if ($res === false) {
  84. return false;
  85. }
  86. $imageInfo = $upload->getUploadInfo();
  87. $imageInfo['image_type'] = $uploadType;
  88. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  89. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  90. if (!$remoteImage['status']) return false;
  91. $systemAttachmentService->save([
  92. 'name' => $imageInfo['name'],
  93. 'att_dir' => $imageInfo['dir'],
  94. 'satt_dir' => $imageInfo['thumb_path'],
  95. 'att_size' => $imageInfo['size'],
  96. 'att_type' => $imageInfo['type'],
  97. 'image_type' => $imageInfo['image_type'],
  98. 'module_type' => 2,
  99. 'time' => time(),
  100. 'pid' => 1,
  101. 'type' => 2
  102. ]);
  103. $url = $imageInfo['dir'];
  104. } else $url = $imageInfo['att_dir'];
  105. if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  106. return $url;
  107. } catch (\Exception $e) {
  108. return '';
  109. }
  110. }
  111. }