AuctionController.php 22 KB

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