StoreBargainController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. namespace app\api\controller\activity;
  3. use app\models\store\StoreDescription;
  4. use app\models\store\StoreProductAttr;
  5. use app\models\store\StoreProductAttrValue;
  6. use app\models\system\SystemAttachment;
  7. use app\models\routine\RoutineCode;
  8. use app\models\routine\RoutineTemplate;
  9. use app\models\store\StoreBargain;
  10. use app\models\store\StoreBargainUser;
  11. use app\models\store\StoreBargainUserHelp;
  12. use app\models\store\StoreOrder;
  13. use app\models\user\User;
  14. use app\models\user\WechatUser;
  15. use app\models\wechat\WechatTemplate;
  16. use app\Request;
  17. use crmeb\services\GroupDataService;
  18. use crmeb\services\QrcodeService;
  19. use crmeb\services\SystemConfigService;
  20. use crmeb\services\UploadService;
  21. use crmeb\services\UtilService;
  22. use crmeb\services\WechatTemplateService;
  23. use think\db\exception\DataNotFoundException;
  24. use think\db\exception\DbException;
  25. use think\db\exception\ModelNotFoundException;
  26. use think\Exception;
  27. use think\facade\Route;
  28. /**
  29. * 砍价商品类
  30. * Class StoreBargainController
  31. * @package app\api\controller\activity
  32. */
  33. class StoreBargainController
  34. {
  35. /**
  36. * 砍价列表顶部图
  37. * @return mixed
  38. * @throws DataNotFoundException
  39. * @throws ModelNotFoundException
  40. * @throws \think\exception\DbException
  41. */
  42. public function config(Request $request)
  43. {
  44. $lovely = sys_data('routine_lovely', 0, $request->mer_id()) ?? [];//banner图
  45. $info = isset($lovely[2]) ? $lovely[2] : [];
  46. return app('json')->successful($info);
  47. }
  48. /**
  49. * 砍价商品列表
  50. * @param Request $request
  51. * @return mixed
  52. * @throws \Exception
  53. */
  54. public function lst(Request $request)
  55. {
  56. list($page, $limit) = UtilService::getMore([
  57. ['page', 0],
  58. ['limit', 0],
  59. ], $request, true);
  60. $bargainList = StoreBargain::getList($page, $limit, $request->mer_id());
  61. return app('json')->successful($bargainList);
  62. }
  63. /**
  64. * 砍价详情和当前登录人信息
  65. * @param Request $request
  66. * @param $id
  67. * @return mixed
  68. * @throws DataNotFoundException
  69. * @throws ModelNotFoundException
  70. * @throws DbException
  71. */
  72. public function detail(Request $request, $id)
  73. {
  74. if (!$id) return app('json')->fail('参数错误');
  75. if (!($bargain = StoreBargain::getBargainTerm($id, $request->mer_id()))) return app('json')->fail('砍价已结束');
  76. StoreBargain::addBargainLook($id);
  77. $bargain['time'] = time();
  78. $user = $request->user();
  79. $data['userInfo']['uid'] = $user['uid'];
  80. $data['userInfo']['nickname'] = $user['nickname'];
  81. $data['userInfo']['avatar'] = $user['avatar'];
  82. $bargain['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id, 2));
  83. list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($id, $user['uid'], 2, 2);
  84. foreach ($productValue as $k => $v) {
  85. $v['product_stock'] = StoreProductAttrValue::where('product_id', $bargain['product_id'])->where('suk', $v['suk'])->where('type', 0)->value('stock');
  86. $bargain['attr'] = $v;
  87. }
  88. $data['bargain'] = $bargain;
  89. $data['bargainSumCount'] = StoreOrder::getBargainPayCount($id);
  90. $data['userBargainStatus'] = StoreBargainUser::isBargainUser($id, $user['uid']);
  91. return app('json')->successful($data);
  92. }
  93. /**
  94. * 砍价 观看/分享/参与次数
  95. * @param Request $request
  96. * @return mixed
  97. * @throws \Exception
  98. */
  99. public function share(Request $request)
  100. {
  101. list($bargainId) = UtilService::postMore([['bargainId', 0]], $request, true);
  102. $data['lookCount'] = StoreBargain::getBargainLook();//TODO 观看人数
  103. $data['shareCount'] = StoreBargain::getBargainShare();//TODO 分享人数
  104. $data['userCount'] = StoreBargainUser::count();//TODO 参与人数
  105. if (!$bargainId) return app('json')->successful($data);
  106. StoreBargain::addBargainShare($bargainId);
  107. $data['shareCount'] = StoreBargain::getBargainShare();//TODO 分享人数
  108. return app('json')->successful($data);
  109. }
  110. /**
  111. * 砍价开启
  112. * @param Request $request
  113. * @return mixed
  114. * @throws DataNotFoundException
  115. * @throws DbException
  116. * @throws Exception
  117. * @throws ModelNotFoundException
  118. * @throws \Exception
  119. */
  120. public function start(Request $request)
  121. {
  122. list($bargainId) = UtilService::postMore([['bargainId', 0]], $request, true);
  123. if (!($bargain = StoreBargain::getBargainTerm($bargainId, $request->mer_id()))) return app('json')->fail('砍价已结束');
  124. if (!$bargainId) return app('json')->fail('参数错误');
  125. $count = StoreBargainUser::isBargainUser($bargainId, $request->uid());
  126. if ($count === false) return app('json')->fail('参数错误');
  127. else if ($count) return app('json')->status('SUCCESSFUL', '参与成功');
  128. else $res = StoreBargainUser::setBargain($bargainId, $request->uid());
  129. if (!$res) return app('json')->fail('参与失败');
  130. else return app('json')->status('SUCCESS', '参与成功');
  131. }
  132. /**
  133. * 砍价 帮助好友砍价
  134. * @param Request $request
  135. * @return mixed
  136. * @throws Exception
  137. * @throws DataNotFoundException
  138. * @throws ModelNotFoundException
  139. * @throws DbException
  140. * @throws \Exception
  141. */
  142. public function help(Request $request)
  143. {
  144. list($bargainId, $bargainUserUid) = UtilService::postMore([['bargainId', 0], ['bargainUserUid', 0]], $request, true);
  145. if (!$bargainId || !$bargainUserUid) return app('json')->fail('参数错误');
  146. if (!StoreBargain::merSet($request->mer_id())->where('id', $bargainId)->where('is_del', 0)->find()) return app('json')->fail('砍价已结束');
  147. $count = StoreBargainUserHelp::isBargainUserHelpCount($bargainId, $bargainUserUid, $request->uid());
  148. if (!$count) return app('json')->status('SUCCESSFUL', '砍价成功');
  149. $res = StoreBargainUserHelp::setBargainUserHelp($bargainId, $bargainUserUid, $request->uid());
  150. if ($res) {
  151. if (!StoreBargainUserHelp::getSurplusPrice($bargainId, $bargainUserUid)) {
  152. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $bargainUserUid);// TODO 获取用户参与砍价表编号
  153. $bargainInfo = StoreBargain::get($bargainId);//TODO 获取砍价商品信息
  154. $bargainUserInfo = StoreBargainUser::get($bargainUserTableId);// TODO 获取用户参与砍价信息
  155. //TODO 砍价成功给开启砍价用户发送模板消息
  156. $openid = WechatUser::uidToOpenid($bargainUserUid, 'openid');
  157. $routineOpenid = WechatUser::uidToOpenid($bargainUserUid, 'routine_openid');
  158. if ($openid) {//公众号
  159. // $urlWeChat = Route::buildUrl('/activity/dargain_detail/' . $bargainId . '/' . $bargainUserUid)->suffix('')->domain(true)->build();
  160. // WechatTemplateService::sendTemplate($openid, WechatTemplateService::BARGAIN_SUCCESS, [
  161. // 'first' => '好腻害!你的朋友们已经帮你砍到底价了!',
  162. // 'keyword1' => $bargainInfo['title'],
  163. // 'keyword2' => $bargainInfo['min_price'],
  164. // 'remark' => '点击查看订单详情'
  165. // ], $urlWeChat);
  166. $wechatTemplate = new WechatTemplate();
  167. $wechatTemplate->sendBrgainSuccess($bargainUserUid, $bargainInfo);
  168. } else if ($routineOpenid) { //小程序
  169. RoutineTemplate::sendBargainSuccess($bargainInfo, $bargainUserInfo, $bargainUserUid);
  170. }
  171. }
  172. return app('json')->status('SUCCESS', '砍价成功');
  173. } else return app('json')->fail('砍价失败');
  174. }
  175. /**
  176. * 砍价 砍掉金额
  177. * @param Request $request
  178. * @return mixed
  179. * @throws \Exception
  180. */
  181. public function help_price(Request $request)
  182. {
  183. list($bargainId, $bargainUserUid) = UtilService::postMore([['bargainId', 0], ['bargainUserUid', 0]], $request, true);
  184. if (!$bargainId || !$bargainUserUid || !!StoreBargain::merSet($request->mer_id())->where('id', $bargainId)->where('is_del', 0)->find()) return app('json')->fail('参数错误');
  185. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $bargainUserUid);//TODO 获取用户参与砍价表编号
  186. $price = StoreBargainUserHelp::getBargainUserBargainPrice($bargainId, $bargainUserTableId, $request->uid(), 'price');// TODO 获取用户砍掉的金额
  187. if ($price) return app('json')->successful(['price' => $price]);
  188. else return app('json')->fail('获取失败');
  189. }
  190. /**
  191. * 砍价 砍价帮总人数、剩余金额、进度条、已经砍掉的价格
  192. * @param Request $request
  193. * @return mixed
  194. * @throws Exception
  195. * @throws DataNotFoundException
  196. * @throws ModelNotFoundException
  197. * @throws DbException
  198. * @throws \Exception
  199. */
  200. public function help_count(Request $request)
  201. {
  202. list($bargainId, $bargainUserUid) = UtilService::postMore([['bargainId', 0], ['bargainUserUid', 0]], $request, true);
  203. $data['userBargainStatus'] = StoreBargainUserHelp::isBargainUserHelpCount($bargainId, $bargainUserUid, $request->uid());
  204. if (!$bargainId || !$bargainUserUid || !StoreBargain::merSet($request->mer_id())->where('id', $bargainId)->where('is_del', 0)->find()) return app('json')->fail('参数错误');
  205. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $bargainUserUid);//TODO 获取用户参与砍价表编号
  206. if ($bargainUserTableId) {
  207. $count = StoreBargainUserHelp::getBargainUserHelpPeopleCount($bargainId, $bargainUserUid);//TODO 获取砍价帮总人数
  208. $price = StoreBargainUserHelp::getSurplusPrice($bargainId, $bargainUserUid);//TODO 获取砍价剩余金额
  209. $alreadyPrice = StoreBargainUser::getBargainUserPrice($bargainUserTableId);//TODO 用户已经砍掉的价格 好友砍价之后获取用户已经砍掉的价格
  210. $pricePercent = StoreBargainUserHelp::getSurplusPricePercent($bargainId, $bargainUserUid);//TODO 获取砍价进度条
  211. $data['count'] = $count;
  212. $data['price'] = $price;
  213. $data['status'] = StoreBargainUser::getBargainUserStatusEnd($bargainUserTableId);
  214. $data['alreadyPrice'] = $alreadyPrice;
  215. $data['pricePercent'] = $pricePercent > 10 ? $pricePercent : 10;
  216. } else {
  217. $data['count'] = 0;
  218. $data['price'] = StoreBargain::where('id', $bargainId)->value('price - min_price');
  219. $data['status'] = StoreBargainUser::getBargainUserStatusEnd($bargainUserTableId);
  220. $data['alreadyPrice'] = 0;
  221. $data['pricePercent'] = 0;
  222. }
  223. return app('json')->successful($data);
  224. }
  225. /**
  226. * 砍价 砍价帮
  227. * @param Request $request
  228. * @return mixed
  229. * @throws Exception
  230. * @throws DataNotFoundException
  231. * @throws ModelNotFoundException
  232. * @throws DbException
  233. */
  234. public function help_list(Request $request)
  235. {
  236. list($bargainId, $bargainUserUid, $page, $limit) = UtilService::postMore([
  237. ['bargainId', 0],
  238. ['bargainUserUid', 0],
  239. ['page', 0],
  240. ['limit', 20]
  241. ], $request, true);
  242. if (!$bargainId || !$bargainUserUid || !StoreBargain::merSet($request->mer_id())->where('id', $bargainId)->where('is_del', 0)->find()) return app('json')->fail('参数错误');
  243. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $bargainUserUid); //TODO 砍价帮获取参与砍价表编号
  244. $storeBargainUserHelp = StoreBargainUserHelp::getList($bargainUserTableId, $page, $limit);
  245. return app('json')->successful($storeBargainUserHelp);
  246. }
  247. /**
  248. * 砍价 开启砍价用户信息
  249. * @param Request $request
  250. * @return mixed
  251. * @throws DataNotFoundException
  252. * @throws ModelNotFoundException
  253. * @throws DbException
  254. * @throws \Exception
  255. */
  256. public function start_user(Request $request)
  257. {
  258. list($bargainId, $bargainUserUid) = UtilService::postMore([
  259. ['bargainId', 0],
  260. ['bargainUserUid', 0],
  261. ], $request, true);
  262. if (!$bargainId || !$bargainUserUid) return app('json')->fail('参数错误');
  263. // $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserUid);//TODO 获取用户参与砍价表编号
  264. // if(!$bargainUserTableId) return app('json')->fail('参数错误');
  265. $userInfo = User::getUserInfo($bargainUserUid, 'nickname,avatar', $request->mer_id());
  266. return app('json')->successful($userInfo);
  267. }
  268. /**
  269. * 砍价列表(已参与)
  270. * @param Request $request
  271. * @return mixed
  272. * @throws \Exception
  273. */
  274. public function user_list(Request $request)
  275. {
  276. list($page, $limit) = UtilService::getMore([
  277. ['page', 0],
  278. ['limit', 0],
  279. ], $request, true);
  280. $uid = $request->uid();
  281. StoreBargainUser::editBargainUserStatus($uid);// TODO 判断过期砍价活动
  282. $list = StoreBargainUser::getBargainUserAll($uid, $page, $limit);
  283. if (count($list)) return app('json')->successful($list);
  284. else return app('json')->fail('暂无参与砍价');
  285. }
  286. /**
  287. * 砍价取消
  288. * @param Request $request
  289. * @return mixed
  290. * @throws \Exception
  291. */
  292. public function user_cancel(Request $request)
  293. {
  294. list($bargainId) = UtilService::postMore([['bargainId', 0]], $request, true);
  295. if (!$bargainId || !StoreBargain::merSet($request->mer_id())->where('id', $bargainId)->where('is_del', 0)->find()) return app('json')->fail('参数错误');
  296. $status = StoreBargainUser::getBargainUserStatus($bargainId, $request->uid());
  297. if ($status != 1) return app('json')->fail('状态错误');
  298. $id = StoreBargainUser::getBargainUserTableId($bargainId, $request->uid());
  299. $res = StoreBargainUser::edit(['is_del' => 1], $id);
  300. if ($res) return app('json')->successful('取消成功');
  301. else return app('json')->successful('取消失败');
  302. }
  303. /**
  304. * 砍价海报
  305. * @param Request $request
  306. * @return mixed
  307. * @throws DataNotFoundException
  308. * @throws ModelNotFoundException
  309. * @throws DbException
  310. * @throws \Exception
  311. */
  312. public function poster(Request $request)
  313. {
  314. $mer_id = $request->mer_id();
  315. list($bargainId, $from) = UtilService::postMore([['bargainId', 0], ['from', 'wechat']], $request, true);
  316. if (!$bargainId || !StoreBargain::merSet($request->mer_id())->where('id', $bargainId)->where('is_del', 0)->find()) return app('json')->fail('参数错误');
  317. $user = $request->user();
  318. $storeBargainInfo = StoreBargain::getBargain($bargainId);
  319. $price = StoreBargainUserHelp::getSurplusPrice($bargainId, $user['uid']);//TODO 获取砍价剩余金额
  320. $alreadyPrice = StoreBargainUser::getBargainUserPrice(StoreBargainUser::getBargainUserTableId($bargainId, $user['uid']));
  321. try {
  322. $siteUrl = sys_config('site_url', '', $mer_id);
  323. $data['title'] = $storeBargainInfo['title'];
  324. $data['image'] = $storeBargainInfo['image'];
  325. $data['price'] = bcsub($storeBargainInfo['price'], $alreadyPrice, 2);
  326. $data['label'] = '已砍至';
  327. $data['msg'] = '还差' . $price . '元即可砍价成功';
  328. if ($from == 'routine') {
  329. //小程序
  330. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_bargain_share_routine.jpg';
  331. $imageInfo = SystemAttachment::getInfo($name, 'name');
  332. if (!$imageInfo) {
  333. $valueData = 'id=' . $bargainId . '&bargain=' . $user['uid'];
  334. if ($user['is_promoter'] || sys_config('store_brokerage_statu', '', $mer_id) == 2) $valueData .= '&pid=' . $user['uid'];
  335. $res = RoutineCode::getPageCode('pages/activity/goods_bargain_details/index', $valueData, 280, $mer_id);
  336. if (!$res) return app('json')->fail('二维码生成失败');
  337. $uploadType = (int)sys_config('upload_type', 1, $mer_id);
  338. $upload = UploadService::init(null, $mer_id);
  339. $res = $upload->to('routine/activity/bargain/code')->validate()->stream($res, $name);
  340. if ($res === false) {
  341. return app('json')->fail($upload->getError());
  342. }
  343. $imageInfo = $upload->getUploadInfo();
  344. $imageInfo['image_type'] = $uploadType;
  345. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  346. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  347. if (!$remoteImage['status']) return app('json')->fail($remoteImage['msg']);
  348. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  349. $url = $imageInfo['dir'];
  350. } else $url = $imageInfo['att_dir'];
  351. $data['url'] = $url;
  352. if ($imageInfo['image_type'] == 1)
  353. $data['url'] = $siteUrl . $url;
  354. $posterImage = UtilService::setShareMarketingPoster($data, 'routine/activity/bargain/poster');
  355. if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
  356. SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
  357. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  358. $routinePosterImage = set_http_type($posterImage['dir'], 0);//小程序推广海报
  359. return app('json')->successful(['url' => $routinePosterImage]);
  360. } else if ($from == 'wechat') {
  361. //公众号
  362. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_bargain_share_wap.jpg';
  363. $imageInfo = SystemAttachment::getInfo($name, 'name');
  364. if (!$imageInfo) {
  365. $codeUrl = set_http_type($siteUrl . '/pages/activity/goods_bargain_details/index?id=' . $bargainId . '&bargain=' . $user['uid'] . '&spread=' . $user['uid'], 1);//二维码链接
  366. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  367. if (is_string($imageInfo)) {
  368. return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
  369. }
  370. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  371. $url = $imageInfo['dir'];
  372. } else $url = $imageInfo['att_dir'];
  373. $data['url'] = $url;
  374. if ($imageInfo['image_type'] == 1) $data['url'] = $siteUrl . $url;
  375. $posterImage = UtilService::setShareMarketingPoster($data, 'wap/activity/bargain/poster');
  376. if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
  377. SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
  378. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  379. $wapPosterImage = set_http_type($posterImage['dir'], 1);//公众号推广海报
  380. return app('json')->successful(['url' => $wapPosterImage]);
  381. }
  382. return app('json')->fail('参数错误');
  383. } catch (\Exception $e) {
  384. return app('json')->fail(['line' => $e->getLine(), 'message' => $e->getMessage()]);
  385. }
  386. }
  387. }