UniversityEvent.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. }
  84. $user_id = $this->auth->id;
  85. $info = UniversityEventModel::with([
  86. 'apply' => function ($query) use ($user_id) {
  87. $query->field('id,event_id,user_id')->where('user_id', $user_id)->where('status', 1);
  88. }
  89. ])
  90. ->field('id,name,price,image,images,start_apply_time,end_apply_time,start_time,address,content')
  91. ->where('id', $params['event_id'])
  92. ->where('status', 1)
  93. ->order('id desc')
  94. ->find();
  95. if (!$info) {
  96. return $this->error('活动不存在');
  97. }
  98. if (!empty($info['apply'])) {
  99. return $this->error('您已报过名了');
  100. }
  101. $nowTime = time();
  102. if ($info['start_apply_time'] > $nowTime) {
  103. return $this->error('报名未开始');
  104. }
  105. if ($info['end_apply_time'] < $nowTime) {
  106. return $this->error('报名已结束');
  107. }
  108. // 开始报名
  109. $num = count($params['apply_list']);
  110. $data = [
  111. 'user_id' => $user_id,
  112. 'event_id' => $params['event_id'],
  113. 'order_no' => createUniqueNo('E', $user_id),
  114. 'pay_amount' => bcmul($info['price'], $num, 2),
  115. 'create_time' => $nowTime
  116. ];
  117. Db::startTrans();
  118. $apply_id = Db::name('university_event_apply')->insertGetId($data);
  119. if (!$apply_id) {
  120. Db::rollback();
  121. return $this->error('订单创建失败');
  122. }
  123. $apply_info = [];
  124. foreach ($params['apply_list'] as $k => $v) {
  125. $apply_info[] = [
  126. 'user_id' => $user_id,
  127. 'event_id' => $params['event_id'],
  128. 'order_id' => $apply_id,
  129. 'order_no' => $data['order_no'],
  130. 'name' => $v['name'],
  131. 'phone' => $v['phone']
  132. ];
  133. }
  134. if (!Db::name('university_event_info')->insertAll($apply_info)) {
  135. Db::rollback();
  136. return $this->error('订单创建失败');
  137. }
  138. Db::commit();
  139. // 创建支付订单
  140. $remark = '老年大学活动报名';
  141. $orderData = [
  142. 'user_id' => $user_id,
  143. 'out_trade_no' => $data['order_no'],
  144. 'order_amount' => $data['pay_amount'],
  145. 'pay_type' => $params['pay_type'],
  146. 'platform' => $params['platform'],
  147. 'table_name' => 'university_event_apply',
  148. 'table_id' => $apply_id,
  149. 'createtime' => time(),
  150. 'args' => json_encode([
  151. 'table_id' => $apply_id,
  152. 'remark' => $remark
  153. ], JSON_UNESCAPED_UNICODE),
  154. ];
  155. if (!Db::name('pay_order')->insert($orderData)) {
  156. return $this->error('订单创建失败');
  157. }
  158. // 拉起支付 余额支付
  159. if ($params['pay_type'] == 'wallet') {
  160. Db::startTrans();
  161. //钱包更新
  162. $walletService = new Wallet();
  163. if (!$walletService->change($user_id, -$orderData['order_amount'], 'money', 20, $remark, $orderData['table_name'], $orderData['table_id'])) {
  164. Db::rollback();
  165. return $this->error($walletService->getMessage());
  166. }
  167. // 支付成功,更改支付金额
  168. [$res,$msg] = PayOrderModel::university_event($orderData['out_trade_no']);
  169. if (!$res){
  170. Db::rollback();
  171. return $this->error($msg);
  172. }
  173. Db::commit();
  174. return $this->success('支付成功');
  175. }
  176. // 第三方支付下单
  177. $params = [
  178. 'type' => $orderData['pay_type'],
  179. 'orderid' => $orderData['out_trade_no'],
  180. 'title' => $remark,
  181. 'amount' => $orderData['order_amount'],
  182. 'method' => $orderData['platform'],
  183. 'notifyurl' => CurlUtil::getHttp("/api/notify/university_event_{$params['pay_type']}"),
  184. 'returnurl' => '',
  185. ];
  186. // 如果是小程序则需要添加 openid
  187. if ($params['pay_type'] == 'wechat' && $params['platform'] == 'miniapp') {
  188. $params['openid'] = $this->auth->mini_openid;
  189. }
  190. $res = Service::submitOrder($params);
  191. if ($params['pay_type'] == 'wechat') {
  192. $this->success('success', json_decode($res, true));
  193. } else {
  194. $this->success('success', $res);
  195. }
  196. }
  197. }