StoreCombinationController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace app\api\controller\activity;
  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\routine\RoutineCode;
  7. use app\models\store\StoreCombination;
  8. use app\models\store\StorePink;
  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\store\StoreVisit;
  14. use app\Request;
  15. use crmeb\services\QrcodeService;
  16. use crmeb\services\UtilService;
  17. use crmeb\services\upload\Upload;
  18. use think\facade\Cache;
  19. /**
  20. * 拼团类
  21. * Class StoreCombinationController
  22. * @package app\api\controller\activity
  23. */
  24. class StoreCombinationController
  25. {
  26. /**
  27. * 拼团列表
  28. * @param Request $request
  29. * @return mixed
  30. */
  31. public function lst(Request $request)
  32. {
  33. list($page, $limit) = UtilService::getMore([
  34. ['page', 1],
  35. ['limit', 10],
  36. ], $request, true);
  37. $combinationList = StoreCombination::getAll($page, $limit);
  38. if (!count($combinationList)) return app('json')->successful([]);
  39. return app('json')->successful($combinationList->hidden(['info', 'product_id', 'images', 'mer_id', 'attr', 'sort', 'stock', 'sales', 'add_time', 'is_del', 'is_show', 'browse', 'cost', 'is_show', 'start_time', 'stop_time', 'postage', 'is_postage', 'is_host', 'mer_use', 'combination'])->toArray());
  40. }
  41. /**
  42. * 拼团产品详情
  43. * @param Request $request
  44. * @param $id
  45. * @return mixed
  46. */
  47. public function detail(Request $request, $id)
  48. {
  49. if (!$id || !($combinationOne = StoreCombination::getCombinationOne($id))) return app('json')->fail('拼团不存在或已下架');
  50. $combinationOne = $combinationOne->hidden(['mer_id', 'attr', 'sort', 'add_time', 'is_host', 'is_show', 'is_del', 'mer_use', 'cost', 'combination'])->toArray();
  51. $combinationOne['images'] = json_decode($combinationOne['images'], true);
  52. list($pink, $pindAll) = StorePink::getPinkAll($id, true);//拼团列表
  53. $siteUrl = sys_config('site_url');
  54. $combinationOne['image'] = set_file_url($combinationOne['image'], $siteUrl);
  55. $combinationOne['image_base'] = set_file_url($combinationOne['image'], $siteUrl);
  56. $combinationOne['code_base'] = QrcodeService::getWechatQrcodePath($id . '_combination_detail_wap.jpg', '/activity/group_detail/' . $id);
  57. $combinationOne['sale_stock'] = 0;
  58. if ($combinationOne['stock'] > 0) $combinationOne['sale_stock'] = 1;
  59. if (!strlen(trim($combinationOne['unit_name']))) $combinationOne['unit_name'] = '个';
  60. $uid = $request->uid();
  61. $combinationOne['userCollect'] = StoreProductRelation::isProductRelation($combinationOne['product_id'], $uid, 'collect');
  62. $combinationOne['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id, 3));
  63. StoreVisit::setView($uid, $id, 'combination',$combinationOne['product_id'], 'viwe');
  64. $data['pink'] = $pink;
  65. $data['pindAll'] = $pindAll;
  66. $data['storeInfo'] = $combinationOne;
  67. $data['pink_ok_list'] = StorePink::getPinkOkList($uid);
  68. $data['pink_ok_sum'] = StorePink::getPinkOkSumTotalNum($id);
  69. $data['reply'] = StoreProductReply::getRecProductReply($combinationOne['product_id']);
  70. $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id', $combinationOne['product_id'])->count();
  71. if ($data['replyCount']) {
  72. $goodReply = StoreProductReply::productValidWhere()->where('product_id', $combinationOne['product_id'])->where('product_score', 5)->count();
  73. $data['replyChance'] = $goodReply;
  74. if ($goodReply) {
  75. $data['replyChance'] = bcdiv($goodReply, $data['replyCount'], 2);
  76. $data['replyChance'] = bcmul($data['replyChance'], 100, 3);
  77. }
  78. } else $data['replyChance'] = 0;
  79. if (StoreProduct::be(['spec_type' => 1, 'id' => $combinationOne['product_id']])) {
  80. list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($id, $uid, 0, 3);
  81. foreach ($productValue as $k => $v) {
  82. $productValue[$k]['product_stock'] = StoreProductAttrValue::where('product_id', $combinationOne['product_id'])->where('suk', $v['suk'])->where('type', 0)->value('stock');
  83. }
  84. $data['productAttr'] = $productAttr;
  85. $data['productValue'] = $productValue;
  86. } else {
  87. $data['productAttr'] = [];
  88. $data['productValue'] = [];
  89. list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($id, $uid, 0, 3);
  90. $value = $productValue['默认'] ?? [];
  91. $data['storeInfo']['product_stock'] = $value['stock'] ?? 0;
  92. $data['storeInfo']['quota'] = $value['quota'] ?? 0;
  93. $data['storeInfo']['quota_show'] = $value['quota_show'] ?? 0;
  94. }
  95. return app('json')->successful($data);
  96. }
  97. /**
  98. * 拼团 开团
  99. * @param Request $request
  100. * @param $id
  101. * @return mixed
  102. */
  103. public function pink(Request $request, $id)
  104. {
  105. $is_ok = 0;//判断拼团是否完成
  106. $userBool = 0;//判断当前用户是否在团内 0未在 1在
  107. $pinkBool = 0;//判断拼团是否成功 0未在 1在
  108. $user = $request->user();
  109. if (!$id) return app('json')->fail('参数错误');
  110. $pink = StorePink::getPinkUserOne($id);
  111. if (!$pink) return app('json')->fail('参数错误');
  112. if (isset($pink['is_refund']) && $pink['is_refund']) {
  113. if ($pink['is_refund'] != $pink['id']) {
  114. $id = $pink['is_refund'];
  115. return $this->pink($request, $id);
  116. } else {
  117. return app('json')->fail('订单已退款');
  118. }
  119. }
  120. list($pinkAll, $pinkT, $count, $idAll, $uidAll) = StorePink::getPinkMemberAndPinkK($pink);
  121. if ($pinkT['status'] == 2) {
  122. $pinkBool = 1;
  123. $is_ok = 1;
  124. } else if ($pinkT['status'] == 3) {
  125. $pinkBool = -1;
  126. $is_ok = 0;
  127. } else {
  128. if ($count < 1) {//组团完成
  129. $is_ok = 1;
  130. $pinkBool = StorePink::PinkComplete($uidAll, $idAll, $user['uid'], $pinkT);
  131. } else {
  132. $pinkBool = StorePink::PinkFail($pinkAll, $pinkT, $pinkBool);
  133. }
  134. }
  135. if (!empty($pinkAll)) {
  136. foreach ($pinkAll as $v) {
  137. if ($v['uid'] == $user['uid']) $userBool = 1;
  138. }
  139. }
  140. if ($pinkT['uid'] == $user['uid']) $userBool = 1;
  141. $combinationOne = StoreCombination::getCombinationOne($pink['cid']);
  142. if (!$combinationOne) return app('json')->fail('拼团不存在或已下架');
  143. $data['userInfo']['uid'] = $user['uid'];
  144. $data['userInfo']['nickname'] = $user['nickname'];
  145. $data['userInfo']['avatar'] = $user['avatar'];
  146. $data['is_ok'] = $is_ok;
  147. $data['userBool'] = $userBool;
  148. $data['pinkBool'] = $pinkBool;
  149. $data['store_combination'] = $combinationOne->hidden(['mer_id', 'images', 'attr', 'info', 'sort', 'sales', 'stock', 'add_time', 'is_host', 'is_show', 'is_del', 'combination', 'mer_use', 'is_postage', 'postage', 'start_time', 'stop_time', 'cost', 'browse', 'product_price'])->toArray();
  150. $data['pinkT'] = $pinkT;
  151. $data['pinkAll'] = $pinkAll;
  152. $data['count'] = $count <= 0 ? 0 : $count;
  153. $data['store_combination_host'] = StoreCombination::getCombinationHost();
  154. $data['current_pink_order'] = StorePink::getCurrentPink($id, $user['uid']);
  155. list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($combinationOne['id'], $user['uid'], 0, 3);
  156. foreach ($productValue as $k => $v) {
  157. $productValue[$k]['product_stock'] = StoreProductAttrValue::where('product_id', $combinationOne['product_id'])->where('suk', $v['suk'])->where('type', 0)->value('stock');
  158. }
  159. $data['store_combination']['productAttr'] = $productAttr;
  160. $data['store_combination']['productValue'] = $productValue;
  161. return app('json')->successful($data);
  162. }
  163. /**
  164. * 拼团 取消开团
  165. * @param Request $request
  166. * @return mixed
  167. */
  168. public function remove(Request $request)
  169. {
  170. list($id, $cid) = UtilService::postMore([
  171. ['id', 0],
  172. ['cid', 0],
  173. ], $request, true);
  174. if (!$id || !$cid) return app('json')->fail('缺少参数');
  175. $res = StorePink::removePink($request->uid(), $cid, $id);
  176. if ($res) {
  177. return app('json')->successful('取消成功');
  178. }
  179. $error = StorePink::getErrorInfo();
  180. if (is_array($error)) return app('json')->status($error['status'], $error['msg']);
  181. return app('json')->fail($error);
  182. }
  183. /**
  184. * 拼团海报
  185. * @param Request $request
  186. * @return mixed
  187. */
  188. public function poster(Request $request)
  189. {
  190. list($pinkId, $from) = UtilService::postMore([['id', 0], ['from', 'wechat']], $request, true);
  191. if (!$pinkId) return app('json')->fail('参数错误');
  192. $user = $request->user();
  193. $pinkInfo = StorePink::getPinkUserOne($pinkId);
  194. $storeCombinationInfo = StoreCombination::getCombinationOne($pinkInfo['cid']);
  195. $data['title'] = $storeCombinationInfo['title'];
  196. $data['image'] = $storeCombinationInfo['image'];
  197. $data['price'] = $pinkInfo['total_price'];
  198. $data['label'] = $pinkInfo['people'] . '人团';
  199. if ($pinkInfo['k_id']) $pinkAll = StorePink::getPinkMember($pinkInfo['k_id']);
  200. else $pinkAll = StorePink::getPinkMember($pinkInfo['id']);
  201. $count = count($pinkAll) + 1;
  202. $data['msg'] = '原价¥' . $storeCombinationInfo['product_price'] . ' 还差' . (int)bcsub((int)$pinkInfo['people'], $count, 0) . '人拼团成功';
  203. try {
  204. $siteUrl = sys_config('site_url');
  205. if ($from == 'routine') {
  206. //小程序
  207. $name = $pinkId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_pink_share_routine.jpg';
  208. $imageInfo = SystemAttachment::getInfo($name, 'name');
  209. if (!$imageInfo) {
  210. $valueData = 'id=' . $pinkId;
  211. if ($user['is_promoter'] || sys_config('store_brokerage_statu') == 2) $valueData .= '&pid=' . $user['uid'];
  212. $res = RoutineCode::getPageCode('pages/activity/goods_combination_status/index', $valueData, 280);
  213. if (!$res) return app('json')->fail('二维码生成失败');
  214. $uploadType = (int)sys_config('upload_type', 1);
  215. $upload = new Upload($uploadType, [
  216. 'accessKey' => sys_config('accessKey'),
  217. 'secretKey' => sys_config('secretKey'),
  218. 'uploadUrl' => sys_config('uploadUrl'),
  219. 'storageName' => sys_config('storage_name'),
  220. 'storageRegion' => sys_config('storage_region'),
  221. ]);
  222. $res = $upload->to('routine/activity/pink/code')->validate()->stream($res, $name);
  223. if ($res === false) {
  224. return app('json')->fail($upload->getError());
  225. }
  226. $imageInfo = $upload->getUploadInfo();
  227. $imageInfo['image_type'] = $uploadType;
  228. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  229. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  230. if (!$remoteImage['status']) return app('json')->fail($remoteImage['msg']);
  231. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  232. $url = $imageInfo['dir'];
  233. } else $url = $imageInfo['att_dir'];
  234. $data['url'] = $url;
  235. if ($imageInfo['image_type'] == 1)
  236. $data['url'] = $siteUrl . $url;
  237. $posterImage = UtilService::setShareMarketingPoster($data, 'routine/activity/pink/poster');
  238. if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
  239. SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
  240. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  241. $routinePosterImage = set_http_type($posterImage['dir'], 0);//小程序推广海报
  242. return app('json')->successful(['url' => $routinePosterImage]);
  243. } else if ($from == 'wechat') {
  244. //公众号
  245. $name = $pinkId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_pink_share_wap.jpg';
  246. $imageInfo = SystemAttachment::getInfo($name, 'name');
  247. if (!$imageInfo) {
  248. $codeUrl = set_http_type($siteUrl . '/activity/group_rule/' . $pinkId . '?spread=' . $user['uid'], 1);//二维码链接
  249. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  250. if (is_string($imageInfo)) {
  251. return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
  252. }
  253. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  254. $url = $imageInfo['dir'];
  255. } else $url = $imageInfo['att_dir'];
  256. $data['url'] = $url;
  257. if ($imageInfo['image_type'] == 1) $data['url'] = $siteUrl . $url;
  258. $posterImage = UtilService::setShareMarketingPoster($data, 'wap/activity/pink/poster');
  259. if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
  260. SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
  261. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  262. $wapPosterImage = set_http_type($posterImage['dir'], 1);//公众号推广海报
  263. return app('json')->successful(['url' => $wapPosterImage]);
  264. }
  265. return app('json')->fail('参数错误');
  266. } catch (\Exception $e) {
  267. return app('json')->fail(['line' => $e->getLine(), 'message' => $e->getMessage()]);
  268. }
  269. }
  270. }