StoreCombinationController.php 12 KB

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