UniversityEvent.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\api\controller;
  3. use addons\epay\library\Service;
  4. use app\common\controller\Api;
  5. use app\common\model\PayOrderModel;
  6. use app\common\model\UniversityEventModel;
  7. use app\common\model\Wallet;
  8. use app\utils\CurlUtil;
  9. use app\utils\Service\Tencent\TencentIm;
  10. use think\Db;
  11. /**
  12. * 老年大学 活动板块
  13. */
  14. class UniversityEvent extends Api
  15. {
  16. protected $noNeedLogin = [''];
  17. protected $noNeedRight = ['*'];
  18. // 活动列表
  19. public function list()
  20. {
  21. $user_id = $this->auth->id;
  22. $list = UniversityEventModel::with([
  23. 'apply' => function ($query) use ($user_id) {
  24. $query->field('id,event_id,user_id')->where('user_id', $user_id)->where('status', 1);
  25. }
  26. ])
  27. ->field('id,name,price,image,start_apply_time,end_apply_time,start_time,address')
  28. ->where('status', 1)
  29. ->order('id desc')
  30. ->autopage()
  31. ->select();
  32. foreach ($list as $k => $v) {
  33. $list[$k]['start_apply_time'] = date('Y-m-d H:i', $v['start_apply_time']);
  34. $list[$k]['end_apply_time'] = date('Y-m-d H:i', $v['end_apply_time']);
  35. $list[$k]['start_time'] = date('Y-m-d H:i', $v['start_time']);
  36. $list[$k]['apply'] = !empty($v['apply']) ? 1 : 0;
  37. }
  38. return $this->success('success', $list);
  39. }
  40. // 活动详情
  41. public function info()
  42. {
  43. $params = $this->request->param();
  44. if (empty($params['event_id'])) {
  45. return $this->error('参数缺失');
  46. }
  47. $user_id = $this->auth->id;
  48. $info = UniversityEventModel::with([
  49. 'apply' => function ($query) use ($user_id) {
  50. $query->field('id,event_id,user_id')->where('user_id', $user_id)->where('status', 1);
  51. }
  52. ])
  53. ->field('id,name,price,image,images,start_apply_time,end_apply_time,start_time,address,content')
  54. ->where('id', $params['event_id'])
  55. ->where('status', 1)
  56. ->order('id desc')
  57. ->find();
  58. $info['start_apply_time'] = date('Y-m-d', $info['start_apply_time']);
  59. $info['end_apply_time'] = date('Y-m-d', $info['end_apply_time']);
  60. $info['start_time'] = date('Y-m-d', $info['start_time']);
  61. $info['apply'] = !empty($info['apply']) ? 1 : 0;
  62. return $this->success('success', $info);
  63. }
  64. public function apply()
  65. {
  66. $params = $this->request->param();
  67. if (empty($params['event_id'])) {
  68. return $this->error('参数缺失');
  69. }
  70. // if (empty($params['pay_type']) || empty($params['platform'])) {
  71. // return $this->error('请选择支付方式');
  72. // }
  73. if (empty($params['apply_list'])) {
  74. return $this->error('请提交报名信息');
  75. }
  76. foreach ($params['apply_list'] as $k => $v) {
  77. if (empty($v['name'])) {
  78. return $this->error('报名信息姓名不能为空');
  79. }
  80. if (empty($v['phone'])) {
  81. return $this->error('报名信息手机号不能为空');
  82. }
  83. if (empty($v['sex'])) {
  84. return $this->error('报名信息性别不能为空');
  85. }
  86. if (empty($v['age'])) {
  87. return $this->error('报名信息年龄不能为空');
  88. }
  89. }
  90. $user_id = $this->auth->id;
  91. $info = UniversityEventModel::with([
  92. 'apply' => function ($query) use ($user_id) {
  93. $query->field('id,event_id,user_id')->where('user_id', $user_id)->where('status', 1);
  94. }
  95. ])
  96. ->field('id,name,price,image,images,start_apply_time,end_apply_time,start_time,address,content')
  97. ->where('id', $params['event_id'])
  98. ->where('status', 1)
  99. ->order('id desc')
  100. ->find();
  101. if (!$info) {
  102. return $this->error('活动不存在');
  103. }
  104. if (!empty($info['apply'])) {
  105. return $this->error('您已报过名了');
  106. }
  107. $nowTime = time();
  108. if ($info['start_apply_time'] > $nowTime) {
  109. return $this->error('报名未开始');
  110. }
  111. if ($info['end_apply_time'] < $nowTime) {
  112. return $this->error('报名已结束');
  113. }
  114. // 开始报名
  115. $num = count($params['apply_list']);
  116. $data = [
  117. 'user_id' => $user_id,
  118. 'event_id' => $params['event_id'],
  119. 'order_no' => createUniqueNo('E', $user_id),
  120. 'pay_amount' => bcmul($info['price'], $num, 2),
  121. 'status' => 1,
  122. 'create_time' => $nowTime
  123. ];
  124. Db::startTrans();
  125. $apply_id = Db::name('university_event_apply')->insertGetId($data);
  126. if (!$apply_id) {
  127. Db::rollback();
  128. return $this->error('订单创建失败');
  129. }
  130. $apply_info = [];
  131. foreach ($params['apply_list'] as $k => $v) {
  132. $apply_info[] = [
  133. 'user_id' => $user_id,
  134. 'event_id' => $params['event_id'],
  135. 'order_id' => $apply_id,
  136. 'order_no' => $data['order_no'],
  137. 'name' => $v['name'],
  138. 'phone' => $v['phone'],
  139. 'sex' => $v['sex'],
  140. 'age' => $v['age'],
  141. ];
  142. }
  143. if (!Db::name('university_event_info')->insertAll($apply_info)) {
  144. Db::rollback();
  145. return $this->error('订单创建失败');
  146. }
  147. Db::commit();
  148. return $this->success('报名成功');
  149. // // 创建支付订单
  150. // $remark = '老年大学活动报名';
  151. // $orderData = [
  152. // 'user_id' => $user_id,
  153. // 'out_trade_no' => $data['order_no'],
  154. // 'order_amount' => $data['pay_amount'],
  155. // 'pay_type' => $params['pay_type'],
  156. // 'platform' => $params['platform'],
  157. // 'table_name' => 'university_event_apply',
  158. // 'table_id' => $apply_id,
  159. // 'createtime' => time(),
  160. // 'args' => json_encode([
  161. // 'table_id' => $apply_id,
  162. // 'remark' => $remark
  163. // ], JSON_UNESCAPED_UNICODE),
  164. // ];
  165. // if (!Db::name('pay_order')->insert($orderData)) {
  166. // return $this->error('订单创建失败');
  167. // }
  168. //
  169. // // 拉起支付 余额支付
  170. // if ($params['pay_type'] == 'wallet') {
  171. // Db::startTrans();
  172. // //钱包更新
  173. // $walletService = new Wallet();
  174. // if (!$walletService->change($user_id, -$orderData['order_amount'], 'money', 20, $remark, $orderData['table_name'], $orderData['table_id'])) {
  175. // Db::rollback();
  176. // return $this->error($walletService->getMessage());
  177. // }
  178. // // 支付成功,更改支付金额
  179. // [$res,$msg] = PayOrderModel::university_event($orderData['out_trade_no']);
  180. // if (!$res){
  181. // Db::rollback();
  182. // return $this->error($msg);
  183. // }
  184. // Db::commit();
  185. // return $this->success('支付成功');
  186. // }
  187. //
  188. // // 第三方支付下单
  189. // $params = [
  190. // 'type' => $orderData['pay_type'],
  191. // 'orderid' => $orderData['out_trade_no'],
  192. // 'title' => $remark,
  193. // 'amount' => $orderData['order_amount'],
  194. // 'method' => $orderData['platform'],
  195. // 'notifyurl' => CurlUtil::getHttp("/api/notify/university_event_{$params['pay_type']}"),
  196. // 'returnurl' => '',
  197. // ];
  198. // // 如果是小程序则需要添加 openid
  199. // if ($params['pay_type'] == 'wechat' && $params['platform'] == 'miniapp') {
  200. // $params['openid'] = $this->auth->mini_openid;
  201. // }
  202. // $res = Service::submitOrder($params);
  203. // if ($params['pay_type'] == 'wechat') {
  204. // $this->success('success', json_decode($res, true));
  205. // } else {
  206. // $this->success('success', $res);
  207. // }
  208. }
  209. }