AuctionController.php 19 KB

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