UniversityEvent.php 8.7 KB

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