StoreProductController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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\routine\RoutineCode;
  6. use app\models\store\StoreOrder;
  7. use app\models\store\StoreVisit;
  8. use app\models\system\SystemStore;
  9. use app\models\store\StoreProduct;
  10. use app\models\store\StoreProductAttr;
  11. use app\models\store\StoreProductRelation;
  12. use app\models\store\StoreProductReply;
  13. use app\models\user\User;
  14. use app\Request;
  15. use crmeb\services\GroupDataService;
  16. use crmeb\services\QrcodeService;
  17. use crmeb\services\SystemConfigService;
  18. use crmeb\services\UploadService;
  19. use crmeb\services\UtilService;
  20. use crmeb\services\upload\Upload;
  21. use Overtrue\Pinyin\Pinyin;
  22. use think\exception\DbException;
  23. /**
  24. * 商品类
  25. * Class StoreProductController
  26. * @package app\api\controller\store
  27. */
  28. class StoreProductController
  29. {
  30. /**
  31. * 商品列表
  32. * @param Request $request
  33. * @return mixed
  34. */
  35. public function lst(Request $request)
  36. {
  37. $data = UtilService::getMore([
  38. ['sid', 0],
  39. ['cid', 0],
  40. ['keyword', ''],
  41. ['priceOrder', ''],
  42. ['salesOrder', ''],
  43. ['news', 0],
  44. ['page', 0],
  45. ['limit', 0],
  46. ['type', 0],
  47. ['is_wholesale',0],
  48. ['mer_id',0]
  49. ], $request);
  50. return app('json')->successful(StoreProduct::getProductList($data, $request->uid()));
  51. }
  52. /**
  53. * 产品分享二维码 推广员
  54. * @param Request $request
  55. * @param $id
  56. * @return mixed
  57. */
  58. public function code(Request $request, $id)
  59. {
  60. if (!$id || !($storeInfo = StoreProduct::getValidProduct($id, 'id'))) return app('json')->fail('商品不存在或已下架');
  61. $userType = $request->get('user_type', 'wechat');
  62. $user = $request->user();
  63. try {
  64. switch ($userType) {
  65. case 'wechat':
  66. //公众号
  67. $name = $id . '_product_detail_' . $user['uid'] . '_is_promoter_' . $user['is_promoter'] . '_wap.jpg';
  68. $url = QrcodeService::getWechatQrcodePath($name, '/detail/' . $id . '?spread=' . $user['uid']);
  69. if ($url === false)
  70. return app('json')->fail('二维码生成失败');
  71. else
  72. return app('json')->successful(['code' => image_to_base64($url)]);
  73. break;
  74. case 'routine':
  75. //小程序
  76. $name = $id . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_product.jpg';
  77. $imageInfo = SystemAttachment::getInfo($name, 'name');
  78. $siteUrl = sys_config('site_url');
  79. if (!$imageInfo) {
  80. $data = 'id=' . $id;
  81. if ($user['is_promoter'] || sys_config('store_brokerage_statu') == 2) $data .= '&pid=' . $user['uid'];
  82. $res = \app\models\routine\RoutineCode::getPageCode('pages/product/product', $data, 280);
  83. if (!$res) return app('json')->fail('二维码生成失败');
  84. $uploadType = (int)sys_config('upload_type', 1);
  85. $upload = new Upload($uploadType, [
  86. 'accessKey' => sys_config('accessKey'),
  87. 'secretKey' => sys_config('secretKey'),
  88. 'uploadUrl' => sys_config('uploadUrl'),
  89. 'storageName' => sys_config('storage_name'),
  90. 'storageRegion' => sys_config('storage_region'),
  91. ]);
  92. $res = $upload->to('routine/product')->validate()->stream($res, $name);
  93. if ($res === false) {
  94. return app('json')->fail($upload->getError());
  95. }
  96. $imageInfo = $upload->getUploadInfo();
  97. $imageInfo['image_type'] = $uploadType;
  98. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  99. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  100. if (!$remoteImage['status']) return app('json')->fail('小程序二维码未能生成');
  101. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  102. $url = $imageInfo['dir'];
  103. } else $url = $imageInfo['att_dir'];
  104. if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  105. return app('json')->successful(['code' => $url]);
  106. }
  107. } catch (\Exception $e) {
  108. return app('json')->fail($e->getMessage(), [
  109. 'code' => $e->getCode(),
  110. 'line' => $e->getLine(),
  111. 'message' => $e->getMessage()
  112. ]);
  113. }
  114. }
  115. /**
  116. * 产品详情
  117. * @param Request $request
  118. * @param $id
  119. * @param int $type
  120. * @return mixed
  121. */
  122. public function detail(Request $request, $id, $type = 0)
  123. {
  124. if (!$id || !($storeInfo = StoreProduct::getValidProduct($id))) return app('json')->fail('商品不存在或已下架');
  125. $siteUrl = sys_config('site_url');
  126. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  127. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  128. $storeInfo['code_base'] = QrcodeService::getWechatQrcodePath($id . '_product_detail_wap.jpg', '/detail/' . $id);
  129. $uid = $request->uid();
  130. $data['uid'] = $uid;
  131. $storeInfo['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id));
  132. //替换windows服务器下正反斜杠问题导致图片无法显示
  133. $storeInfo['description'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
  134. return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
  135. }, $storeInfo['description']);
  136. $storeInfo['userCollect'] = StoreProductRelation::isProductRelation($id, $uid, 'collect');
  137. $storeInfo['userLike'] = StoreProductRelation::isProductRelation($id, $uid, 'like');
  138. list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($id, $uid, $type);
  139. // 特供规格过滤:非经销商用户隐藏特供规格值
  140. $isDealer = $uid ? (User::where('uid', $uid)->value('dealer') > 0) : false;
  141. if (!$isDealer) {
  142. // 收集所有特供规格值
  143. $specialValues = [];
  144. foreach ($productAttr as $attrGroup) {
  145. foreach ($attrGroup['attr_value'] as $av) {
  146. if (!empty($av['is_special'])) {
  147. $specialValues[] = $av['attr'];
  148. }
  149. }
  150. }
  151. // 过滤 productValue:移除包含特供值的SKU
  152. if (!empty($specialValues)) {
  153. foreach ($productValue as $suk => $value) {
  154. foreach ($specialValues as $sv) {
  155. if (in_array($sv, explode(',', $suk))) {
  156. unset($productValue[$suk]);
  157. break;
  158. }
  159. }
  160. }
  161. // 过滤 productAttr:移除特供规格值
  162. foreach ($productAttr as $k => $attrGroup) {
  163. $newAttrValues = [];
  164. $newAttrValue = [];
  165. foreach ($attrGroup['attr_value'] as $av) {
  166. if (empty($av['is_special'])) {
  167. $newAttrValues[] = $av['attr'];
  168. $newAttrValue[] = $av;
  169. }
  170. }
  171. $productAttr[$k]['attr_values'] = $newAttrValues;
  172. $productAttr[$k]['attr_value'] = $newAttrValue;
  173. }
  174. }
  175. }
  176. StoreVisit::setView($uid, $id, $storeInfo['cate_id'], 'viwe');
  177. $data['storeInfo'] = StoreProduct::setLevelPrice($storeInfo, $uid, true);
  178. $data['similarity'] = StoreProduct::cateIdBySimilarityProduct($storeInfo['cate_id'], 'id,store_name,image,price,sales,ficti', 4);
  179. $data['productAttr'] = $productAttr;
  180. $data['productValue'] = $productValue;
  181. $data['priceName'] = 0;
  182. if ($uid) {
  183. $user = $request->user();
  184. if (!$user->is_promoter) {
  185. $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $uid])->sum('pay_price');
  186. $status = is_brokerage_statu($price);
  187. if ($status) {
  188. User::where('uid', $uid)->update(['is_promoter' => 1]);
  189. $user->is_promoter = 1;
  190. }
  191. }
  192. if ($user->is_promoter) {
  193. $data['priceName'] = StoreProduct::getPacketPrice($storeInfo, $productValue);
  194. }
  195. if (!strlen(trim($data['priceName'])))
  196. $data['priceName'] = 0;
  197. }
  198. $data['reply'] = StoreProductReply::getRecProductReply($storeInfo['id']);
  199. $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id', $storeInfo['id'])->count();
  200. if ($data['replyCount']) {
  201. $goodReply = StoreProductReply::productValidWhere()->where('product_id', $storeInfo['id'])->where('product_score', 5)->count();
  202. $data['replyChance'] = $goodReply;
  203. if ($goodReply) {
  204. $data['replyChance'] = bcdiv($goodReply, $data['replyCount'], 2);
  205. $data['replyChance'] = bcmul($data['replyChance'], 100, 2);
  206. }
  207. } else $data['replyChance'] = 0;
  208. $data['mer_id'] = $storeInfo['mer_id'];
  209. $data['system_store'] = ($res = SystemStore::getStoreDispose($data['mer_id'])) ? $res : [];
  210. $data['good_list'] = StoreProduct::getGoodList(18, 'image,store_name,price,id,ot_price');
  211. $data['mapKey'] = sys_config('tengxun_map_key');
  212. $data['store_self_mention'] = (int)sys_config('store_self_mention') ?? 0;//门店自提是否开启
  213. $data['activity'] = StoreProduct::activity($data['storeInfo']['id'], false);
  214. return app('json')->successful($data);
  215. }
  216. /**
  217. * 为你推荐
  218. *
  219. * @param Request $request
  220. * @return mixed
  221. * @throws \think\db\exception\DataNotFoundException
  222. * @throws \think\db\exception\ModelNotFoundException
  223. * @throws DbException
  224. */
  225. public function product_hot(Request $request)
  226. {
  227. list($page, $limit) = UtilService::getMore([
  228. ['page', 0],
  229. ['limit', 0]
  230. ], $request, true);
  231. if (!$limit) return app('json')->successful([]);
  232. $productHot = StoreProduct::getHotProductLoading('id,image,store_name,cate_id,price,unit_name,ot_price', (int)$page, (int)$limit);
  233. if (!empty($productHot)) {
  234. foreach ($productHot as $k => $v) {
  235. $productHot[$k]['activity'] = StoreProduct::activity($v['id']);
  236. }
  237. }
  238. return app('json')->successful($productHot);
  239. }
  240. /**
  241. * 获取首页推荐不同类型产品的轮播图和产品
  242. * @param Request $request
  243. * @param $type
  244. * @return mixed
  245. * @throws \think\db\exception\DataNotFoundException
  246. * @throws \think\db\exception\ModelNotFoundException
  247. * @throws DbException
  248. */
  249. public function groom_list(Request $request, $type)
  250. {
  251. $info['banner'] = [];
  252. $info['list'] = [];
  253. if ($type == 1) {//TODO 精品推荐
  254. $info['banner'] = sys_data('routine_home_bast_banner') ?: [];//TODO 首页精品推荐图片
  255. $info['list'] = StoreProduct::getBestProduct('id,image,store_name,cate_id,price,ot_price,IFNULL(sales,0) + IFNULL(ficti,0) as sales,unit_name,sort');//TODO 精品推荐个数
  256. } else if ($type == 2) {//TODO 热门榜单
  257. $info['banner'] = sys_data('routine_home_hot_banner') ?: [];//TODO 热门榜单 猜你喜欢推荐图片
  258. $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());//TODO 热门榜单 猜你喜欢
  259. } else if ($type == 3) {//TODO 首发新品
  260. $info['banner'] = sys_data('routine_home_new_banner') ?: [];//TODO 首发新品推荐图片
  261. $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());//TODO 首发新品
  262. } else if ($type == 4) {//TODO 促销单品
  263. $info['banner'] = sys_data('routine_home_benefit_banner') ?: [];//TODO 促销单品推荐图片
  264. $info['list'] = StoreProduct::getBenefitProduct('id,image,store_name,cate_id,price,ot_price,stock,unit_name,sort');//TODO 促销单品
  265. } else if ($type == 5) {//TODO 促销单品
  266. $info['banner'] = sys_data('routine_home_benefit_banner') ?: [];//TODO 促销单品推荐图片
  267. $info['list'] = StoreProduct::getIntegralProduct('max_integral,min_integral,id,image,store_name,cate_id,price,ot_price,stock,unit_name,sort');//TODO 促销单品
  268. }
  269. return app('json')->successful($info);
  270. }
  271. /**
  272. * 产品评价数量和好评度
  273. * @param $id
  274. * @return mixed
  275. */
  276. public function reply_config($id)
  277. {
  278. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  279. return app('json')->successful(StoreProductReply::productReplyCount($id));
  280. }
  281. /**
  282. * 获取产品评论
  283. * @param Request $request
  284. * @param $id
  285. * @param $type
  286. * @return mixed
  287. */
  288. public function reply_list(Request $request, $id)
  289. {
  290. list($page, $limit, $type) = UtilService::getMore([
  291. ['page', 0], ['limit', 0], ['type', 0]
  292. ], $request, true);
  293. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  294. $list = StoreProductReply::getProductReplyList($id, (int)$type, $page, $limit);
  295. return app('json')->successful($list);
  296. }
  297. /**
  298. * 砍价海报
  299. * @param Request $request
  300. * @return mixed
  301. */
  302. public function poster(Request $request)
  303. {
  304. list($bargainId, $from) = UtilService::postMore([['productId', 0], ['from', 'wechat']], $request, true);
  305. if (!$bargainId) return app('json')->fail('参数错误');
  306. $user = $request->user();
  307. // var_dump($user);
  308. $storeBargainInfo = StoreProduct::getValidProduct($bargainId);
  309. $price = $storeBargainInfo['price'];//TODO 获取砍价剩余金额
  310. try {
  311. $siteUrl = sys_config('site_url');
  312. $data['title'] = $storeBargainInfo['store_name'];
  313. $data['image'] = $storeBargainInfo['image'];
  314. $data['price'] = $price;
  315. $data['user_nickname'] = $user['nickname'];
  316. $data['user_avatar'] = $user['avatar'];
  317. $pinyin = new Pinyin();
  318. $data['user_nickname_pinyin'] = $pinyin->convert($user['nickname']);
  319. $data['ot_price'] = $storeBargainInfo['ot_price'];
  320. if ($from == 'routine') {
  321. //小程序
  322. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_product_share_routine.jpg';
  323. $imageInfo = SystemAttachment::getInfo($name, 'name');
  324. if (!$imageInfo) {
  325. $valueData = 'id=' . $bargainId;
  326. if ($user['is_promoter'] || sys_config('store_brokerage_statu') == 2) $valueData .= '&pid=' . $user['uid'];
  327. $res = RoutineCode::getPageCode('pages/product/product', $valueData, 280);
  328. if (!$res) return app('json')->fail('二维码生成失败');
  329. $uploadType = (int)sys_config('upload_type', 1);
  330. $upload = new Upload($uploadType, [
  331. 'accessKey' => sys_config('accessKey'),
  332. 'secretKey' => sys_config('secretKey'),
  333. 'uploadUrl' => sys_config('uploadUrl'),
  334. 'storageName' => sys_config('storage_name'),
  335. 'storageRegion' => sys_config('storage_region'),
  336. ]);
  337. $res = $upload->to('routine/product/code')->validate()->stream($res, $name);
  338. if ($res === false) {
  339. return app('json')->fail($upload->getError());
  340. }
  341. $imageInfo = $upload->getUploadInfo();
  342. $imageInfo['image_type'] = $uploadType;
  343. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  344. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  345. if (!$remoteImage['status']) return app('json')->fail($remoteImage['msg']);
  346. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  347. $url = $imageInfo['dir'];
  348. } else $url = $imageInfo['att_dir'];
  349. $data['url'] = $url;
  350. if ($imageInfo['image_type'] == 1)
  351. $data['url'] = $siteUrl . $url;
  352. $posterImage = UtilService::setShareProductPoster($data, 'routine/product/poster');
  353. if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
  354. SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
  355. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  356. $routinePosterImage = UtilService::setHttpType($posterImage['dir'], 0);//小程序推广海报
  357. return app('json')->successful(['url' => $routinePosterImage]);
  358. } else if ($from == 'wechat') {
  359. //公众号
  360. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_product_share_wap.jpg';
  361. $imageInfo = SystemAttachment::getInfo($name, 'name');
  362. if (!$imageInfo) {
  363. $codeUrl = UtilService::setHttpType($siteUrl . '/index/pages/product/product?id=' . $bargainId . '&spread=' . $user['uid'], 0);//二维码链接
  364. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  365. if (!$imageInfo) return app('json')->fail('二维码生成失败');
  366. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  367. $url = $imageInfo['dir'];
  368. } else $url = $imageInfo['att_dir'];
  369. $data['url'] = $url;
  370. if ($imageInfo['image_type'] == 1) $data['url'] = $siteUrl . $url;
  371. $posterImage = UtilService::setShareProductPoster($data, 'wap/product/poster');
  372. if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
  373. SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
  374. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  375. $wapPosterImage = UtilService::setHttpType($posterImage['dir'], 1);//公众号推广海报
  376. return app('json')->successful(['url' => $wapPosterImage]);
  377. }
  378. return app('json')->fail('参数错误');
  379. } catch (\Exception $e) {
  380. return app('json')->fail(['line' => $e->getLine(), 'message' => $e->getMessage()]);
  381. }
  382. }
  383. }