ProductServices.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\other\PosterServices;
  15. use app\services\product\product\StoreProductServices;
  16. use app\services\system\attachment\SystemAttachmentServices;
  17. use app\services\user\UserServices;
  18. use crmeb\services\app\MiniProgramService;
  19. use app\services\other\UploadService;
  20. use Guzzle\Http\EntityBody;
  21. class ProductServices extends BaseServices
  22. {
  23. /**
  24. * PC端获取商品列表
  25. * @param array $where
  26. * @param int $uid
  27. * @return mixed
  28. */
  29. public function getProductList(array $where, int $uid)
  30. {
  31. /** @var StoreProductServices $product */
  32. $product = app()->make(StoreProductServices::class);
  33. $where['is_show'] = 1;
  34. $where['is_del'] = 0;
  35. $where['vip_user'] = $uid ? app()->make(UserServices::class)->value(['uid' => $uid], 'is_money_level') : 0;
  36. $data['count'] = $product->getCount($where);
  37. [$page, $limit] = $this->getPageValue();
  38. $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,presale']);
  39. foreach ($list as &$item) {
  40. if (count($item['star'])) {
  41. $item['star'] = bcdiv((string)array_sum(array_column($item['star'], 'product_score')), (string)count($item['star']), 1);
  42. } else {
  43. $item['star'] = '5.0';
  44. }
  45. }
  46. $list = $product->getActivityList($list);
  47. $data['list'] = get_thumb_water($product->getProduceOtherList($list, $uid, !!$where['type']), 'mid');
  48. return $data;
  49. }
  50. /**
  51. * PC端商品详情小程序码
  52. * @param int $product_id
  53. * @param string $type
  54. * @return false|mixed|string
  55. */
  56. public function getProductRoutineCode(int $product_id, string $type = 'product')
  57. {
  58. try {
  59. $namePath = $type == 'product' ? 'routine_product_' . $product_id . '.jpg' : 'routine_seckill_product_' . $product_id . '.jpg';
  60. $data = 'id=' . $product_id;
  61. /** @var SystemAttachmentServices $systemAttachmentService */
  62. $systemAttachmentService = app()->make(SystemAttachmentServices::class);
  63. $imageInfo = $systemAttachmentService->getOne(['name' => $namePath]);
  64. $siteUrl = sys_config('site_url');
  65. if (!$imageInfo) {
  66. $page = $type == 'product' ? 'pages/goods_details/index' : 'pages/activity/goods_seckill_details/index';
  67. $res = MiniProgramService::appCodeUnlimitService($data, $page, 280);
  68. if (!$res) return false;
  69. $uploadType = (int)sys_config('upload_type', 1);
  70. $upload = UploadService::init();
  71. $res = (string)EntityBody::factory($res);
  72. $res = $upload->to('routine/product')->validate()->setAuthThumb(false)->stream($res, $namePath);
  73. if ($res === false) {
  74. return false;
  75. }
  76. $imageInfo = $upload->getUploadInfo();
  77. $imageInfo['image_type'] = $uploadType;
  78. if ($imageInfo['image_type'] == 1) $remoteImage = PosterServices::remoteImage($siteUrl . $imageInfo['dir']);
  79. else $remoteImage = PosterServices::remoteImage($imageInfo['dir']);
  80. if (!$remoteImage['status']) return false;
  81. $systemAttachmentService->save([
  82. 'name' => $imageInfo['name'],
  83. 'att_dir' => $imageInfo['dir'],
  84. 'satt_dir' => $imageInfo['thumb_path'],
  85. 'att_size' => $imageInfo['size'],
  86. 'att_type' => $imageInfo['type'],
  87. 'image_type' => $imageInfo['image_type'],
  88. 'module_type' => 2,
  89. 'time' => time(),
  90. 'pid' => 1,
  91. 'type' => 2
  92. ]);
  93. $url = $imageInfo['dir'];
  94. } else $url = $imageInfo['att_dir'];
  95. if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  96. return $url;
  97. } catch (\Exception $e) {
  98. return '';
  99. }
  100. }
  101. }