StoreProductController.php 13 KB

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