StoreProductController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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\StoreOrder;
  6. use app\models\store\StoreVisit;
  7. use app\models\system\SystemStore;
  8. use app\models\store\StoreProduct;
  9. use app\models\store\StoreProductAttr;
  10. use app\models\store\StoreProductRelation;
  11. use app\models\store\StoreProductReply;
  12. use app\models\user\User;
  13. use app\Request;
  14. use crmeb\services\GroupDataService;
  15. use crmeb\services\QrcodeService;
  16. use crmeb\services\SystemConfigService;
  17. use crmeb\services\UtilService;
  18. use crmeb\services\upload\Upload;
  19. /**
  20. * 商品类
  21. * Class StoreProductController
  22. * @package app\api\controller\store
  23. */
  24. class StoreProductController
  25. {
  26. /**
  27. * 商品列表
  28. * @param Request $request
  29. * @return mixed
  30. */
  31. public function lst(Request $request)
  32. {
  33. $data = UtilService::getMore([
  34. [['sid', 'd'], 0],
  35. [['cid', 'd'], 0],
  36. ['keyword', ''],
  37. ['priceOrder', ''],
  38. ['salesOrder', ''],
  39. [['news', 'd'], 0],
  40. [['page', 'd'], 0],
  41. [['limit', 'd'], 0],
  42. [['type', 0], 0]
  43. ], $request);
  44. return app('json')->successful(StoreProduct::getProductList($data, $request->uid()));
  45. }
  46. /**
  47. * 产品分享二维码 推广员
  48. * @param Request $request
  49. * @param $id
  50. * @return mixed
  51. */
  52. public function code(Request $request, $id)
  53. {
  54. if (!$id || !($storeInfo = StoreProduct::getValidProduct($id, 'id'))) return app('json')->fail('商品不存在或已下架');
  55. $userType = $request->get('user_type', 'wechat');
  56. $user = $request->user();
  57. try {
  58. switch ($userType) {
  59. case 'wechat':
  60. //公众号
  61. $name = $id . '_product_detail_' . $user['uid'] . '_is_promoter_' . $user['is_promoter'] . '_wap.jpg';
  62. $url = QrcodeService::getWechatQrcodePath($name, '/detail/' . $id . '?spread=' . $user['uid']);
  63. if ($url === false)
  64. return app('json')->fail('二维码生成失败');
  65. else
  66. return app('json')->successful(['code' => image_to_base64($url)]);
  67. break;
  68. case 'routine':
  69. //小程序
  70. $name = $id . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_product.jpg';
  71. $imageInfo = SystemAttachment::getInfo($name, 'name');
  72. $siteUrl = sys_config('site_url');
  73. if (!$imageInfo) {
  74. $data = 'id=' . $id;
  75. if ($user['is_promoter'] || sys_config('store_brokerage_statu') == 2) $data .= '&pid=' . $user['uid'];
  76. $res = \app\models\routine\RoutineCode::getPageCode('pages/goods_details/index', $data, 280);
  77. if (!$res) return app('json')->fail('二维码生成失败');
  78. $uploadType = (int)sys_config('upload_type', 1);
  79. $upload = new Upload($uploadType, [
  80. 'accessKey' => sys_config('accessKey'),
  81. 'secretKey' => sys_config('secretKey'),
  82. 'uploadUrl' => sys_config('uploadUrl'),
  83. 'storageName' => sys_config('storage_name'),
  84. 'storageRegion' => sys_config('storage_region'),
  85. ]);
  86. $upload->delete($name);
  87. $res = $upload->to('routine/product')->validate()->stream($res, $name);
  88. if ($res === false) {
  89. return app('json')->fail($upload->getError());
  90. }
  91. $imageInfo = $upload->getUploadInfo();
  92. $imageInfo['image_type'] = $uploadType;
  93. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  94. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  95. if (!$remoteImage['status']) return app('json')->fail('小程序二维码未能生成');
  96. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  97. $url = $imageInfo['dir'];
  98. } else $url = $imageInfo['att_dir'];
  99. if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  100. return app('json')->successful(['code' => $url]);
  101. }
  102. } catch (\Exception $e) {
  103. return app('json')->fail($e->getMessage(), [
  104. 'code' => $e->getCode(),
  105. 'line' => $e->getLine(),
  106. 'message' => $e->getMessage()
  107. ]);
  108. }
  109. }
  110. /**
  111. * 产品详情
  112. * @param Request $request
  113. * @param $id
  114. * @param int $type
  115. * @return mixed
  116. */
  117. public function detail(Request $request, $id, $type = 0)
  118. {
  119. if (!$id || !($storeInfo = StoreProduct::getValidProduct($id))) return app('json')->fail('商品不存在或已下架');
  120. $siteUrl = sys_config('site_url');
  121. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  122. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  123. $storeInfo['code_base'] = QrcodeService::getWechatQrcodePath($id . '_product_detail_wap.jpg', '/detail/' . $id);
  124. $uid = $request->uid();
  125. $data['uid'] = $uid;
  126. $storeInfo['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id));
  127. //替换windows服务器下正反斜杠问题导致图片无法显示
  128. $storeInfo['description'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
  129. return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
  130. }, $storeInfo['description']);
  131. $storeInfo['userCollect'] = StoreProductRelation::isProductRelation($id, $uid, 'collect');
  132. $storeInfo['userLike'] = StoreProductRelation::isProductRelation($id, $uid, 'like');
  133. list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($id, $uid, $type);
  134. $attrValue = $productValue;
  135. if (!$storeInfo['spec_type']) {
  136. $productAttr = [];
  137. $productValue = [];
  138. }
  139. // //对规格进行排序
  140. // $prices = array_column($productValue, 'price');
  141. // array_multisort($prices, SORT_ASC, SORT_NUMERIC, $productValue);
  142. // $keys = array_keys($productValue);
  143. // $productValue = array_combine($keys, $productValue);
  144. StoreVisit::setView($uid, $id, 'product',$storeInfo['cate_id'], 'viwe');
  145. $data['storeInfo'] = StoreProduct::setLevelPrice($storeInfo, $uid, true);
  146. $data['similarity'] = StoreProduct::cateIdBySimilarityProduct($storeInfo['cate_id'], 'id,store_name,image,price,sales,ficti', 4);
  147. $data['productAttr'] = $productAttr;
  148. $data['productValue'] = $productValue;
  149. $data['priceName'] = 0;
  150. if ($uid) {
  151. $user = $request->user();
  152. if (!$user->is_promoter) {
  153. $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $uid])->sum('pay_price');
  154. $status = is_brokerage_statu($price);
  155. if ($status) {
  156. User::where('uid', $uid)->update(['is_promoter' => 1]);
  157. $user->is_promoter = 1;
  158. }
  159. }
  160. if ($user->is_promoter) {
  161. $data['priceName'] = StoreProduct::getPacketPrice($storeInfo, $attrValue);
  162. }
  163. if (!strlen(trim($data['priceName'])))
  164. $data['priceName'] = 0;
  165. }
  166. $data['reply'] = StoreProductReply::getRecProductReply($storeInfo['id']);
  167. $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id', $storeInfo['id'])->count();
  168. if ($data['replyCount']) {
  169. $goodReply = StoreProductReply::productValidWhere()->where('product_id', $storeInfo['id'])->where('product_score', 5)->count();
  170. $data['replyChance'] = $goodReply;
  171. if ($goodReply) {
  172. $data['replyChance'] = bcdiv($goodReply, $data['replyCount'], 2);
  173. $data['replyChance'] = bcmul($data['replyChance'], 100, 2);
  174. }
  175. } else $data['replyChance'] = 0;
  176. $data['mer_id'] = $storeInfo['mer_id'];
  177. $data['system_store'] = ($res = SystemStore::getStoreDispose()) ? $res : [];
  178. $data['good_list'] = StoreProduct::getGoodList(18, 'image,store_name,price,id,ot_price');
  179. $data['mapKey'] = sys_config('tengxun_map_key');
  180. $data['store_self_mention'] = (int)sys_config('store_self_mention') ?? 0;//门店自提是否开启
  181. $data['activity'] = StoreProduct::activity($data['storeInfo']['id'], false);
  182. return app('json')->successful($data);
  183. }
  184. /**
  185. * 为你推荐
  186. *
  187. * @param Request $request
  188. * @return mixed
  189. * @throws \think\db\exception\DataNotFoundException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. * @throws \think\exception\DbException
  192. */
  193. public function product_hot(Request $request)
  194. {
  195. list($page, $limit) = UtilService::getMore([
  196. [['page', 'd'], 0],
  197. [['limit', 'd'], 0]
  198. ], $request, true);
  199. if (!$limit) return app('json')->successful([]);
  200. $productHot = StoreProduct::getHotProductLoading('id,image,store_name,cate_id,price,unit_name,ot_price', (int)$page, (int)$limit);
  201. if (!empty($productHot)) {
  202. foreach ($productHot as $k => $v) {
  203. $productHot[$k]['activity'] = StoreProduct::activity($v['id']);
  204. }
  205. }
  206. return app('json')->successful($productHot);
  207. }
  208. /**
  209. * 获取首页推荐不同类型产品的轮播图和产品
  210. * @param Request $request
  211. * @param $type
  212. * @return mixed
  213. * @throws \think\db\exception\DataNotFoundException
  214. * @throws \think\db\exception\ModelNotFoundException
  215. * @throws \think\exception\DbException
  216. */
  217. public function groom_list(Request $request, $type)
  218. {
  219. list($page, $limit) = UtilService::getMore([
  220. [['page', 'd'], 0],
  221. [['limit', 'd'], 0]
  222. ], $request, true);
  223. $info['banner'] = [];
  224. $info['list'] = [];
  225. if ($type == 1) {//TODO 精品推荐
  226. $info['banner'] = sys_data('routine_home_bast_banner') ?: [];//TODO 首页精品推荐图片
  227. $info['list'] = StoreProduct::getBestProduct('id,image,store_name,cate_id,price,ot_price,IFNULL(sales,0) + IFNULL(ficti,0) as sales,unit_name,sort', 0, 0, true, $page, $limit);//TODO 精品推荐个数
  228. } else if ($type == 2) {//TODO 热门榜单
  229. $info['banner'] = sys_data('routine_home_hot_banner') ?: [];//TODO 热门榜单 猜你喜欢推荐图片
  230. $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(), $page, $limit);//TODO 热门榜单 猜你喜欢
  231. } else if ($type == 3) {//TODO 首发新品
  232. $info['banner'] = sys_data('routine_home_new_banner') ?: [];//TODO 首发新品推荐图片
  233. $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(), true, $page, $limit);//TODO 首发新品
  234. } else if ($type == 4) {//TODO 促销单品
  235. $info['banner'] = sys_data('routine_home_benefit_banner') ?: [];//TODO 促销单品推荐图片
  236. $info['list'] = StoreProduct::getBenefitProduct('id,image,store_name,cate_id,price,ot_price,stock,unit_name,sort', 0, $page, $limit);//TODO 促销单品
  237. }
  238. return app('json')->successful($info);
  239. }
  240. /**
  241. * 产品评价数量和好评度
  242. * @param $id
  243. * @return mixed
  244. */
  245. public function reply_config($id)
  246. {
  247. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  248. return app('json')->successful(StoreProductReply::productReplyCount($id));
  249. }
  250. /**
  251. * 获取产品评论
  252. * @param Request $request
  253. * @param $id
  254. * @param $type
  255. * @return mixed
  256. */
  257. public function reply_list(Request $request, $id)
  258. {
  259. list($page, $limit, $type) = UtilService::getMore([
  260. [['page', 'd'], 0], [['limit', 'd'], 0], [['type', 'd'], 0]
  261. ], $request, true);
  262. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  263. $list = StoreProductReply::getProductReplyList($id, (int)$type, $page, $limit);
  264. return app('json')->successful($list);
  265. }
  266. }