StoreProductController.php 18 KB

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