StoreProductController.php 18 KB

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