UniversityCourse.php 7.6 KB

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