StoreProductController.php 21 KB

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