VoteController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\api\controller\vote;
  3. use app\admin\model\vote\Vote;
  4. use app\admin\model\vote\VoteJoin;
  5. use app\admin\model\vote\VoteOrder;
  6. use app\Request;
  7. use crmeb\repositories\OrderRepository;
  8. use crmeb\services\UtilService;
  9. class VoteController
  10. {
  11. public function lst(Request $request)
  12. {
  13. $where = UtilService::getMore([['page', 1], ['limit', 10]], $request);
  14. return app('json')->success('ok', Vote::getList($where));
  15. }
  16. public function detail($id, Request $request)
  17. {
  18. $info = Vote::valid()->find($id);
  19. if ($info) {
  20. $info->visit = $info->visit + 1;
  21. $info->save();
  22. $info = $info->toArray();
  23. $info['join_num'] = VoteJoin::valid()->where('vid', $id)->count();
  24. $info['vote_num'] = VoteJoin::valid()->where('vid', $id)->sum('vote');
  25. $info['my_status'] = VoteJoin::where('vid', $id)->where('uid', $request->uid())->find();
  26. if ($request->uid()) {
  27. $info['today_free_vote_num'] = VoteOrder::valid()->where('uid', $request->uid())->where('vid', $id)->where('pay_type', 'free')->whereTime('pay_time', 'today')->sum('vote_num');
  28. }
  29. }
  30. return app('json')->success('ok', $info);
  31. }
  32. public function join_list($id, Request $request)
  33. {
  34. $where = UtilService::getMore([['page', 1], ['limit', 10], ['key_word', ''], ['job', ''], ['rank', 0]], $request);
  35. $list = VoteJoin::getValidList($id, $where);
  36. return app('json')->success('ok', $list);
  37. }
  38. public function join($id, Request $request)
  39. {
  40. $info = Vote::valid()->find($id);
  41. if (!$info) {
  42. return app('json')->fail('活动不存在');
  43. }
  44. // if ($info['start_time'] > time()) {
  45. // return app('json')->fail('活动未开始');
  46. // }
  47. if ($info['end_time'] < time()) {
  48. return app('json')->fail('活动已结束');
  49. }
  50. if (VoteJoin::be(['vid' => $id, 'uid' => $request->uid(), 'status' => [1, 0]])) {
  51. return app('json')->fail('已有报名申请或已经参加活动');
  52. }
  53. $data = UtilService::postMore([
  54. ['type', 1, '', '', 'not_empty_check', '请选择报名类型'],
  55. ['job', '', '', '', 'not_empty_check', '请填写职业'],
  56. ['name', '', '', '', 'not_empty_check', '请填写姓名'],
  57. ['phone', '', '', '', 'not_empty_check', '请填写联系方式话'],
  58. ['address', '', '', '', 'not_empty_check', '请填写地址'],
  59. ['avatar', '', '', '', 'not_empty_check', '请上传参赛头像'],
  60. ['detail', '', '', '', 'not_empty_check', '请填写个人介绍'],
  61. ['company', ''],
  62. ], $request);
  63. $data['vid'] = $id;
  64. $data['uid'] = $request->uid();
  65. if ($data['type'] == 2) {
  66. if (!$data['company']) return app('json')->fail('企业报名请输入企业名称');
  67. } else {
  68. unset($data['company']);
  69. }
  70. if (VoteJoin::where('vid', $id)->where('uid', $request->uid())->find()) {
  71. $data['status'] = 0;
  72. $data['mark'] = '';
  73. $res = VoteJoin::where('vid', $id)->where('uid', $request->uid())->update($data);
  74. } else {
  75. $data['add_time'] = time();
  76. $res = VoteJoin::create($data);
  77. }
  78. if ($res) {
  79. return app('json')->success('报名成功');
  80. } else {
  81. return app('json')->fail('报名失败');
  82. }
  83. }
  84. public function vote($id, Request $request)
  85. {
  86. if (!$id) return app('json')->fail('参数错误!');
  87. $uid = $request->uid();
  88. if (!VoteJoin::valid()->find($id)) return app('json')->fail('参数错误!');
  89. list($num, $payType, $from) = UtilService::postMore([
  90. 'num', 'payType', ['from', 'weixin']
  91. ], $request, true);
  92. $payType = strtolower($payType);
  93. if ($num <= 0) return app('json')->fail('投票张数不小于1!');
  94. $order = VoteOrder::createOrder($request->uid(), $id, $num, $payType);
  95. if ($order === false) return app('json')->fail(VoteOrder::getErrorInfo('订单生成失败'));
  96. $orderId = $order['order_id'];
  97. $info = ['order_id' => $orderId];
  98. if ($orderId) {
  99. switch ($payType) {
  100. case "weixin":
  101. $orderInfo = VoteOrder::where('order_id', $orderId)->find();
  102. if (!$orderInfo || !isset($orderInfo['paid'])) return app('json')->fail('支付订单不存在!');
  103. $orderInfo = $orderInfo->toArray();
  104. if ($orderInfo['paid']) return app('json')->fail('订单已支付!');
  105. //支付金额为0
  106. if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
  107. //创建订单jspay支付
  108. $payPriceStatus = VoteOrder::jsPayPrice($orderId, $uid);
  109. if ($payPriceStatus)//0元支付成功
  110. return app('json')->status('success', '微信支付成功');
  111. else
  112. return app('json')->status('pay_error', VoteOrder::getErrorInfo());
  113. } else {
  114. try {
  115. if ($from == 'routine') {
  116. $jsConfig = OrderRepository::jsVotePay($orderId); //创建订单jspay
  117. } else if ($from == 'weixinh5') {
  118. $jsConfig = OrderRepository::h5VotePay($orderId);
  119. } else {
  120. $jsConfig = OrderRepository::wxVotePay($orderId);
  121. }
  122. } catch (\Exception $e) {
  123. return app('json')->status('pay_error', $e->getMessage());
  124. }
  125. $info['jsConfig'] = $jsConfig;
  126. if ($from == 'weixinh5') {
  127. return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
  128. } else {
  129. return app('json')->status('wechat_pay', '订单创建成功', $info);
  130. }
  131. }
  132. case 'yue':
  133. if (VoteOrder::yuePay($orderId, $request->uid()))
  134. return app('json')->status('success', '余额支付成功', $info);
  135. else {
  136. $errorinfo = VoteOrder::getErrorInfo();
  137. if (is_array($errorinfo))
  138. return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
  139. else
  140. return app('json')->status('pay_error', $errorinfo);
  141. }
  142. break;
  143. case 'free':
  144. if (VoteOrder::freePay($orderId, $request->uid()))
  145. return app('json')->status('success', '投票成功', $info);
  146. else {
  147. $errorinfo = VoteOrder::getErrorInfo();
  148. if (is_array($errorinfo))
  149. return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
  150. else
  151. return app('json')->status('pay_error', $errorinfo);
  152. }
  153. break;
  154. case 'offline':
  155. return app('json')->status('success', '订单创建成功', $info);
  156. break;
  157. }
  158. } else return app('json')->fail(VoteOrder::getErrorInfo('订单生成失败!'));
  159. }
  160. }