StoreProductController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. }
  227. return app('json')->successful($info);
  228. }
  229. /**
  230. * 产品评价数量和好评度
  231. * @param $id
  232. * @return mixed
  233. */
  234. public function reply_config($id)
  235. {
  236. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  237. return app('json')->successful(StoreProductReply::productReplyCount($id));
  238. }
  239. /**
  240. * 获取产品评论
  241. * @param Request $request
  242. * @param $id
  243. * @param $type
  244. * @return mixed
  245. */
  246. public function reply_list(Request $request, $id)
  247. {
  248. list($page, $limit, $type) = UtilService::getMore([
  249. ['page', 0], ['limit', 0], ['type', 0]
  250. ], $request, true);
  251. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  252. $list = StoreProductReply::getProductReplyList($id, (int)$type, $page, $limit);
  253. return app('json')->successful($list);
  254. }
  255. /**
  256. * 砍价海报
  257. * @param Request $request
  258. * @return mixed
  259. */
  260. public function poster(Request $request)
  261. {
  262. list($bargainId, $from) = UtilService::postMore([['productId', 0], ['from', 'wechat']], $request, true);
  263. if (!$bargainId) return app('json')->fail('参数错误');
  264. $user = $request->user();
  265. // var_dump($user);
  266. $storeBargainInfo = StoreProduct::getValidProduct($bargainId);
  267. $price = $storeBargainInfo['price'];//TODO 获取砍价剩余金额
  268. try {
  269. $siteUrl = sys_config('site_url');
  270. $data['title'] = $storeBargainInfo['store_name'];
  271. $data['image'] = $storeBargainInfo['image'];
  272. $data['price'] = $price;
  273. $data['user_nickname'] = $user['nickname'];
  274. $data['user_avatar'] = $user['avatar'];
  275. $pinyin = new Pinyin();
  276. $data['user_nickname_pinyin'] = $pinyin->convert($user['nickname']);
  277. $data['ot_price'] = $storeBargainInfo['ot_price'];
  278. if ($from == 'routine') {
  279. //小程序
  280. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_product_share_routine.jpg';
  281. $imageInfo = SystemAttachment::getInfo($name, 'name');
  282. if (!$imageInfo) {
  283. $valueData = 'id=' . $bargainId;
  284. if ($user['is_promoter'] || sys_config('store_brokerage_statu') == 2) $valueData .= '&pid=' . $user['uid'];
  285. $res = RoutineCode::getPageCode('pages/product/product', $valueData, 280);
  286. if (!$res) return app('json')->fail('二维码生成失败');
  287. $uploadType = (int)sys_config('upload_type', 1);
  288. $upload = new Upload($uploadType, [
  289. 'accessKey' => sys_config('accessKey'),
  290. 'secretKey' => sys_config('secretKey'),
  291. 'uploadUrl' => sys_config('uploadUrl'),
  292. 'storageName' => sys_config('storage_name'),
  293. 'storageRegion' => sys_config('storage_region'),
  294. ]);
  295. $res = $upload->to('routine/product/code')->validate()->stream($res, $name);
  296. if ($res === false) {
  297. return app('json')->fail($upload->getError());
  298. }
  299. $imageInfo = $upload->getUploadInfo();
  300. $imageInfo['image_type'] = $uploadType;
  301. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  302. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  303. if (!$remoteImage['status']) return app('json')->fail($remoteImage['msg']);
  304. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  305. $url = $imageInfo['dir'];
  306. } else $url = $imageInfo['att_dir'];
  307. $data['url'] = $url;
  308. if ($imageInfo['image_type'] == 1)
  309. $data['url'] = $siteUrl . $url;
  310. $posterImage = UtilService::setShareProductPoster($data, 'routine/product/poster');
  311. if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
  312. SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
  313. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  314. $routinePosterImage = UtilService::setHttpType($posterImage['dir'], 0);//小程序推广海报
  315. return app('json')->successful(['url' => $routinePosterImage]);
  316. } else if ($from == 'wechat') {
  317. //公众号
  318. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_product_share_wap.jpg';
  319. $imageInfo = SystemAttachment::getInfo($name, 'name');
  320. if (!$imageInfo) {
  321. $codeUrl = UtilService::setHttpType($siteUrl . '/index/pages/product/product?id=' . $bargainId . '&spread=' . $user['uid'], 0);//二维码链接
  322. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  323. if (!$imageInfo) return app('json')->fail('二维码生成失败');
  324. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  325. $url = $imageInfo['dir'];
  326. } else $url = $imageInfo['att_dir'];
  327. $data['url'] = $url;
  328. if ($imageInfo['image_type'] == 1) $data['url'] = $siteUrl . $url;
  329. $posterImage = UtilService::setShareProductPoster($data, 'wap/product/poster');
  330. if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
  331. SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
  332. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  333. $wapPosterImage = UtilService::setHttpType($posterImage['dir'], 1);//公众号推广海报
  334. return app('json')->successful(['url' => $wapPosterImage]);
  335. }
  336. return app('json')->fail('参数错误');
  337. } catch (\Exception $e) {
  338. return app('json')->fail(['line' => $e->getLine(), 'message' => $e->getMessage()]);
  339. }
  340. }
  341. }