StoreProductController.php 18 KB

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