AuctionController.php 16 KB

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