Seat.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\mobile\controller;
  3. use app\common\model\SeatApplyModel;
  4. use app\common\model\SeatModel;
  5. use app\mobile\MobileBaseController;
  6. use think\facade\Db;
  7. class Seat extends MobileBaseController
  8. {
  9. public function apply()
  10. {
  11. $id = input('id', 0);
  12. if (empty($id)) {
  13. $id = session('mobile.seat.id');
  14. if (empty($id)) {
  15. jump('该活动不存在或已结束');
  16. }
  17. } else {
  18. session('mobile.seat.id', $id);
  19. }
  20. $seat = SeatModel::where('id', $id)->find();
  21. if (empty($seat)) {
  22. jump('该活动不存在或已结束');
  23. }
  24. session('mobile.seat.id', $id);
  25. $open_id = get_open_id();
  26. $apply = SeatApplyModel::where('open_id', $open_id)->where('seat_id', $id)->find();
  27. if (empty($apply)) {
  28. if ($seat['apply'] >= $seat['total']) {
  29. jump('提示', '座位已被选完');
  30. } else {
  31. return redirect(url('seat/fillInfo'));
  32. }
  33. } else {
  34. tip('选座成功', $apply['seat']);
  35. }
  36. }
  37. public function fillInfo()
  38. {
  39. $id = session('mobile.seat.id');
  40. if (empty($id)) {
  41. jump('该活动不存在或已结束');
  42. }
  43. get_open_id();
  44. return view();
  45. }
  46. public function fillInfoPost()
  47. {
  48. $data = input('param.');
  49. if (empty($data['name'])) {
  50. ajax_error('姓名不能为空');
  51. }
  52. if (empty($data['mobile'])) {
  53. ajax_error('手机号不能为空');
  54. }
  55. $id = session('mobile.seat.id');
  56. $open_id = get_open_id();
  57. Db::startTrans();
  58. try {
  59. //是否还有座位
  60. $seat = SeatModel::where('id', $id)->lock(true)->find();
  61. if ($seat['apply'] >= $seat['total']) {
  62. throw new \Exception('座位已被选完');
  63. }
  64. //报名人数+1
  65. $seat->apply++;
  66. $seat->save();
  67. //报名明细
  68. $apply_info = [
  69. 'seat_id' => $id,
  70. 'no' => $seat->apply,
  71. 'open_id' => $open_id,
  72. 'name' => $data['name'],
  73. 'mobile' => $data['mobile'],
  74. ];
  75. $no = $seat->apply;
  76. $seat_info = '';
  77. foreach ($seat['seat_list'] as $k => $v) {
  78. if ($no <= $v) {
  79. $seat_info = ($k + 1) . "排" . $no . '座';
  80. break;
  81. } else {
  82. $no -= $v;
  83. }
  84. }
  85. $apply_info['seat'] = $seat_info;
  86. SeatApplyModel::create($apply_info);
  87. // 提交事务
  88. Db::commit();
  89. } catch (\Exception $e) {
  90. // 回滚事务
  91. Db::rollback();
  92. ajax_error('座位已被选完');
  93. }
  94. ajax_success();
  95. }
  96. public function showSeat()
  97. {
  98. $id = session('mobile.seat.id');
  99. $open_id = get_open_id();
  100. $seat = SeatApplyModel::where('seat_id', $id)->where('open_id',$open_id)->find();
  101. tip('座位信息',$seat['seat']);
  102. }
  103. public function login()
  104. {
  105. return redirect('https://www.jucai.gov.cn/api/auth/wechat_auth?url=' . urlencode(url('/mobile/seat/wechatBack')));
  106. }
  107. /**
  108. * 微信回调
  109. */
  110. public function wechatBack()
  111. {
  112. $param = input('param.');
  113. session('mobile.open_id',$param['openid']);
  114. return redirect(url('seat/apply'));
  115. }
  116. public function login1()
  117. {
  118. session('mobile.open_id','linwu');
  119. return '登录成功';
  120. }
  121. }