AuctionController.php 18 KB

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