StoreCombinationController.php 14 KB

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