AuctionController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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\user\User;
  10. use app\models\user\UserBill;
  11. use app\Request;
  12. use Monolog\Handler\Curl\Util;
  13. use think\facade\Cache;
  14. use crmeb\services\{
  15. CacheService,
  16. ExpressService,
  17. SystemConfigService
  18. };
  19. use crmeb\services\UtilService;
  20. use crmeb\repositories\OrderRepository;
  21. use think\facade\Db;
  22. use think\facade\Validate;
  23. class AuctionController
  24. {
  25. /**
  26. * 场馆列表
  27. * @param Request $request
  28. * @return mixed
  29. */
  30. public function list(Request $request)
  31. {
  32. $data = UtilService::getMore([
  33. [['page', 'd'], 0],
  34. [['limit', 'd'], 0],
  35. ['advance'],
  36. ['auction_gu_id']
  37. ], $request);
  38. if (!$data['auction_gu_id']) return app('json')->fail('数据传入错误');
  39. $auctionModel = new \app\models\auction\Auction();
  40. return app('json')->successful($auctionModel->list($data, $request->uid()));
  41. }
  42. /**
  43. * 预约场馆
  44. * @param Request $request
  45. * @return void
  46. */
  47. public function subscribe(Request $request)
  48. {
  49. $data = UtilService::postMore([
  50. ['id']
  51. ]);
  52. if (!$data['id']) return app('json')->fail('数据传入错误');
  53. $auctionModel = new Auction();
  54. $auction = $auctionModel->find($data['id']);
  55. if (!$auction)return app('json')->fail('没有此数据');
  56. if (time() < strtotime($auction['add_time'])){
  57. return app('json')->fail('预约未开始');
  58. }
  59. if (time() > strtotime($auction['end_time'])){
  60. return app('json')->fail('预约时间已过');
  61. }
  62. if (AuctionBooking::where([['uid', '=', $request->uid()], ['auction_id' , '=', $auction['id']], ['frequency', '=', $auction['frequency']]])->find()){
  63. return app('json')->fail('当前场馆已预约');
  64. }
  65. $userModel = new User();
  66. $user = $userModel->find($request->uid());
  67. if ($user['anticipate'] < $auction['anticipate']) return app('json')->fail('预约卷不够');
  68. $user['anticipate'] = $user['anticipate'] - $auction['anticipate'];// 扣除预约卷
  69. User::rollbackTrans();
  70. $res = $user->save();
  71. if ($res){
  72. AuctionBooking::booking($user['uid'], $auction);
  73. UserBill::expend('预约场馆', $user['uid'], 'anticipate','reduce_anticipate', $auction['anticipate'], 0, $user['anticipate'], '预约扣除艺金券'); // 写入记录
  74. User::commitTrans();
  75. return app('json')->successful('预约成功');
  76. }else{
  77. User::rollbackTrans();
  78. return app('json')->fail('预约失败');
  79. }
  80. }
  81. /**
  82. * 进入场馆
  83. * @param Request $request
  84. * @return void
  85. */
  86. public function advance(Request $request)
  87. {
  88. $data = UtilService::getMore([
  89. ['id']
  90. ]);
  91. if (!$data['id']) return app('json')->fail('数据传入错误');
  92. $auction = Auction::find($data['id']);
  93. $booking = AuctionBooking::where([['auction_id', '=',$auction['id']], ['frequency', '=', $auction['frequency']]])->find();
  94. $radd_time = strtotime($auction['radd_time']) - 360; // 提前五分钟进场
  95. if (!$booking){
  96. return app('json')->fail('未预约');
  97. }
  98. if ($radd_time > time()){
  99. return app('json')->fail('未到进入时间');
  100. }
  101. if (strtotime($auction['rend_time']) < time()){
  102. return app('json')->fail('进场时间已过');
  103. }
  104. return app('json')->successful('可进入');
  105. }
  106. /**
  107. * 用户下级
  108. * @param Request $request
  109. * @return mixed
  110. */
  111. public function lower(Request $request){
  112. $data = UtilService::getMore([
  113. [['page', 'd'], 0],
  114. [['limit', 'd'], 0],
  115. ], $request);
  116. $user = User::where('spread_uid', $request->uid())->page($data['page'], $data['limit'])->field('uid,nickname,avatar')->order('uid DESC')->select();
  117. $user = empty($user) ? [] : $user->toArray();
  118. if ($user) {
  119. foreach ($user as $k => $v){
  120. $user[$k]['count'] = User::where('spread_uid',$v['uid'])->count(); // 粉丝人数
  121. $user[$k]['money'] = AuctionOrder::where('uid', $v['uid'])
  122. ->whereBetweenTime('create_time', date('Y-m-d 00:00:00'), date('Y-m-d 00:00:00', strtotime('+1 day')))
  123. ->where('status', 3)
  124. ->sum('price');
  125. }
  126. }
  127. $count = User::where('spread_uid', $request->uid())->count(); // 粉丝人数
  128. $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(); // 活跃人数
  129. $money = AuctionOrder::where('uid', 'in', User::where('spread_uid', $request->uid())->column('uid'))
  130. ->where('status', 3)
  131. ->sum('price');
  132. $data = [
  133. 'user' => $user, // 下级用户
  134. 'count' => $count, // 下级用户人数
  135. 'active' => $active,// 活跃人数
  136. 'money' => $money // 总金额
  137. ];
  138. return app('json')->successful($data);
  139. }
  140. /**
  141. * 转预约卷给下级
  142. * @param Request $request
  143. * @return mixed
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. */
  148. public function transfer_accounts(Request $request){
  149. $data = UtilService::getMore([
  150. ['uid'],
  151. ['anticipate']
  152. ], $request);
  153. if (!$data['uid'] or !$data['anticipate']) return app('json')->fail('数据传入错误');
  154. $user = User::find($request->uid());
  155. if ($user['anticipate'] < $data['anticipate']) return app('json')->fail('预约卷不够');
  156. $me = User::find($data['uid']);
  157. $user['anticipate'] = $user['anticipate'] - $data['anticipate'];// 扣除预约卷
  158. $me['anticipate'] = $me['anticipate'] + $data['anticipate'];// 增加预约卷
  159. try {
  160. Db::startTrans();
  161. UserBill::expend('预约卷减少',$user['uid'], 'anticipate', 'reduce_anticipate', $data['anticipate'], 0, $user['anticipate'], '转账给用户'.$me['nickname'].$data['anticipate'].'预约卷');
  162. UserBill::income('预约卷增加',$me['uid'], 'anticipate', 'add_anticipate', $data['anticipate'], 0, $me['anticipate'], $me['nickname'].'转账'.$data['anticipate'].'预约卷');
  163. $user->save();
  164. $me->save();
  165. Db::commit();
  166. return app('json')->successful('成功');
  167. } catch (\Exception $e) {
  168. Db::rollback();
  169. return app('json')->fail('失败');
  170. }
  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 auction_gu(Request $request)
  181. {
  182. $data = UtilService::getMore([
  183. [['page', 'd'], 0],
  184. [['limit', 'd'], 0],
  185. ], $request);
  186. $uid = getParent($request->uid());
  187. $uid[] = $request->uid();
  188. $list = [];
  189. if ($uid){
  190. $list = AuctionGu::where('a.uid', 'in', $uid)
  191. ->where('a.status', 1)
  192. ->alias('a')
  193. ->field('a.*,u.nickname,u.avatar')
  194. ->leftJoin('user u', 'a.uid = u.uid')
  195. ->page($data['page'], $data['limit'])
  196. ->select();
  197. }
  198. $list = !empty($list) ? $list->toArray() : [];
  199. return app('json')->successful($list);
  200. }
  201. /**
  202. * 用户管理会馆
  203. * @param Request $request
  204. * @return mixed
  205. */
  206. public function user_gu(Request $request)
  207. {
  208. $data = UtilService::getMore([
  209. [['page', 'd'], 0],
  210. [['limit', 'd'], 0],
  211. ], $request);
  212. $list = AuctionGu::where('uid', $request->uid())->page($data['page'], $data['limit'])->select();
  213. $list = count($list) ? $list->toArray() : [];
  214. return app('json')->successful($list);
  215. }
  216. /**
  217. * 添加收款方式
  218. * @param Request $request
  219. * @return void
  220. */
  221. public function pay(Request $request)
  222. {
  223. $data = UtilService::postMore([
  224. ['payment'],
  225. ['image'],
  226. ['bank'],
  227. ['name'],
  228. ['type'],
  229. ['phone']
  230. ], $request);
  231. if (!$data['type']) return app('json')->fail('数据传入错误');
  232. $data['uid'] =$request->uid();
  233. $model = new AuctionPay();
  234. $pay = $model->where([['uid', '=', $request->uid()], ['type', '=', $data['type']]])->find();
  235. $res = Validate::rule([
  236. 'phone' => 'mobile'
  237. ]);
  238. $res->message([
  239. 'phone.mobile' => '请填写正确手机格式'
  240. ]);
  241. if (!$res->check($data)){
  242. return app('json')->fail($res->getError());
  243. }
  244. if (!empty($pay)){
  245. if ($data['type'] == 1 ){
  246. // 微信收款方式
  247. if (!$data['payment']) return app('json')->fail('微信账号不能为空');
  248. if (!$data['image']) return app('json')->fail('二维码不能为空');
  249. if (!$data['name']) return app('json')->fail('姓名不能为空');
  250. if (!$data['phone']) return app('json')->fail('请填写手机号');
  251. $pay['payment'] = $data['payment'];
  252. $pay['image'] = $data['image'];
  253. $pay['name'] = $data['name'];
  254. $pay['name'] = $data['name'];
  255. }elseif ($data['type'] == 2){
  256. // 支付宝收款方式
  257. if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
  258. if (!$data['name']) return app('json')->fail('姓名不能为空');
  259. if (!$data['phone']) return app('json')->fail('请填写手机号');
  260. $pay['payment'] = $data['payment'];
  261. $pay['name'] = $data['name'];
  262. $pay['name'] = $data['name'];
  263. }elseif ($data['type'] == 3){
  264. // 银行卡收款方式
  265. if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
  266. if (!$data['name']) return app('json')->fail('姓名不能为空');
  267. if (!$data['bank']) return app('json')->fail('开户行');
  268. if (!$data['phone']) return app('json')->fail('请填写手机号');
  269. $pay['payment'] = $data['payment'];
  270. $pay['image'] = $data['image'];
  271. $pay['bank'] = $data['name'];
  272. $pay['phone'] = $data['phone'];
  273. }
  274. $res = $pay->save();
  275. if ($res) return app('json')->successful('修改成功');
  276. return app('json')->fail('修改失败');
  277. }else{
  278. if ($data['type'] == 1 ){
  279. // 微信收款方式
  280. if (!$data['payment']) return app('json')->fail('微信账号不能为空');
  281. if (!$data['image']) return app('json')->fail('二维码不能为空');
  282. if (!$data['name']) return app('json')->fail('姓名不能为空');
  283. if (!$data['phone']) return app('json')->fail('请填写手机号');
  284. }elseif ($data['type'] == 2){
  285. // 支付宝收款方式
  286. if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
  287. if (!$data['name']) return app('json')->fail('姓名不能为空');
  288. if (!$data['phone']) return app('json')->fail('请填写手机号');
  289. }elseif ($data['type'] == 3){
  290. // 银行卡收款方式
  291. if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
  292. if (!$data['name']) return app('json')->fail('姓名不能为空');
  293. if (!$data['bank']) return app('json')->fail('开户行不能为空');
  294. if (!$data['phone']) return app('json')->fail('请填写手机号');
  295. }
  296. $res = $model->save($data);
  297. if ($res) return app('json')->successful('添加成功');
  298. return app('json')->fail('添加失败');
  299. }
  300. }
  301. /**
  302. * 收款方式详情
  303. * @param Request $request
  304. * @return mixed
  305. * @throws \think\db\exception\DataNotFoundException
  306. * @throws \think\db\exception\DbException
  307. * @throws \think\db\exception\ModelNotFoundException
  308. */
  309. public function pay_list(Request $request)
  310. {
  311. $model = new AuctionPay();
  312. $list = $model->where('uid', $request->uid())->select();
  313. $list = empty($list)? []: $list->toArray();
  314. $data['wx'] = [];
  315. $data['zfb'] = [];
  316. $data['bank'] = [];
  317. foreach ($list as $k => $v){
  318. if ($v['type'] == 1){
  319. $data['wx'] = $v;
  320. }elseif ($v['type'] == 2){
  321. $data['zfb'] = $v;
  322. }elseif ($v['type'] == 3){
  323. $data['bank'] = $v;
  324. }
  325. }
  326. return app('json')->successful($data);
  327. }
  328. /**
  329. * 馆长申请
  330. * @param Request $request
  331. * @return mixed
  332. * @throws \think\db\exception\DataNotFoundException
  333. * @throws \think\db\exception\DbException
  334. * @throws \think\db\exception\ModelNotFoundException
  335. */
  336. public function apply(Request $request)
  337. {
  338. $data = AuctionApply::where('status', '>', 0)->where('uid', $request->uid())->find();
  339. if (!$data){
  340. $res = AuctionApply::create([
  341. 'uid' => $request->uid()
  342. ]);
  343. if ($res)return app('json')->successful('申请成功');
  344. return app('json')->fail('申请失败');
  345. }
  346. return app('json')->fail('已申请,请等待管理员操作');
  347. }
  348. /**
  349. * 馆长申请
  350. * @param Request $request
  351. * @return void
  352. * @throws \think\db\exception\DataNotFoundException
  353. * @throws \think\db\exception\DbException
  354. * @throws \think\db\exception\ModelNotFoundException
  355. */
  356. public function apply_status(Request $request)
  357. {
  358. $data = AuctionApply::where('uid', $request->uid())->find();
  359. if (!$data){
  360. return app('json')->success(['status' => 0, 'str' => '未提交申请']); // 未提交申请
  361. }
  362. $data = AuctionApply::where([['status', '=', 1], ['uid', '=', $request->uid()]])->find();
  363. if ($data){
  364. return app('json')->success(['status' => 1, 'str' => '待审核']);// 待审核
  365. }
  366. $data = AuctionApply::where([['status', '=', 2], ['uid', '=', $request->uid()]])->find();
  367. if ($data){
  368. return app('json')->success(['status' => 2 ,'str' => '已完成']);// 已完成
  369. }
  370. $data = AuctionApply::where([['status', '=', 0], ['uid', '=', $request->uid()]])->find();
  371. if ($data){
  372. return app('json')->success(['status' => 3, 'str' => '失败']);// 失败
  373. }
  374. }
  375. /**
  376. * 倒计时
  377. * @param Request $request
  378. * @return mixed
  379. * @throws \think\db\exception\DataNotFoundException
  380. * @throws \think\db\exception\DbException
  381. * @throws \think\db\exception\ModelNotFoundException
  382. */
  383. public function count_down(Request $request)
  384. {
  385. $data = UtilService::postMore([
  386. ['id'],
  387. ], $request);
  388. $list = Auction::where('id', $data['id'])->find();
  389. if (!$list) return app('json')->fail('场次不存在');// 失败
  390. $user = $request->user();
  391. $time = strtotime(date('Y-m-d', time()));// 今天
  392. $today = strtotime(date('Y-m-d', strtotime('+1day')));// 明天
  393. $datas = [];
  394. if ($user['is_new'] == 1 or ($user['green_time'] >= $time and $user['green_time'] <= $today)){
  395. $datas['time'] = strtotime($list['radd_time']) - 180;
  396. }else{
  397. $datas['time'] = strtotime($list['radd_time']);
  398. }
  399. return app('json')->success($datas);// 失败
  400. }
  401. }