AuctionController.php 23 KB

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