UniversityEvent.php 8.8 KB

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