StoreCombinationController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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\StoreOrder;
  9. use app\models\store\StorePink;
  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\JsonService;
  16. use crmeb\services\QrcodeService;
  17. use crmeb\services\UtilService;
  18. use crmeb\services\upload\Upload;
  19. use think\facade\Db;
  20. use app\models\store\StoreProduct;
  21. /**
  22. * 拼团类
  23. * Class StoreCombinationController
  24. * @package app\api\controller\activity
  25. */
  26. class StoreCombinationController
  27. {
  28. /**
  29. *我的开团
  30. */
  31. public function getuserlist(Request $request)
  32. {
  33. $where['uid'] = $request->uid();
  34. $where['status'] = input('status', 1);
  35. return app('json')->successful(StorePink::getuserlist($where));
  36. }
  37. /**
  38. *获取我的团
  39. */
  40. public function getuserpink(Request $request)
  41. {
  42. $where['uid'] = $request->uid();
  43. $where['status'] = input('status', 1);
  44. $where['is_show'] = 0;
  45. return app('json')->successful(StorePink::getuserpink($where));
  46. }
  47. /**
  48. * 拼团列表
  49. * @param Request $request
  50. * @return mixed
  51. */
  52. public function lst(Request $request)
  53. {
  54. list($page, $limit,$is_host,$is_rec) = UtilService::getMore([
  55. ['page', 1],
  56. ['limit', 10],
  57. ['is_host',-1],
  58. ['is_rec',-1],
  59. ], $request, true);
  60. $combinationList = StoreCombination::getAll($page, $limit,$is_host,$is_rec);
  61. if (!count($combinationList)) return app('json')->successful([]);
  62. 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());
  63. }
  64. /**
  65. * 拼团产品详情
  66. * @param Request $request
  67. * @param $id
  68. * @return mixed
  69. */
  70. public function detail(Request $request, $id)
  71. {
  72. if (!$id || !($combinationOne = StoreCombination::getCombinationOne($id))) return app('json')->fail('拼团不存在或已下架');
  73. // echo 'ok';die;
  74. $combinationOne = $combinationOne->hidden(['mer_id', 'attr', 'sort', 'add_time', 'is_host', 'is_show', 'is_del', 'mer_use', 'cost', 'combination'])->toArray();
  75. $combinationOne['images'] = json_decode($combinationOne['images'], true);
  76. list($pink, $pindAll) = StorePink::getPinkAll($id, true);//拼团列表
  77. $siteUrl = sys_config('site_url');
  78. $combinationOne['image'] = set_file_url($combinationOne['image'], $siteUrl);
  79. $combinationOne['image_base'] = set_file_url($combinationOne['image'], $siteUrl);
  80. $combinationOne['code_base'] = QrcodeService::getWechatQrcodePath($id . '_combination_detail_wap.jpg', '/activity/group_detail/' . $id);
  81. $combinationOne['sale_stock'] = 0;
  82. if ($combinationOne['stock'] > 0) $combinationOne['sale_stock'] = 1;
  83. if (!strlen(trim($combinationOne['unit_name']))) $combinationOne['unit_name'] = '个';
  84. $uid = $request->uid();
  85. $combinationOne['userCollect'] = StoreProductRelation::isProductRelation($combinationOne['id'], $uid, 'collect', 'pink');
  86. $combinationOne['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id, 3));
  87. $data['pink'] = $pink;
  88. $data['pindAll'] = $pindAll;
  89. $data['storeInfo'] = $combinationOne;
  90. $data['product']=StoreProduct::where('id',$combinationOne['product_id'])->find()->toArray();
  91. $data['pink_ok_list'] = StorePink::getPinkOkList($uid);
  92. $data['pink_ok_sum'] = StorePink::getPinkOkSumTotalNum($id);
  93. $data['reply'] = StoreProductReply::getRecProductReply($combinationOne['product_id']);
  94. $data['auto_pink_people'] = StoreOrder::alias('order')->field("distinct order.uid, u.avatar,u.nickname")
  95. ->where("order.is_del=0 and order.is_system_del=0 and order.paid=1 and order.combination_id={$id}")
  96. ->order('order.id desc')
  97. ->limit(100)
  98. ->join('user u', 'u.uid=order.uid')->select()->toArray();
  99. $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id', $combinationOne['product_id'])->count();
  100. if ($data['replyCount']) {
  101. $goodReply = StoreProductReply::productValidWhere()->where('product_id', $combinationOne['product_id'])->where('product_score', 5)->count();
  102. $data['replyChance'] = $goodReply;
  103. if ($goodReply) {
  104. $data['replyChance'] = bcdiv($goodReply, $data['replyCount'], 2);
  105. $data['replyChance'] = bcmul($data['replyChance'], 100, 3);
  106. }
  107. } else $data['replyChance'] = 0;
  108. list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($id, $uid, 0, 3);
  109. foreach ($productValue as $k => $v) {
  110. $productValue[$k]['product_stock'] = StoreProductAttrValue::where('product_id', $combinationOne['product_id'])->where('suk', $v['suk'])->where('type', 0)->value('stock');
  111. }
  112. $data['productAttr'] = $productAttr;
  113. $data['productValue'] = $productValue;
  114. return app('json')->successful($data);
  115. }
  116. /**
  117. * 拼团 开团
  118. * @param Request $request
  119. * @param $id
  120. * @return mixed
  121. */
  122. public function pink(Request $request, $id)
  123. {
  124. $is_ok = 0;//判断拼团是否完成
  125. $userBool = 0;//判断当前用户是否在团内 0未在 1在
  126. $pinkBool = 0;//判断拼团是否成功 0未在 1在
  127. $user = $request->user();
  128. if (!$id) return app('json')->fail('参数错误1');
  129. // $suceess_num=sys_config('pink_suceess_num');
  130. // $num=StorePink::where('uid',$request->uid())->where('status',2)->where('is_refund',0)->whereTime('stop_time','today')->count();
  131. // if($suceess_num<=$num) return app('json')->fail('今日拼团次数已用完');
  132. // $auto_pink=StoreOrder::where('uid',$request->uid())->where('combination_id','<>',0)->where('is_ok',0)->find();
  133. // if($auto_pink){
  134. // return app('json')->fail('拼团正在进行');
  135. // }
  136. $pink = StorePink::getPinkUserOne($id);
  137. if (!$pink) return app('json')->fail('参数错误2');
  138. // if (isset($pink['is_refund']) && $pink['is_refund']) {
  139. // if ($pink['is_refund'] != $pink['id']) {
  140. // $id = $pink['is_refund'];
  141. // return $this->pink($request, $id);
  142. // } else {
  143. // return app('json')->fail('订单已退款');
  144. // }
  145. // }
  146. list($pinkAll, $pinkT, $count, $idAll, $uidAll) = StorePink::getPinkMemberAndPinkK($pink);
  147. if ($pinkT['status'] == 2) {
  148. $pinkBool = 1;
  149. $is_ok = 1;
  150. } else if ($pinkT['status'] == 3) {
  151. $pinkBool = -1;
  152. $is_ok = 0;
  153. } else {
  154. if ($count < 1) {//组团完成
  155. $is_ok = 1;
  156. $pinkBool = StorePink::PinkComplete($uidAll, $idAll, $user['uid'], $pinkT);
  157. } else {
  158. $pinkBool = StorePink::PinkFail($pinkAll, $pinkT, $pinkBool);
  159. }
  160. }
  161. if (!empty($pinkAll)) {
  162. foreach ($pinkAll as $v) {
  163. if ($v['uid'] == $user['uid']) $userBool = 1;
  164. }
  165. }
  166. if ($pinkT['uid'] == $user['uid']) $userBool = 1;
  167. $combinationOne = StoreCombination::getCombinationOneN($pink['cid']);
  168. if (!$combinationOne) return app('json')->fail('拼团不存在或已下架');
  169. $data['userInfo']['uid'] = $user['uid'];
  170. $data['userInfo']['nickname'] = $user['nickname'];
  171. $data['userInfo']['avatar'] = $user['avatar'];
  172. $data['is_ok'] = $is_ok;
  173. $data['userBool'] = $userBool;
  174. $data['pinkBool'] = $pinkBool;
  175. $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();
  176. $data['pinkT'] = $pinkT;
  177. $data['pinkAll'] = $pinkAll;
  178. $data['count'] = $count <= 0 ? 0 : $count;
  179. $data['store_combination_host'] = StoreCombination::getCombinationHost();
  180. $data['current_pink_order'] = StorePink::getCurrentPink($id, $user['uid']);
  181. list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($combinationOne['id'], $user['uid'], 0, 3);
  182. foreach ($productValue as $k => $v) {
  183. $productValue[$k]['product_stock'] = StoreProductAttrValue::where('product_id', $combinationOne['product_id'])->where('suk', $v['suk'])->where('type', 0)->value('stock');
  184. }
  185. $data['store_combination']['productAttr'] = $productAttr;
  186. $data['store_combination']['productValue'] = $productValue;
  187. //获取unique
  188. $cartId = Db::name('store_order')->where('id', '=', $pink['order_id_key'])->value('cart_id');
  189. $cartId = str_replace('[', '', $cartId);
  190. $cartId = str_replace(']', '', $cartId);
  191. $data['store_combination']['unique'] = Db::name('store_cart')->where('id', '=', $cartId)->value('product_attr_unique');
  192. return app('json')->successful($data);
  193. }
  194. /**
  195. * 拼团 取消开团
  196. * @param Request $request
  197. * @return mixed
  198. */
  199. public function remove(Request $request)
  200. {
  201. list($id, $cid) = UtilService::postMore([
  202. ['id', 0],
  203. ['cid', 0],
  204. ], $request, true);
  205. if (!$id || !$cid) return app('json')->fail('缺少参数');
  206. $res = StorePink::removePink($request->uid(), $cid, $id);
  207. if ($res) {
  208. return app('json')->successful('取消成功');
  209. }
  210. $error = StorePink::getErrorInfo();
  211. if (is_array($error)) return app('json')->status($error['status'], $error['msg']);
  212. return app('json')->fail($error);
  213. }
  214. /**
  215. * 拼团海报
  216. * @param Request $request
  217. * @return mixed
  218. */
  219. public function poster(Request $request)
  220. {
  221. list($pinkId, $from) = UtilService::postMore([['id', 0], ['from', 'wechat']], $request, true);
  222. if (!$pinkId) return app('json')->fail('参数错误');
  223. $user = $request->user();
  224. $pinkInfo = StorePink::getPinkUserOne($pinkId);
  225. $storeCombinationInfo = StoreCombination::getCombinationOne($pinkInfo['cid']);
  226. $data['title'] = $storeCombinationInfo['title'];
  227. $data['image'] = $storeCombinationInfo['image'];
  228. $data['price'] = $pinkInfo['total_price'];
  229. $data['label'] = $pinkInfo['people'] . '人团';
  230. if ($pinkInfo['k_id']) $pinkAll = StorePink::getPinkMember($pinkInfo['k_id']);
  231. else $pinkAll = StorePink::getPinkMember($pinkInfo['id']);
  232. $count = count($pinkAll) + 1;
  233. $data['msg'] = '原价¥' . $storeCombinationInfo['ot_price'] . ' 还差' . (int)bcsub((int)$pinkInfo['people'], $count, 0) . '人拼团成功';
  234. try {
  235. $siteUrl = sys_config('site_url');
  236. if ($from == 'routine') {
  237. //小程序
  238. $name = $pinkId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_pink_share_routine.jpg';
  239. $imageInfo = SystemAttachment::getInfo($name, 'name');
  240. if (!$imageInfo) {
  241. $valueData = 'qr&pink_id=' . $pinkId . "&spread=" . $user['uid'];
  242. // if ($user['is_promoter'] || sys_config('store_brokerage_statu') == 2) $valueData .= '&pid=' . $user['uid'];
  243. $qurl = $request->param('qurl');
  244. ///@file_put_contents('msg.txt', $qurl . '-' . $valueData);
  245. $res = RoutineCode::getPageCode($qurl, $valueData, 280); //'pages/activity/goods_combination_status/index'
  246. if (!$res) return app('json')->fail('二维码生成失败');
  247. $uploadType = (int)sys_config('upload_type', 1);
  248. $upload = new Upload($uploadType, [
  249. 'accessKey' => sys_config('accessKey'),
  250. 'secretKey' => sys_config('secretKey'),
  251. 'uploadUrl' => sys_config('uploadUrl'),
  252. 'storageName' => sys_config('storage_name'),
  253. 'storageRegion' => sys_config('storage_region'),
  254. ]);
  255. $res = $upload->to('routine/activity/pink/code')->validate()->stream($res, $name);
  256. if ($res === false) {
  257. return app('json')->fail($upload->getError());
  258. }
  259. $imageInfo = $upload->getUploadInfo();
  260. $imageInfo['image_type'] = $uploadType;
  261. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  262. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  263. if (!$remoteImage['status']) return app('json')->fail($remoteImage['msg']);
  264. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  265. $url = $imageInfo['dir'];
  266. } else $url = $imageInfo['att_dir'];
  267. $data['url'] = $url;
  268. if ($imageInfo['image_type'] == 1)
  269. $data['url'] = $siteUrl . $url;
  270. $posterImage = UtilService::setShareMarketingPoster($data, 'routine/activity/pink/poster');
  271. if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
  272. SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
  273. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  274. $routinePosterImage = set_http_type($posterImage['dir'], 0);//小程序推广海报
  275. return app('json')->successful(['url' => $routinePosterImage]);
  276. } else if ($from == 'wechat') {
  277. //公众号
  278. $name = $pinkId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_pink_share_wap.jpg';
  279. $imageInfo = SystemAttachment::getInfo($name, 'name');
  280. if (!$imageInfo) {
  281. $codeUrl = set_http_type($siteUrl . '/activity/group_rule/' . $pinkId . '?spread=' . $user['uid'], 1);//二维码链接
  282. $qurl = $request->param('qurl');
  283. $imageInfo = UtilService::getQRCodePath(empty($qurl) ? $codeUrl : $qurl, $name);
  284. // $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  285. if (is_string($imageInfo)) {
  286. return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
  287. }
  288. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  289. $url = $imageInfo['dir'];
  290. } else $url = $imageInfo['att_dir'];
  291. $data['url'] = $url;
  292. if ($imageInfo['image_type'] == 1) $data['url'] = $siteUrl . $url;
  293. $posterImage = UtilService::setShareMarketingPoster($data, 'wap/activity/pink/poster');
  294. if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
  295. SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
  296. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  297. $wapPosterImage = set_http_type($posterImage['dir'], 1);//公众号推广海报
  298. return app('json')->successful(['url' => $wapPosterImage]);
  299. }
  300. return app('json')->fail('参数错误');
  301. } catch (\Exception $e) {
  302. return app('json')->fail(['line' => $e->getLine(), 'message' => $e->getMessage()]);
  303. }
  304. }
  305. public function hide(Request $request)
  306. {
  307. list($orderId) = UtilService::postMore([['order_id']], $request, true);
  308. $res1 = StorePink::where('order_id', $orderId)->where('is_show', 0)->update([
  309. 'is_show' => 1
  310. ]);
  311. $res2 = StoreOrder::where('order_id', $orderId)->where('is_show', 0)->update([
  312. 'is_show' => 1
  313. ]);
  314. if ($res1 && $res2) {
  315. return app('json')->success('删除成功');
  316. } else {
  317. return app('json')->fail('删除失败');
  318. }
  319. }
  320. public function base(Request $request)
  321. {
  322. $data['level1_reward_rate'] = sys_config('level1_reward_rate');
  323. $data['level2_reward_rate'] = sys_config('level2_reward_rate');
  324. $data['level3_reward_rate'] = sys_config('level3_reward_rate');
  325. $data['pink_pay_person'] = sys_config('pink_pay_person');
  326. $data['pink_prise_rate'] = sys_config('pink_prise_rate');
  327. $data['pink_cost_integral'] = sys_config('pink_cost_integral');
  328. $data['pink_zhitui_prize'] = sys_config('pink_zhitui_prize');
  329. $data['samelevel_prise_maxperson'] = sys_config('samelevel_prise_maxperson');
  330. $data['savelevel_prise_rate'] = sys_config('savelevel_prise_rate');
  331. return app('json')->successful($data);
  332. }
  333. public function is_full(Request $request)
  334. {
  335. $pinkId = input('pinkId',0);
  336. if($pinkId<0) return app('json')->fail('0');
  337. $people = StorePink::where("id", $pinkId)->value('people');
  338. // $count = StorePink::where("id|k_id", $pinkId)->count();
  339. $count = StoreOrder::where("pink_id={$pinkId} and is_del=0")->count();
  340. if ($count >= $people) {
  341. return app('json')->fail('0');
  342. }
  343. else
  344. {
  345. return app('json')->successful('1');
  346. }
  347. }
  348. }