AuctionController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. $user = $request->user();
  95. $time = strtotime(date('Y-m-d', time()));// 今天
  96. $today = strtotime(date('Y-m-d', strtotime('+1day')));// 明天
  97. if ($user['is_new'] == 1 or ($user['green_time'] >= $time and $user['green_time'] <= $today)){
  98. // 新人或者绿色通道提前三分钟入场
  99. if (strtotime($auction['radd_time']) -3000 > time()){
  100. return app('json')->fail('未到进入时间');
  101. }
  102. if (strtotime($auction['rend_time']) < time()){
  103. return app('json')->fail('进场时间已过');
  104. }
  105. }else{
  106. if (strtotime($auction['radd_time']) > time()){
  107. return app('json')->fail('未到进入时间');
  108. }
  109. if (strtotime($auction['rend_time']) < time()){
  110. return app('json')->fail('进场时间已过');
  111. }
  112. }
  113. if ($booking){
  114. return app('json')->successful('可进入');
  115. }else{
  116. return app('json')->fail('未预约');
  117. }
  118. }
  119. /**
  120. * 用户下级
  121. * @param Request $request
  122. * @return mixed
  123. */
  124. public function lower(Request $request){
  125. $data = UtilService::getMore([
  126. [['page', 'd'], 0],
  127. [['limit', 'd'], 0],
  128. ], $request);
  129. $user = User::where('spread_uid', $request->uid())->page($data['page'], $data['limit'])->field('uid,nickname,avatar')->order('uid DESC')->select();
  130. $user = empty($user) ? [] : $user->toArray();
  131. if ($user) {
  132. foreach ($user as $k => $v){
  133. $user[$k]['count'] = User::where('spread_uid',$v['uid'])->count(); // 粉丝人数
  134. $user[$k]['money'] = AuctionOrder::where('uid', $v['uid'])
  135. ->whereBetweenTime('create_time', date('Y-m-d 00:00:00'), date('Y-m-d 00:00:00', strtotime('+1 day')))
  136. ->where('status', 3)
  137. ->sum('price');
  138. }
  139. }
  140. $count = User::where('spread_uid', $request->uid())->count(); // 粉丝人数
  141. $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(); // 活跃人数
  142. $money = AuctionOrder::where('uid', 'in', User::where('spread_uid', $request->uid())->column('uid'))
  143. ->where('status', 3)
  144. ->sum('price');
  145. $data = [
  146. 'user' => $user, // 下级用户
  147. 'count' => $count, // 下级用户人数
  148. 'active' => $active,// 活跃人数
  149. 'money' => $money // 总金额
  150. ];
  151. return app('json')->successful($data);
  152. }
  153. /**
  154. * 转预约卷给下级
  155. * @param Request $request
  156. * @return mixed
  157. * @throws \think\db\exception\DataNotFoundException
  158. * @throws \think\db\exception\DbException
  159. * @throws \think\db\exception\ModelNotFoundException
  160. */
  161. public function transfer_accounts(Request $request){
  162. $data = UtilService::getMore([
  163. ['uid'],
  164. ['anticipate']
  165. ], $request);
  166. if (!$data['uid'] or !$data['anticipate']) return app('json')->fail('数据传入错误');
  167. $user = User::find($request->uid());
  168. if ($user['anticipate'] < $data['anticipate']) return app('json')->fail('预约卷不够');
  169. $me = User::find($data['uid']);
  170. $user['anticipate'] = $user['anticipate'] - $data['anticipate'];// 扣除预约卷
  171. $me['anticipate'] = $me['anticipate'] + $data['anticipate'];// 增加预约卷
  172. try {
  173. Db::startTrans();
  174. UserBill::expend('预约卷减少',$user['uid'], 'anticipate', 'reduce_anticipate', $data['anticipate'], 0, $user['anticipate'], '转账给用户'.$me['nickname'].$data['anticipate'].'预约卷');
  175. UserBill::income('预约卷增加',$me['uid'], 'anticipate', 'add_anticipate', $data['anticipate'], 0, $me['anticipate'], $me['nickname'].'转账'.$data['anticipate'].'预约卷');
  176. $user->save();
  177. $me->save();
  178. Db::commit();
  179. return app('json')->successful('成功');
  180. } catch (\Exception $e) {
  181. Db::rollback();
  182. return app('json')->fail('失败');
  183. }
  184. }
  185. /**
  186. * 会馆
  187. * @param Request $request
  188. * @return mixed
  189. * @throws \think\db\exception\DataNotFoundException
  190. * @throws \think\db\exception\DbException
  191. * @throws \think\db\exception\ModelNotFoundException
  192. */
  193. public function auction_gu(Request $request)
  194. {
  195. $data = UtilService::getMore([
  196. [['page', 'd'], 0],
  197. [['limit', 'd'], 0],
  198. ], $request);
  199. $uid = getParent($request->uid());
  200. $uid[] = $request->uid();
  201. $list = AuctionGu::where('a.uid', 'in', $uid)
  202. ->alias('a')
  203. ->field('a.*,u.nickname,u.avatar')
  204. ->leftJoin('user u', 'a.uid = u.uid')
  205. ->page($data['page'], $data['limit'])
  206. ->select();
  207. $list = count($list) ? $list->toArray() : [];
  208. return app('json')->successful($list);
  209. }
  210. /**
  211. * 用户管理会馆
  212. * @param Request $request
  213. * @return mixed
  214. */
  215. public function user_gu(Request $request)
  216. {
  217. $data = UtilService::getMore([
  218. [['page', 'd'], 0],
  219. [['limit', 'd'], 0],
  220. ], $request);
  221. $list = AuctionGu::where('uid', $request->uid())->page($data['page'], $data['limit'])->select();
  222. $list = count($list) ? $list->toArray() : [];
  223. return app('json')->successful($list);
  224. }
  225. /**
  226. * 添加收款方式
  227. * @param Request $request
  228. * @return void
  229. */
  230. public function pay(Request $request)
  231. {
  232. $data = UtilService::postMore([
  233. ['payment'],
  234. ['image'],
  235. ['bank'],
  236. ['name'],
  237. ['type'],
  238. ['phone']
  239. ], $request);
  240. if (!$data['type']) return app('json')->fail('数据传入错误');
  241. $data['uid'] =$request->uid();
  242. $model = new AuctionPay();
  243. $pay = $model->where([['uid', '=', $request->uid()], ['type', '=', $data['type']]])->find();
  244. $res = Validate::rule([
  245. 'phone' => 'mobile'
  246. ]);
  247. $res->message([
  248. 'phone.mobile' => '请填写正确手机格式'
  249. ]);
  250. if (!$res->check($data)){
  251. return app('json')->fail($res->getError());
  252. }
  253. if (!empty($pay)){
  254. if ($data['type'] == 1 ){
  255. // 微信收款方式
  256. if (!$data['payment']) return app('json')->fail('微信账号不能为空');
  257. if (!$data['image']) 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['image'] = $data['image'];
  262. $pay['name'] = $data['name'];
  263. $pay['name'] = $data['name'];
  264. }elseif ($data['type'] == 2){
  265. // 支付宝收款方式
  266. if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
  267. if (!$data['name']) return app('json')->fail('姓名不能为空');
  268. if (!$data['phone']) return app('json')->fail('请填写手机号');
  269. $pay['payment'] = $data['payment'];
  270. $pay['name'] = $data['name'];
  271. $pay['name'] = $data['name'];
  272. }elseif ($data['type'] == 3){
  273. // 银行卡收款方式
  274. if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
  275. if (!$data['name']) return app('json')->fail('姓名不能为空');
  276. if (!$data['bank']) return app('json')->fail('开户行');
  277. if (!$data['phone']) return app('json')->fail('请填写手机号');
  278. $pay['payment'] = $data['payment'];
  279. $pay['image'] = $data['image'];
  280. $pay['bank'] = $data['name'];
  281. $pay['phone'] = $data['phone'];
  282. }
  283. $res = $pay->save();
  284. if ($res) return app('json')->successful('修改成功');
  285. return app('json')->fail('修改失败');
  286. }else{
  287. if ($data['type'] == 1 ){
  288. // 微信收款方式
  289. if (!$data['payment']) return app('json')->fail('微信账号不能为空');
  290. if (!$data['image']) return app('json')->fail('二维码不能为空');
  291. if (!$data['name']) return app('json')->fail('姓名不能为空');
  292. if (!$data['phone']) return app('json')->fail('请填写手机号');
  293. }elseif ($data['type'] == 2){
  294. // 支付宝收款方式
  295. if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
  296. if (!$data['name']) return app('json')->fail('姓名不能为空');
  297. if (!$data['phone']) return app('json')->fail('请填写手机号');
  298. }elseif ($data['type'] == 3){
  299. // 银行卡收款方式
  300. if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
  301. if (!$data['name']) return app('json')->fail('姓名不能为空');
  302. if (!$data['bank']) return app('json')->fail('开户行不能为空');
  303. if (!$data['phone']) return app('json')->fail('请填写手机号');
  304. }
  305. $res = $model->save($data);
  306. if ($res) return app('json')->successful('添加成功');
  307. return app('json')->fail('添加失败');
  308. }
  309. }
  310. /**
  311. * 收款方式详情
  312. * @param Request $request
  313. * @return mixed
  314. * @throws \think\db\exception\DataNotFoundException
  315. * @throws \think\db\exception\DbException
  316. * @throws \think\db\exception\ModelNotFoundException
  317. */
  318. public function pay_list(Request $request)
  319. {
  320. $model = new AuctionPay();
  321. $list = $model->where('uid', $request->uid())->select();
  322. $list = empty($list)? []: $list->toArray();
  323. $data['wx'] = [];
  324. $data['zfb'] = [];
  325. $data['bank'] = [];
  326. foreach ($list as $k => $v){
  327. if ($v['type'] == 1){
  328. $data['wx'] = $v;
  329. }elseif ($v['type'] == 2){
  330. $data['zfb'] = $v;
  331. }elseif ($v['type'] == 3){
  332. $data['bank'] = $v;
  333. }
  334. }
  335. return app('json')->successful($data);
  336. }
  337. /**
  338. * 馆长申请
  339. * @param Request $request
  340. * @return mixed
  341. * @throws \think\db\exception\DataNotFoundException
  342. * @throws \think\db\exception\DbException
  343. * @throws \think\db\exception\ModelNotFoundException
  344. */
  345. public function apply(Request $request)
  346. {
  347. $data = AuctionApply::where('status', '>', 0)->where('uid', $request->uid())->find();
  348. if (!$data){
  349. $res = AuctionApply::create([
  350. 'uid' => $request->uid()
  351. ]);
  352. if ($res)return app('json')->successful('申请成功');
  353. return app('json')->fail('申请失败');
  354. }
  355. return app('json')->fail('已申请,请等待管理员操作');
  356. }
  357. public function apply_status(Request $request)
  358. {
  359. $data = AuctionApply::where('uid', $request->uid())->find();
  360. if (!$data){
  361. return app('json')->successful(['status' => 0, 'str' => '未提交申请']); // 未提交申请
  362. }
  363. $data = AuctionApply::where([['status', '=', 1], ['uid', '=', $request->uid()]])->find();
  364. if ($data){
  365. return app('json')->successful(['status' => 1, 'str' => '待审核']);// 待审核
  366. }
  367. $data = AuctionApply::where([['status', '=', 2], ['uid', '=', $request->uid()]])->find();
  368. if ($data){
  369. return app('json')->successful(['status' => 2 ,'str' => '已完成']);// 已完成
  370. }
  371. $data = AuctionApply::where([['status', '=', 0], ['uid', '=', $request->uid()]])->find();
  372. if ($data){
  373. return app('json')->successful(['status' => 3, 'str' => '失败']);// 失败
  374. }
  375. }
  376. }