StoreProductController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace app\api\controller\store;
  3. use app\admin\model\store\StoreDescription;
  4. use app\admin\model\system\SystemAttachment;
  5. use app\models\store\StoreOrder;
  6. use app\models\store\StoreVisit;
  7. use app\models\system\SystemStore;
  8. use app\models\store\StoreProduct;
  9. use app\models\store\StoreProductAttr;
  10. use app\models\store\StoreProductRelation;
  11. use app\models\store\StoreProductReply;
  12. use app\models\user\User;
  13. use app\Request;
  14. use crmeb\services\GroupDataService;
  15. use crmeb\services\QrcodeService;
  16. use crmeb\services\SystemConfigService;
  17. use crmeb\services\UtilService;
  18. use crmeb\services\upload\Upload;
  19. /**
  20. * 商品类
  21. * Class StoreProductController
  22. * @package app\api\controller\store
  23. */
  24. class StoreProductController
  25. {
  26. /**
  27. * @api {get} /products 商品列表
  28. * @apiName GetProducts
  29. * @apiGroup Product
  30. *
  31. */
  32. public function lst(Request $request)
  33. {
  34. $data = UtilService::getMore([
  35. [['sid', 'd'], 0],
  36. [['cid', 'd'], 0],
  37. ['keyword', ''],
  38. ['priceOrder', ''],
  39. ['salesOrder', ''],
  40. [['news', 'd'], 0],
  41. [['page', 'd'], 0],
  42. [['limit', 'd'], 0],
  43. [['type', 0], 0]
  44. ], $request);
  45. return app('json')->successful(StoreProduct::getProductList($data, $request->uid()));
  46. }
  47. /**
  48. * @api {get} product/code/:id 产品分享二维码 推广员
  49. * @apiName getProductCode
  50. * @apiGroup User
  51. *
  52. * @apiParam {int} id 产品ID
  53. * @apiQuery {string="wechat","routine",""} user_type 用户类型
  54. *
  55. * @apiSuccessExample Succeed
  56. * {
  57. *
  58. * }
  59. * @apiErrorExample Failed
  60. * {
  61. *
  62. * }
  63. */
  64. public function code(Request $request, $id)
  65. {
  66. if (!$id || !($storeInfo = StoreProduct::getValidProduct($id, 'id'))) return app('json')->fail('商品不存在或已下架');
  67. $userType = $request->get('user_type', 'wechat');
  68. $user = $request->user();
  69. try {
  70. switch ($userType) {
  71. case 'wechat':
  72. //公众号
  73. $name = $id . '_product_detail_' . $user['uid'] . '_is_promoter_' . $user['is_promoter'] . '_wap.jpg';
  74. $url = QrcodeService::getWechatQrcodePath($name, '/detail/' . $id . '?spread=' . $user['uid']);
  75. if ($url === false)
  76. return app('json')->fail('二维码生成失败');
  77. else
  78. return app('json')->successful(['code' => image_to_base64($url)]);
  79. break;
  80. case 'routine':
  81. //小程序
  82. $name = $id . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_product.jpg';
  83. $imageInfo = SystemAttachment::getInfo($name, 'name');
  84. $siteUrl = sys_config('site_url');
  85. if (!$imageInfo) {
  86. $data = 'id=' . $id;
  87. if ($user['is_promoter'] || sys_config('store_brokerage_statu') == DISTRIBUTE_EVERYONE) $data .= '&pid=' . $user['uid'];
  88. $res = \app\models\routine\RoutineCode::getPageCode('pages/goods_details/index', $data, 280);
  89. if (!$res) return app('json')->fail('二维码生成失败');
  90. $uploadType = (int)sys_config('upload_type', 1);
  91. $upload = new Upload($uploadType, [
  92. 'accessKey' => sys_config('accessKey'),
  93. 'secretKey' => sys_config('secretKey'),
  94. 'uploadUrl' => sys_config('uploadUrl'),
  95. 'storageName' => sys_config('storage_name'),
  96. 'storageRegion' => sys_config('storage_region'),
  97. ]);
  98. $upload->delete($name);
  99. $res = $upload->to('routine/product')->validate()->stream($res, $name);
  100. if ($res === false) {
  101. return app('json')->fail($upload->getError());
  102. }
  103. $imageInfo = $upload->getUploadInfo();
  104. $imageInfo['image_type'] = $uploadType;
  105. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  106. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  107. if (!$remoteImage['status']) return app('json')->fail('小程序二维码未能生成');
  108. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  109. $url = $imageInfo['dir'];
  110. } else $url = $imageInfo['att_dir'];
  111. if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  112. return app('json')->successful(['code' => $url]);
  113. }
  114. } catch (\Exception $e) {
  115. return app('json')->fail($e->getMessage(), [
  116. 'code' => $e->getCode(),
  117. 'line' => $e->getLine(),
  118. 'message' => $e->getMessage()
  119. ]);
  120. }
  121. }
  122. /**
  123. * @api {get} /product/detail/:id/[:type] 产品详情
  124. * @apiName GetProductDetails
  125. * @apiGroup Product
  126. *
  127. */
  128. public function detail(Request $request, $id, $type = 0)
  129. {
  130. if (!$id || !($storeInfo = StoreProduct::getValidProduct($id))) return app('json')->fail('商品不存在或已下架');
  131. $siteUrl = sys_config('site_url');
  132. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  133. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  134. $storeInfo['code_base'] = QrcodeService::getWechatQrcodePath($id . '_product_detail_wap.jpg', '/detail/' . $id);
  135. $uid = $request->uid();
  136. $data['uid'] = $uid;
  137. $storeInfo['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id));
  138. //替换windows服务器下正反斜杠问题导致图片无法显示
  139. $storeInfo['description'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
  140. return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
  141. }, $storeInfo['description']);
  142. $storeInfo['userCollect'] = StoreProductRelation::isProductRelation($id, $uid, 'collect');
  143. $storeInfo['userLike'] = StoreProductRelation::isProductRelation($id, $uid, 'like');
  144. list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($id, $uid, $type);
  145. $attrValue = $productValue;
  146. if (!$storeInfo['spec_type']) {
  147. $productAttr = [];
  148. $productValue = [];
  149. }
  150. // 对规格进行排序
  151. // $prices = array_column($productValue, 'price');
  152. // array_multisort($prices, SORT_ASC, SORT_NUMERIC, $productValue);
  153. // $keys = array_keys($productValue);
  154. // $productValue = array_combine($keys, $productValue);
  155. StoreVisit::setView($uid, $id, 'product',$storeInfo['cate_id'], 'viwe');
  156. $data['storeInfo'] = StoreProduct::setLevelPrice($storeInfo, $uid, true);
  157. $data['similarity'] = StoreProduct::cateIdBySimilarityProduct($storeInfo['cate_id'], 'id,store_name,image,price,sales,ficti', 4);
  158. $data['productAttr'] = $productAttr;
  159. $data['productValue'] = $productValue;
  160. $data['priceName'] = 0;
  161. if ($uid) {
  162. $user = $request->user();
  163. if (!$user->is_promoter) {
  164. $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $uid])->sum('pay_price');
  165. $status = is_brokerage_statu($price);
  166. if ($status) {
  167. User::where('uid', $uid)->update(['is_promoter' => 1]);
  168. $user->is_promoter = 1;
  169. event('UserBecomedPromoter', [$user->toArray()]);
  170. }
  171. }
  172. if ($user->is_promoter) {
  173. $data['priceName'] = StoreProduct::getPacketPrice($storeInfo, $attrValue);
  174. }
  175. if (!strlen(trim($data['priceName'])))
  176. $data['priceName'] = 0;
  177. }
  178. $data['reply'] = StoreProductReply::getRecProductReply($storeInfo['id']);
  179. $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id', $storeInfo['id'])->count();
  180. if ($data['replyCount']) {
  181. $goodReply = StoreProductReply::productValidWhere()->where('product_id', $storeInfo['id'])->where('product_score', 5)->count();
  182. $data['replyChance'] = $goodReply;
  183. if ($goodReply) {
  184. $data['replyChance'] = bcdiv($goodReply, $data['replyCount'], 2);
  185. $data['replyChance'] = bcmul($data['replyChance'], 100, 2);
  186. }
  187. } else $data['replyChance'] = 0;
  188. $data['mer_id'] = $storeInfo['mer_id'];
  189. $data['system_store'] = []; // ($res = SystemStore::getStoreDispose()) ? $res : [];
  190. $data['good_list'] = StoreProduct::getGoodList(18, 'image,store_name,price,id,ot_price');
  191. $data['mapKey'] = sys_config('tengxun_map_key');
  192. $data['store_self_mention'] = (int)sys_config('store_self_mention') ?? 0;//门店自提是否开启
  193. $data['activity'] = []; // StoreProduct::activity($data['storeInfo']['id'], false);
  194. return app('json')->successful($data);
  195. }
  196. /**
  197. * @api {get} /product/hot 为你推荐
  198. * @apiName GetProductHot
  199. * @apiGroup Product
  200. *
  201. */
  202. public function product_hot(Request $request)
  203. {
  204. list($page, $limit) = UtilService::getMore([
  205. [['page', 'd'], 0],
  206. [['limit', 'd'], 0]
  207. ], $request, true);
  208. if (!$limit) return app('json')->successful([]);
  209. $productHot = StoreProduct::getHotProductLoading('id,image,store_name,cate_id,price,unit_name,ot_price', (int)$page, (int)$limit);
  210. if (!empty($productHot)) {
  211. foreach ($productHot as $k => $v) {
  212. $productHot[$k]['activity'] = []; // StoreProduct::activity($v['id']); // remove sql in foreach
  213. }
  214. }
  215. return app('json')->successful($productHot);
  216. }
  217. /**
  218. * @api {get} /groom/list/:type 获取首页推荐不同类型产品的轮播图和产品
  219. * @apiName GetGroomList
  220. * @apiGroup Product
  221. *
  222. */
  223. public function groom_list(Request $request, $type)
  224. {
  225. list($page, $limit) = UtilService::getMore([
  226. [['page', 'd'], 0],
  227. [['limit', 'd'], 0]
  228. ], $request, true);
  229. $info['banner'] = [];
  230. $info['list'] = [];
  231. if ($type == 1) {//TODO 精品推荐
  232. $info['banner'] = sys_data('routine_home_bast_banner') ?: [];//TODO 首页精品推荐图片
  233. $info['list'] = StoreProduct::getBestProduct('id,image,store_name,cate_id,price,ot_price,IFNULL(sales,0) + IFNULL(ficti,0) as sales,unit_name,sort', 0, 0, true, $page, $limit);//TODO 精品推荐个数
  234. } else if ($type == 2) {//TODO 热门榜单
  235. $info['banner'] = sys_data('routine_home_hot_banner') ?: [];//TODO 热门榜单 猜你喜欢推荐图片
  236. $info['list'] = StoreProduct::getHotProduct('id,image,store_name,cate_id,price,ot_price,unit_name,sort,IFNULL(sales,0) + IFNULL(ficti,0) as sales', 0, $request->uid(), $page, $limit);//TODO 热门榜单 猜你喜欢
  237. } else if ($type == 3) {//TODO 首发新品
  238. $info['banner'] = sys_data('routine_home_new_banner') ?: [];//TODO 首发新品推荐图片
  239. $info['list'] = StoreProduct::getNewProduct('id,image,store_name,cate_id,price,ot_price,unit_name,sort,IFNULL(sales,0) + IFNULL(ficti,0) as sales', 0, $request->uid(), true, $page, $limit);//TODO 首发新品
  240. } else if ($type == 4) {//TODO 促销单品
  241. $info['banner'] = sys_data('routine_home_benefit_banner') ?: [];//TODO 促销单品推荐图片
  242. $info['list'] = StoreProduct::getBenefitProduct('id,image,store_name,cate_id,price,ot_price,stock,unit_name,sort', 0, $page, $limit);//TODO 促销单品
  243. }
  244. return app('json')->successful($info);
  245. }
  246. /**
  247. * @api {get} /reply/config/:id 产品评价数量和好评度
  248. * @apiName GetReplyConfig
  249. * @apiGroup Product
  250. *
  251. */
  252. public function reply_config($id)
  253. {
  254. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  255. return app('json')->successful(StoreProductReply::productReplyCount($id));
  256. }
  257. /**
  258. * @api {get} /reply/list/:id 获取产品评论
  259. * @apiName GetReplyList
  260. * @apiGroup Product
  261. *
  262. */
  263. public function reply_list(Request $request, $id)
  264. {
  265. list($page, $limit, $type) = UtilService::getMore([
  266. [['page', 'd'], 0], [['limit', 'd'], 0], [['type', 'd'], 0]
  267. ], $request, true);
  268. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  269. $list = StoreProductReply::getProductReplyList($id, (int)$type, $page, $limit);
  270. return app('json')->successful($list);
  271. }
  272. }