Seat.php 3.7 KB

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