StoreBargainController.php 19 KB

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