AuctionController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <?php
  2. namespace app\api\controller\auction;
  3. use app\models\auction\Auction;
  4. use app\models\auction\AuctionApply;
  5. use app\models\auction\AuctionBooking;
  6. use app\models\auction\AuctionGu;
  7. use app\models\auction\AuctionOrder;
  8. use app\models\auction\AuctionPay;
  9. use app\models\auction\AuctionProduct;
  10. use app\models\auction\AuctionTime;
  11. use app\models\store\StoreProduct;
  12. use app\models\user\User;
  13. use app\models\user\UserBill;
  14. use app\Request;
  15. use Monolog\Handler\Curl\Util;
  16. use think\facade\Cache;
  17. use crmeb\services\{
  18. CacheService,
  19. ExpressService,
  20. SystemConfigService
  21. };
  22. use crmeb\services\UtilService;
  23. use crmeb\repositories\OrderRepository;
  24. use think\facade\Db;
  25. use think\facade\Validate;
  26. class AuctionController
  27. {
  28. /**
  29. * 场馆列表
  30. * @param Request $request
  31. * @return mixed
  32. */
  33. public function list(Request $request)
  34. {
  35. $data = UtilService::getMore([
  36. [['page', 'd'], 0],
  37. [['limit', 'd'], 0],
  38. ['advance'],
  39. ['auction_gu_id']
  40. ], $request);
  41. if (!$data['auction_gu_id']) return app('json')->fail('数据传入错误');
  42. $auctionModel = new \app\models\auction\Auction();
  43. return app('json')->successful($auctionModel->list($data, $request->uid()));
  44. }
  45. /**
  46. * 预约场馆
  47. * @param Request $request
  48. * @return void
  49. */
  50. public function subscribe(Request $request)
  51. {
  52. $data = UtilService::postMore([
  53. ['id']
  54. ]);
  55. if (!$data['id']) return app('json')->fail('数据传入错误');
  56. Db::startTrans();
  57. $auction = Auction::lock(true)->find($data['id']);
  58. $user = User::find($request->uid());
  59. if (!$auction)return app('json')->fail('没有此数据');
  60. if ($user['shop_integral'] < $auction['threshold']) return app('json')->fail('未到预约门槛');
  61. if (time() < strtotime($auction['add_time'])){
  62. return app('json')->fail('预约未开始');
  63. }
  64. if (time() > strtotime($auction['end_time'])){
  65. return app('json')->fail('预约时间已过');
  66. }
  67. if (AuctionBooking::where([['uid', '=', $request->uid()], ['auction_id' , '=', $auction['id']], ['frequency', '=', $auction['frequency']]])->find()){
  68. return app('json')->fail('当前场次已预约');
  69. }
  70. $product = AuctionProduct::alias('a')
  71. ->field('a.*')
  72. ->where([['a.uid', '<>', $request->uid()], ['a.is_show', '=', 1], ['b.add_time', '<=', strtotime('today')], ['a.auction_id', '=', $data['id']], ['succeed_time', '<', strtotime('today')]])
  73. ->leftJoin('auction_time b', 'a.id = b.product_id')
  74. ->limit(1)
  75. ->orderRaw('rand()')
  76. ->select();
  77. if (count($product) == 0) {
  78. $product = AuctionProduct::where([['uid', '<>', $request->uid()], ['is_admin', '=', 1],['is_show', '=', 1], ['auction_id', '=', $data['id']], ['succeed_time', '<', strtotime('today')]])
  79. ->limit(1)
  80. ->orderRaw('rand()')
  81. ->select();
  82. if (count($product) == 0) {
  83. return app('json')->fail('商品已认购完');
  84. }
  85. }
  86. if ($user['is_auth'] != 2) return app('json')->fail('未实名认证');
  87. // if ($user['shop_integral'] <= 0) return app('json')->fail('账户内没有购物券,无法进行预约');
  88. if ($user['anticipate'] < $auction['anticipate']) return app('json')->fail('广告值不足');
  89. $user['anticipate'] = $user['anticipate'] - $auction['anticipate'];// 扣除广告值
  90. try {
  91. $user->save();
  92. AuctionOrder::create([
  93. 'uid' => $request->uid(),
  94. 'collection_id' => $product[0]['uid'],// 商品拥有有人
  95. 'order_id' => getNewOrderId(),
  96. 'name' => $product[0]['name'],
  97. 'product_id' => $product[0]['id'],
  98. 'auction_id' => $auction['id'],
  99. 'image'=> $product[0]['image'],
  100. 'price' => $product[0]['hanging_price'],
  101. 'frequency' => $auction['frequency']
  102. ]);
  103. AuctionProduct::where('id', $product[0]['id'])->update(['succeed_time' => strtotime('today')]);
  104. AuctionBooking::booking($user['uid'], $auction);
  105. UserBill::expend('预约认购', $user['uid'], 'anticipate','reduce_anticipate', $auction['anticipate'], 0, $user['anticipate'], '预约扣除广告值'); // 写入记录
  106. Db::commit();
  107. return app('json')->successful('认购成功');
  108. } catch (\Exception $e) {
  109. Db::rollback();
  110. return app('json')->fail('认购失败');
  111. }
  112. }
  113. /**
  114. * 进入场馆
  115. * @param Request $request
  116. * @return void
  117. */
  118. public function advance(Request $request)
  119. {
  120. $data = UtilService::getMore([
  121. ['id']
  122. ]);
  123. if (!$data['id']) return app('json')->fail('数据传入错误');
  124. $auction = Auction::find($data['id']);
  125. $booking = AuctionBooking::where([['auction_id', '=',$auction['id']], ['frequency', '=', $auction['frequency']]])->find();
  126. $radd_time = strtotime($auction['radd_time']) - 360; // 提前6分钟进场
  127. if (!$booking){
  128. return app('json')->fail('未预约');
  129. }
  130. if ($radd_time > time()){
  131. return app('json')->fail('未到进入时间');
  132. }
  133. if (strtotime($auction['rend_time']) < time()){
  134. return app('json')->fail('进场时间已过');
  135. }
  136. return app('json')->successful('可进入');
  137. }
  138. /**
  139. * 用户下级
  140. * @param Request $request
  141. * @return mixed
  142. */
  143. public function lower(Request $request){
  144. $data = UtilService::getMore([
  145. [['page', 'd'], 0],
  146. [['limit', 'd'], 0],
  147. ], $request);
  148. $user = User::where('spread_uid', $request->uid())->page($data['page'], $data['limit'])->field('uid,nickname,avatar')->order('uid DESC')->select();
  149. $user = empty($user) ? [] : $user->toArray();
  150. if ($user) {
  151. foreach ($user as $k => $v){
  152. $user[$k]['count'] = User::where('spread_uid',$v['uid'])->count(); // 粉丝人数
  153. $user[$k]['money'] = AuctionOrder::where('uid', $v['uid'])
  154. ->whereBetweenTime('create_time', date('Y-m-d 00:00:00'), date('Y-m-d 00:00:00', strtotime('+1 day')))
  155. ->where('status', 3)
  156. ->sum('price');
  157. }
  158. }
  159. $count = User::where('spread_uid', $request->uid())->count(); // 粉丝人数
  160. $active = User::where('spread_uid', $request->uid())->whereBetweenTime('last_time', date('Y-m-d H:i:s', strtotime('-1 week')), date('Y-m-d H:i:s'))->count(); // 活跃人数
  161. $money = AuctionOrder::where('uid', 'in', User::where('spread_uid', $request->uid())->column('uid'))
  162. ->where('status', 3)
  163. ->sum('price');
  164. $data = [
  165. 'user' => $user, // 下级用户
  166. 'count' => $count, // 下级用户人数
  167. 'active' => $active,// 活跃人数
  168. 'money' => $money // 总金额
  169. ];
  170. return app('json')->successful($data);
  171. }
  172. /**
  173. * 转广告值给下级
  174. * @param Request $request
  175. * @return mixed
  176. * @throws \think\db\exception\DataNotFoundException
  177. * @throws \think\db\exception\DbException
  178. * @throws \think\db\exception\ModelNotFoundException
  179. */
  180. public function transfer_accounts(Request $request){
  181. $data = UtilService::getMore([
  182. ['uid'],
  183. ['anticipate'],
  184. ['payment'],
  185. ['type']
  186. ], $request);
  187. if (!$data['uid'] or !$data['anticipate']) return app('json')->fail('数据传入错误');
  188. if (!$data['payment']) return app('json')->fail('请填写支付密码');
  189. $user = User::find($request->uid());
  190. $userList = User::select();
  191. $uid = get_downline($userList, $user['uid']);
  192. if ($user['payment'] != md5($data['payment'])) return app('json')->fail('支付密码错误');
  193. if (!in_array($data['uid'],$uid)) return app('json')->fail('该用户不是你下级');
  194. if ($data['anticipate'] < 10) return app('json')->fail('最少转账10');
  195. if ($data['type'] == 1){
  196. if ($user['anticipate'] < $data['anticipate']) return app('json')->fail('广告值不足');
  197. $me = User::find($data['uid']);
  198. $user['anticipate'] -= $data['anticipate'];// 扣除广告值
  199. $me['anticipate'] += $data['anticipate'];// 增加广告值
  200. $title = ['广告值减少', '广告值增加'];
  201. $category = 'anticipate';
  202. $type = ['zz_anticipate', 'js_anticipate'];
  203. $mark = '广告值';
  204. }elseif ($data['type'] == 2){
  205. //趣豆
  206. if ($user['integral'] < $data['anticipate']) return app('json')->fail('趣豆不足');
  207. $me = User::find($data['uid']);
  208. $user['integral'] -= $data['anticipate'];// 扣除趣豆
  209. $me['integral'] += $data['anticipate'];// 增加趣豆
  210. $title = ['趣豆减少', '趣豆增加'];
  211. $category = 'integral';
  212. $type = ['zz_integral', 'js_integral'];
  213. $mark = '趣豆';
  214. }elseif ($data['type'] == 3){
  215. //金豆
  216. if ($user['golden_bean'] < $data['anticipate']) return app('json')->fail('金豆不足');
  217. $me = User::find($data['uid']);
  218. $user['golden_bean'] -= $data['anticipate'];// 扣除金豆
  219. $me['golden_bean'] += $data['anticipate'];// 增加金豆
  220. $title = ['金豆减少', '金豆增加'];
  221. $category = 'golden_bean';
  222. $type = ['zz_golden_bean', 'js_golden_bean'];
  223. $mark = '金豆';
  224. }elseif ($data['type'] == 4){
  225. //购物券
  226. if ($user['shop_integral'] < $data['anticipate']) return app('json')->fail('金豆不足');
  227. $me = User::find($data['uid']);
  228. $user['shop_integral'] -= $data['anticipate'];// 扣除购物券
  229. $me['shop_integral'] += $data['anticipate'];// 增加购物券
  230. $title = ['购物券减少', '购物券增加'];
  231. $category = 'shop_integral';
  232. $type = ['zz_shop_integral', 'js_shop_integral'];
  233. $mark = '购物券';
  234. }
  235. try {
  236. Db::startTrans();
  237. UserBill::expend($title[0],$user['uid'], $category, $type[0], $data['anticipate'], 0, $user['anticipate'], '转账给('.$me['nickname'].'-'.$me['uid'].')'.$data['anticipate'].$mark);
  238. UserBill::income($title[1],$me['uid'], $category, $type[1], $data['anticipate'], 0, $me['anticipate'], '接收('.$user['nickname'].'-'.$user['uid'].')的'.$data['anticipate'].$mark);
  239. $user->save();
  240. $me->save();
  241. Db::commit();
  242. return app('json')->successful('成功');
  243. } catch (\Exception $e) {
  244. Db::rollback();
  245. return app('json')->fail('失败');
  246. }
  247. }
  248. /**
  249. * 会馆
  250. * @param Request $request
  251. * @return mixed
  252. * @throws \think\db\exception\DataNotFoundException
  253. * @throws \think\db\exception\DbException
  254. * @throws \think\db\exception\ModelNotFoundException
  255. */
  256. public function auction_gu(Request $request)
  257. {
  258. $data = UtilService::getMore([
  259. [['page', 'd'], 0],
  260. [['limit', 'd'], 0],
  261. ], $request);
  262. $uid = getParent($request->uid());
  263. $uid[] = $request->uid();
  264. $list = [];
  265. if ($uid){
  266. $list = AuctionGu::where('a.uid', 'in', $uid)
  267. ->where('a.status', 1)
  268. ->alias('a')
  269. ->field('a.*,u.nickname,u.avatar')
  270. ->leftJoin('user u', 'a.uid = u.uid')
  271. ->order('sort DESC')
  272. ->page($data['page'], $data['limit'])
  273. ->select();
  274. }
  275. $list = !empty($list) ? $list->toArray() : [];
  276. return app('json')->successful($list);
  277. }
  278. /**
  279. * 用户管理会馆
  280. * @param Request $request
  281. * @return mixed
  282. */
  283. public function user_gu(Request $request)
  284. {
  285. $data = UtilService::getMore([
  286. [['page', 'd'], 0],
  287. [['limit', 'd'], 0],
  288. ], $request);
  289. $list = AuctionGu::where('uid', $request->uid())->page($data['page'], $data['limit'])->select();
  290. $list = count($list) ? $list->toArray() : [];
  291. return app('json')->successful($list);
  292. }
  293. /**
  294. * 添加收款方式
  295. * @param Request $request
  296. * @return void
  297. */
  298. public function pay(Request $request)
  299. {
  300. $data = UtilService::postMore([
  301. ['payment'],
  302. ['image'],
  303. ['bank'],
  304. ['name'],
  305. ['type'],
  306. ['phone'],
  307. ['bank_name']
  308. ], $request);
  309. if (!$data['type']) return app('json')->fail('数据传入错误');
  310. $data['uid'] =$request->uid();
  311. $model = new AuctionPay();
  312. $pay = $model->where([['uid', '=', $request->uid()], ['type', '=', $data['type']]])->find();
  313. $res = Validate::rule([
  314. 'phone' => 'mobile'
  315. ]);
  316. $res->message([
  317. 'phone.mobile' => '请填写正确手机格式'
  318. ]);
  319. if (!$res->check($data)){
  320. return app('json')->fail($res->getError());
  321. }
  322. if (!empty($pay)){
  323. if ($data['type'] == 1 ){
  324. // 微信收款方式
  325. if (!$data['payment']) return app('json')->fail('微信账号不能为空');
  326. if (!$data['image']) return app('json')->fail('二维码不能为空');
  327. if (!$data['name']) return app('json')->fail('姓名不能为空');
  328. if (!$data['phone']) return app('json')->fail('请填写手机号');
  329. $pay['payment'] = $data['payment'];
  330. $pay['image'] = $data['image'];
  331. $pay['name'] = $data['name'];
  332. $pay['phone'] = $data['phone'];
  333. }elseif ($data['type'] == 2){
  334. // 支付宝收款方式
  335. if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
  336. if (!$data['name']) return app('json')->fail('姓名不能为空');
  337. if (!$data['phone']) return app('json')->fail('请填写手机号');
  338. $pay['payment'] = $data['payment'];
  339. $pay['name'] = $data['name'];
  340. $pay['phone'] = $data['phone'];
  341. }elseif ($data['type'] == 3){
  342. // 银行卡收款方式
  343. if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
  344. if (!$data['name']) return app('json')->fail('姓名不能为空');
  345. if (!$data['bank']) return app('json')->fail('开户行不能为空');
  346. if (!$data['bank_name']) return app('json')->fail('开户支行不能为空');
  347. if (!$data['phone']) return app('json')->fail('请填写手机号');
  348. $pay['payment'] = $data['payment'];
  349. $pay['bank'] = $data['bank'];
  350. $pay['bank_name'] = $data['bank_name'];
  351. $pay['phone'] = $data['phone'];
  352. $pay['name'] = $data['name'];
  353. }
  354. $res = $pay->save();
  355. if ($res) return app('json')->successful('修改成功');
  356. return app('json')->fail('修改失败');
  357. }else{
  358. if ($data['type'] == 1 ){
  359. // 微信收款方式
  360. if (!$data['payment']) return app('json')->fail('微信账号不能为空');
  361. if (!$data['image']) return app('json')->fail('二维码不能为空');
  362. if (!$data['name']) return app('json')->fail('姓名不能为空');
  363. if (!$data['phone']) return app('json')->fail('请填写手机号');
  364. }elseif ($data['type'] == 2){
  365. // 支付宝收款方式
  366. if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
  367. if (!$data['name']) return app('json')->fail('姓名不能为空');
  368. if (!$data['phone']) return app('json')->fail('请填写手机号');
  369. }elseif ($data['type'] == 3){
  370. // 银行卡收款方式
  371. if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
  372. if (!$data['name']) return app('json')->fail('姓名不能为空');
  373. if (!$data['bank']) return app('json')->fail('开户行不能为空');
  374. if (!$data['bank_name']) return app('json')->fail('开户支行不能为空');
  375. if (!$data['phone']) return app('json')->fail('请填写手机号');
  376. }
  377. $res = $model->save($data);
  378. if ($res) return app('json')->successful('添加成功');
  379. return app('json')->fail('添加失败');
  380. }
  381. }
  382. /**
  383. * 收款方式详情
  384. * @param Request $request
  385. * @return mixed
  386. * @throws \think\db\exception\DataNotFoundException
  387. * @throws \think\db\exception\DbException
  388. * @throws \think\db\exception\ModelNotFoundException
  389. */
  390. public function pay_list(Request $request)
  391. {
  392. $model = new AuctionPay();
  393. $list = $model->where('uid', $request->uid())->select();
  394. $list = empty($list)? []: $list->toArray();
  395. $data['wx'] = [];
  396. $data['zfb'] = [];
  397. $data['bank'] = [];
  398. foreach ($list as $k => $v){
  399. if ($v['type'] == 1){
  400. $data['wx'] = $v;
  401. }elseif ($v['type'] == 2){
  402. $data['zfb'] = $v;
  403. }elseif ($v['type'] == 3){
  404. $data['bank'] = $v;
  405. }
  406. }
  407. return app('json')->successful($data);
  408. }
  409. /**
  410. * 馆长申请
  411. * @param Request $request
  412. * @return mixed
  413. * @throws \think\db\exception\DataNotFoundException
  414. * @throws \think\db\exception\DbException
  415. * @throws \think\db\exception\ModelNotFoundException
  416. */
  417. public function apply(Request $request)
  418. {
  419. $data = AuctionApply::where('status', '>', 0)->where('uid', $request->uid())->find();
  420. if (!$data){
  421. $res = AuctionApply::create([
  422. 'uid' => $request->uid()
  423. ]);
  424. if ($res)return app('json')->successful('申请成功');
  425. return app('json')->fail('申请失败');
  426. }
  427. return app('json')->fail('已申请,请等待管理员操作');
  428. }
  429. /**
  430. * 馆长申请
  431. * @param Request $request
  432. * @return void
  433. * @throws \think\db\exception\DataNotFoundException
  434. * @throws \think\db\exception\DbException
  435. * @throws \think\db\exception\ModelNotFoundException
  436. */
  437. public function apply_status(Request $request)
  438. {
  439. $data = AuctionApply::where('uid', $request->uid())->find();
  440. if (!$data){
  441. return app('json')->success(['status' => 0, 'str' => '未提交申请']); // 未提交申请
  442. }
  443. $data = AuctionApply::where([['status', '=', 1], ['uid', '=', $request->uid()]])->find();
  444. if ($data){
  445. return app('json')->success(['status' => 1, 'str' => '待审核']);// 待审核
  446. }
  447. $data = AuctionApply::where([['status', '=', 2], ['uid', '=', $request->uid()]])->find();
  448. if ($data){
  449. return app('json')->success(['status' => 2 ,'str' => '已完成']);// 已完成
  450. }
  451. $data = AuctionApply::where([['status', '=', 0], ['uid', '=', $request->uid()]])->find();
  452. if ($data){
  453. return app('json')->success(['status' => 3, 'str' => '失败']);// 失败
  454. }
  455. }
  456. /**
  457. * 倒计时
  458. * @param Request $request
  459. * @return mixed
  460. * @throws \think\db\exception\DataNotFoundException
  461. * @throws \think\db\exception\DbException
  462. * @throws \think\db\exception\ModelNotFoundException
  463. */
  464. public function count_down(Request $request)
  465. {
  466. $data = UtilService::postMore([
  467. ['id'],
  468. ], $request);
  469. $list = Auction::where('id', $data['id'])->find();
  470. if (!$list) return app('json')->fail('场次不存在');// 失败
  471. $user = $request->user();
  472. $time = strtotime(date('Y-m-d', time()));// 今天
  473. $today = strtotime(date('Y-m-d', strtotime('+1day')));// 明天
  474. $datas = [];
  475. $datas['time'] = strtotime($list['radd_time']) - 60;
  476. $datas['times'] = strtotime($list['radd_time']);
  477. return app('json')->success($datas);// 失败
  478. }
  479. /**
  480. * 金豆兑换
  481. * @param Request $request
  482. * @return mixed
  483. * @throws \think\db\exception\DataNotFoundException
  484. * @throws \think\db\exception\DbException
  485. * @throws \think\db\exception\ModelNotFoundException
  486. */
  487. public function exchange_golden(Request $request)
  488. {
  489. $data = UtilService::postMore([
  490. ['price'],
  491. ['type']
  492. ], $request);
  493. if (empty($data['price'])) return app('json')->fail('传入兑换数量');
  494. if (empty($data['type'])) return app('json')->fail('传入兑换类型');
  495. $user = User::where('uid', $request->uid())->find();
  496. if ($data['type'] == 1){
  497. // 金豆兑换广告值
  498. $proportion = 2;
  499. $price = $data['price'] * $proportion;
  500. if ($user['golden_bean'] < $price) return app('json')->fail('金豆不足');
  501. $user['golden_bean'] -= $price;
  502. $user['anticipate'] += $data['price'];
  503. $user->save();
  504. UserBill::expend('金豆兑换广告值', $request->uid(), 'golden_bean', 'dh_golden_bean', $price, '', $user['golden_bean'], '金豆兑换'.$data['price'].'广告值');
  505. UserBill::income('广告值兑换', $request->uid(), 'anticipate', 'dh_anticipate', $data['price'], '', $user['anticipate'], '金豆兑换'.$data['price'].'广告值');
  506. }elseif ($data['type'] == 2) {
  507. // 金豆兑换趣豆
  508. $proportion = sys_config('integral');
  509. $price = $data['price'] * $proportion;
  510. if ($user['golden_bean'] < $price) return app('json')->fail('金豆不足');
  511. $user['golden_bean'] -= $price;
  512. $user['integral'] += $data['price'];
  513. $user->save();
  514. UserBill::expend('金豆兑换趣豆', $request->uid(), 'golden_bean', 'dh_golden_bean', $price, '', $user['golden_bean'], '金豆兑换'.$data['price'].'趣豆');
  515. UserBill::income('趣豆兑换', $request->uid(), 'integral', 'dh_integral', $data['price'], '', $user['integral'], '金豆兑换'.$data['price'].'趣豆');
  516. }else{
  517. return app('json')->fail('兑换类型不存在');
  518. }
  519. return app('json')->success('兑换成功');
  520. }
  521. }