VoteController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_detail($id, Request $request)
  39. {
  40. $list = VoteJoin::getValidDetail($id);
  41. return app('json')->success('ok', ['data' => $list]);
  42. }
  43. public function join($id, Request $request)
  44. {
  45. $info = Vote::valid()->find($id);
  46. if (!$info) {
  47. return app('json')->fail('活动不存在');
  48. }
  49. // if ($info['start_time'] > time()) {
  50. // return app('json')->fail('活动未开始');
  51. // }
  52. if ($info['end_time'] < time()) {
  53. return app('json')->fail('活动已结束');
  54. }
  55. if (VoteJoin::be(['vid' => $id, 'uid' => $request->uid(), 'status' => [1, 0]])) {
  56. return app('json')->fail('已有报名申请或已经参加活动');
  57. }
  58. $data = UtilService::postMore([
  59. ['type', 1, '', '', 'not_empty_check', '请选择报名类型'],
  60. ['job', '', '', '', 'not_empty_check', '请填写职业'],
  61. ['name', '', '', '', 'not_empty_check', '请填写姓名'],
  62. ['phone', '', '', '', 'not_empty_check', '请填写联系方式话'],
  63. ['address', '', '', '', 'not_empty_check', '请填写地址'],
  64. ['avatar', '', '', '', 'not_empty_check', '请上传参赛头像'],
  65. ['detail', '', '', '', 'not_empty_check', '请填写个人介绍'],
  66. ['company', ''],
  67. ], $request);
  68. $data['vid'] = $id;
  69. $data['uid'] = $request->uid();
  70. if ($data['type'] == 2) {
  71. if (!$data['company']) return app('json')->fail('企业报名请输入企业名称');
  72. } else {
  73. unset($data['company']);
  74. }
  75. if (VoteJoin::where('vid', $id)->where('uid', $request->uid())->find()) {
  76. $data['status'] = 0;
  77. $data['mark'] = '';
  78. $res = VoteJoin::where('vid', $id)->where('uid', $request->uid())->update($data);
  79. } else {
  80. $data['add_time'] = time();
  81. $res = VoteJoin::create($data);
  82. }
  83. if ($res) {
  84. return app('json')->success('报名成功');
  85. } else {
  86. return app('json')->fail('报名失败');
  87. }
  88. }
  89. public function vote($id, Request $request)
  90. {
  91. if (!$id) return app('json')->fail('参数错误!');
  92. $uid = $request->uid();
  93. if (!VoteJoin::valid()->find($id)) return app('json')->fail('参数错误!');
  94. list($num, $payType, $from) = UtilService::postMore([
  95. 'num', 'payType', ['from', 'weixin']
  96. ], $request, true);
  97. $payType = strtolower($payType);
  98. if ($num <= 0) return app('json')->fail('投票张数不小于1!');
  99. $order = VoteOrder::createOrder($request->uid(), $id, $num, $payType);
  100. if ($order === false) return app('json')->fail(VoteOrder::getErrorInfo('订单生成失败'));
  101. $orderId = $order['order_id'];
  102. $info = ['order_id' => $orderId];
  103. if ($orderId) {
  104. switch ($payType) {
  105. case "weixin":
  106. $orderInfo = VoteOrder::where('order_id', $orderId)->find();
  107. if (!$orderInfo || !isset($orderInfo['paid'])) return app('json')->fail('支付订单不存在!');
  108. $orderInfo = $orderInfo->toArray();
  109. if ($orderInfo['paid']) return app('json')->fail('订单已支付!');
  110. //支付金额为0
  111. if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
  112. //创建订单jspay支付
  113. $payPriceStatus = VoteOrder::jsPayPrice($orderId, $uid);
  114. if ($payPriceStatus)//0元支付成功
  115. return app('json')->status('success', '微信支付成功');
  116. else
  117. return app('json')->status('pay_error', VoteOrder::getErrorInfo());
  118. } else {
  119. try {
  120. if ($from == 'routine') {
  121. $jsConfig = OrderRepository::jsVotePay($orderId); //创建订单jspay
  122. } else if ($from == 'weixinh5') {
  123. $jsConfig = OrderRepository::h5VotePay($orderId);
  124. } else {
  125. $jsConfig = OrderRepository::wxVotePay($orderId);
  126. }
  127. } catch (\Exception $e) {
  128. return app('json')->status('pay_error', $e->getMessage());
  129. }
  130. $info['jsConfig'] = $jsConfig;
  131. if ($from == 'weixinh5') {
  132. return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
  133. } else {
  134. return app('json')->status('wechat_pay', '订单创建成功', $info);
  135. }
  136. }
  137. case 'yue':
  138. if (VoteOrder::yuePay($orderId, $request->uid()))
  139. return app('json')->status('success', '余额支付成功', $info);
  140. else {
  141. $errorinfo = VoteOrder::getErrorInfo();
  142. if (is_array($errorinfo))
  143. return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
  144. else
  145. return app('json')->status('pay_error', $errorinfo);
  146. }
  147. break;
  148. case 'free':
  149. if (VoteOrder::freePay($orderId, $request->uid()))
  150. return app('json')->status('success', '投票成功', $info);
  151. else {
  152. $errorinfo = VoteOrder::getErrorInfo();
  153. if (is_array($errorinfo))
  154. return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
  155. else
  156. return app('json')->status('pay_error', $errorinfo);
  157. }
  158. break;
  159. case 'offline':
  160. return app('json')->status('success', '订单创建成功', $info);
  161. break;
  162. }
  163. } else return app('json')->fail(VoteOrder::getErrorInfo('订单生成失败!'));
  164. }
  165. }